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