xref: /openbmc/linux/net/9p/client.c (revision 68ec5395)
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 	struct p9_client *clnt;
980 	char *client_id;
981 	char *cache_name;
982 
983 	clnt = kmalloc(sizeof(*clnt), GFP_KERNEL);
984 	if (!clnt)
985 		return ERR_PTR(-ENOMEM);
986 
987 	clnt->trans_mod = NULL;
988 	clnt->trans = NULL;
989 	clnt->fcall_cache = NULL;
990 
991 	client_id = utsname()->nodename;
992 	memcpy(clnt->name, client_id, strlen(client_id) + 1);
993 
994 	spin_lock_init(&clnt->lock);
995 	idr_init(&clnt->fids);
996 	idr_init(&clnt->reqs);
997 
998 	err = parse_opts(options, clnt);
999 	if (err < 0)
1000 		goto free_client;
1001 
1002 	if (!clnt->trans_mod)
1003 		clnt->trans_mod = v9fs_get_default_trans();
1004 
1005 	if (!clnt->trans_mod) {
1006 		err = -EPROTONOSUPPORT;
1007 		p9_debug(P9_DEBUG_ERROR,
1008 			 "No transport defined or default transport\n");
1009 		goto free_client;
1010 	}
1011 
1012 	p9_debug(P9_DEBUG_MUX, "clnt %p trans %p msize %d protocol %d\n",
1013 		 clnt, clnt->trans_mod, clnt->msize, clnt->proto_version);
1014 
1015 	err = clnt->trans_mod->create(clnt, dev_name, options);
1016 	if (err)
1017 		goto put_trans;
1018 
1019 	if (clnt->msize > clnt->trans_mod->maxsize) {
1020 		clnt->msize = clnt->trans_mod->maxsize;
1021 		pr_info("Limiting 'msize' to %d as this is the maximum "
1022 			"supported by transport %s\n",
1023 			clnt->msize, clnt->trans_mod->name
1024 		);
1025 	}
1026 
1027 	if (clnt->msize < 4096) {
1028 		p9_debug(P9_DEBUG_ERROR,
1029 			 "Please specify a msize of at least 4k\n");
1030 		err = -EINVAL;
1031 		goto close_trans;
1032 	}
1033 
1034 	err = p9_client_version(clnt);
1035 	if (err)
1036 		goto close_trans;
1037 
1038 	cache_name = kasprintf(GFP_KERNEL, "9p-fcall-cache-%s", dev_name);
1039 	if (!cache_name) {
1040 		err = -ENOMEM;
1041 		goto close_trans;
1042 	}
1043 
1044 	/* P9_HDRSZ + 4 is the smallest packet header we can have that is
1045 	 * followed by data accessed from userspace by read
1046 	 */
1047 	clnt->fcall_cache =
1048 		kmem_cache_create_usercopy(cache_name, clnt->msize,
1049 					   0, 0, P9_HDRSZ + 4,
1050 					   clnt->msize - (P9_HDRSZ + 4),
1051 					   NULL);
1052 
1053 	kfree(cache_name);
1054 	return clnt;
1055 
1056 close_trans:
1057 	clnt->trans_mod->close(clnt);
1058 put_trans:
1059 	v9fs_put_trans(clnt->trans_mod);
1060 free_client:
1061 	kfree(clnt);
1062 	return ERR_PTR(err);
1063 }
1064 EXPORT_SYMBOL(p9_client_create);
1065 
1066 void p9_client_destroy(struct p9_client *clnt)
1067 {
1068 	struct p9_fid *fid;
1069 	int id;
1070 
1071 	p9_debug(P9_DEBUG_MUX, "clnt %p\n", clnt);
1072 
1073 	if (clnt->trans_mod)
1074 		clnt->trans_mod->close(clnt);
1075 
1076 	v9fs_put_trans(clnt->trans_mod);
1077 
1078 	idr_for_each_entry(&clnt->fids, fid, id) {
1079 		pr_info("Found fid %d not clunked\n", fid->fid);
1080 		p9_fid_destroy(fid);
1081 	}
1082 
1083 	p9_tag_cleanup(clnt);
1084 
1085 	kmem_cache_destroy(clnt->fcall_cache);
1086 	kfree(clnt);
1087 }
1088 EXPORT_SYMBOL(p9_client_destroy);
1089 
1090 void p9_client_disconnect(struct p9_client *clnt)
1091 {
1092 	p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
1093 	clnt->status = Disconnected;
1094 }
1095 EXPORT_SYMBOL(p9_client_disconnect);
1096 
1097 void p9_client_begin_disconnect(struct p9_client *clnt)
1098 {
1099 	p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
1100 	clnt->status = BeginDisconnect;
1101 }
1102 EXPORT_SYMBOL(p9_client_begin_disconnect);
1103 
1104 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
1105 				const char *uname, kuid_t n_uname,
1106 				const char *aname)
1107 {
1108 	int err;
1109 	struct p9_req_t *req;
1110 	struct p9_fid *fid;
1111 	struct p9_qid qid;
1112 
1113 	p9_debug(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
1114 		 afid ? afid->fid : -1, uname, aname);
1115 	fid = p9_fid_create(clnt);
1116 	if (!fid) {
1117 		err = -ENOMEM;
1118 		goto error;
1119 	}
1120 	fid->uid = n_uname;
1121 
1122 	req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid,
1123 			    afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
1124 	if (IS_ERR(req)) {
1125 		err = PTR_ERR(req);
1126 		goto error;
1127 	}
1128 
1129 	err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", &qid);
1130 	if (err) {
1131 		trace_9p_protocol_dump(clnt, &req->rc);
1132 		p9_req_put(clnt, req);
1133 		goto error;
1134 	}
1135 
1136 	p9_debug(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
1137 		 qid.type, qid.path, qid.version);
1138 
1139 	memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1140 
1141 	p9_req_put(clnt, req);
1142 	return fid;
1143 
1144 error:
1145 	if (fid)
1146 		p9_fid_destroy(fid);
1147 	return ERR_PTR(err);
1148 }
1149 EXPORT_SYMBOL(p9_client_attach);
1150 
1151 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname,
1152 			      const unsigned char * const *wnames, int clone)
1153 {
1154 	int err;
1155 	struct p9_client *clnt;
1156 	struct p9_fid *fid;
1157 	struct p9_qid *wqids;
1158 	struct p9_req_t *req;
1159 	u16 nwqids, count;
1160 
1161 	wqids = NULL;
1162 	clnt = oldfid->clnt;
1163 	if (clone) {
1164 		fid = p9_fid_create(clnt);
1165 		if (!fid) {
1166 			err = -ENOMEM;
1167 			goto error;
1168 		}
1169 
1170 		fid->uid = oldfid->uid;
1171 	} else {
1172 		fid = oldfid;
1173 	}
1174 
1175 	p9_debug(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %ud wname[0] %s\n",
1176 		 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
1177 	req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
1178 			    nwname, wnames);
1179 	if (IS_ERR(req)) {
1180 		err = PTR_ERR(req);
1181 		goto error;
1182 	}
1183 
1184 	err = p9pdu_readf(&req->rc, clnt->proto_version, "R", &nwqids, &wqids);
1185 	if (err) {
1186 		trace_9p_protocol_dump(clnt, &req->rc);
1187 		p9_req_put(clnt, req);
1188 		goto clunk_fid;
1189 	}
1190 	p9_req_put(clnt, req);
1191 
1192 	p9_debug(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
1193 
1194 	if (nwqids != nwname) {
1195 		err = -ENOENT;
1196 		goto clunk_fid;
1197 	}
1198 
1199 	for (count = 0; count < nwqids; count++)
1200 		p9_debug(P9_DEBUG_9P, "<<<     [%d] %x.%llx.%x\n",
1201 			 count, wqids[count].type,
1202 			 wqids[count].path,
1203 			 wqids[count].version);
1204 
1205 	if (nwname)
1206 		memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
1207 	else
1208 		memmove(&fid->qid, &oldfid->qid, sizeof(struct p9_qid));
1209 
1210 	kfree(wqids);
1211 	return fid;
1212 
1213 clunk_fid:
1214 	kfree(wqids);
1215 	p9_fid_put(fid);
1216 	fid = NULL;
1217 
1218 error:
1219 	if (fid && fid != oldfid)
1220 		p9_fid_destroy(fid);
1221 
1222 	return ERR_PTR(err);
1223 }
1224 EXPORT_SYMBOL(p9_client_walk);
1225 
1226 int p9_client_open(struct p9_fid *fid, int mode)
1227 {
1228 	int err;
1229 	struct p9_client *clnt;
1230 	struct p9_req_t *req;
1231 	struct p9_qid qid;
1232 	int iounit;
1233 
1234 	clnt = fid->clnt;
1235 	p9_debug(P9_DEBUG_9P, ">>> %s fid %d mode %d\n",
1236 		 p9_is_proto_dotl(clnt) ? "TLOPEN" : "TOPEN", fid->fid, mode);
1237 
1238 	if (fid->mode != -1)
1239 		return -EINVAL;
1240 
1241 	if (p9_is_proto_dotl(clnt))
1242 		req = p9_client_rpc(clnt, P9_TLOPEN, "dd", fid->fid, mode & P9L_MODE_MASK);
1243 	else
1244 		req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode & P9L_MODE_MASK);
1245 	if (IS_ERR(req)) {
1246 		err = PTR_ERR(req);
1247 		goto error;
1248 	}
1249 
1250 	err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit);
1251 	if (err) {
1252 		trace_9p_protocol_dump(clnt, &req->rc);
1253 		goto free_and_error;
1254 	}
1255 
1256 	p9_debug(P9_DEBUG_9P, "<<< %s qid %x.%llx.%x iounit %x\n",
1257 		 p9_is_proto_dotl(clnt) ? "RLOPEN" : "ROPEN",  qid.type,
1258 		 qid.path, qid.version, iounit);
1259 
1260 	memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1261 	fid->mode = mode;
1262 	fid->iounit = iounit;
1263 
1264 free_and_error:
1265 	p9_req_put(clnt, req);
1266 error:
1267 	return err;
1268 }
1269 EXPORT_SYMBOL(p9_client_open);
1270 
1271 int p9_client_create_dotl(struct p9_fid *ofid, const char *name, u32 flags,
1272 			  u32 mode, kgid_t gid, struct p9_qid *qid)
1273 {
1274 	int err;
1275 	struct p9_client *clnt;
1276 	struct p9_req_t *req;
1277 	int iounit;
1278 
1279 	p9_debug(P9_DEBUG_9P,
1280 		 ">>> TLCREATE fid %d name %s flags %d mode %d gid %d\n",
1281 		 ofid->fid, name, flags, mode,
1282 		 from_kgid(&init_user_ns, gid));
1283 	clnt = ofid->clnt;
1284 
1285 	if (ofid->mode != -1)
1286 		return -EINVAL;
1287 
1288 	req = p9_client_rpc(clnt, P9_TLCREATE, "dsddg", ofid->fid, name, flags,
1289 			    mode & P9L_MODE_MASK, gid);
1290 	if (IS_ERR(req)) {
1291 		err = PTR_ERR(req);
1292 		goto error;
1293 	}
1294 
1295 	err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", qid, &iounit);
1296 	if (err) {
1297 		trace_9p_protocol_dump(clnt, &req->rc);
1298 		goto free_and_error;
1299 	}
1300 
1301 	p9_debug(P9_DEBUG_9P, "<<< RLCREATE qid %x.%llx.%x iounit %x\n",
1302 		 qid->type, qid->path, qid->version, iounit);
1303 
1304 	memmove(&ofid->qid, qid, sizeof(struct p9_qid));
1305 	ofid->mode = flags;
1306 	ofid->iounit = iounit;
1307 
1308 free_and_error:
1309 	p9_req_put(clnt, req);
1310 error:
1311 	return err;
1312 }
1313 EXPORT_SYMBOL(p9_client_create_dotl);
1314 
1315 int p9_client_fcreate(struct p9_fid *fid, const char *name, u32 perm, int mode,
1316 		     char *extension)
1317 {
1318 	int err;
1319 	struct p9_client *clnt;
1320 	struct p9_req_t *req;
1321 	struct p9_qid qid;
1322 	int iounit;
1323 
1324 	p9_debug(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
1325 		 fid->fid, name, perm, mode);
1326 	clnt = fid->clnt;
1327 
1328 	if (fid->mode != -1)
1329 		return -EINVAL;
1330 
1331 	req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
1332 			    mode & P9L_MODE_MASK, extension);
1333 	if (IS_ERR(req)) {
1334 		err = PTR_ERR(req);
1335 		goto error;
1336 	}
1337 
1338 	err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit);
1339 	if (err) {
1340 		trace_9p_protocol_dump(clnt, &req->rc);
1341 		goto free_and_error;
1342 	}
1343 
1344 	p9_debug(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
1345 		 qid.type, qid.path, qid.version, iounit);
1346 
1347 	memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1348 	fid->mode = mode;
1349 	fid->iounit = iounit;
1350 
1351 free_and_error:
1352 	p9_req_put(clnt, req);
1353 error:
1354 	return err;
1355 }
1356 EXPORT_SYMBOL(p9_client_fcreate);
1357 
1358 int p9_client_symlink(struct p9_fid *dfid, const char *name,
1359 		      const char *symtgt, kgid_t gid, struct p9_qid *qid)
1360 {
1361 	int err;
1362 	struct p9_client *clnt;
1363 	struct p9_req_t *req;
1364 
1365 	p9_debug(P9_DEBUG_9P, ">>> TSYMLINK dfid %d name %s  symtgt %s\n",
1366 		 dfid->fid, name, symtgt);
1367 	clnt = dfid->clnt;
1368 
1369 	req = p9_client_rpc(clnt, P9_TSYMLINK, "dssg", dfid->fid, name, symtgt,
1370 			    gid);
1371 	if (IS_ERR(req)) {
1372 		err = PTR_ERR(req);
1373 		goto error;
1374 	}
1375 
1376 	err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
1377 	if (err) {
1378 		trace_9p_protocol_dump(clnt, &req->rc);
1379 		goto free_and_error;
1380 	}
1381 
1382 	p9_debug(P9_DEBUG_9P, "<<< RSYMLINK qid %x.%llx.%x\n",
1383 		 qid->type, qid->path, qid->version);
1384 
1385 free_and_error:
1386 	p9_req_put(clnt, req);
1387 error:
1388 	return err;
1389 }
1390 EXPORT_SYMBOL(p9_client_symlink);
1391 
1392 int p9_client_link(struct p9_fid *dfid, struct p9_fid *oldfid, const char *newname)
1393 {
1394 	struct p9_client *clnt;
1395 	struct p9_req_t *req;
1396 
1397 	p9_debug(P9_DEBUG_9P, ">>> TLINK dfid %d oldfid %d newname %s\n",
1398 		 dfid->fid, oldfid->fid, newname);
1399 	clnt = dfid->clnt;
1400 	req = p9_client_rpc(clnt, P9_TLINK, "dds", dfid->fid, oldfid->fid,
1401 			    newname);
1402 	if (IS_ERR(req))
1403 		return PTR_ERR(req);
1404 
1405 	p9_debug(P9_DEBUG_9P, "<<< RLINK\n");
1406 	p9_req_put(clnt, req);
1407 	return 0;
1408 }
1409 EXPORT_SYMBOL(p9_client_link);
1410 
1411 int p9_client_fsync(struct p9_fid *fid, int datasync)
1412 {
1413 	int err = 0;
1414 	struct p9_client *clnt;
1415 	struct p9_req_t *req;
1416 
1417 	p9_debug(P9_DEBUG_9P, ">>> TFSYNC fid %d datasync:%d\n",
1418 		 fid->fid, datasync);
1419 	clnt = fid->clnt;
1420 
1421 	req = p9_client_rpc(clnt, P9_TFSYNC, "dd", fid->fid, datasync);
1422 	if (IS_ERR(req)) {
1423 		err = PTR_ERR(req);
1424 		goto error;
1425 	}
1426 
1427 	p9_debug(P9_DEBUG_9P, "<<< RFSYNC fid %d\n", fid->fid);
1428 
1429 	p9_req_put(clnt, req);
1430 
1431 error:
1432 	return err;
1433 }
1434 EXPORT_SYMBOL(p9_client_fsync);
1435 
1436 int p9_client_clunk(struct p9_fid *fid)
1437 {
1438 	int err = 0;
1439 	struct p9_client *clnt;
1440 	struct p9_req_t *req;
1441 	int retries = 0;
1442 
1443 again:
1444 	p9_debug(P9_DEBUG_9P, ">>> TCLUNK fid %d (try %d)\n",
1445 		 fid->fid, retries);
1446 	clnt = fid->clnt;
1447 
1448 	req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
1449 	if (IS_ERR(req)) {
1450 		err = PTR_ERR(req);
1451 		goto error;
1452 	}
1453 
1454 	p9_debug(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
1455 
1456 	p9_req_put(clnt, req);
1457 error:
1458 	/* Fid is not valid even after a failed clunk
1459 	 * If interrupted, retry once then give up and
1460 	 * leak fid until umount.
1461 	 */
1462 	if (err == -ERESTARTSYS) {
1463 		if (retries++ == 0)
1464 			goto again;
1465 	} else {
1466 		p9_fid_destroy(fid);
1467 	}
1468 	return err;
1469 }
1470 EXPORT_SYMBOL(p9_client_clunk);
1471 
1472 int p9_client_remove(struct p9_fid *fid)
1473 {
1474 	int err = 0;
1475 	struct p9_client *clnt;
1476 	struct p9_req_t *req;
1477 
1478 	p9_debug(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
1479 	clnt = fid->clnt;
1480 
1481 	req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
1482 	if (IS_ERR(req)) {
1483 		err = PTR_ERR(req);
1484 		goto error;
1485 	}
1486 
1487 	p9_debug(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
1488 
1489 	p9_req_put(clnt, req);
1490 error:
1491 	if (err == -ERESTARTSYS)
1492 		p9_fid_put(fid);
1493 	else
1494 		p9_fid_destroy(fid);
1495 	return err;
1496 }
1497 EXPORT_SYMBOL(p9_client_remove);
1498 
1499 int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags)
1500 {
1501 	int err = 0;
1502 	struct p9_req_t *req;
1503 	struct p9_client *clnt;
1504 
1505 	p9_debug(P9_DEBUG_9P, ">>> TUNLINKAT fid %d %s %d\n",
1506 		 dfid->fid, name, flags);
1507 
1508 	clnt = dfid->clnt;
1509 	req = p9_client_rpc(clnt, P9_TUNLINKAT, "dsd", dfid->fid, name, flags);
1510 	if (IS_ERR(req)) {
1511 		err = PTR_ERR(req);
1512 		goto error;
1513 	}
1514 	p9_debug(P9_DEBUG_9P, "<<< RUNLINKAT fid %d %s\n", dfid->fid, name);
1515 
1516 	p9_req_put(clnt, req);
1517 error:
1518 	return err;
1519 }
1520 EXPORT_SYMBOL(p9_client_unlinkat);
1521 
1522 int
1523 p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err)
1524 {
1525 	int total = 0;
1526 	*err = 0;
1527 
1528 	while (iov_iter_count(to)) {
1529 		int count;
1530 
1531 		count = p9_client_read_once(fid, offset, to, err);
1532 		if (!count || *err)
1533 			break;
1534 		offset += count;
1535 		total += count;
1536 	}
1537 	return total;
1538 }
1539 EXPORT_SYMBOL(p9_client_read);
1540 
1541 int
1542 p9_client_read_once(struct p9_fid *fid, u64 offset, struct iov_iter *to,
1543 		    int *err)
1544 {
1545 	struct p9_client *clnt = fid->clnt;
1546 	struct p9_req_t *req;
1547 	int count = iov_iter_count(to);
1548 	int rsize, received, non_zc = 0;
1549 	char *dataptr;
1550 
1551 	*err = 0;
1552 	p9_debug(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %zu\n",
1553 		 fid->fid, offset, iov_iter_count(to));
1554 
1555 	rsize = fid->iounit;
1556 	if (!rsize || rsize > clnt->msize - P9_IOHDRSZ)
1557 		rsize = clnt->msize - P9_IOHDRSZ;
1558 
1559 	if (count < rsize)
1560 		rsize = count;
1561 
1562 	/* Don't bother zerocopy for small IO (< 1024) */
1563 	if (clnt->trans_mod->zc_request && rsize > 1024) {
1564 		/* response header len is 11
1565 		 * PDU Header(7) + IO Size (4)
1566 		 */
1567 		req = p9_client_zc_rpc(clnt, P9_TREAD, to, NULL, rsize,
1568 				       0, 11, "dqd", fid->fid,
1569 				       offset, rsize);
1570 	} else {
1571 		non_zc = 1;
1572 		req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset,
1573 				    rsize);
1574 	}
1575 	if (IS_ERR(req)) {
1576 		*err = PTR_ERR(req);
1577 		if (!non_zc)
1578 			iov_iter_revert(to, count - iov_iter_count(to));
1579 		return 0;
1580 	}
1581 
1582 	*err = p9pdu_readf(&req->rc, clnt->proto_version,
1583 			   "D", &received, &dataptr);
1584 	if (*err) {
1585 		if (!non_zc)
1586 			iov_iter_revert(to, count - iov_iter_count(to));
1587 		trace_9p_protocol_dump(clnt, &req->rc);
1588 		p9_req_put(clnt, req);
1589 		return 0;
1590 	}
1591 	if (rsize < received) {
1592 		pr_err("bogus RREAD count (%d > %d)\n", received, rsize);
1593 		received = rsize;
1594 	}
1595 
1596 	p9_debug(P9_DEBUG_9P, "<<< RREAD count %d\n", received);
1597 
1598 	if (non_zc) {
1599 		int n = copy_to_iter(dataptr, received, to);
1600 
1601 		if (n != received) {
1602 			*err = -EFAULT;
1603 			p9_req_put(clnt, req);
1604 			return n;
1605 		}
1606 	} else {
1607 		iov_iter_revert(to, count - received - iov_iter_count(to));
1608 	}
1609 	p9_req_put(clnt, req);
1610 	return received;
1611 }
1612 EXPORT_SYMBOL(p9_client_read_once);
1613 
1614 int
1615 p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err)
1616 {
1617 	struct p9_client *clnt = fid->clnt;
1618 	struct p9_req_t *req;
1619 	int total = 0;
1620 	*err = 0;
1621 
1622 	while (iov_iter_count(from)) {
1623 		int count = iov_iter_count(from);
1624 		int rsize = fid->iounit;
1625 		int written;
1626 
1627 		if (!rsize || rsize > clnt->msize - P9_IOHDRSZ)
1628 			rsize = clnt->msize - P9_IOHDRSZ;
1629 
1630 		if (count < rsize)
1631 			rsize = count;
1632 
1633 		p9_debug(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %d (/%d)\n",
1634 			 fid->fid, offset, rsize, count);
1635 
1636 		/* Don't bother zerocopy for small IO (< 1024) */
1637 		if (clnt->trans_mod->zc_request && rsize > 1024) {
1638 			req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, from, 0,
1639 					       rsize, P9_ZC_HDR_SZ, "dqd",
1640 					       fid->fid, offset, rsize);
1641 		} else {
1642 			req = p9_client_rpc(clnt, P9_TWRITE, "dqV", fid->fid,
1643 					    offset, rsize, from);
1644 		}
1645 		if (IS_ERR(req)) {
1646 			iov_iter_revert(from, count - iov_iter_count(from));
1647 			*err = PTR_ERR(req);
1648 			break;
1649 		}
1650 
1651 		*err = p9pdu_readf(&req->rc, clnt->proto_version, "d", &written);
1652 		if (*err) {
1653 			iov_iter_revert(from, count - iov_iter_count(from));
1654 			trace_9p_protocol_dump(clnt, &req->rc);
1655 			p9_req_put(clnt, req);
1656 			break;
1657 		}
1658 		if (rsize < written) {
1659 			pr_err("bogus RWRITE count (%d > %d)\n", written, rsize);
1660 			written = rsize;
1661 		}
1662 
1663 		p9_debug(P9_DEBUG_9P, "<<< RWRITE count %d\n", written);
1664 
1665 		p9_req_put(clnt, req);
1666 		iov_iter_revert(from, count - written - iov_iter_count(from));
1667 		total += written;
1668 		offset += written;
1669 	}
1670 	return total;
1671 }
1672 EXPORT_SYMBOL(p9_client_write);
1673 
1674 struct p9_wstat *p9_client_stat(struct p9_fid *fid)
1675 {
1676 	int err;
1677 	struct p9_client *clnt;
1678 	struct p9_wstat *ret;
1679 	struct p9_req_t *req;
1680 	u16 ignored;
1681 
1682 	p9_debug(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
1683 
1684 	ret = kmalloc(sizeof(*ret), GFP_KERNEL);
1685 	if (!ret)
1686 		return ERR_PTR(-ENOMEM);
1687 
1688 	clnt = fid->clnt;
1689 
1690 	req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
1691 	if (IS_ERR(req)) {
1692 		err = PTR_ERR(req);
1693 		goto error;
1694 	}
1695 
1696 	err = p9pdu_readf(&req->rc, clnt->proto_version, "wS", &ignored, ret);
1697 	if (err) {
1698 		trace_9p_protocol_dump(clnt, &req->rc);
1699 		p9_req_put(clnt, req);
1700 		goto error;
1701 	}
1702 
1703 	p9_debug(P9_DEBUG_9P,
1704 		 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1705 		 "<<<    mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1706 		 "<<<    name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1707 		 "<<<    uid=%d gid=%d n_muid=%d\n",
1708 		 ret->size, ret->type, ret->dev, ret->qid.type, ret->qid.path,
1709 		 ret->qid.version, ret->mode,
1710 		 ret->atime, ret->mtime, ret->length,
1711 		 ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
1712 		 from_kuid(&init_user_ns, ret->n_uid),
1713 		 from_kgid(&init_user_ns, ret->n_gid),
1714 		 from_kuid(&init_user_ns, ret->n_muid));
1715 
1716 	p9_req_put(clnt, req);
1717 	return ret;
1718 
1719 error:
1720 	kfree(ret);
1721 	return ERR_PTR(err);
1722 }
1723 EXPORT_SYMBOL(p9_client_stat);
1724 
1725 struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid,
1726 					    u64 request_mask)
1727 {
1728 	int err;
1729 	struct p9_client *clnt;
1730 	struct p9_stat_dotl *ret;
1731 	struct p9_req_t *req;
1732 
1733 	p9_debug(P9_DEBUG_9P, ">>> TGETATTR fid %d, request_mask %lld\n",
1734 		 fid->fid, request_mask);
1735 
1736 	ret = kmalloc(sizeof(*ret), GFP_KERNEL);
1737 	if (!ret)
1738 		return ERR_PTR(-ENOMEM);
1739 
1740 	clnt = fid->clnt;
1741 
1742 	req = p9_client_rpc(clnt, P9_TGETATTR, "dq", fid->fid, request_mask);
1743 	if (IS_ERR(req)) {
1744 		err = PTR_ERR(req);
1745 		goto error;
1746 	}
1747 
1748 	err = p9pdu_readf(&req->rc, clnt->proto_version, "A", ret);
1749 	if (err) {
1750 		trace_9p_protocol_dump(clnt, &req->rc);
1751 		p9_req_put(clnt, req);
1752 		goto error;
1753 	}
1754 
1755 	p9_debug(P9_DEBUG_9P, "<<< RGETATTR st_result_mask=%lld\n"
1756 		 "<<< qid=%x.%llx.%x\n"
1757 		 "<<< st_mode=%8.8x st_nlink=%llu\n"
1758 		 "<<< st_uid=%d st_gid=%d\n"
1759 		 "<<< st_rdev=%llx st_size=%llx st_blksize=%llu st_blocks=%llu\n"
1760 		 "<<< st_atime_sec=%lld st_atime_nsec=%lld\n"
1761 		 "<<< st_mtime_sec=%lld st_mtime_nsec=%lld\n"
1762 		 "<<< st_ctime_sec=%lld st_ctime_nsec=%lld\n"
1763 		 "<<< st_btime_sec=%lld st_btime_nsec=%lld\n"
1764 		 "<<< st_gen=%lld st_data_version=%lld\n",
1765 		 ret->st_result_mask,
1766 		 ret->qid.type, ret->qid.path, ret->qid.version,
1767 		 ret->st_mode, ret->st_nlink,
1768 		 from_kuid(&init_user_ns, ret->st_uid),
1769 		 from_kgid(&init_user_ns, ret->st_gid),
1770 		 ret->st_rdev, ret->st_size, ret->st_blksize, ret->st_blocks,
1771 		 ret->st_atime_sec, ret->st_atime_nsec,
1772 		 ret->st_mtime_sec, ret->st_mtime_nsec,
1773 		 ret->st_ctime_sec, ret->st_ctime_nsec,
1774 		 ret->st_btime_sec, ret->st_btime_nsec,
1775 		 ret->st_gen, ret->st_data_version);
1776 
1777 	p9_req_put(clnt, req);
1778 	return ret;
1779 
1780 error:
1781 	kfree(ret);
1782 	return ERR_PTR(err);
1783 }
1784 EXPORT_SYMBOL(p9_client_getattr_dotl);
1785 
1786 static int p9_client_statsize(struct p9_wstat *wst, int proto_version)
1787 {
1788 	int ret;
1789 
1790 	/* NOTE: size shouldn't include its own length */
1791 	/* size[2] type[2] dev[4] qid[13] */
1792 	/* mode[4] atime[4] mtime[4] length[8]*/
1793 	/* name[s] uid[s] gid[s] muid[s] */
1794 	ret = 2 + 4 + 13 + 4 + 4 + 4 + 8 + 2 + 2 + 2 + 2;
1795 
1796 	if (wst->name)
1797 		ret += strlen(wst->name);
1798 	if (wst->uid)
1799 		ret += strlen(wst->uid);
1800 	if (wst->gid)
1801 		ret += strlen(wst->gid);
1802 	if (wst->muid)
1803 		ret += strlen(wst->muid);
1804 
1805 	if (proto_version == p9_proto_2000u ||
1806 	    proto_version == p9_proto_2000L) {
1807 		/* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1808 		ret += 2 + 4 + 4 + 4;
1809 		if (wst->extension)
1810 			ret += strlen(wst->extension);
1811 	}
1812 
1813 	return ret;
1814 }
1815 
1816 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1817 {
1818 	int err = 0;
1819 	struct p9_req_t *req;
1820 	struct p9_client *clnt;
1821 
1822 	clnt = fid->clnt;
1823 	wst->size = p9_client_statsize(wst, clnt->proto_version);
1824 	p9_debug(P9_DEBUG_9P, ">>> TWSTAT fid %d\n",
1825 		 fid->fid);
1826 	p9_debug(P9_DEBUG_9P,
1827 		 "     sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1828 		 "     mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1829 		 "     name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1830 		 "     uid=%d gid=%d n_muid=%d\n",
1831 		 wst->size, wst->type, wst->dev, wst->qid.type,
1832 		 wst->qid.path, wst->qid.version,
1833 		 wst->mode, wst->atime, wst->mtime, wst->length,
1834 		 wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
1835 		 from_kuid(&init_user_ns, wst->n_uid),
1836 		 from_kgid(&init_user_ns, wst->n_gid),
1837 		 from_kuid(&init_user_ns, wst->n_muid));
1838 
1839 	req = p9_client_rpc(clnt, P9_TWSTAT, "dwS",
1840 			    fid->fid, wst->size + 2, wst);
1841 	if (IS_ERR(req)) {
1842 		err = PTR_ERR(req);
1843 		goto error;
1844 	}
1845 
1846 	p9_debug(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
1847 
1848 	p9_req_put(clnt, req);
1849 error:
1850 	return err;
1851 }
1852 EXPORT_SYMBOL(p9_client_wstat);
1853 
1854 int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *p9attr)
1855 {
1856 	int err = 0;
1857 	struct p9_req_t *req;
1858 	struct p9_client *clnt;
1859 
1860 	clnt = fid->clnt;
1861 	p9_debug(P9_DEBUG_9P, ">>> TSETATTR fid %d\n", fid->fid);
1862 	p9_debug(P9_DEBUG_9P, "    valid=%x mode=%x uid=%d gid=%d size=%lld\n",
1863 		 p9attr->valid, p9attr->mode,
1864 		 from_kuid(&init_user_ns, p9attr->uid),
1865 		 from_kgid(&init_user_ns, p9attr->gid),
1866 		 p9attr->size);
1867 	p9_debug(P9_DEBUG_9P, "    atime_sec=%lld atime_nsec=%lld\n",
1868 		 p9attr->atime_sec, p9attr->atime_nsec);
1869 	p9_debug(P9_DEBUG_9P, "    mtime_sec=%lld mtime_nsec=%lld\n",
1870 		 p9attr->mtime_sec, p9attr->mtime_nsec);
1871 
1872 	req = p9_client_rpc(clnt, P9_TSETATTR, "dI", fid->fid, p9attr);
1873 
1874 	if (IS_ERR(req)) {
1875 		err = PTR_ERR(req);
1876 		goto error;
1877 	}
1878 	p9_debug(P9_DEBUG_9P, "<<< RSETATTR fid %d\n", fid->fid);
1879 	p9_req_put(clnt, req);
1880 error:
1881 	return err;
1882 }
1883 EXPORT_SYMBOL(p9_client_setattr);
1884 
1885 int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb)
1886 {
1887 	int err;
1888 	struct p9_req_t *req;
1889 	struct p9_client *clnt;
1890 
1891 	clnt = fid->clnt;
1892 
1893 	p9_debug(P9_DEBUG_9P, ">>> TSTATFS fid %d\n", fid->fid);
1894 
1895 	req = p9_client_rpc(clnt, P9_TSTATFS, "d", fid->fid);
1896 	if (IS_ERR(req)) {
1897 		err = PTR_ERR(req);
1898 		goto error;
1899 	}
1900 
1901 	err = p9pdu_readf(&req->rc, clnt->proto_version, "ddqqqqqqd", &sb->type,
1902 			  &sb->bsize, &sb->blocks, &sb->bfree, &sb->bavail,
1903 			  &sb->files, &sb->ffree, &sb->fsid, &sb->namelen);
1904 	if (err) {
1905 		trace_9p_protocol_dump(clnt, &req->rc);
1906 		p9_req_put(clnt, req);
1907 		goto error;
1908 	}
1909 
1910 	p9_debug(P9_DEBUG_9P,
1911 		 "<<< RSTATFS fid %d type 0x%x bsize %u blocks %llu bfree %llu bavail %llu files %llu ffree %llu fsid %llu namelen %u\n",
1912 		 fid->fid, sb->type, sb->bsize, sb->blocks, sb->bfree,
1913 		 sb->bavail, sb->files, sb->ffree, sb->fsid, sb->namelen);
1914 
1915 	p9_req_put(clnt, req);
1916 error:
1917 	return err;
1918 }
1919 EXPORT_SYMBOL(p9_client_statfs);
1920 
1921 int p9_client_rename(struct p9_fid *fid,
1922 		     struct p9_fid *newdirfid, const char *name)
1923 {
1924 	int err = 0;
1925 	struct p9_req_t *req;
1926 	struct p9_client *clnt;
1927 
1928 	clnt = fid->clnt;
1929 
1930 	p9_debug(P9_DEBUG_9P, ">>> TRENAME fid %d newdirfid %d name %s\n",
1931 		 fid->fid, newdirfid->fid, name);
1932 
1933 	req = p9_client_rpc(clnt, P9_TRENAME, "dds", fid->fid,
1934 			    newdirfid->fid, name);
1935 	if (IS_ERR(req)) {
1936 		err = PTR_ERR(req);
1937 		goto error;
1938 	}
1939 
1940 	p9_debug(P9_DEBUG_9P, "<<< RRENAME fid %d\n", fid->fid);
1941 
1942 	p9_req_put(clnt, req);
1943 error:
1944 	return err;
1945 }
1946 EXPORT_SYMBOL(p9_client_rename);
1947 
1948 int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name,
1949 		       struct p9_fid *newdirfid, const char *new_name)
1950 {
1951 	int err = 0;
1952 	struct p9_req_t *req;
1953 	struct p9_client *clnt;
1954 
1955 	clnt = olddirfid->clnt;
1956 
1957 	p9_debug(P9_DEBUG_9P,
1958 		 ">>> TRENAMEAT olddirfid %d old name %s newdirfid %d new name %s\n",
1959 		 olddirfid->fid, old_name, newdirfid->fid, new_name);
1960 
1961 	req = p9_client_rpc(clnt, P9_TRENAMEAT, "dsds", olddirfid->fid,
1962 			    old_name, newdirfid->fid, new_name);
1963 	if (IS_ERR(req)) {
1964 		err = PTR_ERR(req);
1965 		goto error;
1966 	}
1967 
1968 	p9_debug(P9_DEBUG_9P, "<<< RRENAMEAT newdirfid %d new name %s\n",
1969 		 newdirfid->fid, new_name);
1970 
1971 	p9_req_put(clnt, req);
1972 error:
1973 	return err;
1974 }
1975 EXPORT_SYMBOL(p9_client_renameat);
1976 
1977 /* An xattrwalk without @attr_name gives the fid for the lisxattr namespace
1978  */
1979 struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid,
1980 				   const char *attr_name, u64 *attr_size)
1981 {
1982 	int err;
1983 	struct p9_req_t *req;
1984 	struct p9_client *clnt;
1985 	struct p9_fid *attr_fid;
1986 
1987 	clnt = file_fid->clnt;
1988 	attr_fid = p9_fid_create(clnt);
1989 	if (!attr_fid) {
1990 		err = -ENOMEM;
1991 		goto error;
1992 	}
1993 	p9_debug(P9_DEBUG_9P,
1994 		 ">>> TXATTRWALK file_fid %d, attr_fid %d name '%s'\n",
1995 		 file_fid->fid, attr_fid->fid, attr_name);
1996 
1997 	req = p9_client_rpc(clnt, P9_TXATTRWALK, "dds",
1998 			    file_fid->fid, attr_fid->fid, attr_name);
1999 	if (IS_ERR(req)) {
2000 		err = PTR_ERR(req);
2001 		goto error;
2002 	}
2003 	err = p9pdu_readf(&req->rc, clnt->proto_version, "q", attr_size);
2004 	if (err) {
2005 		trace_9p_protocol_dump(clnt, &req->rc);
2006 		p9_req_put(clnt, req);
2007 		goto clunk_fid;
2008 	}
2009 	p9_req_put(clnt, req);
2010 	p9_debug(P9_DEBUG_9P, "<<<  RXATTRWALK fid %d size %llu\n",
2011 		 attr_fid->fid, *attr_size);
2012 	return attr_fid;
2013 clunk_fid:
2014 	p9_fid_put(attr_fid);
2015 	attr_fid = NULL;
2016 error:
2017 	if (attr_fid && attr_fid != file_fid)
2018 		p9_fid_destroy(attr_fid);
2019 
2020 	return ERR_PTR(err);
2021 }
2022 EXPORT_SYMBOL_GPL(p9_client_xattrwalk);
2023 
2024 int p9_client_xattrcreate(struct p9_fid *fid, const char *name,
2025 			  u64 attr_size, int flags)
2026 {
2027 	int err = 0;
2028 	struct p9_req_t *req;
2029 	struct p9_client *clnt;
2030 
2031 	p9_debug(P9_DEBUG_9P,
2032 		 ">>> TXATTRCREATE fid %d name  %s size %llu flag %d\n",
2033 		 fid->fid, name, attr_size, flags);
2034 	clnt = fid->clnt;
2035 	req = p9_client_rpc(clnt, P9_TXATTRCREATE, "dsqd",
2036 			    fid->fid, name, attr_size, flags);
2037 	if (IS_ERR(req)) {
2038 		err = PTR_ERR(req);
2039 		goto error;
2040 	}
2041 	p9_debug(P9_DEBUG_9P, "<<< RXATTRCREATE fid %d\n", fid->fid);
2042 	p9_req_put(clnt, req);
2043 error:
2044 	return err;
2045 }
2046 EXPORT_SYMBOL_GPL(p9_client_xattrcreate);
2047 
2048 int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset)
2049 {
2050 	int err, rsize, non_zc = 0;
2051 	struct p9_client *clnt;
2052 	struct p9_req_t *req;
2053 	char *dataptr;
2054 	struct kvec kv = {.iov_base = data, .iov_len = count};
2055 	struct iov_iter to;
2056 
2057 	iov_iter_kvec(&to, ITER_DEST, &kv, 1, count);
2058 
2059 	p9_debug(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n",
2060 		 fid->fid, offset, count);
2061 
2062 	clnt = fid->clnt;
2063 
2064 	rsize = fid->iounit;
2065 	if (!rsize || rsize > clnt->msize - P9_READDIRHDRSZ)
2066 		rsize = clnt->msize - P9_READDIRHDRSZ;
2067 
2068 	if (count < rsize)
2069 		rsize = count;
2070 
2071 	/* Don't bother zerocopy for small IO (< 1024) */
2072 	if (clnt->trans_mod->zc_request && rsize > 1024) {
2073 		/* response header len is 11
2074 		 * PDU Header(7) + IO Size (4)
2075 		 */
2076 		req = p9_client_zc_rpc(clnt, P9_TREADDIR, &to, NULL, rsize, 0,
2077 				       11, "dqd", fid->fid, offset, rsize);
2078 	} else {
2079 		non_zc = 1;
2080 		req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid,
2081 				    offset, rsize);
2082 	}
2083 	if (IS_ERR(req)) {
2084 		err = PTR_ERR(req);
2085 		goto error;
2086 	}
2087 
2088 	err = p9pdu_readf(&req->rc, clnt->proto_version, "D", &count, &dataptr);
2089 	if (err) {
2090 		trace_9p_protocol_dump(clnt, &req->rc);
2091 		goto free_and_error;
2092 	}
2093 	if (rsize < count) {
2094 		pr_err("bogus RREADDIR count (%d > %d)\n", count, rsize);
2095 		count = rsize;
2096 	}
2097 
2098 	p9_debug(P9_DEBUG_9P, "<<< RREADDIR count %d\n", count);
2099 
2100 	if (non_zc)
2101 		memmove(data, dataptr, count);
2102 
2103 	p9_req_put(clnt, req);
2104 	return count;
2105 
2106 free_and_error:
2107 	p9_req_put(clnt, req);
2108 error:
2109 	return err;
2110 }
2111 EXPORT_SYMBOL(p9_client_readdir);
2112 
2113 int p9_client_mknod_dotl(struct p9_fid *fid, const char *name, int mode,
2114 			 dev_t rdev, kgid_t gid, struct p9_qid *qid)
2115 {
2116 	int err;
2117 	struct p9_client *clnt;
2118 	struct p9_req_t *req;
2119 
2120 	clnt = fid->clnt;
2121 	p9_debug(P9_DEBUG_9P,
2122 		 ">>> TMKNOD fid %d name %s mode %d major %d minor %d\n",
2123 		 fid->fid, name, mode, MAJOR(rdev), MINOR(rdev));
2124 	req = p9_client_rpc(clnt, P9_TMKNOD, "dsdddg", fid->fid, name, mode,
2125 			    MAJOR(rdev), MINOR(rdev), gid);
2126 	if (IS_ERR(req))
2127 		return PTR_ERR(req);
2128 
2129 	err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
2130 	if (err) {
2131 		trace_9p_protocol_dump(clnt, &req->rc);
2132 		goto error;
2133 	}
2134 	p9_debug(P9_DEBUG_9P, "<<< RMKNOD qid %x.%llx.%x\n",
2135 		 qid->type, qid->path, qid->version);
2136 
2137 error:
2138 	p9_req_put(clnt, req);
2139 	return err;
2140 }
2141 EXPORT_SYMBOL(p9_client_mknod_dotl);
2142 
2143 int p9_client_mkdir_dotl(struct p9_fid *fid, const char *name, int mode,
2144 			 kgid_t gid, struct p9_qid *qid)
2145 {
2146 	int err;
2147 	struct p9_client *clnt;
2148 	struct p9_req_t *req;
2149 
2150 	clnt = fid->clnt;
2151 	p9_debug(P9_DEBUG_9P, ">>> TMKDIR fid %d name %s mode %d gid %d\n",
2152 		 fid->fid, name, mode, from_kgid(&init_user_ns, gid));
2153 	req = p9_client_rpc(clnt, P9_TMKDIR, "dsdg",
2154 			    fid->fid, name, mode, gid);
2155 	if (IS_ERR(req))
2156 		return PTR_ERR(req);
2157 
2158 	err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
2159 	if (err) {
2160 		trace_9p_protocol_dump(clnt, &req->rc);
2161 		goto error;
2162 	}
2163 	p9_debug(P9_DEBUG_9P, "<<< RMKDIR qid %x.%llx.%x\n", qid->type,
2164 		 qid->path, qid->version);
2165 
2166 error:
2167 	p9_req_put(clnt, req);
2168 	return err;
2169 }
2170 EXPORT_SYMBOL(p9_client_mkdir_dotl);
2171 
2172 int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status)
2173 {
2174 	int err;
2175 	struct p9_client *clnt;
2176 	struct p9_req_t *req;
2177 
2178 	clnt = fid->clnt;
2179 	p9_debug(P9_DEBUG_9P,
2180 		 ">>> TLOCK fid %d type %i flags %d start %lld length %lld proc_id %d client_id %s\n",
2181 		 fid->fid, flock->type, flock->flags, flock->start,
2182 		 flock->length, flock->proc_id, flock->client_id);
2183 
2184 	req = p9_client_rpc(clnt, P9_TLOCK, "dbdqqds", fid->fid, flock->type,
2185 			    flock->flags, flock->start, flock->length,
2186 			    flock->proc_id, flock->client_id);
2187 
2188 	if (IS_ERR(req))
2189 		return PTR_ERR(req);
2190 
2191 	err = p9pdu_readf(&req->rc, clnt->proto_version, "b", status);
2192 	if (err) {
2193 		trace_9p_protocol_dump(clnt, &req->rc);
2194 		goto error;
2195 	}
2196 	p9_debug(P9_DEBUG_9P, "<<< RLOCK status %i\n", *status);
2197 error:
2198 	p9_req_put(clnt, req);
2199 	return err;
2200 }
2201 EXPORT_SYMBOL(p9_client_lock_dotl);
2202 
2203 int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *glock)
2204 {
2205 	int err;
2206 	struct p9_client *clnt;
2207 	struct p9_req_t *req;
2208 
2209 	clnt = fid->clnt;
2210 	p9_debug(P9_DEBUG_9P,
2211 		 ">>> TGETLOCK fid %d, type %i start %lld length %lld proc_id %d client_id %s\n",
2212 		 fid->fid, glock->type, glock->start, glock->length,
2213 		 glock->proc_id, glock->client_id);
2214 
2215 	req = p9_client_rpc(clnt, P9_TGETLOCK, "dbqqds", fid->fid,
2216 			    glock->type, glock->start, glock->length,
2217 			    glock->proc_id, glock->client_id);
2218 
2219 	if (IS_ERR(req))
2220 		return PTR_ERR(req);
2221 
2222 	err = p9pdu_readf(&req->rc, clnt->proto_version, "bqqds", &glock->type,
2223 			  &glock->start, &glock->length, &glock->proc_id,
2224 			  &glock->client_id);
2225 	if (err) {
2226 		trace_9p_protocol_dump(clnt, &req->rc);
2227 		goto error;
2228 	}
2229 	p9_debug(P9_DEBUG_9P,
2230 		 "<<< RGETLOCK type %i start %lld length %lld proc_id %d client_id %s\n",
2231 		 glock->type, glock->start, glock->length,
2232 		 glock->proc_id, glock->client_id);
2233 error:
2234 	p9_req_put(clnt, req);
2235 	return err;
2236 }
2237 EXPORT_SYMBOL(p9_client_getlock_dotl);
2238 
2239 int p9_client_readlink(struct p9_fid *fid, char **target)
2240 {
2241 	int err;
2242 	struct p9_client *clnt;
2243 	struct p9_req_t *req;
2244 
2245 	clnt = fid->clnt;
2246 	p9_debug(P9_DEBUG_9P, ">>> TREADLINK fid %d\n", fid->fid);
2247 
2248 	req = p9_client_rpc(clnt, P9_TREADLINK, "d", fid->fid);
2249 	if (IS_ERR(req))
2250 		return PTR_ERR(req);
2251 
2252 	err = p9pdu_readf(&req->rc, clnt->proto_version, "s", target);
2253 	if (err) {
2254 		trace_9p_protocol_dump(clnt, &req->rc);
2255 		goto error;
2256 	}
2257 	p9_debug(P9_DEBUG_9P, "<<< RREADLINK target %s\n", *target);
2258 error:
2259 	p9_req_put(clnt, req);
2260 	return err;
2261 }
2262 EXPORT_SYMBOL(p9_client_readlink);
2263 
2264 int __init p9_client_init(void)
2265 {
2266 	p9_req_cache = KMEM_CACHE(p9_req_t, SLAB_TYPESAFE_BY_RCU);
2267 	return p9_req_cache ? 0 : -ENOMEM;
2268 }
2269 
2270 void __exit p9_client_exit(void)
2271 {
2272 	kmem_cache_destroy(p9_req_cache);
2273 }
2274