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