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