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