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