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