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