fs/9p: Update zero-copy implementation in 9p
[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/slab.h>
33 #include <linux/sched.h>
34 #include <linux/uaccess.h>
35 #include <net/9p/9p.h>
36 #include <linux/parser.h>
37 #include <net/9p/client.h>
38 #include <net/9p/transport.h>
39 #include "protocol.h"
40
41 /*
42   * Client Option Parsing (code inspired by NFS code)
43   *  - a little lazy - parse all client options
44   */
45
46 enum {
47         Opt_msize,
48         Opt_trans,
49         Opt_legacy,
50         Opt_version,
51         Opt_err,
52 };
53
54 static const match_table_t tokens = {
55         {Opt_msize, "msize=%u"},
56         {Opt_legacy, "noextend"},
57         {Opt_trans, "trans=%s"},
58         {Opt_version, "version=%s"},
59         {Opt_err, NULL},
60 };
61
62 inline int p9_is_proto_dotl(struct p9_client *clnt)
63 {
64         return clnt->proto_version == p9_proto_2000L;
65 }
66 EXPORT_SYMBOL(p9_is_proto_dotl);
67
68 inline int p9_is_proto_dotu(struct p9_client *clnt)
69 {
70         return clnt->proto_version == p9_proto_2000u;
71 }
72 EXPORT_SYMBOL(p9_is_proto_dotu);
73
74 /* Interpret mount option for protocol version */
75 static int get_protocol_version(char *s)
76 {
77         int version = -EINVAL;
78
79         if (!strcmp(s, "9p2000")) {
80                 version = p9_proto_legacy;
81                 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: Legacy\n");
82         } else if (!strcmp(s, "9p2000.u")) {
83                 version = p9_proto_2000u;
84                 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: 9P2000.u\n");
85         } else if (!strcmp(s, "9p2000.L")) {
86                 version = p9_proto_2000L;
87                 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: 9P2000.L\n");
88         } else
89                 printk(KERN_INFO "9p: Unknown protocol version %s.\n", s);
90
91         return version;
92 }
93
94 /**
95  * parse_options - parse mount options into client structure
96  * @opts: options string passed from mount
97  * @clnt: existing v9fs client information
98  *
99  * Return 0 upon success, -ERRNO upon failure
100  */
101
102 static int parse_opts(char *opts, struct p9_client *clnt)
103 {
104         char *options, *tmp_options;
105         char *p;
106         substring_t args[MAX_OPT_ARGS];
107         int option;
108         char *s;
109         int ret = 0;
110
111         clnt->proto_version = p9_proto_2000u;
112         clnt->msize = 8192;
113
114         if (!opts)
115                 return 0;
116
117         tmp_options = kstrdup(opts, GFP_KERNEL);
118         if (!tmp_options) {
119                 P9_DPRINTK(P9_DEBUG_ERROR,
120                                 "failed to allocate copy of option string\n");
121                 return -ENOMEM;
122         }
123         options = tmp_options;
124
125         while ((p = strsep(&options, ",")) != NULL) {
126                 int token;
127                 if (!*p)
128                         continue;
129                 token = match_token(p, tokens, args);
130                 if (token < Opt_trans) {
131                         int r = match_int(&args[0], &option);
132                         if (r < 0) {
133                                 P9_DPRINTK(P9_DEBUG_ERROR,
134                                         "integer field, but no integer?\n");
135                                 ret = r;
136                                 continue;
137                         }
138                 }
139                 switch (token) {
140                 case Opt_msize:
141                         clnt->msize = option;
142                         break;
143                 case Opt_trans:
144                         s = match_strdup(&args[0]);
145                         if (!s) {
146                                 ret = -ENOMEM;
147                                 P9_DPRINTK(P9_DEBUG_ERROR,
148                                         "problem allocating copy of trans arg\n");
149                                 goto free_and_return;
150                          }
151                         clnt->trans_mod = v9fs_get_trans_by_name(s);
152                         if (clnt->trans_mod == NULL) {
153                                 printk(KERN_INFO
154                                         "9p: Could not find "
155                                         "request transport: %s\n", s);
156                                 ret = -EINVAL;
157                                 kfree(s);
158                                 goto free_and_return;
159                         }
160                         kfree(s);
161                         break;
162                 case Opt_legacy:
163                         clnt->proto_version = p9_proto_legacy;
164                         break;
165                 case Opt_version:
166                         s = match_strdup(&args[0]);
167                         if (!s) {
168                                 ret = -ENOMEM;
169                                 P9_DPRINTK(P9_DEBUG_ERROR,
170                                         "problem allocating copy of version arg\n");
171                                 goto free_and_return;
172                         }
173                         ret = get_protocol_version(s);
174                         if (ret == -EINVAL) {
175                                 kfree(s);
176                                 goto free_and_return;
177                         }
178                         kfree(s);
179                         clnt->proto_version = ret;
180                         break;
181                 default:
182                         continue;
183                 }
184         }
185
186 free_and_return:
187         kfree(tmp_options);
188         return ret;
189 }
190
191 /**
192  * p9_tag_alloc - lookup/allocate a request by tag
193  * @c: client session to lookup tag within
194  * @tag: numeric id for transaction
195  *
196  * this is a simple array lookup, but will grow the
197  * request_slots as necessary to accommodate transaction
198  * ids which did not previously have a slot.
199  *
200  * this code relies on the client spinlock to manage locks, its
201  * possible we should switch to something else, but I'd rather
202  * stick with something low-overhead for the common case.
203  *
204  */
205
206 static struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag, int max_size)
207 {
208         unsigned long flags;
209         int row, col;
210         struct p9_req_t *req;
211         int alloc_msize = min(c->msize, max_size);
212
213         /* This looks up the original request by tag so we know which
214          * buffer to read the data into */
215         tag++;
216
217         if (tag >= c->max_tag) {
218                 spin_lock_irqsave(&c->lock, flags);
219                 /* check again since original check was outside of lock */
220                 while (tag >= c->max_tag) {
221                         row = (tag / P9_ROW_MAXTAG);
222                         c->reqs[row] = kcalloc(P9_ROW_MAXTAG,
223                                         sizeof(struct p9_req_t), GFP_ATOMIC);
224
225                         if (!c->reqs[row]) {
226                                 printk(KERN_ERR "Couldn't grow tag array\n");
227                                 spin_unlock_irqrestore(&c->lock, flags);
228                                 return ERR_PTR(-ENOMEM);
229                         }
230                         for (col = 0; col < P9_ROW_MAXTAG; col++) {
231                                 c->reqs[row][col].status = REQ_STATUS_IDLE;
232                                 c->reqs[row][col].tc = NULL;
233                         }
234                         c->max_tag += P9_ROW_MAXTAG;
235                 }
236                 spin_unlock_irqrestore(&c->lock, flags);
237         }
238         row = tag / P9_ROW_MAXTAG;
239         col = tag % P9_ROW_MAXTAG;
240
241         req = &c->reqs[row][col];
242         if (!req->tc) {
243                 req->wq = kmalloc(sizeof(wait_queue_head_t), GFP_NOFS);
244                 if (!req->wq) {
245                         printk(KERN_ERR "Couldn't grow tag array\n");
246                         return ERR_PTR(-ENOMEM);
247                 }
248                 init_waitqueue_head(req->wq);
249                 req->tc = kmalloc(sizeof(struct p9_fcall) + alloc_msize,
250                                   GFP_NOFS);
251                 req->tc->capacity = alloc_msize;
252                 req->rc = kmalloc(sizeof(struct p9_fcall) + alloc_msize,
253                                   GFP_NOFS);
254                 req->rc->capacity = alloc_msize;
255                 if ((!req->tc) || (!req->rc)) {
256                         printk(KERN_ERR "Couldn't grow tag array\n");
257                         kfree(req->tc);
258                         kfree(req->rc);
259                         kfree(req->wq);
260                         req->tc = req->rc = NULL;
261                         req->wq = NULL;
262                         return ERR_PTR(-ENOMEM);
263                 }
264                 req->tc->sdata = (char *) req->tc + sizeof(struct p9_fcall);
265                 req->rc->sdata = (char *) req->rc + sizeof(struct p9_fcall);
266         }
267
268         p9pdu_reset(req->tc);
269         p9pdu_reset(req->rc);
270
271         req->tc->tag = tag-1;
272         req->status = REQ_STATUS_ALLOC;
273
274         return &c->reqs[row][col];
275 }
276
277 /**
278  * p9_tag_lookup - lookup a request by tag
279  * @c: client session to lookup tag within
280  * @tag: numeric id for transaction
281  *
282  */
283
284 struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
285 {
286         int row, col;
287
288         /* This looks up the original request by tag so we know which
289          * buffer to read the data into */
290         tag++;
291
292         if(tag >= c->max_tag) 
293                 return NULL;
294
295         row = tag / P9_ROW_MAXTAG;
296         col = tag % P9_ROW_MAXTAG;
297
298         return &c->reqs[row][col];
299 }
300 EXPORT_SYMBOL(p9_tag_lookup);
301
302 /**
303  * p9_tag_init - setup tags structure and contents
304  * @c:  v9fs client struct
305  *
306  * This initializes the tags structure for each client instance.
307  *
308  */
309
310 static int p9_tag_init(struct p9_client *c)
311 {
312         int err = 0;
313
314         c->tagpool = p9_idpool_create();
315         if (IS_ERR(c->tagpool)) {
316                 err = PTR_ERR(c->tagpool);
317                 goto error;
318         }
319         err = p9_idpool_get(c->tagpool); /* reserve tag 0 */
320         if (err < 0) {
321                 p9_idpool_destroy(c->tagpool);
322                 goto error;
323         }
324         c->max_tag = 0;
325 error:
326         return err;
327 }
328
329 /**
330  * p9_tag_cleanup - cleans up tags structure and reclaims resources
331  * @c:  v9fs client struct
332  *
333  * This frees resources associated with the tags structure
334  *
335  */
336 static void p9_tag_cleanup(struct p9_client *c)
337 {
338         int row, col;
339
340         /* check to insure all requests are idle */
341         for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
342                 for (col = 0; col < P9_ROW_MAXTAG; col++) {
343                         if (c->reqs[row][col].status != REQ_STATUS_IDLE) {
344                                 P9_DPRINTK(P9_DEBUG_MUX,
345                                   "Attempting to cleanup non-free tag %d,%d\n",
346                                   row, col);
347                                 /* TODO: delay execution of cleanup */
348                                 return;
349                         }
350                 }
351         }
352
353         if (c->tagpool) {
354                 p9_idpool_put(0, c->tagpool); /* free reserved tag 0 */
355                 p9_idpool_destroy(c->tagpool);
356         }
357
358         /* free requests associated with tags */
359         for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
360                 for (col = 0; col < P9_ROW_MAXTAG; col++) {
361                         kfree(c->reqs[row][col].wq);
362                         kfree(c->reqs[row][col].tc);
363                         kfree(c->reqs[row][col].rc);
364                 }
365                 kfree(c->reqs[row]);
366         }
367         c->max_tag = 0;
368 }
369
370 /**
371  * p9_free_req - free a request and clean-up as necessary
372  * c: client state
373  * r: request to release
374  *
375  */
376
377 static void p9_free_req(struct p9_client *c, struct p9_req_t *r)
378 {
379         int tag = r->tc->tag;
380         P9_DPRINTK(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag);
381
382         r->status = REQ_STATUS_IDLE;
383         if (tag != P9_NOTAG && p9_idpool_check(tag, c->tagpool))
384                 p9_idpool_put(tag, c->tagpool);
385 }
386
387 /**
388  * p9_client_cb - call back from transport to client
389  * c: client state
390  * req: request received
391  *
392  */
393 void p9_client_cb(struct p9_client *c, struct p9_req_t *req)
394 {
395         P9_DPRINTK(P9_DEBUG_MUX, " tag %d\n", req->tc->tag);
396         wake_up(req->wq);
397         P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag);
398 }
399 EXPORT_SYMBOL(p9_client_cb);
400
401 /**
402  * p9_parse_header - parse header arguments out of a packet
403  * @pdu: packet to parse
404  * @size: size of packet
405  * @type: type of request
406  * @tag: tag of packet
407  * @rewind: set if we need to rewind offset afterwards
408  */
409
410 int
411 p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag,
412                                                                 int rewind)
413 {
414         int8_t r_type;
415         int16_t r_tag;
416         int32_t r_size;
417         int offset = pdu->offset;
418         int err;
419
420         pdu->offset = 0;
421         if (pdu->size == 0)
422                 pdu->size = 7;
423
424         err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag);
425         if (err)
426                 goto rewind_and_exit;
427
428         pdu->size = r_size;
429         pdu->id = r_type;
430         pdu->tag = r_tag;
431
432         P9_DPRINTK(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n", pdu->size,
433                                                         pdu->id, pdu->tag);
434
435         if (type)
436                 *type = r_type;
437         if (tag)
438                 *tag = r_tag;
439         if (size)
440                 *size = r_size;
441
442
443 rewind_and_exit:
444         if (rewind)
445                 pdu->offset = offset;
446         return err;
447 }
448 EXPORT_SYMBOL(p9_parse_header);
449
450 /**
451  * p9_check_errors - check 9p packet for error return and process it
452  * @c: current client instance
453  * @req: request to parse and check for error conditions
454  *
455  * returns error code if one is discovered, otherwise returns 0
456  *
457  * this will have to be more complicated if we have multiple
458  * error packet types
459  */
460
461 static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
462 {
463         int8_t type;
464         int err;
465         int ecode;
466
467         err = p9_parse_header(req->rc, NULL, &type, NULL, 0);
468         if (err) {
469                 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
470                 return err;
471         }
472
473         if (type != P9_RERROR && type != P9_RLERROR)
474                 return 0;
475
476         if (!p9_is_proto_dotl(c)) {
477                 char *ename;
478                 err = p9pdu_readf(req->rc, c->proto_version, "s?d",
479                                   &ename, &ecode);
480                 if (err)
481                         goto out_err;
482
483                 if (p9_is_proto_dotu(c))
484                         err = -ecode;
485
486                 if (!err || !IS_ERR_VALUE(err)) {
487                         err = p9_errstr2errno(ename, strlen(ename));
488
489                         P9_DPRINTK(P9_DEBUG_9P, "<<< RERROR (%d) %s\n",
490                                    -ecode, ename);
491                 }
492                 kfree(ename);
493         } else {
494                 err = p9pdu_readf(req->rc, c->proto_version, "d", &ecode);
495                 err = -ecode;
496
497                 P9_DPRINTK(P9_DEBUG_9P, "<<< RLERROR (%d)\n", -ecode);
498         }
499
500         return err;
501
502 out_err:
503         P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse error%d\n", err);
504
505         return err;
506 }
507
508 /**
509  * p9_check_zc_errors - check 9p packet for error return and process it
510  * @c: current client instance
511  * @req: request to parse and check for error conditions
512  * @in_hdrlen: Size of response protocol buffer.
513  *
514  * returns error code if one is discovered, otherwise returns 0
515  *
516  * this will have to be more complicated if we have multiple
517  * error packet types
518  */
519
520 static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req,
521                               char *uidata, int in_hdrlen, int kern_buf)
522 {
523         int err;
524         int ecode;
525         int8_t type;
526         char *ename = NULL;
527
528         err = p9_parse_header(req->rc, NULL, &type, NULL, 0);
529         if (err) {
530                 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
531                 return err;
532         }
533
534         if (type != P9_RERROR && type != P9_RLERROR)
535                 return 0;
536
537         if (!p9_is_proto_dotl(c)) {
538                 /* Error is reported in string format */
539                 uint16_t len;
540                 /* 7 = header size for RERROR, 2 is the size of string len; */
541                 int inline_len = in_hdrlen - (7 + 2);
542
543                 /* Read the size of error string */
544                 err = p9pdu_readf(req->rc, c->proto_version, "w", &len);
545                 if (err)
546                         goto out_err;
547
548                 ename = kmalloc(len + 1, GFP_NOFS);
549                 if (!ename) {
550                         err = -ENOMEM;
551                         goto out_err;
552                 }
553                 if (len <= inline_len) {
554                         /* We have error in protocol buffer itself */
555                         if (pdu_read(req->rc, ename, len)) {
556                                 err = -EFAULT;
557                                 goto out_free;
558
559                         }
560                 } else {
561                         /*
562                          *  Part of the data is in user space buffer.
563                          */
564                         if (pdu_read(req->rc, ename, inline_len)) {
565                                 err = -EFAULT;
566                                 goto out_free;
567
568                         }
569                         if (kern_buf) {
570                                 memcpy(ename + inline_len, uidata,
571                                        len - inline_len);
572                         } else {
573                                 err = copy_from_user(ename + inline_len,
574                                                      uidata, len - inline_len);
575                                 if (err) {
576                                         err = -EFAULT;
577                                         goto out_free;
578                                 }
579                         }
580                 }
581                 ename[len] = 0;
582                 if (p9_is_proto_dotu(c)) {
583                         /* For dotu we also have error code */
584                         err = p9pdu_readf(req->rc,
585                                           c->proto_version, "d", &ecode);
586                         if (err)
587                                 goto out_free;
588                         err = -ecode;
589                 }
590                 if (!err || !IS_ERR_VALUE(err)) {
591                         err = p9_errstr2errno(ename, strlen(ename));
592
593                         P9_DPRINTK(P9_DEBUG_9P, "<<< RERROR (%d) %s\n",
594                                    -ecode, ename);
595                 }
596                 kfree(ename);
597         } else {
598                 err = p9pdu_readf(req->rc, c->proto_version, "d", &ecode);
599                 err = -ecode;
600
601                 P9_DPRINTK(P9_DEBUG_9P, "<<< RLERROR (%d)\n", -ecode);
602         }
603         return err;
604
605 out_free:
606         kfree(ename);
607 out_err:
608         P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse error%d\n", err);
609         return err;
610 }
611
612 static struct p9_req_t *
613 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...);
614
615 /**
616  * p9_client_flush - flush (cancel) a request
617  * @c: client state
618  * @oldreq: request to cancel
619  *
620  * This sents a flush for a particular request and links
621  * the flush request to the original request.  The current
622  * code only supports a single flush request although the protocol
623  * allows for multiple flush requests to be sent for a single request.
624  *
625  */
626
627 static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
628 {
629         struct p9_req_t *req;
630         int16_t oldtag;
631         int err;
632
633         err = p9_parse_header(oldreq->tc, NULL, NULL, &oldtag, 1);
634         if (err)
635                 return err;
636
637         P9_DPRINTK(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag);
638
639         req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag);
640         if (IS_ERR(req))
641                 return PTR_ERR(req);
642
643
644         /* if we haven't received a response for oldreq,
645            remove it from the list. */
646         spin_lock(&c->lock);
647         if (oldreq->status == REQ_STATUS_FLSH)
648                 list_del(&oldreq->req_list);
649         spin_unlock(&c->lock);
650
651         p9_free_req(c, req);
652         return 0;
653 }
654
655 static struct p9_req_t *p9_client_prepare_req(struct p9_client *c,
656                                               int8_t type, int req_size,
657                                               const char *fmt, va_list ap)
658 {
659         int tag, err;
660         struct p9_req_t *req;
661
662         P9_DPRINTK(P9_DEBUG_MUX, "client %p op %d\n", c, type);
663
664         /* we allow for any status other than disconnected */
665         if (c->status == Disconnected)
666                 return ERR_PTR(-EIO);
667
668         /* if status is begin_disconnected we allow only clunk request */
669         if ((c->status == BeginDisconnect) && (type != P9_TCLUNK))
670                 return ERR_PTR(-EIO);
671
672         tag = P9_NOTAG;
673         if (type != P9_TVERSION) {
674                 tag = p9_idpool_get(c->tagpool);
675                 if (tag < 0)
676                         return ERR_PTR(-ENOMEM);
677         }
678
679         req = p9_tag_alloc(c, tag, req_size);
680         if (IS_ERR(req))
681                 return req;
682
683         /* marshall the data */
684         p9pdu_prepare(req->tc, tag, type);
685         err = p9pdu_vwritef(req->tc, c->proto_version, fmt, ap);
686         if (err)
687                 goto reterr;
688         p9pdu_finalize(req->tc);
689         return req;
690 reterr:
691         p9_free_req(c, req);
692         return ERR_PTR(err);
693 }
694
695 /**
696  * p9_client_rpc - issue a request and wait for a response
697  * @c: client session
698  * @type: type of request
699  * @fmt: protocol format string (see protocol.c)
700  *
701  * Returns request structure (which client must free using p9_free_req)
702  */
703
704 static struct p9_req_t *
705 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
706 {
707         va_list ap;
708         int sigpending, err;
709         unsigned long flags;
710         struct p9_req_t *req;
711
712         va_start(ap, fmt);
713         req = p9_client_prepare_req(c, type, c->msize, fmt, ap);
714         va_end(ap);
715         if (IS_ERR(req))
716                 return req;
717
718         if (signal_pending(current)) {
719                 sigpending = 1;
720                 clear_thread_flag(TIF_SIGPENDING);
721         } else
722                 sigpending = 0;
723
724         err = c->trans_mod->request(c, req);
725         if (err < 0) {
726                 if (err != -ERESTARTSYS && err != -EFAULT)
727                         c->status = Disconnected;
728                 goto reterr;
729         }
730         /* Wait for the response */
731         err = wait_event_interruptible(*req->wq,
732                                        req->status >= REQ_STATUS_RCVD);
733
734         if (req->status == REQ_STATUS_ERROR) {
735                 P9_DPRINTK(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
736                 err = req->t_err;
737         }
738         if ((err == -ERESTARTSYS) && (c->status == Connected)) {
739                 P9_DPRINTK(P9_DEBUG_MUX, "flushing\n");
740                 sigpending = 1;
741                 clear_thread_flag(TIF_SIGPENDING);
742
743                 if (c->trans_mod->cancel(c, req))
744                         p9_client_flush(c, req);
745
746                 /* if we received the response anyway, don't signal error */
747                 if (req->status == REQ_STATUS_RCVD)
748                         err = 0;
749         }
750         if (sigpending) {
751                 spin_lock_irqsave(&current->sighand->siglock, flags);
752                 recalc_sigpending();
753                 spin_unlock_irqrestore(&current->sighand->siglock, flags);
754         }
755         if (err < 0)
756                 goto reterr;
757
758         err = p9_check_errors(c, req);
759         if (!err) {
760                 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d\n", c, type);
761                 return req;
762         }
763 reterr:
764         P9_DPRINTK(P9_DEBUG_MUX,
765                    "exit: client %p op %d error: %d\n", c, type, err);
766         p9_free_req(c, req);
767         return ERR_PTR(err);
768 }
769
770 /**
771  * p9_client_zc_rpc - issue a request and wait for a response
772  * @c: client session
773  * @type: type of request
774  * @uidata: user bffer that should be ued for zero copy read
775  * @uodata: user buffer that shoud be user for zero copy write
776  * @inlen: read buffer size
777  * @olen: write buffer size
778  * @hdrlen: reader header size, This is the size of response protocol data
779  * @fmt: protocol format string (see protocol.c)
780  *
781  * Returns request structure (which client must free using p9_free_req)
782  */
783 static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
784                                          char *uidata, char *uodata,
785                                          int inlen, int olen, int in_hdrlen,
786                                          int kern_buf, const char *fmt, ...)
787 {
788         va_list ap;
789         int sigpending, err;
790         unsigned long flags;
791         struct p9_req_t *req;
792
793         va_start(ap, fmt);
794         /*
795          * We allocate a inline protocol data of only 4k bytes.
796          * The actual content is passed in zero-copy fashion.
797          */
798         req = p9_client_prepare_req(c, type, P9_ZC_HDR_SZ, fmt, ap);
799         va_end(ap);
800         if (IS_ERR(req))
801                 return req;
802
803         if (signal_pending(current)) {
804                 sigpending = 1;
805                 clear_thread_flag(TIF_SIGPENDING);
806         } else
807                 sigpending = 0;
808
809         /* If we are called with KERNEL_DS force kern_buf */
810         if (segment_eq(get_fs(), KERNEL_DS))
811                 kern_buf = 1;
812
813         err = c->trans_mod->zc_request(c, req, uidata, uodata,
814                                        inlen, olen, in_hdrlen, kern_buf);
815         if (err < 0) {
816                 if (err == -EIO)
817                         c->status = Disconnected;
818                 goto reterr;
819         }
820         if (req->status == REQ_STATUS_ERROR) {
821                 P9_DPRINTK(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
822                 err = req->t_err;
823         }
824         if ((err == -ERESTARTSYS) && (c->status == Connected)) {
825                 P9_DPRINTK(P9_DEBUG_MUX, "flushing\n");
826                 sigpending = 1;
827                 clear_thread_flag(TIF_SIGPENDING);
828
829                 if (c->trans_mod->cancel(c, req))
830                         p9_client_flush(c, req);
831
832                 /* if we received the response anyway, don't signal error */
833                 if (req->status == REQ_STATUS_RCVD)
834                         err = 0;
835         }
836         if (sigpending) {
837                 spin_lock_irqsave(&current->sighand->siglock, flags);
838                 recalc_sigpending();
839                 spin_unlock_irqrestore(&current->sighand->siglock, flags);
840         }
841         if (err < 0)
842                 goto reterr;
843
844         err = p9_check_zc_errors(c, req, uidata, in_hdrlen, kern_buf);
845         if (!err) {
846                 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d\n", c, type);
847                 return req;
848         }
849 reterr:
850         P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d error: %d\n", c, type,
851                                                                         err);
852         p9_free_req(c, req);
853         return ERR_PTR(err);
854 }
855
856 static struct p9_fid *p9_fid_create(struct p9_client *clnt)
857 {
858         int ret;
859         struct p9_fid *fid;
860         unsigned long flags;
861
862         P9_DPRINTK(P9_DEBUG_FID, "clnt %p\n", clnt);
863         fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
864         if (!fid)
865                 return ERR_PTR(-ENOMEM);
866
867         ret = p9_idpool_get(clnt->fidpool);
868         if (ret < 0) {
869                 ret = -ENOSPC;
870                 goto error;
871         }
872         fid->fid = ret;
873
874         memset(&fid->qid, 0, sizeof(struct p9_qid));
875         fid->mode = -1;
876         fid->uid = current_fsuid();
877         fid->clnt = clnt;
878         fid->rdir = NULL;
879         spin_lock_irqsave(&clnt->lock, flags);
880         list_add(&fid->flist, &clnt->fidlist);
881         spin_unlock_irqrestore(&clnt->lock, flags);
882
883         return fid;
884
885 error:
886         kfree(fid);
887         return ERR_PTR(ret);
888 }
889
890 static void p9_fid_destroy(struct p9_fid *fid)
891 {
892         struct p9_client *clnt;
893         unsigned long flags;
894
895         P9_DPRINTK(P9_DEBUG_FID, "fid %d\n", fid->fid);
896         clnt = fid->clnt;
897         p9_idpool_put(fid->fid, clnt->fidpool);
898         spin_lock_irqsave(&clnt->lock, flags);
899         list_del(&fid->flist);
900         spin_unlock_irqrestore(&clnt->lock, flags);
901         kfree(fid->rdir);
902         kfree(fid);
903 }
904
905 static int p9_client_version(struct p9_client *c)
906 {
907         int err = 0;
908         struct p9_req_t *req;
909         char *version;
910         int msize;
911
912         P9_DPRINTK(P9_DEBUG_9P, ">>> TVERSION msize %d protocol %d\n",
913                                                 c->msize, c->proto_version);
914
915         switch (c->proto_version) {
916         case p9_proto_2000L:
917                 req = p9_client_rpc(c, P9_TVERSION, "ds",
918                                         c->msize, "9P2000.L");
919                 break;
920         case p9_proto_2000u:
921                 req = p9_client_rpc(c, P9_TVERSION, "ds",
922                                         c->msize, "9P2000.u");
923                 break;
924         case p9_proto_legacy:
925                 req = p9_client_rpc(c, P9_TVERSION, "ds",
926                                         c->msize, "9P2000");
927                 break;
928         default:
929                 return -EINVAL;
930                 break;
931         }
932
933         if (IS_ERR(req))
934                 return PTR_ERR(req);
935
936         err = p9pdu_readf(req->rc, c->proto_version, "ds", &msize, &version);
937         if (err) {
938                 P9_DPRINTK(P9_DEBUG_9P, "version error %d\n", err);
939                 P9_DUMP_PKT(1, req->rc);
940                 goto error;
941         }
942
943         P9_DPRINTK(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version);
944         if (!strncmp(version, "9P2000.L", 8))
945                 c->proto_version = p9_proto_2000L;
946         else if (!strncmp(version, "9P2000.u", 8))
947                 c->proto_version = p9_proto_2000u;
948         else if (!strncmp(version, "9P2000", 6))
949                 c->proto_version = p9_proto_legacy;
950         else {
951                 err = -EREMOTEIO;
952                 goto error;
953         }
954
955         if (msize < c->msize)
956                 c->msize = msize;
957
958 error:
959         kfree(version);
960         p9_free_req(c, req);
961
962         return err;
963 }
964
965 struct p9_client *p9_client_create(const char *dev_name, char *options)
966 {
967         int err;
968         struct p9_client *clnt;
969
970         err = 0;
971         clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
972         if (!clnt)
973                 return ERR_PTR(-ENOMEM);
974
975         clnt->trans_mod = NULL;
976         clnt->trans = NULL;
977         spin_lock_init(&clnt->lock);
978         INIT_LIST_HEAD(&clnt->fidlist);
979
980         err = p9_tag_init(clnt);
981         if (err < 0)
982                 goto free_client;
983
984         err = parse_opts(options, clnt);
985         if (err < 0)
986                 goto destroy_tagpool;
987
988         if (!clnt->trans_mod)
989                 clnt->trans_mod = v9fs_get_default_trans();
990
991         if (clnt->trans_mod == NULL) {
992                 err = -EPROTONOSUPPORT;
993                 P9_DPRINTK(P9_DEBUG_ERROR,
994                                 "No transport defined or default transport\n");
995                 goto destroy_tagpool;
996         }
997
998         clnt->fidpool = p9_idpool_create();
999         if (IS_ERR(clnt->fidpool)) {
1000                 err = PTR_ERR(clnt->fidpool);
1001                 goto put_trans;
1002         }
1003
1004         P9_DPRINTK(P9_DEBUG_MUX, "clnt %p trans %p msize %d protocol %d\n",
1005                 clnt, clnt->trans_mod, clnt->msize, clnt->proto_version);
1006
1007         err = clnt->trans_mod->create(clnt, dev_name, options);
1008         if (err)
1009                 goto destroy_fidpool;
1010
1011         if (clnt->msize > clnt->trans_mod->maxsize)
1012                 clnt->msize = clnt->trans_mod->maxsize;
1013
1014         err = p9_client_version(clnt);
1015         if (err)
1016                 goto close_trans;
1017
1018         return clnt;
1019
1020 close_trans:
1021         clnt->trans_mod->close(clnt);
1022 destroy_fidpool:
1023         p9_idpool_destroy(clnt->fidpool);
1024 put_trans:
1025         v9fs_put_trans(clnt->trans_mod);
1026 destroy_tagpool:
1027         p9_idpool_destroy(clnt->tagpool);
1028 free_client:
1029         kfree(clnt);
1030         return ERR_PTR(err);
1031 }
1032 EXPORT_SYMBOL(p9_client_create);
1033
1034 void p9_client_destroy(struct p9_client *clnt)
1035 {
1036         struct p9_fid *fid, *fidptr;
1037
1038         P9_DPRINTK(P9_DEBUG_MUX, "clnt %p\n", clnt);
1039
1040         if (clnt->trans_mod)
1041                 clnt->trans_mod->close(clnt);
1042
1043         v9fs_put_trans(clnt->trans_mod);
1044
1045         list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist) {
1046                 printk(KERN_INFO "Found fid %d not clunked\n", fid->fid);
1047                 p9_fid_destroy(fid);
1048         }
1049
1050         if (clnt->fidpool)
1051                 p9_idpool_destroy(clnt->fidpool);
1052
1053         p9_tag_cleanup(clnt);
1054
1055         kfree(clnt);
1056 }
1057 EXPORT_SYMBOL(p9_client_destroy);
1058
1059 void p9_client_disconnect(struct p9_client *clnt)
1060 {
1061         P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
1062         clnt->status = Disconnected;
1063 }
1064 EXPORT_SYMBOL(p9_client_disconnect);
1065
1066 void p9_client_begin_disconnect(struct p9_client *clnt)
1067 {
1068         P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
1069         clnt->status = BeginDisconnect;
1070 }
1071 EXPORT_SYMBOL(p9_client_begin_disconnect);
1072
1073 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
1074         char *uname, u32 n_uname, char *aname)
1075 {
1076         int err;
1077         struct p9_req_t *req;
1078         struct p9_fid *fid;
1079         struct p9_qid qid;
1080
1081         P9_DPRINTK(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
1082                                         afid ? afid->fid : -1, uname, aname);
1083         err = 0;
1084
1085         fid = p9_fid_create(clnt);
1086         if (IS_ERR(fid)) {
1087                 err = PTR_ERR(fid);
1088                 fid = NULL;
1089                 goto error;
1090         }
1091
1092         req = p9_client_rpc(clnt, P9_TATTACH, "ddss?d", fid->fid,
1093                         afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
1094         if (IS_ERR(req)) {
1095                 err = PTR_ERR(req);
1096                 goto error;
1097         }
1098
1099         err = p9pdu_readf(req->rc, clnt->proto_version, "Q", &qid);
1100         if (err) {
1101                 P9_DUMP_PKT(1, req->rc);
1102                 p9_free_req(clnt, req);
1103                 goto error;
1104         }
1105
1106         P9_DPRINTK(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
1107                                         qid.type,
1108                                         (unsigned long long)qid.path,
1109                                         qid.version);
1110
1111         memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1112
1113         p9_free_req(clnt, req);
1114         return fid;
1115
1116 error:
1117         if (fid)
1118                 p9_fid_destroy(fid);
1119         return ERR_PTR(err);
1120 }
1121 EXPORT_SYMBOL(p9_client_attach);
1122
1123 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname,
1124                 char **wnames, int clone)
1125 {
1126         int err;
1127         struct p9_client *clnt;
1128         struct p9_fid *fid;
1129         struct p9_qid *wqids;
1130         struct p9_req_t *req;
1131         uint16_t nwqids, count;
1132
1133         err = 0;
1134         wqids = NULL;
1135         clnt = oldfid->clnt;
1136         if (clone) {
1137                 fid = p9_fid_create(clnt);
1138                 if (IS_ERR(fid)) {
1139                         err = PTR_ERR(fid);
1140                         fid = NULL;
1141                         goto error;
1142                 }
1143
1144                 fid->uid = oldfid->uid;
1145         } else
1146                 fid = oldfid;
1147
1148
1149         P9_DPRINTK(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %ud wname[0] %s\n",
1150                 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
1151
1152         req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
1153                                                                 nwname, wnames);
1154         if (IS_ERR(req)) {
1155                 err = PTR_ERR(req);
1156                 goto error;
1157         }
1158
1159         err = p9pdu_readf(req->rc, clnt->proto_version, "R", &nwqids, &wqids);
1160         if (err) {
1161                 P9_DUMP_PKT(1, req->rc);
1162                 p9_free_req(clnt, req);
1163                 goto clunk_fid;
1164         }
1165         p9_free_req(clnt, req);
1166
1167         P9_DPRINTK(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
1168
1169         if (nwqids != nwname) {
1170                 err = -ENOENT;
1171                 goto clunk_fid;
1172         }
1173
1174         for (count = 0; count < nwqids; count++)
1175                 P9_DPRINTK(P9_DEBUG_9P, "<<<     [%d] %x.%llx.%x\n",
1176                         count, wqids[count].type,
1177                         (unsigned long long)wqids[count].path,
1178                         wqids[count].version);
1179
1180         if (nwname)
1181                 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
1182         else
1183                 fid->qid = oldfid->qid;
1184
1185         kfree(wqids);
1186         return fid;
1187
1188 clunk_fid:
1189         kfree(wqids);
1190         p9_client_clunk(fid);
1191         fid = NULL;
1192
1193 error:
1194         if (fid && (fid != oldfid))
1195                 p9_fid_destroy(fid);
1196
1197         return ERR_PTR(err);
1198 }
1199 EXPORT_SYMBOL(p9_client_walk);
1200
1201 int p9_client_open(struct p9_fid *fid, int mode)
1202 {
1203         int err;
1204         struct p9_client *clnt;
1205         struct p9_req_t *req;
1206         struct p9_qid qid;
1207         int iounit;
1208
1209         clnt = fid->clnt;
1210         P9_DPRINTK(P9_DEBUG_9P, ">>> %s fid %d mode %d\n",
1211                 p9_is_proto_dotl(clnt) ? "TLOPEN" : "TOPEN", fid->fid, mode);
1212         err = 0;
1213
1214         if (fid->mode != -1)
1215                 return -EINVAL;
1216
1217         if (p9_is_proto_dotl(clnt))
1218                 req = p9_client_rpc(clnt, P9_TLOPEN, "dd", fid->fid, mode);
1219         else
1220                 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
1221         if (IS_ERR(req)) {
1222                 err = PTR_ERR(req);
1223                 goto error;
1224         }
1225
1226         err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", &qid, &iounit);
1227         if (err) {
1228                 P9_DUMP_PKT(1, req->rc);
1229                 goto free_and_error;
1230         }
1231
1232         P9_DPRINTK(P9_DEBUG_9P, "<<< %s qid %x.%llx.%x iounit %x\n",
1233                 p9_is_proto_dotl(clnt) ? "RLOPEN" : "ROPEN",  qid.type,
1234                 (unsigned long long)qid.path, qid.version, iounit);
1235
1236         fid->mode = mode;
1237         fid->iounit = iounit;
1238
1239 free_and_error:
1240         p9_free_req(clnt, req);
1241 error:
1242         return err;
1243 }
1244 EXPORT_SYMBOL(p9_client_open);
1245
1246 int p9_client_create_dotl(struct p9_fid *ofid, char *name, u32 flags, u32 mode,
1247                 gid_t gid, struct p9_qid *qid)
1248 {
1249         int err = 0;
1250         struct p9_client *clnt;
1251         struct p9_req_t *req;
1252         int iounit;
1253
1254         P9_DPRINTK(P9_DEBUG_9P,
1255                         ">>> TLCREATE fid %d name %s flags %d mode %d gid %d\n",
1256                         ofid->fid, name, flags, mode, gid);
1257         clnt = ofid->clnt;
1258
1259         if (ofid->mode != -1)
1260                 return -EINVAL;
1261
1262         req = p9_client_rpc(clnt, P9_TLCREATE, "dsddd", ofid->fid, name, flags,
1263                         mode, gid);
1264         if (IS_ERR(req)) {
1265                 err = PTR_ERR(req);
1266                 goto error;
1267         }
1268
1269         err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", qid, &iounit);
1270         if (err) {
1271                 P9_DUMP_PKT(1, req->rc);
1272                 goto free_and_error;
1273         }
1274
1275         P9_DPRINTK(P9_DEBUG_9P, "<<< RLCREATE qid %x.%llx.%x iounit %x\n",
1276                         qid->type,
1277                         (unsigned long long)qid->path,
1278                         qid->version, iounit);
1279
1280         ofid->mode = mode;
1281         ofid->iounit = iounit;
1282
1283 free_and_error:
1284         p9_free_req(clnt, req);
1285 error:
1286         return err;
1287 }
1288 EXPORT_SYMBOL(p9_client_create_dotl);
1289
1290 int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
1291                      char *extension)
1292 {
1293         int err;
1294         struct p9_client *clnt;
1295         struct p9_req_t *req;
1296         struct p9_qid qid;
1297         int iounit;
1298
1299         P9_DPRINTK(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
1300                                                 fid->fid, name, perm, mode);
1301         err = 0;
1302         clnt = fid->clnt;
1303
1304         if (fid->mode != -1)
1305                 return -EINVAL;
1306
1307         req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
1308                                 mode, extension);
1309         if (IS_ERR(req)) {
1310                 err = PTR_ERR(req);
1311                 goto error;
1312         }
1313
1314         err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", &qid, &iounit);
1315         if (err) {
1316                 P9_DUMP_PKT(1, req->rc);
1317                 goto free_and_error;
1318         }
1319
1320         P9_DPRINTK(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
1321                                 qid.type,
1322                                 (unsigned long long)qid.path,
1323                                 qid.version, iounit);
1324
1325         fid->mode = mode;
1326         fid->iounit = iounit;
1327
1328 free_and_error:
1329         p9_free_req(clnt, req);
1330 error:
1331         return err;
1332 }
1333 EXPORT_SYMBOL(p9_client_fcreate);
1334
1335 int p9_client_symlink(struct p9_fid *dfid, char *name, char *symtgt, gid_t gid,
1336                 struct p9_qid *qid)
1337 {
1338         int err = 0;
1339         struct p9_client *clnt;
1340         struct p9_req_t *req;
1341
1342         P9_DPRINTK(P9_DEBUG_9P, ">>> TSYMLINK dfid %d name %s  symtgt %s\n",
1343                         dfid->fid, name, symtgt);
1344         clnt = dfid->clnt;
1345
1346         req = p9_client_rpc(clnt, P9_TSYMLINK, "dssd", dfid->fid, name, symtgt,
1347                         gid);
1348         if (IS_ERR(req)) {
1349                 err = PTR_ERR(req);
1350                 goto error;
1351         }
1352
1353         err = p9pdu_readf(req->rc, clnt->proto_version, "Q", qid);
1354         if (err) {
1355                 P9_DUMP_PKT(1, req->rc);
1356                 goto free_and_error;
1357         }
1358
1359         P9_DPRINTK(P9_DEBUG_9P, "<<< RSYMLINK qid %x.%llx.%x\n",
1360                         qid->type, (unsigned long long)qid->path, qid->version);
1361
1362 free_and_error:
1363         p9_free_req(clnt, req);
1364 error:
1365         return err;
1366 }
1367 EXPORT_SYMBOL(p9_client_symlink);
1368
1369 int p9_client_link(struct p9_fid *dfid, struct p9_fid *oldfid, char *newname)
1370 {
1371         struct p9_client *clnt;
1372         struct p9_req_t *req;
1373
1374         P9_DPRINTK(P9_DEBUG_9P, ">>> TLINK dfid %d oldfid %d newname %s\n",
1375                         dfid->fid, oldfid->fid, newname);
1376         clnt = dfid->clnt;
1377         req = p9_client_rpc(clnt, P9_TLINK, "dds", dfid->fid, oldfid->fid,
1378                         newname);
1379         if (IS_ERR(req))
1380                 return PTR_ERR(req);
1381
1382         P9_DPRINTK(P9_DEBUG_9P, "<<< RLINK\n");
1383         p9_free_req(clnt, req);
1384         return 0;
1385 }
1386 EXPORT_SYMBOL(p9_client_link);
1387
1388 int p9_client_fsync(struct p9_fid *fid, int datasync)
1389 {
1390         int err;
1391         struct p9_client *clnt;
1392         struct p9_req_t *req;
1393
1394         P9_DPRINTK(P9_DEBUG_9P, ">>> TFSYNC fid %d datasync:%d\n",
1395                         fid->fid, datasync);
1396         err = 0;
1397         clnt = fid->clnt;
1398
1399         req = p9_client_rpc(clnt, P9_TFSYNC, "dd", fid->fid, datasync);
1400         if (IS_ERR(req)) {
1401                 err = PTR_ERR(req);
1402                 goto error;
1403         }
1404
1405         P9_DPRINTK(P9_DEBUG_9P, "<<< RFSYNC fid %d\n", fid->fid);
1406
1407         p9_free_req(clnt, req);
1408
1409 error:
1410         return err;
1411 }
1412 EXPORT_SYMBOL(p9_client_fsync);
1413
1414 int p9_client_clunk(struct p9_fid *fid)
1415 {
1416         int err;
1417         struct p9_client *clnt;
1418         struct p9_req_t *req;
1419
1420         if (!fid) {
1421                 P9_EPRINTK(KERN_WARNING, "Trying to clunk with NULL fid\n");
1422                 dump_stack();
1423                 return 0;
1424         }
1425
1426         P9_DPRINTK(P9_DEBUG_9P, ">>> TCLUNK fid %d\n", fid->fid);
1427         err = 0;
1428         clnt = fid->clnt;
1429
1430         req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
1431         if (IS_ERR(req)) {
1432                 err = PTR_ERR(req);
1433                 goto error;
1434         }
1435
1436         P9_DPRINTK(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
1437
1438         p9_free_req(clnt, req);
1439 error:
1440         /*
1441          * Fid is not valid even after a failed clunk
1442          */
1443         p9_fid_destroy(fid);
1444         return err;
1445 }
1446 EXPORT_SYMBOL(p9_client_clunk);
1447
1448 int p9_client_remove(struct p9_fid *fid)
1449 {
1450         int err;
1451         struct p9_client *clnt;
1452         struct p9_req_t *req;
1453
1454         P9_DPRINTK(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
1455         err = 0;
1456         clnt = fid->clnt;
1457
1458         req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
1459         if (IS_ERR(req)) {
1460                 err = PTR_ERR(req);
1461                 goto error;
1462         }
1463
1464         P9_DPRINTK(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
1465
1466         p9_free_req(clnt, req);
1467 error:
1468         p9_fid_destroy(fid);
1469         return err;
1470 }
1471 EXPORT_SYMBOL(p9_client_remove);
1472
1473 int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags)
1474 {
1475         int err = 0;
1476         struct p9_req_t *req;
1477         struct p9_client *clnt;
1478
1479         P9_DPRINTK(P9_DEBUG_9P, ">>> TUNLINKAT fid %d %s %d\n",
1480                    dfid->fid, name, flags);
1481
1482         clnt = dfid->clnt;
1483         req = p9_client_rpc(clnt, P9_TUNLINKAT, "dsd", dfid->fid, name, flags);
1484         if (IS_ERR(req)) {
1485                 err = PTR_ERR(req);
1486                 goto error;
1487         }
1488         P9_DPRINTK(P9_DEBUG_9P, "<<< RUNLINKAT fid %d %s\n", dfid->fid, name);
1489
1490         p9_free_req(clnt, req);
1491 error:
1492         return err;
1493 }
1494 EXPORT_SYMBOL(p9_client_unlinkat);
1495
1496 int
1497 p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
1498                                                                 u32 count)
1499 {
1500         char *dataptr;
1501         int kernel_buf = 0;
1502         struct p9_req_t *req;
1503         struct p9_client *clnt;
1504         int err, rsize, non_zc = 0;
1505
1506
1507         P9_DPRINTK(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n",
1508                    fid->fid, (long long unsigned) offset, count);
1509         err = 0;
1510         clnt = fid->clnt;
1511
1512         rsize = fid->iounit;
1513         if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1514                 rsize = clnt->msize - P9_IOHDRSZ;
1515
1516         if (count < rsize)
1517                 rsize = count;
1518
1519         /* Don't bother zerocopy for small IO (< 1024) */
1520         if (clnt->trans_mod->zc_request && rsize > 1024) {
1521                 char *indata;
1522                 if (data) {
1523                         kernel_buf = 1;
1524                         indata = data;
1525                 } else
1526                         indata = (char *)udata;
1527                 /*
1528                  * response header len is 11
1529                  * PDU Header(7) + IO Size (4)
1530                  */
1531                 req = p9_client_zc_rpc(clnt, P9_TREAD, indata, NULL, rsize, 0,
1532                                        11, kernel_buf, "dqd", fid->fid,
1533                                        offset, rsize);
1534         } else {
1535                 non_zc = 1;
1536                 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset,
1537                                     rsize);
1538         }
1539         if (IS_ERR(req)) {
1540                 err = PTR_ERR(req);
1541                 goto error;
1542         }
1543
1544         err = p9pdu_readf(req->rc, clnt->proto_version, "D", &count, &dataptr);
1545         if (err) {
1546                 P9_DUMP_PKT(1, req->rc);
1547                 goto free_and_error;
1548         }
1549
1550         P9_DPRINTK(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
1551         P9_DUMP_PKT(1, req->rc);
1552
1553         if (non_zc) {
1554                 if (data) {
1555                         memmove(data, dataptr, count);
1556                 } else {
1557                         err = copy_to_user(udata, dataptr, count);
1558                         if (err) {
1559                                 err = -EFAULT;
1560                                 goto free_and_error;
1561                         }
1562                 }
1563         }
1564         p9_free_req(clnt, req);
1565         return count;
1566
1567 free_and_error:
1568         p9_free_req(clnt, req);
1569 error:
1570         return err;
1571 }
1572 EXPORT_SYMBOL(p9_client_read);
1573
1574 int
1575 p9_client_write(struct p9_fid *fid, char *data, const char __user *udata,
1576                                                         u64 offset, u32 count)
1577 {
1578         int err, rsize;
1579         int kernel_buf = 0;
1580         struct p9_client *clnt;
1581         struct p9_req_t *req;
1582
1583         P9_DPRINTK(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %d\n",
1584                                 fid->fid, (long long unsigned) offset, count);
1585         err = 0;
1586         clnt = fid->clnt;
1587
1588         rsize = fid->iounit;
1589         if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1590                 rsize = clnt->msize - P9_IOHDRSZ;
1591
1592         if (count < rsize)
1593                 rsize = count;
1594
1595         /* Don't bother zerocopy for small IO (< 1024) */
1596         if (clnt->trans_mod->zc_request && rsize > 1024) {
1597                 char *odata;
1598                 if (data) {
1599                         kernel_buf = 1;
1600                         odata = data;
1601                 } else
1602                         odata = (char *)udata;
1603                 req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, odata, 0, rsize,
1604                                        P9_ZC_HDR_SZ, kernel_buf, "dqd",
1605                                        fid->fid, offset, rsize);
1606         } else {
1607                 if (data)
1608                         req = p9_client_rpc(clnt, P9_TWRITE, "dqD", fid->fid,
1609                                             offset, rsize, data);
1610                 else
1611                         req = p9_client_rpc(clnt, P9_TWRITE, "dqU", fid->fid,
1612                                             offset, rsize, udata);
1613         }
1614         if (IS_ERR(req)) {
1615                 err = PTR_ERR(req);
1616                 goto error;
1617         }
1618
1619         err = p9pdu_readf(req->rc, clnt->proto_version, "d", &count);
1620         if (err) {
1621                 P9_DUMP_PKT(1, req->rc);
1622                 goto free_and_error;
1623         }
1624
1625         P9_DPRINTK(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
1626
1627         p9_free_req(clnt, req);
1628         return count;
1629
1630 free_and_error:
1631         p9_free_req(clnt, req);
1632 error:
1633         return err;
1634 }
1635 EXPORT_SYMBOL(p9_client_write);
1636
1637 struct p9_wstat *p9_client_stat(struct p9_fid *fid)
1638 {
1639         int err;
1640         struct p9_client *clnt;
1641         struct p9_wstat *ret = kmalloc(sizeof(struct p9_wstat), GFP_KERNEL);
1642         struct p9_req_t *req;
1643         u16 ignored;
1644
1645         P9_DPRINTK(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
1646
1647         if (!ret)
1648                 return ERR_PTR(-ENOMEM);
1649
1650         err = 0;
1651         clnt = fid->clnt;
1652
1653         req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
1654         if (IS_ERR(req)) {
1655                 err = PTR_ERR(req);
1656                 goto error;
1657         }
1658
1659         err = p9pdu_readf(req->rc, clnt->proto_version, "wS", &ignored, ret);
1660         if (err) {
1661                 P9_DUMP_PKT(1, req->rc);
1662                 p9_free_req(clnt, req);
1663                 goto error;
1664         }
1665
1666         P9_DPRINTK(P9_DEBUG_9P,
1667                 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1668                 "<<<    mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1669                 "<<<    name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1670                 "<<<    uid=%d gid=%d n_muid=%d\n",
1671                 ret->size, ret->type, ret->dev, ret->qid.type,
1672                 (unsigned long long)ret->qid.path, ret->qid.version, ret->mode,
1673                 ret->atime, ret->mtime, (unsigned long long)ret->length,
1674                 ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
1675                 ret->n_uid, ret->n_gid, ret->n_muid);
1676
1677         p9_free_req(clnt, req);
1678         return ret;
1679
1680 error:
1681         kfree(ret);
1682         return ERR_PTR(err);
1683 }
1684 EXPORT_SYMBOL(p9_client_stat);
1685
1686 struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid,
1687                                                         u64 request_mask)
1688 {
1689         int err;
1690         struct p9_client *clnt;
1691         struct p9_stat_dotl *ret = kmalloc(sizeof(struct p9_stat_dotl),
1692                                                                 GFP_KERNEL);
1693         struct p9_req_t *req;
1694
1695         P9_DPRINTK(P9_DEBUG_9P, ">>> TGETATTR fid %d, request_mask %lld\n",
1696                                                         fid->fid, request_mask);
1697
1698         if (!ret)
1699                 return ERR_PTR(-ENOMEM);
1700
1701         err = 0;
1702         clnt = fid->clnt;
1703
1704         req = p9_client_rpc(clnt, P9_TGETATTR, "dq", fid->fid, request_mask);
1705         if (IS_ERR(req)) {
1706                 err = PTR_ERR(req);
1707                 goto error;
1708         }
1709
1710         err = p9pdu_readf(req->rc, clnt->proto_version, "A", ret);
1711         if (err) {
1712                 P9_DUMP_PKT(1, req->rc);
1713                 p9_free_req(clnt, req);
1714                 goto error;
1715         }
1716
1717         P9_DPRINTK(P9_DEBUG_9P,
1718                 "<<< RGETATTR st_result_mask=%lld\n"
1719                 "<<< qid=%x.%llx.%x\n"
1720                 "<<< st_mode=%8.8x st_nlink=%llu\n"
1721                 "<<< st_uid=%d st_gid=%d\n"
1722                 "<<< st_rdev=%llx st_size=%llx st_blksize=%llu st_blocks=%llu\n"
1723                 "<<< st_atime_sec=%lld st_atime_nsec=%lld\n"
1724                 "<<< st_mtime_sec=%lld st_mtime_nsec=%lld\n"
1725                 "<<< st_ctime_sec=%lld st_ctime_nsec=%lld\n"
1726                 "<<< st_btime_sec=%lld st_btime_nsec=%lld\n"
1727                 "<<< st_gen=%lld st_data_version=%lld",
1728                 ret->st_result_mask, ret->qid.type, ret->qid.path,
1729                 ret->qid.version, ret->st_mode, ret->st_nlink, ret->st_uid,
1730                 ret->st_gid, ret->st_rdev, ret->st_size, ret->st_blksize,
1731                 ret->st_blocks, ret->st_atime_sec, ret->st_atime_nsec,
1732                 ret->st_mtime_sec, ret->st_mtime_nsec, ret->st_ctime_sec,
1733                 ret->st_ctime_nsec, ret->st_btime_sec, ret->st_btime_nsec,
1734                 ret->st_gen, ret->st_data_version);
1735
1736         p9_free_req(clnt, req);
1737         return ret;
1738
1739 error:
1740         kfree(ret);
1741         return ERR_PTR(err);
1742 }
1743 EXPORT_SYMBOL(p9_client_getattr_dotl);
1744
1745 static int p9_client_statsize(struct p9_wstat *wst, int proto_version)
1746 {
1747         int ret;
1748
1749         /* NOTE: size shouldn't include its own length */
1750         /* size[2] type[2] dev[4] qid[13] */
1751         /* mode[4] atime[4] mtime[4] length[8]*/
1752         /* name[s] uid[s] gid[s] muid[s] */
1753         ret = 2+4+13+4+4+4+8+2+2+2+2;
1754
1755         if (wst->name)
1756                 ret += strlen(wst->name);
1757         if (wst->uid)
1758                 ret += strlen(wst->uid);
1759         if (wst->gid)
1760                 ret += strlen(wst->gid);
1761         if (wst->muid)
1762                 ret += strlen(wst->muid);
1763
1764         if ((proto_version == p9_proto_2000u) ||
1765                 (proto_version == p9_proto_2000L)) {
1766                 ret += 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1767                 if (wst->extension)
1768                         ret += strlen(wst->extension);
1769         }
1770
1771         return ret;
1772 }
1773
1774 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1775 {
1776         int err;
1777         struct p9_req_t *req;
1778         struct p9_client *clnt;
1779
1780         err = 0;
1781         clnt = fid->clnt;
1782         wst->size = p9_client_statsize(wst, clnt->proto_version);
1783         P9_DPRINTK(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", fid->fid);
1784         P9_DPRINTK(P9_DEBUG_9P,
1785                 "     sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1786                 "     mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1787                 "     name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1788                 "     uid=%d gid=%d n_muid=%d\n",
1789                 wst->size, wst->type, wst->dev, wst->qid.type,
1790                 (unsigned long long)wst->qid.path, wst->qid.version, wst->mode,
1791                 wst->atime, wst->mtime, (unsigned long long)wst->length,
1792                 wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
1793                 wst->n_uid, wst->n_gid, wst->n_muid);
1794
1795         req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, wst->size+2, wst);
1796         if (IS_ERR(req)) {
1797                 err = PTR_ERR(req);
1798                 goto error;
1799         }
1800
1801         P9_DPRINTK(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
1802
1803         p9_free_req(clnt, req);
1804 error:
1805         return err;
1806 }
1807 EXPORT_SYMBOL(p9_client_wstat);
1808
1809 int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *p9attr)
1810 {
1811         int err;
1812         struct p9_req_t *req;
1813         struct p9_client *clnt;
1814
1815         err = 0;
1816         clnt = fid->clnt;
1817         P9_DPRINTK(P9_DEBUG_9P, ">>> TSETATTR fid %d\n", fid->fid);
1818         P9_DPRINTK(P9_DEBUG_9P,
1819                 "    valid=%x mode=%x uid=%d gid=%d size=%lld\n"
1820                 "    atime_sec=%lld atime_nsec=%lld\n"
1821                 "    mtime_sec=%lld mtime_nsec=%lld\n",
1822                 p9attr->valid, p9attr->mode, p9attr->uid, p9attr->gid,
1823                 p9attr->size, p9attr->atime_sec, p9attr->atime_nsec,
1824                 p9attr->mtime_sec, p9attr->mtime_nsec);
1825
1826         req = p9_client_rpc(clnt, P9_TSETATTR, "dI", fid->fid, p9attr);
1827
1828         if (IS_ERR(req)) {
1829                 err = PTR_ERR(req);
1830                 goto error;
1831         }
1832         P9_DPRINTK(P9_DEBUG_9P, "<<< RSETATTR fid %d\n", fid->fid);
1833         p9_free_req(clnt, req);
1834 error:
1835         return err;
1836 }
1837 EXPORT_SYMBOL(p9_client_setattr);
1838
1839 int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb)
1840 {
1841         int err;
1842         struct p9_req_t *req;
1843         struct p9_client *clnt;
1844
1845         err = 0;
1846         clnt = fid->clnt;
1847
1848         P9_DPRINTK(P9_DEBUG_9P, ">>> TSTATFS fid %d\n", fid->fid);
1849
1850         req = p9_client_rpc(clnt, P9_TSTATFS, "d", fid->fid);
1851         if (IS_ERR(req)) {
1852                 err = PTR_ERR(req);
1853                 goto error;
1854         }
1855
1856         err = p9pdu_readf(req->rc, clnt->proto_version, "ddqqqqqqd", &sb->type,
1857                 &sb->bsize, &sb->blocks, &sb->bfree, &sb->bavail,
1858                 &sb->files, &sb->ffree, &sb->fsid, &sb->namelen);
1859         if (err) {
1860                 P9_DUMP_PKT(1, req->rc);
1861                 p9_free_req(clnt, req);
1862                 goto error;
1863         }
1864
1865         P9_DPRINTK(P9_DEBUG_9P, "<<< RSTATFS fid %d type 0x%lx bsize %ld "
1866                 "blocks %llu bfree %llu bavail %llu files %llu ffree %llu "
1867                 "fsid %llu namelen %ld\n",
1868                 fid->fid, (long unsigned int)sb->type, (long int)sb->bsize,
1869                 sb->blocks, sb->bfree, sb->bavail, sb->files,  sb->ffree,
1870                 sb->fsid, (long int)sb->namelen);
1871
1872         p9_free_req(clnt, req);
1873 error:
1874         return err;
1875 }
1876 EXPORT_SYMBOL(p9_client_statfs);
1877
1878 int p9_client_rename(struct p9_fid *fid,
1879                      struct p9_fid *newdirfid, const char *name)
1880 {
1881         int err;
1882         struct p9_req_t *req;
1883         struct p9_client *clnt;
1884
1885         err = 0;
1886         clnt = fid->clnt;
1887
1888         P9_DPRINTK(P9_DEBUG_9P, ">>> TRENAME fid %d newdirfid %d name %s\n",
1889                         fid->fid, newdirfid->fid, name);
1890
1891         req = p9_client_rpc(clnt, P9_TRENAME, "dds", fid->fid,
1892                         newdirfid->fid, name);
1893         if (IS_ERR(req)) {
1894                 err = PTR_ERR(req);
1895                 goto error;
1896         }
1897
1898         P9_DPRINTK(P9_DEBUG_9P, "<<< RRENAME fid %d\n", fid->fid);
1899
1900         p9_free_req(clnt, req);
1901 error:
1902         return err;
1903 }
1904 EXPORT_SYMBOL(p9_client_rename);
1905
1906 int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name,
1907                        struct p9_fid *newdirfid, const char *new_name)
1908 {
1909         int err;
1910         struct p9_req_t *req;
1911         struct p9_client *clnt;
1912
1913         err = 0;
1914         clnt = olddirfid->clnt;
1915
1916         P9_DPRINTK(P9_DEBUG_9P, ">>> TRENAMEAT olddirfid %d old name %s"
1917                    " newdirfid %d new name %s\n", olddirfid->fid, old_name,
1918                    newdirfid->fid, new_name);
1919
1920         req = p9_client_rpc(clnt, P9_TRENAMEAT, "dsds", olddirfid->fid,
1921                             old_name, newdirfid->fid, new_name);
1922         if (IS_ERR(req)) {
1923                 err = PTR_ERR(req);
1924                 goto error;
1925         }
1926
1927         P9_DPRINTK(P9_DEBUG_9P, "<<< RRENAMEAT newdirfid %d new name %s\n",
1928                    newdirfid->fid, new_name);
1929
1930         p9_free_req(clnt, req);
1931 error:
1932         return err;
1933 }
1934 EXPORT_SYMBOL(p9_client_renameat);
1935
1936 /*
1937  * An xattrwalk without @attr_name gives the fid for the lisxattr namespace
1938  */
1939 struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid,
1940                                 const char *attr_name, u64 *attr_size)
1941 {
1942         int err;
1943         struct p9_req_t *req;
1944         struct p9_client *clnt;
1945         struct p9_fid *attr_fid;
1946
1947         err = 0;
1948         clnt = file_fid->clnt;
1949         attr_fid = p9_fid_create(clnt);
1950         if (IS_ERR(attr_fid)) {
1951                 err = PTR_ERR(attr_fid);
1952                 attr_fid = NULL;
1953                 goto error;
1954         }
1955         P9_DPRINTK(P9_DEBUG_9P,
1956                 ">>> TXATTRWALK file_fid %d, attr_fid %d name %s\n",
1957                 file_fid->fid, attr_fid->fid, attr_name);
1958
1959         req = p9_client_rpc(clnt, P9_TXATTRWALK, "dds",
1960                         file_fid->fid, attr_fid->fid, attr_name);
1961         if (IS_ERR(req)) {
1962                 err = PTR_ERR(req);
1963                 goto error;
1964         }
1965         err = p9pdu_readf(req->rc, clnt->proto_version, "q", attr_size);
1966         if (err) {
1967                 P9_DUMP_PKT(1, req->rc);
1968                 p9_free_req(clnt, req);
1969                 goto clunk_fid;
1970         }
1971         p9_free_req(clnt, req);
1972         P9_DPRINTK(P9_DEBUG_9P, "<<<  RXATTRWALK fid %d size %llu\n",
1973                 attr_fid->fid, *attr_size);
1974         return attr_fid;
1975 clunk_fid:
1976         p9_client_clunk(attr_fid);
1977         attr_fid = NULL;
1978 error:
1979         if (attr_fid && (attr_fid != file_fid))
1980                 p9_fid_destroy(attr_fid);
1981
1982         return ERR_PTR(err);
1983 }
1984 EXPORT_SYMBOL_GPL(p9_client_xattrwalk);
1985
1986 int p9_client_xattrcreate(struct p9_fid *fid, const char *name,
1987                         u64 attr_size, int flags)
1988 {
1989         int err;
1990         struct p9_req_t *req;
1991         struct p9_client *clnt;
1992
1993         P9_DPRINTK(P9_DEBUG_9P,
1994                 ">>> TXATTRCREATE fid %d name  %s size %lld flag %d\n",
1995                 fid->fid, name, (long long)attr_size, flags);
1996         err = 0;
1997         clnt = fid->clnt;
1998         req = p9_client_rpc(clnt, P9_TXATTRCREATE, "dsqd",
1999                         fid->fid, name, attr_size, flags);
2000         if (IS_ERR(req)) {
2001                 err = PTR_ERR(req);
2002                 goto error;
2003         }
2004         P9_DPRINTK(P9_DEBUG_9P, "<<< RXATTRCREATE fid %d\n", fid->fid);
2005         p9_free_req(clnt, req);
2006 error:
2007         return err;
2008 }
2009 EXPORT_SYMBOL_GPL(p9_client_xattrcreate);
2010
2011 int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset)
2012 {
2013         int err, rsize, non_zc = 0;
2014         struct p9_client *clnt;
2015         struct p9_req_t *req;
2016         char *dataptr;
2017
2018         P9_DPRINTK(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n",
2019                                 fid->fid, (long long unsigned) offset, count);
2020
2021         err = 0;
2022         clnt = fid->clnt;
2023
2024         rsize = fid->iounit;
2025         if (!rsize || rsize > clnt->msize-P9_READDIRHDRSZ)
2026                 rsize = clnt->msize - P9_READDIRHDRSZ;
2027
2028         if (count < rsize)
2029                 rsize = count;
2030
2031         /* Don't bother zerocopy for small IO (< 1024) */
2032         if (clnt->trans_mod->zc_request && rsize > 1024) {
2033                 /*
2034                  * response header len is 11
2035                  * PDU Header(7) + IO Size (4)
2036                  */
2037                 req = p9_client_zc_rpc(clnt, P9_TREADDIR, data, NULL, rsize, 0,
2038                                        11, 1, "dqd", fid->fid, offset, rsize);
2039         } else {
2040                 non_zc = 1;
2041                 req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid,
2042                                     offset, rsize);
2043         }
2044         if (IS_ERR(req)) {
2045                 err = PTR_ERR(req);
2046                 goto error;
2047         }
2048
2049         err = p9pdu_readf(req->rc, clnt->proto_version, "D", &count, &dataptr);
2050         if (err) {
2051                 P9_DUMP_PKT(1, req->rc);
2052                 goto free_and_error;
2053         }
2054
2055         P9_DPRINTK(P9_DEBUG_9P, "<<< RREADDIR count %d\n", count);
2056
2057         if (non_zc)
2058                 memmove(data, dataptr, count);
2059
2060         p9_free_req(clnt, req);
2061         return count;
2062
2063 free_and_error:
2064         p9_free_req(clnt, req);
2065 error:
2066         return err;
2067 }
2068 EXPORT_SYMBOL(p9_client_readdir);
2069
2070 int p9_client_mknod_dotl(struct p9_fid *fid, char *name, int mode,
2071                         dev_t rdev, gid_t gid, struct p9_qid *qid)
2072 {
2073         int err;
2074         struct p9_client *clnt;
2075         struct p9_req_t *req;
2076
2077         err = 0;
2078         clnt = fid->clnt;
2079         P9_DPRINTK(P9_DEBUG_9P, ">>> TMKNOD fid %d name %s mode %d major %d "
2080                 "minor %d\n", fid->fid, name, mode, MAJOR(rdev), MINOR(rdev));
2081         req = p9_client_rpc(clnt, P9_TMKNOD, "dsdddd", fid->fid, name, mode,
2082                 MAJOR(rdev), MINOR(rdev), gid);
2083         if (IS_ERR(req))
2084                 return PTR_ERR(req);
2085
2086         err = p9pdu_readf(req->rc, clnt->proto_version, "Q", qid);
2087         if (err) {
2088                 P9_DUMP_PKT(1, req->rc);
2089                 goto error;
2090         }
2091         P9_DPRINTK(P9_DEBUG_9P, "<<< RMKNOD qid %x.%llx.%x\n", qid->type,
2092                                 (unsigned long long)qid->path, qid->version);
2093
2094 error:
2095         p9_free_req(clnt, req);
2096         return err;
2097
2098 }
2099 EXPORT_SYMBOL(p9_client_mknod_dotl);
2100
2101 int p9_client_mkdir_dotl(struct p9_fid *fid, char *name, int mode,
2102                                 gid_t gid, struct p9_qid *qid)
2103 {
2104         int err;
2105         struct p9_client *clnt;
2106         struct p9_req_t *req;
2107
2108         err = 0;
2109         clnt = fid->clnt;
2110         P9_DPRINTK(P9_DEBUG_9P, ">>> TMKDIR fid %d name %s mode %d gid %d\n",
2111                  fid->fid, name, mode, gid);
2112         req = p9_client_rpc(clnt, P9_TMKDIR, "dsdd", fid->fid, name, mode,
2113                 gid);
2114         if (IS_ERR(req))
2115                 return PTR_ERR(req);
2116
2117         err = p9pdu_readf(req->rc, clnt->proto_version, "Q", qid);
2118         if (err) {
2119                 P9_DUMP_PKT(1, req->rc);
2120                 goto error;
2121         }
2122         P9_DPRINTK(P9_DEBUG_9P, "<<< RMKDIR qid %x.%llx.%x\n", qid->type,
2123                                 (unsigned long long)qid->path, qid->version);
2124
2125 error:
2126         p9_free_req(clnt, req);
2127         return err;
2128
2129 }
2130 EXPORT_SYMBOL(p9_client_mkdir_dotl);
2131
2132 int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status)
2133 {
2134         int err;
2135         struct p9_client *clnt;
2136         struct p9_req_t *req;
2137
2138         err = 0;
2139         clnt = fid->clnt;
2140         P9_DPRINTK(P9_DEBUG_9P, ">>> TLOCK fid %d type %i flags %d "
2141                         "start %lld length %lld proc_id %d client_id %s\n",
2142                         fid->fid, flock->type, flock->flags, flock->start,
2143                         flock->length, flock->proc_id, flock->client_id);
2144
2145         req = p9_client_rpc(clnt, P9_TLOCK, "dbdqqds", fid->fid, flock->type,
2146                                 flock->flags, flock->start, flock->length,
2147                                         flock->proc_id, flock->client_id);
2148
2149         if (IS_ERR(req))
2150                 return PTR_ERR(req);
2151
2152         err = p9pdu_readf(req->rc, clnt->proto_version, "b", status);
2153         if (err) {
2154                 P9_DUMP_PKT(1, req->rc);
2155                 goto error;
2156         }
2157         P9_DPRINTK(P9_DEBUG_9P, "<<< RLOCK status %i\n", *status);
2158 error:
2159         p9_free_req(clnt, req);
2160         return err;
2161
2162 }
2163 EXPORT_SYMBOL(p9_client_lock_dotl);
2164
2165 int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *glock)
2166 {
2167         int err;
2168         struct p9_client *clnt;
2169         struct p9_req_t *req;
2170
2171         err = 0;
2172         clnt = fid->clnt;
2173         P9_DPRINTK(P9_DEBUG_9P, ">>> TGETLOCK fid %d, type %i start %lld "
2174                 "length %lld proc_id %d client_id %s\n", fid->fid, glock->type,
2175                 glock->start, glock->length, glock->proc_id, glock->client_id);
2176
2177         req = p9_client_rpc(clnt, P9_TGETLOCK, "dbqqds", fid->fid,  glock->type,
2178                 glock->start, glock->length, glock->proc_id, glock->client_id);
2179
2180         if (IS_ERR(req))
2181                 return PTR_ERR(req);
2182
2183         err = p9pdu_readf(req->rc, clnt->proto_version, "bqqds", &glock->type,
2184                         &glock->start, &glock->length, &glock->proc_id,
2185                         &glock->client_id);
2186         if (err) {
2187                 P9_DUMP_PKT(1, req->rc);
2188                 goto error;
2189         }
2190         P9_DPRINTK(P9_DEBUG_9P, "<<< RGETLOCK type %i start %lld length %lld "
2191                 "proc_id %d client_id %s\n", glock->type, glock->start,
2192                 glock->length, glock->proc_id, glock->client_id);
2193 error:
2194         p9_free_req(clnt, req);
2195         return err;
2196 }
2197 EXPORT_SYMBOL(p9_client_getlock_dotl);
2198
2199 int p9_client_readlink(struct p9_fid *fid, char **target)
2200 {
2201         int err;
2202         struct p9_client *clnt;
2203         struct p9_req_t *req;
2204
2205         err = 0;
2206         clnt = fid->clnt;
2207         P9_DPRINTK(P9_DEBUG_9P, ">>> TREADLINK fid %d\n", fid->fid);
2208
2209         req = p9_client_rpc(clnt, P9_TREADLINK, "d", fid->fid);
2210         if (IS_ERR(req))
2211                 return PTR_ERR(req);
2212
2213         err = p9pdu_readf(req->rc, clnt->proto_version, "s", target);
2214         if (err) {
2215                 P9_DUMP_PKT(1, req->rc);
2216                 goto error;
2217         }
2218         P9_DPRINTK(P9_DEBUG_9P, "<<< RREADLINK target %s\n", *target);
2219 error:
2220         p9_free_req(clnt, req);
2221         return err;
2222 }
2223 EXPORT_SYMBOL(p9_client_readlink);