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