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