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