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