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