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