]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/9p/client.c
9p: encapsulate version function
[linux-2.6-omap-h63xx.git] / net / 9p / client.c
1 /*
2  * net/9p/clnt.c
3  *
4  * 9P Client
5  *
6  *  Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
7  *  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>
29 #include <linux/poll.h>
30 #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>
35 #include <linux/parser.h>
36 #include <net/9p/client.h>
37 #include <net/9p/transport.h>
38
39 /*
40   * Client Option Parsing (code inspired by NFS code)
41   *  - a little lazy - parse all client options
42   */
43
44 enum {
45         Opt_msize,
46         Opt_trans,
47         Opt_legacy,
48         Opt_err,
49 };
50
51 static const match_table_t tokens = {
52         {Opt_msize, "msize=%u"},
53         {Opt_legacy, "noextend"},
54         {Opt_trans, "trans=%s"},
55         {Opt_err, NULL},
56 };
57
58 static int
59 p9_client_rpc(struct p9_client *c, struct p9_fcall *tc, struct p9_fcall **rc);
60
61 /**
62  * v9fs_parse_options - parse mount options into session structure
63  * @options: options string passed from mount
64  * @v9ses: existing v9fs session information
65  *
66  * Return 0 upon success, -ERRNO upon failure
67  */
68
69 static int parse_opts(char *opts, struct p9_client *clnt)
70 {
71         char *options;
72         char *p;
73         substring_t args[MAX_OPT_ARGS];
74         int option;
75         int ret = 0;
76
77         clnt->dotu = 1;
78         clnt->msize = 8192;
79
80         if (!opts)
81                 return 0;
82
83         options = kstrdup(opts, GFP_KERNEL);
84         if (!options) {
85                 P9_DPRINTK(P9_DEBUG_ERROR,
86                                 "failed to allocate copy of option string\n");
87                 return -ENOMEM;
88         }
89
90         while ((p = strsep(&options, ",")) != NULL) {
91                 int token;
92                 if (!*p)
93                         continue;
94                 token = match_token(p, tokens, args);
95                 if (token < Opt_trans) {
96                         int r = match_int(&args[0], &option);
97                         if (r < 0) {
98                                 P9_DPRINTK(P9_DEBUG_ERROR,
99                                         "integer field, but no integer?\n");
100                                 ret = r;
101                                 continue;
102                         }
103                 }
104                 switch (token) {
105                 case Opt_msize:
106                         clnt->msize = option;
107                         break;
108                 case Opt_trans:
109                         clnt->trans_mod = v9fs_get_trans_by_name(&args[0]);
110                         break;
111                 case Opt_legacy:
112                         clnt->dotu = 0;
113                         break;
114                 default:
115                         continue;
116                 }
117         }
118
119         if (!clnt->trans_mod)
120                 clnt->trans_mod = v9fs_get_default_trans();
121
122         kfree(options);
123         return ret;
124 }
125
126 /**
127  * p9_tag_alloc - lookup/allocate a request by tag
128  * @c: client session to lookup tag within
129  * @tag: numeric id for transaction
130  *
131  * this is a simple array lookup, but will grow the
132  * request_slots as necessary to accomodate transaction
133  * ids which did not previously have a slot.
134  *
135  * this code relies on the client spinlock to manage locks, its
136  * possible we should switch to something else, but I'd rather
137  * stick with something low-overhead for the common case.
138  *
139  */
140
141 struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag)
142 {
143         unsigned long flags;
144         int row, col;
145
146         /* This looks up the original request by tag so we know which
147          * buffer to read the data into */
148         tag++;
149
150         if (tag >= c->max_tag) {
151                 spin_lock_irqsave(&c->lock, flags);
152                 /* check again since original check was outside of lock */
153                 while (tag >= c->max_tag) {
154                         row = (tag / P9_ROW_MAXTAG);
155                         c->reqs[row] = kcalloc(P9_ROW_MAXTAG,
156                                         sizeof(struct p9_req_t), GFP_ATOMIC);
157
158                         if (!c->reqs[row]) {
159                                 printk(KERN_ERR "Couldn't grow tag array\n");
160                                 BUG();
161                         }
162                         for (col = 0; col < P9_ROW_MAXTAG; col++) {
163                                 c->reqs[row][col].status = REQ_STATUS_IDLE;
164                                 c->reqs[row][col].flush_tag = P9_NOTAG;
165                                 c->reqs[row][col].wq = kmalloc(
166                                         sizeof(wait_queue_head_t), GFP_ATOMIC);
167                                 if (!c->reqs[row][col].wq) {
168                                         printk(KERN_ERR
169                                                 "Couldn't grow tag array\n");
170                                         BUG();
171                                 }
172                                 init_waitqueue_head(c->reqs[row][col].wq);
173                         }
174                         c->max_tag += P9_ROW_MAXTAG;
175                 }
176                 spin_unlock_irqrestore(&c->lock, flags);
177         }
178         row = tag / P9_ROW_MAXTAG;
179         col = tag % P9_ROW_MAXTAG;
180
181         c->reqs[row][col].status = REQ_STATUS_ALLOC;
182         c->reqs[row][col].flush_tag = P9_NOTAG;
183
184         return &c->reqs[row][col];
185 }
186 EXPORT_SYMBOL(p9_tag_alloc);
187
188 /**
189  * p9_tag_lookup - lookup a request by tag
190  * @c: client session to lookup tag within
191  * @tag: numeric id for transaction
192  *
193  */
194
195 struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
196 {
197         int row, col;
198
199         /* This looks up the original request by tag so we know which
200          * buffer to read the data into */
201         tag++;
202
203         BUG_ON(tag >= c->max_tag);
204
205         row = tag / P9_ROW_MAXTAG;
206         col = tag % P9_ROW_MAXTAG;
207
208         return &c->reqs[row][col];
209 }
210 EXPORT_SYMBOL(p9_tag_lookup);
211
212 /**
213  * p9_tag_init - setup tags structure and contents
214  * @tags: tags structure from the client struct
215  *
216  * This initializes the tags structure for each client instance.
217  *
218  */
219
220 static int p9_tag_init(struct p9_client *c)
221 {
222         int err = 0;
223
224         c->tagpool = p9_idpool_create();
225         if (IS_ERR(c->tagpool)) {
226                 err = PTR_ERR(c->tagpool);
227                 c->tagpool = NULL;
228                 goto error;
229         }
230
231         p9_idpool_get(c->tagpool); /* reserve tag 0 */
232
233         c->max_tag = 0;
234 error:
235         return err;
236 }
237
238 /**
239  * p9_tag_cleanup - cleans up tags structure and reclaims resources
240  * @tags: tags structure from the client struct
241  *
242  * This frees resources associated with the tags structure
243  *
244  */
245 static void p9_tag_cleanup(struct p9_client *c)
246 {
247         int row, col;
248
249         /* check to insure all requests are idle */
250         for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
251                 for (col = 0; col < P9_ROW_MAXTAG; col++) {
252                         if (c->reqs[row][col].status != REQ_STATUS_IDLE) {
253                                 P9_DPRINTK(P9_DEBUG_MUX,
254                                   "Attempting to cleanup non-free tag %d,%d\n",
255                                   row, col);
256                                 /* TODO: delay execution of cleanup */
257                                 return;
258                         }
259                 }
260         }
261
262         if (c->tagpool)
263                 p9_idpool_destroy(c->tagpool);
264
265         /* free requests associated with tags */
266         for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
267                 for (col = 0; col < P9_ROW_MAXTAG; col++)
268                         kfree(c->reqs[row][col].wq);
269                 kfree(c->reqs[row]);
270         }
271         c->max_tag = 0;
272 }
273
274 /**
275  * p9_client_flush - flush (cancel) a request
276  * c: client state
277  * req: request to cancel
278  *
279  * This sents a flush for a particular requests and links
280  * the flush request to the original request.  The current
281  * code only supports a single flush request although the protocol
282  * allows for multiple flush requests to be sent for a single request.
283  *
284  */
285
286 static int p9_client_flush(struct p9_client *c, struct p9_req_t *req)
287 {
288         struct p9_fcall *tc, *rc = NULL;
289         int err;
290
291         P9_DPRINTK(P9_DEBUG_9P, "client %p tag %d\n", c, req->tc->tag);
292
293         tc = p9_create_tflush(req->tc->tag);
294         if (IS_ERR(tc))
295                 return PTR_ERR(tc);
296
297         err = p9_client_rpc(c, tc, &rc);
298
299         /* we don't free anything here because RPC isn't complete */
300
301         return err;
302 }
303
304 /**
305  * p9_free_req - free a request and clean-up as necessary
306  * c: client state
307  * r: request to release
308  *
309  */
310
311 void p9_free_req(struct p9_client *c, struct p9_req_t *r)
312 {
313         r->flush_tag = P9_NOTAG;
314         r->status = REQ_STATUS_IDLE;
315         if (r->tc->tag != P9_NOTAG && p9_idpool_check(r->tc->tag, c->tagpool))
316                 p9_idpool_put(r->tc->tag, c->tagpool);
317
318         /* if this was a flush request we have to free response fcall */
319         if (r->tc->id == P9_TFLUSH) {
320                 kfree(r->tc);
321                 kfree(r->rc);
322         }
323 }
324
325 /**
326  * p9_client_cb - call back from transport to client
327  * c: client state
328  * req: request received
329  *
330  */
331 void p9_client_cb(struct p9_client *c, struct p9_req_t *req)
332 {
333         struct p9_req_t *other_req;
334         unsigned long flags;
335
336         P9_DPRINTK(P9_DEBUG_MUX, ": %d\n", req->tc->tag);
337
338         if (req->status == REQ_STATUS_ERROR)
339                 wake_up(req->wq);
340
341         if (req->tc->id == P9_TFLUSH) { /* flush receive path */
342                 P9_DPRINTK(P9_DEBUG_MUX, "flush: %d\n", req->tc->tag);
343                 spin_lock_irqsave(&c->lock, flags);
344                 other_req = p9_tag_lookup(c, req->tc->params.tflush.oldtag);
345                 if (other_req->flush_tag != req->tc->tag) /* stale flush */
346                         spin_unlock_irqrestore(&c->lock, flags);
347                 else {
348                         BUG_ON(other_req->status != REQ_STATUS_FLSH);
349                         other_req->status = REQ_STATUS_FLSHD;
350                         spin_unlock_irqrestore(&c->lock, flags);
351                         wake_up(other_req->wq);
352                 }
353                 p9_free_req(c, req);
354         } else {                                /* normal receive path */
355                 P9_DPRINTK(P9_DEBUG_MUX, "normal: %d\n", req->tc->tag);
356                 spin_lock_irqsave(&c->lock, flags);
357                 if (req->status != REQ_STATUS_FLSHD)
358                         req->status = REQ_STATUS_RCVD;
359                 req->flush_tag = P9_NOTAG;
360                 spin_unlock_irqrestore(&c->lock, flags);
361                 wake_up(req->wq);
362                 P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag);
363         }
364 }
365 EXPORT_SYMBOL(p9_client_cb);
366
367 /**
368  * p9_client_rpc - issue a request and wait for a response
369  * @c: client session
370  * @tc: &p9_fcall request to transmit
371  * @rc: &p9_fcall to put reponse into
372  *
373  * Returns 0 on success, error code on failure
374  */
375
376 static int
377 p9_client_rpc(struct p9_client *c, struct p9_fcall *tc, struct p9_fcall **rc)
378 {
379         int tag, err, size;
380         char *rdata;
381         struct p9_req_t *req;
382         unsigned long flags;
383         int sigpending;
384         int flushed = 0;
385
386         P9_DPRINTK(P9_DEBUG_9P, "client %p tc %p rc %p\n", c, tc, rc);
387
388         if (c->status != Connected)
389                 return -EIO;
390
391         if (signal_pending(current)) {
392                 sigpending = 1;
393                 clear_thread_flag(TIF_SIGPENDING);
394         } else
395                 sigpending = 0;
396
397         tag = P9_NOTAG;
398         if (tc->id != P9_TVERSION) {
399                 tag = p9_idpool_get(c->tagpool);
400                 if (tag < 0)
401                         return -ENOMEM;
402         }
403
404         req = p9_tag_alloc(c, tag);
405
406         /* if this is a flush request, backlink flush request now to
407          * avoid race conditions later. */
408         if (tc->id == P9_TFLUSH) {
409                 struct p9_req_t *other_req =
410                                 p9_tag_lookup(c, tc->params.tflush.oldtag);
411                 if (other_req->status == REQ_STATUS_FLSH)
412                         other_req->flush_tag = tag;
413         }
414
415         p9_set_tag(tc, tag);
416
417         /*
418          * if client passed in a pre-allocated response fcall struct
419          * then we just use that, otherwise we allocate one.
420          */
421
422         if (rc == NULL)
423                 req->rc = NULL;
424         else
425                 req->rc = *rc;
426         if (req->rc == NULL) {
427                 req->rc = kmalloc(sizeof(struct p9_fcall) + c->msize,
428                                                                 GFP_KERNEL);
429                 if (!req->rc) {
430                         err = -ENOMEM;
431                         p9_idpool_put(tag, c->tagpool);
432                         p9_free_req(c, req);
433                         goto reterr;
434                 }
435                 *rc = req->rc;
436         }
437
438         rdata = (char *)req->rc+sizeof(struct p9_fcall);
439
440         req->tc = tc;
441         P9_DPRINTK(P9_DEBUG_9P, "request: tc: %p rc: %p\n", req->tc, req->rc);
442
443         err = c->trans_mod->request(c, req);
444         if (err < 0) {
445                 c->status = Disconnected;
446                 goto reterr;
447         }
448
449         /* if it was a flush we just transmitted, return our tag */
450         if (tc->id == P9_TFLUSH)
451                 return 0;
452 again:
453         P9_DPRINTK(P9_DEBUG_9P, "wait %p tag: %d\n", req->wq, tag);
454         err = wait_event_interruptible(*req->wq,
455                                                 req->status >= REQ_STATUS_RCVD);
456         P9_DPRINTK(P9_DEBUG_9P, "wait %p tag: %d returned %d (flushed=%d)\n",
457                                                 req->wq, tag, err, flushed);
458
459         if (req->status == REQ_STATUS_ERROR) {
460                 P9_DPRINTK(P9_DEBUG_9P, "req_status error %d\n", req->t_err);
461                 err = req->t_err;
462         } else if (err == -ERESTARTSYS && flushed) {
463                 P9_DPRINTK(P9_DEBUG_9P, "flushed - going again\n");
464                 goto again;
465         } else if (req->status == REQ_STATUS_FLSHD) {
466                 P9_DPRINTK(P9_DEBUG_9P, "flushed - erestartsys\n");
467                 err = -ERESTARTSYS;
468         }
469
470         if ((err == -ERESTARTSYS) && (c->status == Connected) && (!flushed)) {
471                 P9_DPRINTK(P9_DEBUG_9P, "flushing\n");
472                 spin_lock_irqsave(&c->lock, flags);
473                 if (req->status == REQ_STATUS_SENT)
474                         req->status = REQ_STATUS_FLSH;
475                 spin_unlock_irqrestore(&c->lock, flags);
476                 sigpending = 1;
477                 flushed = 1;
478                 clear_thread_flag(TIF_SIGPENDING);
479
480                 if (c->trans_mod->cancel(c, req)) {
481                         err = p9_client_flush(c, req);
482                         if (err == 0)
483                                 goto again;
484                 }
485         }
486
487         if (sigpending) {
488                 spin_lock_irqsave(&current->sighand->siglock, flags);
489                 recalc_sigpending();
490                 spin_unlock_irqrestore(&current->sighand->siglock, flags);
491         }
492
493         if (err < 0)
494                 goto reterr;
495
496         size = le32_to_cpu(*(__le32 *) rdata);
497
498         err = p9_deserialize_fcall(rdata, size, req->rc, c->dotu);
499         if (err < 0) {
500                 P9_DPRINTK(P9_DEBUG_9P,
501                         "9p debug: client rpc deserialize returned %d\n", err);
502                 goto reterr;
503         }
504
505 #ifdef CONFIG_NET_9P_DEBUG
506         if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
507                 char buf[150];
508
509                 p9_printfcall(buf, sizeof(buf), req->rc, c->dotu);
510                 printk(KERN_NOTICE ">>> %p %s\n", c, buf);
511         }
512 #endif
513
514         if (req->rc->id == P9_RERROR) {
515                 int ecode = req->rc->params.rerror.errno;
516                 struct p9_str *ename = &req->rc->params.rerror.error;
517
518                 P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len,
519                                                                 ename->str);
520
521                 if (c->dotu)
522                         err = -ecode;
523
524                 if (!err) {
525                         err = p9_errstr2errno(ename->str, ename->len);
526
527                         /* string match failed */
528                         if (!err) {
529                                 PRINT_FCALL_ERROR("unknown error", req->rc);
530                                 err = -ESERVERFAULT;
531                         }
532                 }
533         } else
534                 err = 0;
535
536 reterr:
537         p9_free_req(c, req);
538
539         P9_DPRINTK(P9_DEBUG_9P, "returning %d\n", err);
540         return err;
541 }
542
543 static struct p9_fid *p9_fid_create(struct p9_client *clnt)
544 {
545         int err;
546         struct p9_fid *fid;
547
548         P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
549         fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
550         if (!fid)
551                 return ERR_PTR(-ENOMEM);
552
553         fid->fid = p9_idpool_get(clnt->fidpool);
554         if (fid->fid < 0) {
555                 err = -ENOSPC;
556                 goto error;
557         }
558
559         memset(&fid->qid, 0, sizeof(struct p9_qid));
560         fid->mode = -1;
561         fid->rdir_fpos = 0;
562         fid->uid = current->fsuid;
563         fid->clnt = clnt;
564         fid->aux = NULL;
565
566         spin_lock(&clnt->lock);
567         list_add(&fid->flist, &clnt->fidlist);
568         spin_unlock(&clnt->lock);
569
570         return fid;
571
572 error:
573         kfree(fid);
574         return ERR_PTR(err);
575 }
576
577 static void p9_fid_destroy(struct p9_fid *fid)
578 {
579         struct p9_client *clnt;
580
581         P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
582         clnt = fid->clnt;
583         p9_idpool_put(fid->fid, clnt->fidpool);
584         spin_lock(&clnt->lock);
585         list_del(&fid->flist);
586         spin_unlock(&clnt->lock);
587         kfree(fid);
588 }
589
590 static int p9_client_version(struct p9_client *clnt)
591 {
592         int err = 0;
593         struct p9_fcall *tc, *rc;
594         struct p9_str *version;
595
596         P9_DPRINTK(P9_DEBUG_9P, "%p\n", clnt);
597         err = 0;
598         tc = NULL;
599         rc = NULL;
600
601         tc = p9_create_tversion(clnt->msize,
602                                         clnt->dotu ? "9P2000.u" : "9P2000");
603         if (IS_ERR(tc)) {
604                 err = PTR_ERR(tc);
605                 tc = NULL;
606                 goto error;
607         }
608
609         err = p9_client_rpc(clnt, tc, &rc);
610         if (err)
611                 goto error;
612
613         version = &rc->params.rversion.version;
614         if (version->len == 8 && !memcmp(version->str, "9P2000.u", 8))
615                 clnt->dotu = 1;
616         else if (version->len == 6 && !memcmp(version->str, "9P2000", 6))
617                 clnt->dotu = 0;
618         else {
619                 err = -EREMOTEIO;
620                 goto error;
621         }
622
623         if (rc->params.rversion.msize < clnt->msize)
624                 clnt->msize = rc->params.rversion.msize;
625
626 error:
627         kfree(tc);
628         kfree(rc);
629
630         return err;
631 }
632 EXPORT_SYMBOL(p9_client_auth);
633
634 struct p9_client *p9_client_create(const char *dev_name, char *options)
635 {
636         int err;
637         struct p9_client *clnt;
638
639         err = 0;
640         clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
641         if (!clnt)
642                 return ERR_PTR(-ENOMEM);
643
644         clnt->trans_mod = NULL;
645         clnt->trans = NULL;
646         spin_lock_init(&clnt->lock);
647         INIT_LIST_HEAD(&clnt->fidlist);
648         clnt->fidpool = p9_idpool_create();
649         if (IS_ERR(clnt->fidpool)) {
650                 err = PTR_ERR(clnt->fidpool);
651                 clnt->fidpool = NULL;
652                 goto error;
653         }
654
655         p9_tag_init(clnt);
656
657         err = parse_opts(options, clnt);
658         if (err < 0)
659                 goto error;
660
661         if (clnt->trans_mod == NULL) {
662                 err = -EPROTONOSUPPORT;
663                 P9_DPRINTK(P9_DEBUG_ERROR,
664                                 "No transport defined or default transport\n");
665                 goto error;
666         }
667
668         P9_DPRINTK(P9_DEBUG_9P, "clnt %p trans %p msize %d dotu %d\n",
669                 clnt, clnt->trans_mod, clnt->msize, clnt->dotu);
670
671         err = clnt->trans_mod->create(clnt, dev_name, options);
672         if (err)
673                 goto error;
674
675         if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize)
676                 clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ;
677
678         err = p9_client_version(clnt);
679         if (err)
680                 goto error;
681
682         return clnt;
683
684 error:
685         p9_client_destroy(clnt);
686         return ERR_PTR(err);
687 }
688 EXPORT_SYMBOL(p9_client_create);
689
690 void p9_client_destroy(struct p9_client *clnt)
691 {
692         struct p9_fid *fid, *fidptr;
693
694         P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
695
696         if (clnt->trans_mod)
697                 clnt->trans_mod->close(clnt);
698
699         v9fs_put_trans(clnt->trans_mod);
700
701         list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist)
702                 p9_fid_destroy(fid);
703
704         if (clnt->fidpool)
705                 p9_idpool_destroy(clnt->fidpool);
706
707         p9_tag_cleanup(clnt);
708
709         kfree(clnt);
710 }
711 EXPORT_SYMBOL(p9_client_destroy);
712
713 void p9_client_disconnect(struct p9_client *clnt)
714 {
715         P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
716         clnt->status = Disconnected;
717 }
718 EXPORT_SYMBOL(p9_client_disconnect);
719
720 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
721         char *uname, u32 n_uname, char *aname)
722 {
723         int err;
724         struct p9_fcall *tc, *rc;
725         struct p9_fid *fid;
726
727         P9_DPRINTK(P9_DEBUG_9P, "clnt %p afid %d uname %s aname %s\n",
728                 clnt, afid?afid->fid:-1, uname, aname);
729         err = 0;
730         tc = NULL;
731         rc = NULL;
732
733         fid = p9_fid_create(clnt);
734         if (IS_ERR(fid)) {
735                 err = PTR_ERR(fid);
736                 fid = NULL;
737                 goto error;
738         }
739
740         tc = p9_create_tattach(fid->fid, afid?afid->fid:P9_NOFID, uname, aname,
741                 n_uname, clnt->dotu);
742         if (IS_ERR(tc)) {
743                 err = PTR_ERR(tc);
744                 tc = NULL;
745                 goto error;
746         }
747
748         err = p9_client_rpc(clnt, tc, &rc);
749         if (err)
750                 goto error;
751
752         memmove(&fid->qid, &rc->params.rattach.qid, sizeof(struct p9_qid));
753         kfree(tc);
754         kfree(rc);
755         return fid;
756
757 error:
758         kfree(tc);
759         kfree(rc);
760         if (fid)
761                 p9_fid_destroy(fid);
762         return ERR_PTR(err);
763 }
764 EXPORT_SYMBOL(p9_client_attach);
765
766 struct p9_fid *p9_client_auth(struct p9_client *clnt, char *uname,
767         u32 n_uname, char *aname)
768 {
769         int err;
770         struct p9_fcall *tc, *rc;
771         struct p9_fid *fid;
772
773         P9_DPRINTK(P9_DEBUG_9P, "clnt %p uname %s aname %s\n", clnt, uname,
774                                                                         aname);
775         err = 0;
776         tc = NULL;
777         rc = NULL;
778
779         fid = p9_fid_create(clnt);
780         if (IS_ERR(fid)) {
781                 err = PTR_ERR(fid);
782                 fid = NULL;
783                 goto error;
784         }
785
786         tc = p9_create_tauth(fid->fid, uname, aname, n_uname, clnt->dotu);
787         if (IS_ERR(tc)) {
788                 err = PTR_ERR(tc);
789                 tc = NULL;
790                 goto error;
791         }
792
793         err = p9_client_rpc(clnt, tc, &rc);
794         if (err)
795                 goto error;
796
797         memmove(&fid->qid, &rc->params.rauth.qid, sizeof(struct p9_qid));
798         kfree(tc);
799         kfree(rc);
800         return fid;
801
802 error:
803         kfree(tc);
804         kfree(rc);
805         if (fid)
806                 p9_fid_destroy(fid);
807         return ERR_PTR(err);
808 }
809 EXPORT_SYMBOL(p9_client_auth);
810
811 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames,
812         int clone)
813 {
814         int err;
815         struct p9_fcall *tc, *rc;
816         struct p9_client *clnt;
817         struct p9_fid *fid;
818
819         P9_DPRINTK(P9_DEBUG_9P, "fid %d nwname %d wname[0] %s\n",
820                 oldfid->fid, nwname, wnames?wnames[0]:NULL);
821         err = 0;
822         tc = NULL;
823         rc = NULL;
824         clnt = oldfid->clnt;
825         if (clone) {
826                 fid = p9_fid_create(clnt);
827                 if (IS_ERR(fid)) {
828                         err = PTR_ERR(fid);
829                         fid = NULL;
830                         goto error;
831                 }
832
833                 fid->uid = oldfid->uid;
834         } else
835                 fid = oldfid;
836
837         tc = p9_create_twalk(oldfid->fid, fid->fid, nwname, wnames);
838         if (IS_ERR(tc)) {
839                 err = PTR_ERR(tc);
840                 tc = NULL;
841                 goto error;
842         }
843
844         err = p9_client_rpc(clnt, tc, &rc);
845         if (err) {
846                 if (rc && rc->id == P9_RWALK)
847                         goto clunk_fid;
848                 else
849                         goto error;
850         }
851
852         if (rc->params.rwalk.nwqid != nwname) {
853                 err = -ENOENT;
854                 goto clunk_fid;
855         }
856
857         if (nwname)
858                 memmove(&fid->qid,
859                         &rc->params.rwalk.wqids[rc->params.rwalk.nwqid - 1],
860                         sizeof(struct p9_qid));
861         else
862                 fid->qid = oldfid->qid;
863
864         kfree(tc);
865         kfree(rc);
866         return fid;
867
868 clunk_fid:
869         kfree(tc);
870         kfree(rc);
871         rc = NULL;
872         tc = p9_create_tclunk(fid->fid);
873         if (IS_ERR(tc)) {
874                 err = PTR_ERR(tc);
875                 tc = NULL;
876                 goto error;
877         }
878
879         p9_client_rpc(clnt, tc, &rc);
880
881 error:
882         kfree(tc);
883         kfree(rc);
884         if (fid && (fid != oldfid))
885                 p9_fid_destroy(fid);
886
887         return ERR_PTR(err);
888 }
889 EXPORT_SYMBOL(p9_client_walk);
890
891 int p9_client_open(struct p9_fid *fid, int mode)
892 {
893         int err;
894         struct p9_fcall *tc, *rc;
895         struct p9_client *clnt;
896
897         P9_DPRINTK(P9_DEBUG_9P, "fid %d mode %d\n", fid->fid, mode);
898         err = 0;
899         tc = NULL;
900         rc = NULL;
901         clnt = fid->clnt;
902
903         if (fid->mode != -1)
904                 return -EINVAL;
905
906         tc = p9_create_topen(fid->fid, mode);
907         if (IS_ERR(tc)) {
908                 err = PTR_ERR(tc);
909                 tc = NULL;
910                 goto done;
911         }
912
913         err = p9_client_rpc(clnt, tc, &rc);
914         if (err)
915                 goto done;
916
917         fid->mode = mode;
918         fid->iounit = rc->params.ropen.iounit;
919
920 done:
921         kfree(tc);
922         kfree(rc);
923         return err;
924 }
925 EXPORT_SYMBOL(p9_client_open);
926
927 int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
928                      char *extension)
929 {
930         int err;
931         struct p9_fcall *tc, *rc;
932         struct p9_client *clnt;
933
934         P9_DPRINTK(P9_DEBUG_9P, "fid %d name %s perm %d mode %d\n", fid->fid,
935                 name, perm, mode);
936         err = 0;
937         tc = NULL;
938         rc = NULL;
939         clnt = fid->clnt;
940
941         if (fid->mode != -1)
942                 return -EINVAL;
943
944         tc = p9_create_tcreate(fid->fid, name, perm, mode, extension,
945                                                                clnt->dotu);
946         if (IS_ERR(tc)) {
947                 err = PTR_ERR(tc);
948                 tc = NULL;
949                 goto done;
950         }
951
952         err = p9_client_rpc(clnt, tc, &rc);
953         if (err)
954                 goto done;
955
956         fid->mode = mode;
957         fid->iounit = rc->params.ropen.iounit;
958
959 done:
960         kfree(tc);
961         kfree(rc);
962         return err;
963 }
964 EXPORT_SYMBOL(p9_client_fcreate);
965
966 int p9_client_clunk(struct p9_fid *fid)
967 {
968         int err;
969         struct p9_fcall *tc, *rc;
970         struct p9_client *clnt;
971
972         P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
973         err = 0;
974         tc = NULL;
975         rc = NULL;
976         clnt = fid->clnt;
977
978         tc = p9_create_tclunk(fid->fid);
979         if (IS_ERR(tc)) {
980                 err = PTR_ERR(tc);
981                 tc = NULL;
982                 goto done;
983         }
984
985         err = p9_client_rpc(clnt, tc, &rc);
986         if (err)
987                 goto done;
988
989         p9_fid_destroy(fid);
990
991 done:
992         kfree(tc);
993         kfree(rc);
994         return err;
995 }
996 EXPORT_SYMBOL(p9_client_clunk);
997
998 int p9_client_remove(struct p9_fid *fid)
999 {
1000         int err;
1001         struct p9_fcall *tc, *rc;
1002         struct p9_client *clnt;
1003
1004         P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
1005         err = 0;
1006         tc = NULL;
1007         rc = NULL;
1008         clnt = fid->clnt;
1009
1010         tc = p9_create_tremove(fid->fid);
1011         if (IS_ERR(tc)) {
1012                 err = PTR_ERR(tc);
1013                 tc = NULL;
1014                 goto done;
1015         }
1016
1017         err = p9_client_rpc(clnt, tc, &rc);
1018         if (err)
1019                 goto done;
1020
1021         p9_fid_destroy(fid);
1022
1023 done:
1024         kfree(tc);
1025         kfree(rc);
1026         return err;
1027 }
1028 EXPORT_SYMBOL(p9_client_remove);
1029
1030 int
1031 p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
1032                                                                 u32 count)
1033 {
1034         int err, n, rsize, total;
1035         struct p9_fcall *tc, *rc;
1036         struct p9_client *clnt;
1037
1038         P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu %d\n", fid->fid,
1039                                         (long long unsigned) offset, count);
1040         err = 0;
1041         tc = NULL;
1042         rc = NULL;
1043         clnt = fid->clnt;
1044         total = 0;
1045
1046         rsize = fid->iounit;
1047         if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1048                 rsize = clnt->msize - P9_IOHDRSZ;
1049
1050         do {
1051                 if (count < rsize)
1052                         rsize = count;
1053
1054                 tc = p9_create_tread(fid->fid, offset, rsize);
1055                 if (IS_ERR(tc)) {
1056                         err = PTR_ERR(tc);
1057                         tc = NULL;
1058                         goto error;
1059                 }
1060
1061                 err = p9_client_rpc(clnt, tc, &rc);
1062                 if (err)
1063                         goto error;
1064
1065                 n = rc->params.rread.count;
1066                 if (n > count)
1067                         n = count;
1068
1069                 if (data) {
1070                         memmove(data, rc->params.rread.data, n);
1071                         data += n;
1072                 }
1073
1074                 if (udata) {
1075                         err = copy_to_user(udata, rc->params.rread.data, n);
1076                         if (err) {
1077                                 err = -EFAULT;
1078                                 goto error;
1079                         }
1080                         udata += n;
1081                 }
1082
1083                 count -= n;
1084                 offset += n;
1085                 total += n;
1086                 kfree(tc);
1087                 tc = NULL;
1088                 kfree(rc);
1089                 rc = NULL;
1090         } while (count > 0 && n == rsize);
1091
1092         return total;
1093
1094 error:
1095         kfree(tc);
1096         kfree(rc);
1097         return err;
1098 }
1099 EXPORT_SYMBOL(p9_client_read);
1100
1101 int
1102 p9_client_write(struct p9_fid *fid, char *data, const char __user *udata,
1103                                                         u64 offset, u32 count)
1104 {
1105         int err, n, rsize, total;
1106         struct p9_fcall *tc, *rc;
1107         struct p9_client *clnt;
1108
1109         P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid,
1110                                         (long long unsigned) offset, count);
1111         err = 0;
1112         tc = NULL;
1113         rc = NULL;
1114         clnt = fid->clnt;
1115         total = 0;
1116
1117         rsize = fid->iounit;
1118         if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1119                 rsize = clnt->msize - P9_IOHDRSZ;
1120
1121         do {
1122                 if (count < rsize)
1123                         rsize = count;
1124
1125                 if (data)
1126                         tc = p9_create_twrite(fid->fid, offset, rsize, data);
1127                 else
1128                         tc = p9_create_twrite_u(fid->fid, offset, rsize, udata);
1129                 if (IS_ERR(tc)) {
1130                         err = PTR_ERR(tc);
1131                         tc = NULL;
1132                         goto error;
1133                 }
1134
1135                 err = p9_client_rpc(clnt, tc, &rc);
1136                 if (err)
1137                         goto error;
1138
1139                 n = rc->params.rread.count;
1140                 count -= n;
1141
1142                 if (data)
1143                         data += n;
1144                 else
1145                         udata += n;
1146
1147                 offset += n;
1148                 total += n;
1149                 kfree(tc);
1150                 tc = NULL;
1151                 kfree(rc);
1152                 rc = NULL;
1153         } while (count > 0);
1154
1155         return total;
1156
1157 error:
1158         kfree(tc);
1159         kfree(rc);
1160         return err;
1161 }
1162 EXPORT_SYMBOL(p9_client_write);
1163
1164 static struct p9_stat *p9_clone_stat(struct p9_stat *st, int dotu)
1165 {
1166         int n;
1167         char *p;
1168         struct p9_stat *ret;
1169
1170         n = sizeof(struct p9_stat) + st->name.len + st->uid.len + st->gid.len +
1171                 st->muid.len;
1172
1173         if (dotu)
1174                 n += st->extension.len;
1175
1176         ret = kmalloc(n, GFP_KERNEL);
1177         if (!ret)
1178                 return ERR_PTR(-ENOMEM);
1179
1180         memmove(ret, st, sizeof(struct p9_stat));
1181         p = ((char *) ret) + sizeof(struct p9_stat);
1182         memmove(p, st->name.str, st->name.len);
1183         ret->name.str = p;
1184         p += st->name.len;
1185         memmove(p, st->uid.str, st->uid.len);
1186         ret->uid.str = p;
1187         p += st->uid.len;
1188         memmove(p, st->gid.str, st->gid.len);
1189         ret->gid.str = p;
1190         p += st->gid.len;
1191         memmove(p, st->muid.str, st->muid.len);
1192         ret->muid.str = p;
1193         p += st->muid.len;
1194
1195         if (dotu) {
1196                 memmove(p, st->extension.str, st->extension.len);
1197                 ret->extension.str = p;
1198                 p += st->extension.len;
1199         }
1200
1201         return ret;
1202 }
1203
1204 struct p9_stat *p9_client_stat(struct p9_fid *fid)
1205 {
1206         int err;
1207         struct p9_fcall *tc, *rc;
1208         struct p9_client *clnt;
1209         struct p9_stat *ret;
1210
1211         P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
1212         err = 0;
1213         tc = NULL;
1214         rc = NULL;
1215         ret = NULL;
1216         clnt = fid->clnt;
1217
1218         tc = p9_create_tstat(fid->fid);
1219         if (IS_ERR(tc)) {
1220                 err = PTR_ERR(tc);
1221                 tc = NULL;
1222                 goto error;
1223         }
1224
1225         err = p9_client_rpc(clnt, tc, &rc);
1226         if (err)
1227                 goto error;
1228
1229         ret = p9_clone_stat(&rc->params.rstat.stat, clnt->dotu);
1230         if (IS_ERR(ret)) {
1231                 err = PTR_ERR(ret);
1232                 ret = NULL;
1233                 goto error;
1234         }
1235
1236         kfree(tc);
1237         kfree(rc);
1238         return ret;
1239
1240 error:
1241         kfree(tc);
1242         kfree(rc);
1243         kfree(ret);
1244         return ERR_PTR(err);
1245 }
1246 EXPORT_SYMBOL(p9_client_stat);
1247
1248 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1249 {
1250         int err;
1251         struct p9_fcall *tc, *rc;
1252         struct p9_client *clnt;
1253
1254         P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
1255         err = 0;
1256         tc = NULL;
1257         rc = NULL;
1258         clnt = fid->clnt;
1259
1260         tc = p9_create_twstat(fid->fid, wst, clnt->dotu);
1261         if (IS_ERR(tc)) {
1262                 err = PTR_ERR(tc);
1263                 tc = NULL;
1264                 goto done;
1265         }
1266
1267         err = p9_client_rpc(clnt, tc, &rc);
1268
1269 done:
1270         kfree(tc);
1271         kfree(rc);
1272         return err;
1273 }
1274 EXPORT_SYMBOL(p9_client_wstat);