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