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