blob: 867031934f75b30cac9b7812efef0b21d8d96192 [file] [log] [blame]
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001/*
2 * net/9p/clnt.c
3 *
4 * 9P Client
5 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06006 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05007 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/fs.h>
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060029#include <linux/poll.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050030#include <linux/idr.h>
31#include <linux/mutex.h>
32#include <linux/sched.h>
33#include <linux/uaccess.h>
34#include <net/9p/9p.h>
Eric Van Hensbergenfb0466c2007-10-17 14:31:07 -050035#include <linux/parser.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050036#include <net/9p/client.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050037#include <net/9p/transport.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050038
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060039/*
40 * Client Option Parsing (code inspired by NFS code)
41 * - a little lazy - parse all client options
42 */
43
44enum {
45 Opt_msize,
46 Opt_trans,
47 Opt_legacy,
48 Opt_err,
49};
50
Steven Whitehousea447c092008-10-13 10:46:57 +010051static const match_table_t tokens = {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060052 {Opt_msize, "msize=%u"},
53 {Opt_legacy, "noextend"},
54 {Opt_trans, "trans=%s"},
55 {Opt_err, NULL},
56};
57
58/**
59 * v9fs_parse_options - parse mount options into session structure
60 * @options: options string passed from mount
61 * @v9ses: existing v9fs session information
62 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060063 * Return 0 upon success, -ERRNO upon failure
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060064 */
65
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060066static int parse_opts(char *opts, struct p9_client *clnt)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060067{
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060068 char *options;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060069 char *p;
70 substring_t args[MAX_OPT_ARGS];
71 int option;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060072 int ret = 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060073
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060074 clnt->dotu = 1;
75 clnt->msize = 8192;
76
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060077 if (!opts)
78 return 0;
79
80 options = kstrdup(opts, GFP_KERNEL);
81 if (!options) {
82 P9_DPRINTK(P9_DEBUG_ERROR,
83 "failed to allocate copy of option string\n");
84 return -ENOMEM;
85 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060086
87 while ((p = strsep(&options, ",")) != NULL) {
88 int token;
89 if (!*p)
90 continue;
91 token = match_token(p, tokens, args);
92 if (token < Opt_trans) {
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060093 int r = match_int(&args[0], &option);
94 if (r < 0) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060095 P9_DPRINTK(P9_DEBUG_ERROR,
96 "integer field, but no integer?\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060097 ret = r;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060098 continue;
99 }
100 }
101 switch (token) {
102 case Opt_msize:
103 clnt->msize = option;
104 break;
105 case Opt_trans:
Tejun Heo72029fe2008-09-24 16:22:23 -0500106 clnt->trans_mod = v9fs_get_trans_by_name(&args[0]);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600107 break;
108 case Opt_legacy:
109 clnt->dotu = 0;
110 break;
111 default:
112 continue;
113 }
114 }
Tejun Heo72029fe2008-09-24 16:22:23 -0500115
116 if (!clnt->trans_mod)
117 clnt->trans_mod = v9fs_get_default_trans();
118
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600119 kfree(options);
120 return ret;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600121}
122
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500123/**
124 * p9_tag_alloc - lookup/allocate a request by tag
125 * @c: client session to lookup tag within
126 * @tag: numeric id for transaction
127 *
128 * this is a simple array lookup, but will grow the
129 * request_slots as necessary to accomodate transaction
130 * ids which did not previously have a slot.
131 *
132 * this code relies on the client spinlock to manage locks, its
133 * possible we should switch to something else, but I'd rather
134 * stick with something low-overhead for the common case.
135 *
136 */
137
138struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag)
139{
140 unsigned long flags;
141 int row, col;
142
143 /* This looks up the original request by tag so we know which
144 * buffer to read the data into */
145 tag++;
146
147 if (tag >= c->max_tag) {
148 spin_lock_irqsave(&c->lock, flags);
149 /* check again since original check was outside of lock */
150 while (tag >= c->max_tag) {
151 row = (tag / P9_ROW_MAXTAG);
152 c->reqs[row] = kcalloc(P9_ROW_MAXTAG,
153 sizeof(struct p9_req_t), GFP_ATOMIC);
154
155 if (!c->reqs[row]) {
156 printk(KERN_ERR "Couldn't grow tag array\n");
157 BUG();
158 }
159 for (col = 0; col < P9_ROW_MAXTAG; col++) {
160 c->reqs[row][col].status = REQ_STATUS_IDLE;
161 c->reqs[row][col].flush_tag = P9_NOTAG;
162 c->reqs[row][col].wq = kmalloc(
163 sizeof(wait_queue_head_t), GFP_ATOMIC);
164 if (!c->reqs[row][col].wq) {
165 printk(KERN_ERR
166 "Couldn't grow tag array\n");
167 BUG();
168 }
169 init_waitqueue_head(c->reqs[row][col].wq);
170 }
171 c->max_tag += P9_ROW_MAXTAG;
172 }
173 spin_unlock_irqrestore(&c->lock, flags);
174 }
175 row = tag / P9_ROW_MAXTAG;
176 col = tag % P9_ROW_MAXTAG;
177
178 c->reqs[row][col].status = REQ_STATUS_ALLOC;
179 c->reqs[row][col].flush_tag = P9_NOTAG;
180
181 return &c->reqs[row][col];
182}
183EXPORT_SYMBOL(p9_tag_alloc);
184
185/**
186 * p9_tag_lookup - lookup a request by tag
187 * @c: client session to lookup tag within
188 * @tag: numeric id for transaction
189 *
190 */
191
192struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
193{
194 int row, col;
195
196 /* This looks up the original request by tag so we know which
197 * buffer to read the data into */
198 tag++;
199
200 BUG_ON(tag >= c->max_tag);
201
202 row = tag / P9_ROW_MAXTAG;
203 col = tag % P9_ROW_MAXTAG;
204
205 return &c->reqs[row][col];
206}
207EXPORT_SYMBOL(p9_tag_lookup);
208
209/**
210 * p9_tag_init - setup tags structure and contents
211 * @tags: tags structure from the client struct
212 *
213 * This initializes the tags structure for each client instance.
214 *
215 */
216
217static int p9_tag_init(struct p9_client *c)
218{
219 int err = 0;
220
221 c->tagpool = p9_idpool_create();
222 if (IS_ERR(c->tagpool)) {
223 err = PTR_ERR(c->tagpool);
224 c->tagpool = NULL;
225 goto error;
226 }
227
228 p9_idpool_get(c->tagpool); /* reserve tag 0 */
229
230 c->max_tag = 0;
231error:
232 return err;
233}
234
235/**
236 * p9_tag_cleanup - cleans up tags structure and reclaims resources
237 * @tags: tags structure from the client struct
238 *
239 * This frees resources associated with the tags structure
240 *
241 */
242static void p9_tag_cleanup(struct p9_client *c)
243{
244 int row, col;
245
246 /* check to insure all requests are idle */
247 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
248 for (col = 0; col < P9_ROW_MAXTAG; col++) {
249 if (c->reqs[row][col].status != REQ_STATUS_IDLE) {
250 P9_DPRINTK(P9_DEBUG_MUX,
251 "Attempting to cleanup non-free tag %d,%d\n",
252 row, col);
253 /* TODO: delay execution of cleanup */
254 return;
255 }
256 }
257 }
258
259 if (c->tagpool)
260 p9_idpool_destroy(c->tagpool);
261
262 /* free requests associated with tags */
263 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
264 for (col = 0; col < P9_ROW_MAXTAG; col++)
265 kfree(c->reqs[row][col].wq);
266 kfree(c->reqs[row]);
267 }
268 c->max_tag = 0;
269}
270
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500271static struct p9_fid *p9_fid_create(struct p9_client *clnt)
272{
273 int err;
274 struct p9_fid *fid;
275
276 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
277 fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
278 if (!fid)
279 return ERR_PTR(-ENOMEM);
280
281 fid->fid = p9_idpool_get(clnt->fidpool);
282 if (fid->fid < 0) {
283 err = -ENOSPC;
284 goto error;
285 }
286
287 memset(&fid->qid, 0, sizeof(struct p9_qid));
288 fid->mode = -1;
289 fid->rdir_fpos = 0;
290 fid->rdir_pos = 0;
291 fid->rdir_fcall = NULL;
292 fid->uid = current->fsuid;
293 fid->clnt = clnt;
294 fid->aux = NULL;
295
296 spin_lock(&clnt->lock);
297 list_add(&fid->flist, &clnt->fidlist);
298 spin_unlock(&clnt->lock);
299
300 return fid;
301
302error:
303 kfree(fid);
304 return ERR_PTR(err);
305}
306
307static void p9_fid_destroy(struct p9_fid *fid)
308{
309 struct p9_client *clnt;
310
311 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
312 clnt = fid->clnt;
313 p9_idpool_put(fid->fid, clnt->fidpool);
314 spin_lock(&clnt->lock);
315 list_del(&fid->flist);
316 spin_unlock(&clnt->lock);
317 kfree(fid->rdir_fcall);
318 kfree(fid);
319}
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600320
Eric Van Hensbergen043aba42008-02-06 19:25:09 -0600321/**
322 * p9_client_rpc - sends 9P request and waits until a response is available.
323 * The function can be interrupted.
324 * @c: client data
325 * @tc: request to be sent
326 * @rc: pointer where a pointer to the response is stored
327 */
328int
329p9_client_rpc(struct p9_client *c, struct p9_fcall *tc,
330 struct p9_fcall **rc)
331{
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500332 return c->trans_mod->rpc(c, tc, rc);
Eric Van Hensbergen043aba42008-02-06 19:25:09 -0600333}
334
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600335struct p9_client *p9_client_create(const char *dev_name, char *options)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500336{
337 int err, n;
338 struct p9_client *clnt;
339 struct p9_fcall *tc, *rc;
340 struct p9_str *version;
341
342 err = 0;
343 tc = NULL;
344 rc = NULL;
345 clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
346 if (!clnt)
347 return ERR_PTR(-ENOMEM);
348
Tejun Heo72029fe2008-09-24 16:22:23 -0500349 clnt->trans_mod = NULL;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600350 clnt->trans = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500351 spin_lock_init(&clnt->lock);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500352 INIT_LIST_HEAD(&clnt->fidlist);
353 clnt->fidpool = p9_idpool_create();
Josef 'Jeff' Sipek728fc4e2008-03-07 11:40:33 -0600354 if (IS_ERR(clnt->fidpool)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500355 err = PTR_ERR(clnt->fidpool);
356 clnt->fidpool = NULL;
357 goto error;
358 }
359
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500360 p9_tag_init(clnt);
361
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600362 err = parse_opts(options, clnt);
363 if (err < 0)
364 goto error;
365
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600366 if (clnt->trans_mod == NULL) {
367 err = -EPROTONOSUPPORT;
368 P9_DPRINTK(P9_DEBUG_ERROR,
369 "No transport defined or default transport\n");
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500370 goto error;
371 }
372
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600373 P9_DPRINTK(P9_DEBUG_9P, "clnt %p trans %p msize %d dotu %d\n",
374 clnt, clnt->trans_mod, clnt->msize, clnt->dotu);
375
376
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500377 err = clnt->trans_mod->create(clnt, dev_name, options);
378 if (err)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600379 goto error;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600380
381 if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize)
382 clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ;
383
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500384 tc = p9_create_tversion(clnt->msize, clnt->dotu?"9P2000.u":"9P2000");
385 if (IS_ERR(tc)) {
386 err = PTR_ERR(tc);
387 tc = NULL;
388 goto error;
389 }
390
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600391 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500392 if (err)
393 goto error;
394
395 version = &rc->params.rversion.version;
396 if (version->len == 8 && !memcmp(version->str, "9P2000.u", 8))
397 clnt->dotu = 1;
398 else if (version->len == 6 && !memcmp(version->str, "9P2000", 6))
399 clnt->dotu = 0;
400 else {
401 err = -EREMOTEIO;
402 goto error;
403 }
404
405 n = rc->params.rversion.msize;
406 if (n < clnt->msize)
407 clnt->msize = n;
408
409 kfree(tc);
410 kfree(rc);
411 return clnt;
412
413error:
414 kfree(tc);
415 kfree(rc);
416 p9_client_destroy(clnt);
417 return ERR_PTR(err);
418}
419EXPORT_SYMBOL(p9_client_create);
420
421void p9_client_destroy(struct p9_client *clnt)
422{
423 struct p9_fid *fid, *fidptr;
424
425 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500426
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500427 if (clnt->trans_mod)
428 clnt->trans_mod->close(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500429
Tejun Heo72029fe2008-09-24 16:22:23 -0500430 v9fs_put_trans(clnt->trans_mod);
431
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500432 list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist)
433 p9_fid_destroy(fid);
434
Eric Van Hensbergen0af88872007-07-13 16:47:58 -0500435 if (clnt->fidpool)
436 p9_idpool_destroy(clnt->fidpool);
437
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500438 p9_tag_cleanup(clnt);
439
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500440 kfree(clnt);
441}
442EXPORT_SYMBOL(p9_client_destroy);
443
444void p9_client_disconnect(struct p9_client *clnt)
445{
446 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500447 clnt->status = Disconnected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500448}
449EXPORT_SYMBOL(p9_client_disconnect);
450
451struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
Latchesar Ionkovba176742007-10-17 14:31:07 -0500452 char *uname, u32 n_uname, char *aname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500453{
454 int err;
455 struct p9_fcall *tc, *rc;
456 struct p9_fid *fid;
457
458 P9_DPRINTK(P9_DEBUG_9P, "clnt %p afid %d uname %s aname %s\n",
459 clnt, afid?afid->fid:-1, uname, aname);
460 err = 0;
461 tc = NULL;
462 rc = NULL;
463
464 fid = p9_fid_create(clnt);
465 if (IS_ERR(fid)) {
466 err = PTR_ERR(fid);
467 fid = NULL;
468 goto error;
469 }
470
Latchesar Ionkovba176742007-10-17 14:31:07 -0500471 tc = p9_create_tattach(fid->fid, afid?afid->fid:P9_NOFID, uname, aname,
472 n_uname, clnt->dotu);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500473 if (IS_ERR(tc)) {
474 err = PTR_ERR(tc);
475 tc = NULL;
476 goto error;
477 }
478
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600479 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500480 if (err)
481 goto error;
482
483 memmove(&fid->qid, &rc->params.rattach.qid, sizeof(struct p9_qid));
484 kfree(tc);
485 kfree(rc);
486 return fid;
487
488error:
489 kfree(tc);
490 kfree(rc);
491 if (fid)
492 p9_fid_destroy(fid);
493 return ERR_PTR(err);
494}
495EXPORT_SYMBOL(p9_client_attach);
496
Latchesar Ionkovba176742007-10-17 14:31:07 -0500497struct p9_fid *p9_client_auth(struct p9_client *clnt, char *uname,
498 u32 n_uname, char *aname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500499{
500 int err;
501 struct p9_fcall *tc, *rc;
502 struct p9_fid *fid;
503
504 P9_DPRINTK(P9_DEBUG_9P, "clnt %p uname %s aname %s\n", clnt, uname,
505 aname);
506 err = 0;
507 tc = NULL;
508 rc = NULL;
509
510 fid = p9_fid_create(clnt);
511 if (IS_ERR(fid)) {
512 err = PTR_ERR(fid);
513 fid = NULL;
514 goto error;
515 }
516
Latchesar Ionkovba176742007-10-17 14:31:07 -0500517 tc = p9_create_tauth(fid->fid, uname, aname, n_uname, clnt->dotu);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500518 if (IS_ERR(tc)) {
519 err = PTR_ERR(tc);
520 tc = NULL;
521 goto error;
522 }
523
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600524 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500525 if (err)
526 goto error;
527
528 memmove(&fid->qid, &rc->params.rauth.qid, sizeof(struct p9_qid));
529 kfree(tc);
530 kfree(rc);
531 return fid;
532
533error:
534 kfree(tc);
535 kfree(rc);
536 if (fid)
537 p9_fid_destroy(fid);
538 return ERR_PTR(err);
539}
540EXPORT_SYMBOL(p9_client_auth);
541
542struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames,
543 int clone)
544{
545 int err;
546 struct p9_fcall *tc, *rc;
547 struct p9_client *clnt;
548 struct p9_fid *fid;
549
550 P9_DPRINTK(P9_DEBUG_9P, "fid %d nwname %d wname[0] %s\n",
551 oldfid->fid, nwname, wnames?wnames[0]:NULL);
552 err = 0;
553 tc = NULL;
554 rc = NULL;
555 clnt = oldfid->clnt;
556 if (clone) {
557 fid = p9_fid_create(clnt);
558 if (IS_ERR(fid)) {
559 err = PTR_ERR(fid);
560 fid = NULL;
561 goto error;
562 }
563
564 fid->uid = oldfid->uid;
565 } else
566 fid = oldfid;
567
568 tc = p9_create_twalk(oldfid->fid, fid->fid, nwname, wnames);
569 if (IS_ERR(tc)) {
570 err = PTR_ERR(tc);
571 tc = NULL;
572 goto error;
573 }
574
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600575 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500576 if (err) {
577 if (rc && rc->id == P9_RWALK)
578 goto clunk_fid;
579 else
580 goto error;
581 }
582
583 if (rc->params.rwalk.nwqid != nwname) {
584 err = -ENOENT;
585 goto clunk_fid;
586 }
587
588 if (nwname)
589 memmove(&fid->qid,
590 &rc->params.rwalk.wqids[rc->params.rwalk.nwqid - 1],
591 sizeof(struct p9_qid));
592 else
593 fid->qid = oldfid->qid;
594
595 kfree(tc);
596 kfree(rc);
597 return fid;
598
599clunk_fid:
600 kfree(tc);
601 kfree(rc);
602 rc = NULL;
603 tc = p9_create_tclunk(fid->fid);
604 if (IS_ERR(tc)) {
605 err = PTR_ERR(tc);
606 tc = NULL;
607 goto error;
608 }
609
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600610 p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500611
612error:
613 kfree(tc);
614 kfree(rc);
615 if (fid && (fid != oldfid))
616 p9_fid_destroy(fid);
617
618 return ERR_PTR(err);
619}
620EXPORT_SYMBOL(p9_client_walk);
621
622int p9_client_open(struct p9_fid *fid, int mode)
623{
624 int err;
625 struct p9_fcall *tc, *rc;
626 struct p9_client *clnt;
627
628 P9_DPRINTK(P9_DEBUG_9P, "fid %d mode %d\n", fid->fid, mode);
629 err = 0;
630 tc = NULL;
631 rc = NULL;
632 clnt = fid->clnt;
633
634 if (fid->mode != -1)
635 return -EINVAL;
636
637 tc = p9_create_topen(fid->fid, mode);
638 if (IS_ERR(tc)) {
639 err = PTR_ERR(tc);
640 tc = NULL;
641 goto done;
642 }
643
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600644 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500645 if (err)
646 goto done;
647
648 fid->mode = mode;
649 fid->iounit = rc->params.ropen.iounit;
650
651done:
652 kfree(tc);
653 kfree(rc);
654 return err;
655}
656EXPORT_SYMBOL(p9_client_open);
657
658int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
659 char *extension)
660{
661 int err;
662 struct p9_fcall *tc, *rc;
663 struct p9_client *clnt;
664
665 P9_DPRINTK(P9_DEBUG_9P, "fid %d name %s perm %d mode %d\n", fid->fid,
666 name, perm, mode);
667 err = 0;
668 tc = NULL;
669 rc = NULL;
670 clnt = fid->clnt;
671
672 if (fid->mode != -1)
673 return -EINVAL;
674
675 tc = p9_create_tcreate(fid->fid, name, perm, mode, extension,
676 clnt->dotu);
677 if (IS_ERR(tc)) {
678 err = PTR_ERR(tc);
679 tc = NULL;
680 goto done;
681 }
682
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600683 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500684 if (err)
685 goto done;
686
687 fid->mode = mode;
688 fid->iounit = rc->params.ropen.iounit;
689
690done:
691 kfree(tc);
692 kfree(rc);
693 return err;
694}
695EXPORT_SYMBOL(p9_client_fcreate);
696
697int p9_client_clunk(struct p9_fid *fid)
698{
699 int err;
700 struct p9_fcall *tc, *rc;
701 struct p9_client *clnt;
702
703 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
704 err = 0;
705 tc = NULL;
706 rc = NULL;
707 clnt = fid->clnt;
708
709 tc = p9_create_tclunk(fid->fid);
710 if (IS_ERR(tc)) {
711 err = PTR_ERR(tc);
712 tc = NULL;
713 goto done;
714 }
715
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600716 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500717 if (err)
718 goto done;
719
720 p9_fid_destroy(fid);
721
722done:
723 kfree(tc);
724 kfree(rc);
725 return err;
726}
727EXPORT_SYMBOL(p9_client_clunk);
728
729int p9_client_remove(struct p9_fid *fid)
730{
731 int err;
732 struct p9_fcall *tc, *rc;
733 struct p9_client *clnt;
734
735 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
736 err = 0;
737 tc = NULL;
738 rc = NULL;
739 clnt = fid->clnt;
740
741 tc = p9_create_tremove(fid->fid);
742 if (IS_ERR(tc)) {
743 err = PTR_ERR(tc);
744 tc = NULL;
745 goto done;
746 }
747
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600748 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500749 if (err)
750 goto done;
751
752 p9_fid_destroy(fid);
753
754done:
755 kfree(tc);
756 kfree(rc);
757 return err;
758}
759EXPORT_SYMBOL(p9_client_remove);
760
761int p9_client_read(struct p9_fid *fid, char *data, u64 offset, u32 count)
762{
763 int err, n, rsize, total;
764 struct p9_fcall *tc, *rc;
765 struct p9_client *clnt;
766
767 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu %d\n", fid->fid,
768 (long long unsigned) offset, count);
769 err = 0;
770 tc = NULL;
771 rc = NULL;
772 clnt = fid->clnt;
773 total = 0;
774
775 rsize = fid->iounit;
776 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
777 rsize = clnt->msize - P9_IOHDRSZ;
778
779 do {
780 if (count < rsize)
781 rsize = count;
782
783 tc = p9_create_tread(fid->fid, offset, rsize);
784 if (IS_ERR(tc)) {
785 err = PTR_ERR(tc);
786 tc = NULL;
787 goto error;
788 }
789
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600790 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500791 if (err)
792 goto error;
793
794 n = rc->params.rread.count;
795 if (n > count)
796 n = count;
797
798 memmove(data, rc->params.rread.data, n);
799 count -= n;
800 data += n;
801 offset += n;
802 total += n;
803 kfree(tc);
804 tc = NULL;
805 kfree(rc);
806 rc = NULL;
807 } while (count > 0 && n == rsize);
808
809 return total;
810
811error:
812 kfree(tc);
813 kfree(rc);
814 return err;
815}
816EXPORT_SYMBOL(p9_client_read);
817
818int p9_client_write(struct p9_fid *fid, char *data, u64 offset, u32 count)
819{
820 int err, n, rsize, total;
821 struct p9_fcall *tc, *rc;
822 struct p9_client *clnt;
823
824 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid,
825 (long long unsigned) offset, count);
826 err = 0;
827 tc = NULL;
828 rc = NULL;
829 clnt = fid->clnt;
830 total = 0;
831
832 rsize = fid->iounit;
833 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
834 rsize = clnt->msize - P9_IOHDRSZ;
835
836 do {
837 if (count < rsize)
838 rsize = count;
839
840 tc = p9_create_twrite(fid->fid, offset, rsize, data);
841 if (IS_ERR(tc)) {
842 err = PTR_ERR(tc);
843 tc = NULL;
844 goto error;
845 }
846
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600847 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500848 if (err)
849 goto error;
850
851 n = rc->params.rread.count;
852 count -= n;
853 data += n;
854 offset += n;
855 total += n;
856 kfree(tc);
857 tc = NULL;
858 kfree(rc);
859 rc = NULL;
860 } while (count > 0);
861
862 return total;
863
864error:
865 kfree(tc);
866 kfree(rc);
867 return err;
868}
869EXPORT_SYMBOL(p9_client_write);
870
871int
872p9_client_uread(struct p9_fid *fid, char __user *data, u64 offset, u32 count)
873{
874 int err, n, rsize, total;
875 struct p9_fcall *tc, *rc;
876 struct p9_client *clnt;
877
878 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid,
879 (long long unsigned) offset, count);
880 err = 0;
881 tc = NULL;
882 rc = NULL;
883 clnt = fid->clnt;
884 total = 0;
885
886 rsize = fid->iounit;
887 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
888 rsize = clnt->msize - P9_IOHDRSZ;
889
890 do {
891 if (count < rsize)
892 rsize = count;
893
894 tc = p9_create_tread(fid->fid, offset, rsize);
895 if (IS_ERR(tc)) {
896 err = PTR_ERR(tc);
897 tc = NULL;
898 goto error;
899 }
900
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600901 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500902 if (err)
903 goto error;
904
905 n = rc->params.rread.count;
906 if (n > count)
907 n = count;
908
909 err = copy_to_user(data, rc->params.rread.data, n);
910 if (err) {
911 err = -EFAULT;
912 goto error;
913 }
914
915 count -= n;
916 data += n;
917 offset += n;
918 total += n;
919 kfree(tc);
920 tc = NULL;
921 kfree(rc);
922 rc = NULL;
923 } while (count > 0 && n == rsize);
924
925 return total;
926
927error:
928 kfree(tc);
929 kfree(rc);
930 return err;
931}
932EXPORT_SYMBOL(p9_client_uread);
933
934int
935p9_client_uwrite(struct p9_fid *fid, const char __user *data, u64 offset,
936 u32 count)
937{
938 int err, n, rsize, total;
939 struct p9_fcall *tc, *rc;
940 struct p9_client *clnt;
941
942 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid,
943 (long long unsigned) offset, count);
944 err = 0;
945 tc = NULL;
946 rc = NULL;
947 clnt = fid->clnt;
948 total = 0;
949
950 rsize = fid->iounit;
951 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
952 rsize = clnt->msize - P9_IOHDRSZ;
953
954 do {
955 if (count < rsize)
956 rsize = count;
957
958 tc = p9_create_twrite_u(fid->fid, offset, rsize, data);
959 if (IS_ERR(tc)) {
960 err = PTR_ERR(tc);
961 tc = NULL;
962 goto error;
963 }
964
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600965 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500966 if (err)
967 goto error;
968
969 n = rc->params.rread.count;
970 count -= n;
971 data += n;
972 offset += n;
973 total += n;
974 kfree(tc);
975 tc = NULL;
976 kfree(rc);
977 rc = NULL;
978 } while (count > 0);
979
980 return total;
981
982error:
983 kfree(tc);
984 kfree(rc);
985 return err;
986}
987EXPORT_SYMBOL(p9_client_uwrite);
988
989int p9_client_readn(struct p9_fid *fid, char *data, u64 offset, u32 count)
990{
991 int n, total;
992
993 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid,
994 (long long unsigned) offset, count);
995 n = 0;
996 total = 0;
997 while (count) {
998 n = p9_client_read(fid, data, offset, count);
999 if (n <= 0)
1000 break;
1001
1002 data += n;
1003 offset += n;
1004 count -= n;
1005 total += n;
1006 }
1007
1008 if (n < 0)
1009 total = n;
1010
1011 return total;
1012}
1013EXPORT_SYMBOL(p9_client_readn);
1014
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001015static struct p9_stat *p9_clone_stat(struct p9_stat *st, int dotu)
1016{
1017 int n;
1018 char *p;
1019 struct p9_stat *ret;
1020
1021 n = sizeof(struct p9_stat) + st->name.len + st->uid.len + st->gid.len +
1022 st->muid.len;
1023
1024 if (dotu)
1025 n += st->extension.len;
1026
1027 ret = kmalloc(n, GFP_KERNEL);
1028 if (!ret)
1029 return ERR_PTR(-ENOMEM);
1030
1031 memmove(ret, st, sizeof(struct p9_stat));
1032 p = ((char *) ret) + sizeof(struct p9_stat);
1033 memmove(p, st->name.str, st->name.len);
1034 ret->name.str = p;
1035 p += st->name.len;
1036 memmove(p, st->uid.str, st->uid.len);
1037 ret->uid.str = p;
1038 p += st->uid.len;
1039 memmove(p, st->gid.str, st->gid.len);
1040 ret->gid.str = p;
1041 p += st->gid.len;
1042 memmove(p, st->muid.str, st->muid.len);
1043 ret->muid.str = p;
1044 p += st->muid.len;
1045
1046 if (dotu) {
1047 memmove(p, st->extension.str, st->extension.len);
1048 ret->extension.str = p;
1049 p += st->extension.len;
1050 }
1051
1052 return ret;
1053}
1054
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001055struct p9_stat *p9_client_stat(struct p9_fid *fid)
1056{
1057 int err;
1058 struct p9_fcall *tc, *rc;
1059 struct p9_client *clnt;
1060 struct p9_stat *ret;
1061
1062 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
1063 err = 0;
1064 tc = NULL;
1065 rc = NULL;
1066 ret = NULL;
1067 clnt = fid->clnt;
1068
1069 tc = p9_create_tstat(fid->fid);
1070 if (IS_ERR(tc)) {
1071 err = PTR_ERR(tc);
1072 tc = NULL;
1073 goto error;
1074 }
1075
Eric Van Hensbergene2735b72008-02-06 19:25:58 -06001076 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001077 if (err)
1078 goto error;
1079
1080 ret = p9_clone_stat(&rc->params.rstat.stat, clnt->dotu);
1081 if (IS_ERR(ret)) {
1082 err = PTR_ERR(ret);
1083 ret = NULL;
1084 goto error;
1085 }
1086
1087 kfree(tc);
1088 kfree(rc);
1089 return ret;
1090
1091error:
1092 kfree(tc);
1093 kfree(rc);
1094 kfree(ret);
1095 return ERR_PTR(err);
1096}
1097EXPORT_SYMBOL(p9_client_stat);
1098
1099int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1100{
1101 int err;
1102 struct p9_fcall *tc, *rc;
1103 struct p9_client *clnt;
1104
1105 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
1106 err = 0;
1107 tc = NULL;
1108 rc = NULL;
1109 clnt = fid->clnt;
1110
1111 tc = p9_create_twstat(fid->fid, wst, clnt->dotu);
1112 if (IS_ERR(tc)) {
1113 err = PTR_ERR(tc);
1114 tc = NULL;
1115 goto done;
1116 }
1117
Eric Van Hensbergene2735b72008-02-06 19:25:58 -06001118 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001119
1120done:
1121 kfree(tc);
1122 kfree(rc);
1123 return err;
1124}
1125EXPORT_SYMBOL(p9_client_wstat);
1126
1127struct p9_stat *p9_client_dirread(struct p9_fid *fid, u64 offset)
1128{
1129 int err, n, m;
1130 struct p9_fcall *tc, *rc;
1131 struct p9_client *clnt;
1132 struct p9_stat st, *ret;
1133
1134 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu\n", fid->fid,
1135 (long long unsigned) offset);
1136 err = 0;
1137 tc = NULL;
1138 rc = NULL;
1139 ret = NULL;
1140 clnt = fid->clnt;
1141
1142 /* if the offset is below or above the current response, free it */
1143 if (offset < fid->rdir_fpos || (fid->rdir_fcall &&
1144 offset >= fid->rdir_fpos+fid->rdir_fcall->params.rread.count)) {
1145 fid->rdir_pos = 0;
1146 if (fid->rdir_fcall)
1147 fid->rdir_fpos += fid->rdir_fcall->params.rread.count;
1148
1149 kfree(fid->rdir_fcall);
1150 fid->rdir_fcall = NULL;
1151 if (offset < fid->rdir_fpos)
1152 fid->rdir_fpos = 0;
1153 }
1154
1155 if (!fid->rdir_fcall) {
1156 n = fid->iounit;
1157 if (!n || n > clnt->msize-P9_IOHDRSZ)
1158 n = clnt->msize - P9_IOHDRSZ;
1159
1160 while (1) {
1161 if (fid->rdir_fcall) {
1162 fid->rdir_fpos +=
1163 fid->rdir_fcall->params.rread.count;
1164 kfree(fid->rdir_fcall);
1165 fid->rdir_fcall = NULL;
1166 }
1167
1168 tc = p9_create_tread(fid->fid, fid->rdir_fpos, n);
1169 if (IS_ERR(tc)) {
1170 err = PTR_ERR(tc);
1171 tc = NULL;
1172 goto error;
1173 }
1174
Eric Van Hensbergene2735b72008-02-06 19:25:58 -06001175 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001176 if (err)
1177 goto error;
1178
1179 n = rc->params.rread.count;
1180 if (n == 0)
1181 goto done;
1182
1183 fid->rdir_fcall = rc;
1184 rc = NULL;
1185 if (offset >= fid->rdir_fpos &&
1186 offset < fid->rdir_fpos+n)
1187 break;
1188 }
1189
1190 fid->rdir_pos = 0;
1191 }
1192
1193 m = offset - fid->rdir_fpos;
1194 if (m < 0)
1195 goto done;
1196
1197 n = p9_deserialize_stat(fid->rdir_fcall->params.rread.data + m,
1198 fid->rdir_fcall->params.rread.count - m, &st, clnt->dotu);
1199
1200 if (!n) {
1201 err = -EIO;
1202 goto error;
1203 }
1204
1205 fid->rdir_pos += n;
1206 st.size = n;
1207 ret = p9_clone_stat(&st, clnt->dotu);
1208 if (IS_ERR(ret)) {
1209 err = PTR_ERR(ret);
1210 ret = NULL;
1211 goto error;
1212 }
1213
1214done:
1215 kfree(tc);
1216 kfree(rc);
1217 return ret;
1218
1219error:
1220 kfree(tc);
1221 kfree(rc);
1222 kfree(ret);
1223 return ERR_PTR(err);
1224}
1225EXPORT_SYMBOL(p9_client_dirread);