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