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