blob: 8bba0d9cf975417b64b0483849f9201bd7fd4d2a [file] [log] [blame]
Thomas Gleixner1f327612019-05-28 09:57:16 -07001// SPDX-License-Identifier: GPL-2.0-only
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05002/*
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05003 * 9P Client
4 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06005 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05006 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05007 */
8
Joe Perches5d385152011-11-28 10:40:46 -08009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050011#include <linux/module.h>
12#include <linux/errno.h>
13#include <linux/fs.h>
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060014#include <linux/poll.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050015#include <linux/idr.h>
16#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010018#include <linux/sched/signal.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050019#include <linux/uaccess.h>
Al Viro4f3b35c2015-04-01 19:57:53 -040020#include <linux/uio.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050021#include <net/9p/9p.h>
Eric Van Hensbergenfb0466c2007-10-17 14:31:07 -050022#include <linux/parser.h>
David Howellsc4fac912017-07-05 16:25:37 +010023#include <linux/seq_file.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050024#include <net/9p/client.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050025#include <net/9p/transport.h>
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050026#include "protocol.h"
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050027
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +053028#define CREATE_TRACE_POINTS
29#include <trace/events/9p.h>
30
Christian Schoenebeck9c4d94d2021-09-04 17:12:51 +020031#define DEFAULT_MSIZE (128 * 1024)
Christian Schoenebeck9210fc02021-09-04 17:07:12 +020032
Dominique Martinet6e195b02021-11-02 22:16:43 +090033/* Client Option Parsing (code inspired by NFS code)
34 * - a little lazy - parse all client options
35 */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060036
37enum {
38 Opt_msize,
39 Opt_trans,
40 Opt_legacy,
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +000041 Opt_version,
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060042 Opt_err,
43};
44
Steven Whitehousea447c092008-10-13 10:46:57 +010045static const match_table_t tokens = {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060046 {Opt_msize, "msize=%u"},
47 {Opt_legacy, "noextend"},
48 {Opt_trans, "trans=%s"},
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +000049 {Opt_version, "version=%s"},
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060050 {Opt_err, NULL},
51};
52
Sripathi Kodi342fee12010-03-05 18:50:14 +000053inline int p9_is_proto_dotl(struct p9_client *clnt)
54{
Eric Dumazeta02cec22010-09-22 20:43:57 +000055 return clnt->proto_version == p9_proto_2000L;
Sripathi Kodi342fee12010-03-05 18:50:14 +000056}
57EXPORT_SYMBOL(p9_is_proto_dotl);
58
59inline int p9_is_proto_dotu(struct p9_client *clnt)
60{
Eric Dumazeta02cec22010-09-22 20:43:57 +000061 return clnt->proto_version == p9_proto_2000u;
Sripathi Kodi342fee12010-03-05 18:50:14 +000062}
63EXPORT_SYMBOL(p9_is_proto_dotu);
64
David Howellsc4fac912017-07-05 16:25:37 +010065int p9_show_client_options(struct seq_file *m, struct p9_client *clnt)
66{
Christian Schoenebeck9210fc02021-09-04 17:07:12 +020067 if (clnt->msize != DEFAULT_MSIZE)
David Howellsc4fac912017-07-05 16:25:37 +010068 seq_printf(m, ",msize=%u", clnt->msize);
Tuomas Tynkkynen61b272c2017-11-19 11:28:43 +020069 seq_printf(m, ",trans=%s", clnt->trans_mod->name);
David Howellsc4fac912017-07-05 16:25:37 +010070
71 switch (clnt->proto_version) {
72 case p9_proto_legacy:
73 seq_puts(m, ",noextend");
74 break;
75 case p9_proto_2000u:
76 seq_puts(m, ",version=9p2000.u");
77 break;
78 case p9_proto_2000L:
79 /* Default */
80 break;
81 }
82
83 if (clnt->trans_mod->show_options)
84 return clnt->trans_mod->show_options(m, clnt);
85 return 0;
86}
87EXPORT_SYMBOL(p9_show_client_options);
88
Dominique Martinet6e195b02021-11-02 22:16:43 +090089/* Some error codes are taken directly from the server replies,
Simon Derr43def352012-08-10 15:52:06 +020090 * make sure they are valid.
91 */
92static int safe_errno(int err)
93{
Dominique Martinet6e195b02021-11-02 22:16:43 +090094 if (err > 0 || err < -MAX_ERRNO) {
Simon Derr43def352012-08-10 15:52:06 +020095 p9_debug(P9_DEBUG_ERROR, "Invalid error code %d\n", err);
96 return -EPROTO;
97 }
98 return err;
99}
100
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +0000101/* Interpret mount option for protocol version */
Prem Karat4d630552011-05-06 18:35:32 +0530102static int get_protocol_version(char *s)
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +0000103{
Dan Carpenter3dc9fef2010-04-05 14:37:28 -0500104 int version = -EINVAL;
105
Prem Karat4d630552011-05-06 18:35:32 +0530106 if (!strcmp(s, "9p2000")) {
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +0000107 version = p9_proto_legacy;
Joe Perches5d385152011-11-28 10:40:46 -0800108 p9_debug(P9_DEBUG_9P, "Protocol version: Legacy\n");
Prem Karat4d630552011-05-06 18:35:32 +0530109 } else if (!strcmp(s, "9p2000.u")) {
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +0000110 version = p9_proto_2000u;
Joe Perches5d385152011-11-28 10:40:46 -0800111 p9_debug(P9_DEBUG_9P, "Protocol version: 9P2000.u\n");
Prem Karat4d630552011-05-06 18:35:32 +0530112 } else if (!strcmp(s, "9p2000.L")) {
Sripathi Kodi45bc21e2010-03-08 17:33:04 +0000113 version = p9_proto_2000L;
Joe Perches5d385152011-11-28 10:40:46 -0800114 p9_debug(P9_DEBUG_9P, "Protocol version: 9P2000.L\n");
Dominique Martinet6e195b02021-11-02 22:16:43 +0900115 } else {
Joe Perches5d385152011-11-28 10:40:46 -0800116 pr_info("Unknown protocol version %s\n", s);
Dominique Martinet6e195b02021-11-02 22:16:43 +0900117 }
Prem Karat4d630552011-05-06 18:35:32 +0530118
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +0000119 return version;
120}
121
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600122/**
Xiongfeng Wang03ff7372021-03-27 16:15:54 +0800123 * parse_opts - parse mount options into client structure
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600124 * @opts: options string passed from mount
125 * @clnt: existing v9fs client information
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600126 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600127 * Return 0 upon success, -ERRNO upon failure
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600128 */
129
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600130static int parse_opts(char *opts, struct p9_client *clnt)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600131{
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600132 char *options, *tmp_options;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600133 char *p;
134 substring_t args[MAX_OPT_ARGS];
135 int option;
Prem Karat4d630552011-05-06 18:35:32 +0530136 char *s;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600137 int ret = 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600138
Aneesh Kumar K.V095e7992013-05-20 12:20:54 +0530139 clnt->proto_version = p9_proto_2000L;
Christian Schoenebeck9210fc02021-09-04 17:07:12 +0200140 clnt->msize = DEFAULT_MSIZE;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600141
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600142 if (!opts)
143 return 0;
144
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600145 tmp_options = kstrdup(opts, GFP_KERNEL);
Dominique Martinet6e195b02021-11-02 22:16:43 +0900146 if (!tmp_options)
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600147 return -ENOMEM;
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600148 options = tmp_options;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600149
150 while ((p = strsep(&options, ",")) != NULL) {
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530151 int token, r;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900152
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600153 if (!*p)
154 continue;
155 token = match_token(p, tokens, args);
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530156 switch (token) {
157 case Opt_msize:
158 r = match_int(&args[0], &option);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600159 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800160 p9_debug(P9_DEBUG_ERROR,
161 "integer field, but no integer?\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600162 ret = r;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600163 continue;
164 }
Dominique Martinet574d3562018-11-05 09:52:48 +0100165 if (option < 4096) {
166 p9_debug(P9_DEBUG_ERROR,
167 "msize should be at least 4k\n");
168 ret = -EINVAL;
169 continue;
170 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600171 clnt->msize = option;
172 break;
173 case Opt_trans:
Prem Karat4d630552011-05-06 18:35:32 +0530174 s = match_strdup(&args[0]);
175 if (!s) {
176 ret = -ENOMEM;
Joe Perches5d385152011-11-28 10:40:46 -0800177 p9_debug(P9_DEBUG_ERROR,
178 "problem allocating copy of trans arg\n");
Prem Karat4d630552011-05-06 18:35:32 +0530179 goto free_and_return;
Chengguang Xu9421c3e2018-04-05 16:20:01 -0700180 }
181
182 v9fs_put_trans(clnt->trans_mod);
Prem Karat4d630552011-05-06 18:35:32 +0530183 clnt->trans_mod = v9fs_get_trans_by_name(s);
Dominique Martinet6e195b02021-11-02 22:16:43 +0900184 if (!clnt->trans_mod) {
Joe Perches5d385152011-11-28 10:40:46 -0800185 pr_info("Could not find request transport: %s\n",
186 s);
Eric Van Hensbergen349d3bb2010-01-15 19:01:10 -0600187 ret = -EINVAL;
Eric Van Hensbergen349d3bb2010-01-15 19:01:10 -0600188 }
Prem Karat4d630552011-05-06 18:35:32 +0530189 kfree(s);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600190 break;
191 case Opt_legacy:
Sripathi Kodi342fee12010-03-05 18:50:14 +0000192 clnt->proto_version = p9_proto_legacy;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600193 break;
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +0000194 case Opt_version:
Prem Karat4d630552011-05-06 18:35:32 +0530195 s = match_strdup(&args[0]);
196 if (!s) {
197 ret = -ENOMEM;
Joe Perches5d385152011-11-28 10:40:46 -0800198 p9_debug(P9_DEBUG_ERROR,
199 "problem allocating copy of version arg\n");
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +0000200 goto free_and_return;
Prem Karat4d630552011-05-06 18:35:32 +0530201 }
Chengguang Xu8d856c72018-06-07 17:05:03 -0700202 r = get_protocol_version(s);
203 if (r < 0)
204 ret = r;
205 else
206 clnt->proto_version = r;
Prem Karat4d630552011-05-06 18:35:32 +0530207 kfree(s);
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +0000208 break;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600209 default:
210 continue;
211 }
212 }
Tejun Heo72029fe2008-09-24 16:22:23 -0500213
Eric Van Hensbergen349d3bb2010-01-15 19:01:10 -0600214free_and_return:
piaojunc290fba2018-07-13 16:59:06 -0700215 if (ret)
216 v9fs_put_trans(clnt->trans_mod);
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600217 kfree(tmp_options);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600218 return ret;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600219}
220
Dominique Martinet91a76be2018-07-30 15:14:37 +0900221static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc,
222 int alloc_msize)
Simon Derr53873202013-06-21 15:32:36 +0200223{
Dominique Martinet91a76be2018-07-30 15:14:37 +0900224 if (likely(c->fcall_cache) && alloc_msize == c->msize) {
225 fc->sdata = kmem_cache_alloc(c->fcall_cache, GFP_NOFS);
226 fc->cache = c->fcall_cache;
227 } else {
228 fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
229 fc->cache = NULL;
230 }
Dominique Martinet523adb62018-07-30 05:55:19 +0000231 if (!fc->sdata)
232 return -ENOMEM;
Simon Derr53873202013-06-21 15:32:36 +0200233 fc->capacity = alloc_msize;
Dominique Martinet523adb62018-07-30 05:55:19 +0000234 return 0;
Simon Derr53873202013-06-21 15:32:36 +0200235}
236
Dominique Martinet523adb62018-07-30 05:55:19 +0000237void p9_fcall_fini(struct p9_fcall *fc)
238{
Dominique Martinet91a76be2018-07-30 15:14:37 +0900239 /* sdata can be NULL for interrupted requests in trans_rdma,
240 * and kmem_cache_free does not do NULL-check for us
241 */
242 if (unlikely(!fc->sdata))
243 return;
244
245 if (fc->cache)
246 kmem_cache_free(fc->cache, fc->sdata);
247 else
248 kfree(fc->sdata);
Dominique Martinet523adb62018-07-30 05:55:19 +0000249}
250EXPORT_SYMBOL(p9_fcall_fini);
251
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700252static struct kmem_cache *p9_req_cache;
253
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500254/**
Xiongfeng Wang03ff7372021-03-27 16:15:54 +0800255 * p9_tag_alloc - Allocate a new request.
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700256 * @c: Client session.
257 * @type: Transaction type.
258 * @max_size: Maximum packet size for this request.
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500259 *
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700260 * Context: Process context.
261 * Return: Pointer to new request.
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500262 */
Dan Carpenteref6b0802011-08-26 19:57:40 +0300263static struct p9_req_t *
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700264p9_tag_alloc(struct p9_client *c, int8_t type, unsigned int max_size)
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500265{
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700266 struct p9_req_t *req = kmem_cache_alloc(p9_req_cache, GFP_NOFS);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530267 int alloc_msize = min(c->msize, max_size);
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700268 int tag;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500269
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700270 if (!req)
Dan Carpenter72ea0322018-09-26 13:39:34 +0300271 return ERR_PTR(-ENOMEM);
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500272
Dominique Martinet91a76be2018-07-30 15:14:37 +0900273 if (p9_fcall_init(c, &req->tc, alloc_msize))
Dominique Martinet523adb62018-07-30 05:55:19 +0000274 goto free_req;
Dominique Martinet91a76be2018-07-30 15:14:37 +0900275 if (p9_fcall_init(c, &req->rc, alloc_msize))
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700276 goto free;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500277
Dominique Martinet523adb62018-07-30 05:55:19 +0000278 p9pdu_reset(&req->tc);
279 p9pdu_reset(&req->rc);
Lu Shuaibing0ce772f2019-06-13 15:08:54 +0800280 req->t_err = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500281 req->status = REQ_STATUS_ALLOC;
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700282 init_waitqueue_head(&req->wq);
283 INIT_LIST_HEAD(&req->req_list);
284
285 idr_preload(GFP_NOFS);
286 spin_lock_irq(&c->lock);
287 if (type == P9_TVERSION)
288 tag = idr_alloc(&c->reqs, req, P9_NOTAG, P9_NOTAG + 1,
289 GFP_NOWAIT);
290 else
291 tag = idr_alloc(&c->reqs, req, 0, P9_NOTAG, GFP_NOWAIT);
Dominique Martinet523adb62018-07-30 05:55:19 +0000292 req->tc.tag = tag;
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700293 spin_unlock_irq(&c->lock);
294 idr_preload_end();
295 if (tag < 0)
296 goto free;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500297
Tomas Bortoli728356d2018-08-14 19:43:42 +0200298 /* Init ref to two because in the general case there is one ref
299 * that is put asynchronously by a writer thread, one ref
300 * temporarily given by p9_tag_lookup and put by p9_client_cb
301 * in the recv thread, and one ref put by p9_tag_remove in the
302 * main thread. The only exception is virtio that does not use
303 * p9_tag_lookup but does not have a writer thread either
304 * (the write happens synchronously in the request/zc_request
305 * callback), so p9_client_cb eats the second ref there
306 * as the pointer is duplicated directly by virtqueue_add_sgs()
307 */
308 refcount_set(&req->refcount.refcount, 2);
309
Simon Derrea071aa2013-06-21 15:32:34 +0200310 return req;
311
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700312free:
Dominique Martinet523adb62018-07-30 05:55:19 +0000313 p9_fcall_fini(&req->tc);
314 p9_fcall_fini(&req->rc);
315free_req:
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700316 kmem_cache_free(p9_req_cache, req);
Simon Derrea071aa2013-06-21 15:32:34 +0200317 return ERR_PTR(-ENOMEM);
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500318}
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500319
320/**
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700321 * p9_tag_lookup - Look up a request by tag.
322 * @c: Client session.
323 * @tag: Transaction ID.
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500324 *
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700325 * Context: Any context.
326 * Return: A request, or %NULL if there is no request with that tag.
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500327 */
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500328struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
329{
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700330 struct p9_req_t *req;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500331
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700332 rcu_read_lock();
Tomas Bortoli728356d2018-08-14 19:43:42 +0200333again:
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700334 req = idr_find(&c->reqs, tag);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200335 if (req) {
336 /* We have to be careful with the req found under rcu_read_lock
337 * Thanks to SLAB_TYPESAFE_BY_RCU we can safely try to get the
338 * ref again without corrupting other data, then check again
339 * that the tag matches once we have the ref
340 */
341 if (!p9_req_try_get(req))
342 goto again;
343 if (req->tc.tag != tag) {
344 p9_req_put(req);
345 goto again;
346 }
347 }
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700348 rcu_read_unlock();
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500349
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700350 return req;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500351}
352EXPORT_SYMBOL(p9_tag_lookup);
353
354/**
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +0200355 * p9_tag_remove - Remove a tag.
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700356 * @c: Client session.
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +0200357 * @r: Request of reference.
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500358 *
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700359 * Context: Any context.
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500360 */
Tomas Bortoli728356d2018-08-14 19:43:42 +0200361static int p9_tag_remove(struct p9_client *c, struct p9_req_t *r)
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500362{
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700363 unsigned long flags;
Dominique Martinet523adb62018-07-30 05:55:19 +0000364 u16 tag = r->tc.tag;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500365
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700366 p9_debug(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag);
367 spin_lock_irqsave(&c->lock, flags);
368 idr_remove(&c->reqs, tag);
369 spin_unlock_irqrestore(&c->lock, flags);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200370 return p9_req_put(r);
371}
372
373static void p9_req_free(struct kref *ref)
374{
375 struct p9_req_t *r = container_of(ref, struct p9_req_t, refcount);
Dominique Martinet6e195b02021-11-02 22:16:43 +0900376
Dominique Martinet523adb62018-07-30 05:55:19 +0000377 p9_fcall_fini(&r->tc);
378 p9_fcall_fini(&r->rc);
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700379 kmem_cache_free(p9_req_cache, r);
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500380}
381
Tomas Bortoli728356d2018-08-14 19:43:42 +0200382int p9_req_put(struct p9_req_t *r)
383{
384 return kref_put(&r->refcount, p9_req_free);
385}
386EXPORT_SYMBOL(p9_req_put);
387
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500388/**
389 * p9_tag_cleanup - cleans up tags structure and reclaims resources
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600390 * @c: v9fs client struct
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500391 *
392 * This frees resources associated with the tags structure
393 *
394 */
395static void p9_tag_cleanup(struct p9_client *c)
396{
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700397 struct p9_req_t *req;
398 int id;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500399
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700400 rcu_read_lock();
401 idr_for_each_entry(&c->reqs, req, id) {
402 pr_info("Tag %d still in use\n", id);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200403 if (p9_tag_remove(c, req) == 0)
404 pr_warn("Packet with tag %d has still references",
405 req->tc.tag);
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500406 }
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700407 rcu_read_unlock();
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500408}
409
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500410/**
411 * p9_client_cb - call back from transport to client
Andrew Lunn760b3d62020-10-31 19:26:55 +0100412 * @c: client state
413 * @req: request received
414 * @status: request status, one of REQ_STATUS_*
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500415 *
416 */
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100417void p9_client_cb(struct p9_client *c, struct p9_req_t *req, int status)
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500418{
Dominique Martinet523adb62018-07-30 05:55:19 +0000419 p9_debug(P9_DEBUG_MUX, " tag %d\n", req->tc.tag);
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100420
Dominique Martinet6e195b02021-11-02 22:16:43 +0900421 /* This barrier is needed to make sure any change made to req before
Matthew Wilcox2d58f632018-07-11 14:02:20 -0700422 * the status change is visible to another thread
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100423 */
424 smp_wmb();
425 req->status = status;
426
Matthew Wilcox2557d0c2018-07-11 14:02:23 -0700427 wake_up(&req->wq);
Dominique Martinet523adb62018-07-30 05:55:19 +0000428 p9_debug(P9_DEBUG_MUX, "wakeup: %d\n", req->tc.tag);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200429 p9_req_put(req);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500430}
431EXPORT_SYMBOL(p9_client_cb);
432
433/**
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500434 * p9_parse_header - parse header arguments out of a packet
435 * @pdu: packet to parse
436 * @size: size of packet
437 * @type: type of request
438 * @tag: tag of packet
439 * @rewind: set if we need to rewind offset afterwards
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500440 */
441
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500442int
Dominique Martinet6e195b02021-11-02 22:16:43 +0900443p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type,
444 int16_t *tag, int rewind)
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500445{
Dominique Martinet6e195b02021-11-02 22:16:43 +0900446 s8 r_type;
447 s16 r_tag;
448 s32 r_size;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500449 int offset = pdu->offset;
450 int err;
451
452 pdu->offset = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500453
454 err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag);
455 if (err)
456 goto rewind_and_exit;
457
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500458 if (type)
459 *type = r_type;
460 if (tag)
461 *tag = r_tag;
462 if (size)
463 *size = r_size;
464
Tomas Bortolif9845792018-07-23 17:44:04 +0200465 if (pdu->size != r_size || r_size < 7) {
466 err = -EINVAL;
467 goto rewind_and_exit;
468 }
469
470 pdu->id = r_type;
471 pdu->tag = r_tag;
472
473 p9_debug(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n",
474 pdu->size, pdu->id, pdu->tag);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500475
476rewind_and_exit:
477 if (rewind)
478 pdu->offset = offset;
479 return err;
480}
481EXPORT_SYMBOL(p9_parse_header);
482
483/**
484 * p9_check_errors - check 9p packet for error return and process it
485 * @c: current client instance
486 * @req: request to parse and check for error conditions
487 *
488 * returns error code if one is discovered, otherwise returns 0
489 *
490 * this will have to be more complicated if we have multiple
491 * error packet types
492 */
493
494static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
495{
Dominique Martinet6e195b02021-11-02 22:16:43 +0900496 s8 type;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500497 int err;
Venkateswararao Jujjuri (JV)ca41bb32011-02-01 20:04:59 -0800498 int ecode;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500499
Dominique Martinet523adb62018-07-30 05:55:19 +0000500 err = p9_parse_header(&req->rc, NULL, &type, NULL, 0);
501 if (req->rc.size >= c->msize) {
Tomas Bortolif9845792018-07-23 17:44:04 +0200502 p9_debug(P9_DEBUG_ERROR,
503 "requested packet size too big: %d\n",
Dominique Martinet523adb62018-07-30 05:55:19 +0000504 req->rc.size);
Tomas Bortolif9845792018-07-23 17:44:04 +0200505 return -EIO;
506 }
Dominique Martinet6e195b02021-11-02 22:16:43 +0900507 /* dump the response from server
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530508 * This should be after check errors which poplulate pdu_fcall.
509 */
Dominique Martinet523adb62018-07-30 05:55:19 +0000510 trace_9p_protocol_dump(c, &req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500511 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800512 p9_debug(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500513 return err;
514 }
Venkateswararao Jujjuri (JV)ca41bb32011-02-01 20:04:59 -0800515 if (type != P9_RERROR && type != P9_RLERROR)
516 return 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500517
Venkateswararao Jujjuri (JV)ca41bb32011-02-01 20:04:59 -0800518 if (!p9_is_proto_dotl(c)) {
519 char *ename;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900520
Dominique Martinet523adb62018-07-30 05:55:19 +0000521 err = p9pdu_readf(&req->rc, c->proto_version, "s?d",
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530522 &ename, &ecode);
Venkateswararao Jujjuri (JV)ca41bb32011-02-01 20:04:59 -0800523 if (err)
524 goto out_err;
525
Arnd Bergmann287980e2016-05-27 23:23:25 +0200526 if (p9_is_proto_dotu(c) && ecode < 512)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500527 err = -ecode;
528
Arnd Bergmann287980e2016-05-27 23:23:25 +0200529 if (!err) {
Venkateswararao Jujjuri (JV)ca41bb32011-02-01 20:04:59 -0800530 err = p9_errstr2errno(ename, strlen(ename));
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500531
Joe Perches5d385152011-11-28 10:40:46 -0800532 p9_debug(P9_DEBUG_9P, "<<< RERROR (%d) %s\n",
533 -ecode, ename);
Venkateswararao Jujjuri (JV)ca41bb32011-02-01 20:04:59 -0800534 }
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530535 kfree(ename);
Venkateswararao Jujjuri (JV)ca41bb32011-02-01 20:04:59 -0800536 } else {
Dominique Martinet523adb62018-07-30 05:55:19 +0000537 err = p9pdu_readf(&req->rc, c->proto_version, "d", &ecode);
Dominique Martinet27eb4c32021-11-02 19:47:47 +0900538 if (err)
539 goto out_err;
Venkateswararao Jujjuri (JV)ca41bb32011-02-01 20:04:59 -0800540 err = -ecode;
541
Joe Perches5d385152011-11-28 10:40:46 -0800542 p9_debug(P9_DEBUG_9P, "<<< RLERROR (%d)\n", -ecode);
Venkateswararao Jujjuri (JV)ca41bb32011-02-01 20:04:59 -0800543 }
544
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500545 return err;
Arun R Bharadwaj4f7ebe82010-07-28 14:17:26 +0530546
547out_err:
Joe Perches5d385152011-11-28 10:40:46 -0800548 p9_debug(P9_DEBUG_ERROR, "couldn't parse error%d\n", err);
Arun R Bharadwaj4f7ebe82010-07-28 14:17:26 +0530549
550 return err;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500551}
552
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530553/**
554 * p9_check_zc_errors - check 9p packet for error return and process it
555 * @c: current client instance
556 * @req: request to parse and check for error conditions
Andrew Lunn760b3d62020-10-31 19:26:55 +0100557 * @uidata: external buffer containing error
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530558 * @in_hdrlen: Size of response protocol buffer.
559 *
560 * returns error code if one is discovered, otherwise returns 0
561 *
562 * this will have to be more complicated if we have multiple
563 * error packet types
564 */
565
566static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req,
Al Viro4f3b35c2015-04-01 19:57:53 -0400567 struct iov_iter *uidata, int in_hdrlen)
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530568{
569 int err;
570 int ecode;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900571 s8 type;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530572 char *ename = NULL;
573
Dominique Martinet523adb62018-07-30 05:55:19 +0000574 err = p9_parse_header(&req->rc, NULL, &type, NULL, 0);
Dominique Martinet6e195b02021-11-02 22:16:43 +0900575 /* dump the response from server
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530576 * This should be after parse_header which poplulate pdu_fcall.
577 */
Dominique Martinet523adb62018-07-30 05:55:19 +0000578 trace_9p_protocol_dump(c, &req->rc);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530579 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800580 p9_debug(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530581 return err;
582 }
583
584 if (type != P9_RERROR && type != P9_RLERROR)
585 return 0;
586
587 if (!p9_is_proto_dotl(c)) {
588 /* Error is reported in string format */
Aneesh Kumar K.V42fe6482013-05-20 23:05:15 +0530589 int len;
590 /* 7 = header size for RERROR; */
591 int inline_len = in_hdrlen - 7;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530592
Dominique Martinet523adb62018-07-30 05:55:19 +0000593 len = req->rc.size - req->rc.offset;
Aneesh Kumar K.V42fe6482013-05-20 23:05:15 +0530594 if (len > (P9_ZC_HDR_SZ - 7)) {
595 err = -EFAULT;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530596 goto out_err;
597 }
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530598
Dominique Martinet523adb62018-07-30 05:55:19 +0000599 ename = &req->rc.sdata[req->rc.offset];
Aneesh Kumar K.V42fe6482013-05-20 23:05:15 +0530600 if (len > inline_len) {
601 /* We have error in external buffer */
Al Viro1c512a72017-02-17 23:16:09 -0500602 if (!copy_from_iter_full(ename + inline_len,
Dominique Martinet6e195b02021-11-02 22:16:43 +0900603 len - inline_len, uidata)) {
Al Viro4f3b35c2015-04-01 19:57:53 -0400604 err = -EFAULT;
605 goto out_err;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530606 }
607 }
Aneesh Kumar K.V42fe6482013-05-20 23:05:15 +0530608 ename = NULL;
Dominique Martinet523adb62018-07-30 05:55:19 +0000609 err = p9pdu_readf(&req->rc, c->proto_version, "s?d",
Aneesh Kumar K.V42fe6482013-05-20 23:05:15 +0530610 &ename, &ecode);
611 if (err)
612 goto out_err;
613
Arnd Bergmann287980e2016-05-27 23:23:25 +0200614 if (p9_is_proto_dotu(c) && ecode < 512)
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530615 err = -ecode;
Aneesh Kumar K.V42fe6482013-05-20 23:05:15 +0530616
Arnd Bergmann287980e2016-05-27 23:23:25 +0200617 if (!err) {
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530618 err = p9_errstr2errno(ename, strlen(ename));
619
Joe Perches5d385152011-11-28 10:40:46 -0800620 p9_debug(P9_DEBUG_9P, "<<< RERROR (%d) %s\n",
621 -ecode, ename);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530622 }
623 kfree(ename);
624 } else {
Dominique Martinet523adb62018-07-30 05:55:19 +0000625 err = p9pdu_readf(&req->rc, c->proto_version, "d", &ecode);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530626 err = -ecode;
627
Joe Perches5d385152011-11-28 10:40:46 -0800628 p9_debug(P9_DEBUG_9P, "<<< RLERROR (%d)\n", -ecode);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530629 }
630 return err;
631
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530632out_err:
Joe Perches5d385152011-11-28 10:40:46 -0800633 p9_debug(P9_DEBUG_ERROR, "couldn't parse error%d\n", err);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530634 return err;
635}
636
Rob Landleyaca00762011-05-08 18:46:38 +0000637static struct p9_req_t *
638p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...);
639
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500640/**
641 * p9_client_flush - flush (cancel) a request
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600642 * @c: client state
643 * @oldreq: request to cancel
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500644 *
Rob Landleyaca00762011-05-08 18:46:38 +0000645 * This sents a flush for a particular request and links
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500646 * the flush request to the original request. The current
647 * code only supports a single flush request although the protocol
648 * allows for multiple flush requests to be sent for a single request.
649 *
650 */
651
652static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
653{
654 struct p9_req_t *req;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900655 s16 oldtag;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500656 int err;
657
Dominique Martinet523adb62018-07-30 05:55:19 +0000658 err = p9_parse_header(&oldreq->tc, NULL, NULL, &oldtag, 1);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500659 if (err)
660 return err;
661
Joe Perches5d385152011-11-28 10:40:46 -0800662 p9_debug(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500663
664 req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag);
665 if (IS_ERR(req))
666 return PTR_ERR(req);
667
Dominique Martinet6e195b02021-11-02 22:16:43 +0900668 /* if we haven't received a response for oldreq,
Andi Shyti60ff7792013-07-25 10:54:24 +0200669 * remove it from the list
Simon Derr1cff3302013-06-21 15:32:42 +0200670 */
Tomas Bortoli728356d2018-08-14 19:43:42 +0200671 if (oldreq->status == REQ_STATUS_SENT) {
Simon Derrafd8d652014-03-10 16:38:49 +0100672 if (c->trans_mod->cancelled)
673 c->trans_mod->cancelled(c, oldreq);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200674 }
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500675
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +0200676 p9_tag_remove(c, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500677 return 0;
678}
679
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530680static struct p9_req_t *p9_client_prepare_req(struct p9_client *c,
681 int8_t type, int req_size,
682 const char *fmt, va_list ap)
683{
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700684 int err;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530685 struct p9_req_t *req;
686
Joe Perches5d385152011-11-28 10:40:46 -0800687 p9_debug(P9_DEBUG_MUX, "client %p op %d\n", c, type);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530688
689 /* we allow for any status other than disconnected */
690 if (c->status == Disconnected)
691 return ERR_PTR(-EIO);
692
693 /* if status is begin_disconnected we allow only clunk request */
Dominique Martinet6e195b02021-11-02 22:16:43 +0900694 if (c->status == BeginDisconnect && type != P9_TCLUNK)
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530695 return ERR_PTR(-EIO);
696
Matthew Wilcox996d5b42018-07-11 14:02:24 -0700697 req = p9_tag_alloc(c, type, req_size);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530698 if (IS_ERR(req))
699 return req;
700
701 /* marshall the data */
Dominique Martinet523adb62018-07-30 05:55:19 +0000702 p9pdu_prepare(&req->tc, req->tc.tag, type);
703 err = p9pdu_vwritef(&req->tc, c->proto_version, fmt, ap);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530704 if (err)
705 goto reterr;
Dominique Martinet523adb62018-07-30 05:55:19 +0000706 p9pdu_finalize(c, &req->tc);
707 trace_9p_client_req(c, type, req->tc.tag);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530708 return req;
709reterr:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +0200710 p9_tag_remove(c, req);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200711 /* We have to put also the 2nd reference as it won't be used */
712 p9_req_put(req);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530713 return ERR_PTR(err);
714}
715
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500716/**
717 * p9_client_rpc - issue a request and wait for a response
718 * @c: client session
719 * @type: type of request
720 * @fmt: protocol format string (see protocol.c)
721 *
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +0200722 * Returns request structure (which client must free using p9_tag_remove)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500723 */
724
725static struct p9_req_t *
726p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
727{
728 va_list ap;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530729 int sigpending, err;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500730 unsigned long flags;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530731 struct p9_req_t *req;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500732
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530733 va_start(ap, fmt);
734 req = p9_client_prepare_req(c, type, c->msize, fmt, ap);
735 va_end(ap);
736 if (IS_ERR(req))
737 return req;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500738
739 if (signal_pending(current)) {
740 sigpending = 1;
741 clear_thread_flag(TIF_SIGPENDING);
Dominique Martinet6e195b02021-11-02 22:16:43 +0900742 } else {
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500743 sigpending = 0;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900744 }
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500745
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500746 err = c->trans_mod->request(c, req);
747 if (err < 0) {
Tomas Bortoli728356d2018-08-14 19:43:42 +0200748 /* write won't happen */
749 p9_req_put(req);
M. Mohan Kumar3cd79672011-04-15 13:59:33 +0530750 if (err != -ERESTARTSYS && err != -EFAULT)
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700751 c->status = Disconnected;
Greg Kurza8522242018-04-05 16:19:44 -0700752 goto recalc_sigpending;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500753 }
Jim Garlicka314f272012-02-01 12:48:53 -0800754again:
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530755 /* Wait for the response */
Matthew Wilcox2557d0c2018-07-11 14:02:23 -0700756 err = wait_event_killable(req->wq, req->status >= REQ_STATUS_RCVD);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500757
Dominique Martinet6e195b02021-11-02 22:16:43 +0900758 /* Make sure our req is coherent with regard to updates in other
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100759 * threads - echoes to wmb() in the callback
760 */
761 smp_rmb();
762
Dominique Martinet6e195b02021-11-02 22:16:43 +0900763 if (err == -ERESTARTSYS && c->status == Connected &&
764 type == P9_TFLUSH) {
Jim Garlicka314f272012-02-01 12:48:53 -0800765 sigpending = 1;
766 clear_thread_flag(TIF_SIGPENDING);
767 goto again;
768 }
769
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500770 if (req->status == REQ_STATUS_ERROR) {
Joe Perches5d385152011-11-28 10:40:46 -0800771 p9_debug(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500772 err = req->t_err;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500773 }
Dominique Martinet6e195b02021-11-02 22:16:43 +0900774 if (err == -ERESTARTSYS && c->status == Connected) {
Joe Perches5d385152011-11-28 10:40:46 -0800775 p9_debug(P9_DEBUG_MUX, "flushing\n");
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500776 sigpending = 1;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500777 clear_thread_flag(TIF_SIGPENDING);
778
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500779 if (c->trans_mod->cancel(c, req))
780 p9_client_flush(c, req);
781
782 /* if we received the response anyway, don't signal error */
783 if (req->status == REQ_STATUS_RCVD)
784 err = 0;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500785 }
Greg Kurza8522242018-04-05 16:19:44 -0700786recalc_sigpending:
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500787 if (sigpending) {
788 spin_lock_irqsave(&current->sighand->siglock, flags);
789 recalc_sigpending();
790 spin_unlock_irqrestore(&current->sighand->siglock, flags);
791 }
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500792 if (err < 0)
793 goto reterr;
794
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500795 err = p9_check_errors(c, req);
Dominique Martinet523adb62018-07-30 05:55:19 +0000796 trace_9p_client_res(c, type, req->rc.tag, err);
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530797 if (!err)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500798 return req;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530799reterr:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +0200800 p9_tag_remove(c, req);
Simon Derr43def352012-08-10 15:52:06 +0200801 return ERR_PTR(safe_errno(err));
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530802}
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500803
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530804/**
805 * p9_client_zc_rpc - issue a request and wait for a response
806 * @c: client session
807 * @type: type of request
Al Viro4f3b35c2015-04-01 19:57:53 -0400808 * @uidata: destination for zero copy read
809 * @uodata: source for zero copy write
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530810 * @inlen: read buffer size
811 * @olen: write buffer size
Andrew Lunn15e522a2020-07-13 01:14:57 +0200812 * @in_hdrlen: reader header size, This is the size of response protocol data
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530813 * @fmt: protocol format string (see protocol.c)
814 *
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +0200815 * Returns request structure (which client must free using p9_tag_remove)
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530816 */
817static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
Al Viro4f3b35c2015-04-01 19:57:53 -0400818 struct iov_iter *uidata,
819 struct iov_iter *uodata,
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530820 int inlen, int olen, int in_hdrlen,
Al Viro4f3b35c2015-04-01 19:57:53 -0400821 const char *fmt, ...)
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530822{
823 va_list ap;
824 int sigpending, err;
825 unsigned long flags;
826 struct p9_req_t *req;
827
828 va_start(ap, fmt);
Dominique Martinet6e195b02021-11-02 22:16:43 +0900829 /* We allocate a inline protocol data of only 4k bytes.
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530830 * The actual content is passed in zero-copy fashion.
831 */
832 req = p9_client_prepare_req(c, type, P9_ZC_HDR_SZ, fmt, ap);
833 va_end(ap);
834 if (IS_ERR(req))
835 return req;
836
837 if (signal_pending(current)) {
838 sigpending = 1;
839 clear_thread_flag(TIF_SIGPENDING);
Dominique Martinet6e195b02021-11-02 22:16:43 +0900840 } else {
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530841 sigpending = 0;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900842 }
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530843
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530844 err = c->trans_mod->zc_request(c, req, uidata, uodata,
Al Viro4f3b35c2015-04-01 19:57:53 -0400845 inlen, olen, in_hdrlen);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530846 if (err < 0) {
847 if (err == -EIO)
848 c->status = Disconnected;
Al Viroa84b69c2015-07-04 16:04:19 -0400849 if (err != -ERESTARTSYS)
Greg Kurza8522242018-04-05 16:19:44 -0700850 goto recalc_sigpending;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530851 }
852 if (req->status == REQ_STATUS_ERROR) {
Joe Perches5d385152011-11-28 10:40:46 -0800853 p9_debug(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530854 err = req->t_err;
855 }
Dominique Martinet6e195b02021-11-02 22:16:43 +0900856 if (err == -ERESTARTSYS && c->status == Connected) {
Joe Perches5d385152011-11-28 10:40:46 -0800857 p9_debug(P9_DEBUG_MUX, "flushing\n");
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530858 sigpending = 1;
859 clear_thread_flag(TIF_SIGPENDING);
860
861 if (c->trans_mod->cancel(c, req))
862 p9_client_flush(c, req);
863
864 /* if we received the response anyway, don't signal error */
865 if (req->status == REQ_STATUS_RCVD)
866 err = 0;
867 }
Greg Kurza8522242018-04-05 16:19:44 -0700868recalc_sigpending:
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530869 if (sigpending) {
870 spin_lock_irqsave(&current->sighand->siglock, flags);
871 recalc_sigpending();
872 spin_unlock_irqrestore(&current->sighand->siglock, flags);
873 }
874 if (err < 0)
875 goto reterr;
876
Al Viro4f3b35c2015-04-01 19:57:53 -0400877 err = p9_check_zc_errors(c, req, uidata, in_hdrlen);
Dominique Martinet523adb62018-07-30 05:55:19 +0000878 trace_9p_client_res(c, type, req->rc.tag, err);
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530879 if (!err)
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530880 return req;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500881reterr:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +0200882 p9_tag_remove(c, req);
Simon Derr43def352012-08-10 15:52:06 +0200883 return ERR_PTR(safe_errno(err));
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500884}
885
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500886static struct p9_fid *p9_fid_create(struct p9_client *clnt)
887{
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500888 int ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500889 struct p9_fid *fid;
890
Joe Perches5d385152011-11-28 10:40:46 -0800891 p9_debug(P9_DEBUG_FID, "clnt %p\n", clnt);
Dominique Martinet6e195b02021-11-02 22:16:43 +0900892 fid = kmalloc(sizeof(*fid), GFP_KERNEL);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500893 if (!fid)
Matthew Wilcoxb5303be2018-07-11 14:02:21 -0700894 return NULL;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500895
Dominique Martinet6e195b02021-11-02 22:16:43 +0900896 memset(&fid->qid, 0, sizeof(fid->qid));
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500897 fid->mode = -1;
David Howellsf8b9d532008-11-14 10:38:44 +1100898 fid->uid = current_fsuid();
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500899 fid->clnt = clnt;
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600900 fid->rdir = NULL;
Matthew Wilcoxf28cdf02018-07-11 14:02:22 -0700901 fid->fid = 0;
Dominique Martinetff5e72e2020-11-03 09:35:57 +0100902 refcount_set(&fid->count, 1);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500903
Matthew Wilcoxf28cdf02018-07-11 14:02:22 -0700904 idr_preload(GFP_KERNEL);
905 spin_lock_irq(&clnt->lock);
906 ret = idr_alloc_u32(&clnt->fids, fid, &fid->fid, P9_NOFID - 1,
907 GFP_NOWAIT);
908 spin_unlock_irq(&clnt->lock);
909 idr_preload_end();
Matthew Wilcoxf28cdf02018-07-11 14:02:22 -0700910 if (!ret)
911 return fid;
912
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500913 kfree(fid);
Matthew Wilcoxb5303be2018-07-11 14:02:21 -0700914 return NULL;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500915}
916
917static void p9_fid_destroy(struct p9_fid *fid)
918{
919 struct p9_client *clnt;
Tom Tuckercac23d62008-10-23 16:31:02 -0500920 unsigned long flags;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500921
Joe Perches5d385152011-11-28 10:40:46 -0800922 p9_debug(P9_DEBUG_FID, "fid %d\n", fid->fid);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500923 clnt = fid->clnt;
Tom Tuckercac23d62008-10-23 16:31:02 -0500924 spin_lock_irqsave(&clnt->lock, flags);
Matthew Wilcoxf28cdf02018-07-11 14:02:22 -0700925 idr_remove(&clnt->fids, fid->fid);
Tom Tuckercac23d62008-10-23 16:31:02 -0500926 spin_unlock_irqrestore(&clnt->lock, flags);
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600927 kfree(fid->rdir);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500928 kfree(fid);
929}
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600930
stephen hemminger32a875a2010-10-19 06:48:16 +0000931static int p9_client_version(struct p9_client *c)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500932{
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500933 int err = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500934 struct p9_req_t *req;
Tomas Bortoli79136902018-07-10 00:29:43 +0200935 char *version = NULL;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500936 int msize;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500937
Joe Perches5d385152011-11-28 10:40:46 -0800938 p9_debug(P9_DEBUG_9P, ">>> TVERSION msize %d protocol %d\n",
939 c->msize, c->proto_version);
Sripathi Kodic5a76972010-03-05 18:51:04 +0000940
941 switch (c->proto_version) {
Sripathi Kodi45bc21e2010-03-08 17:33:04 +0000942 case p9_proto_2000L:
Sripathi Kodic5a76972010-03-05 18:51:04 +0000943 req = p9_client_rpc(c, P9_TVERSION, "ds",
Dominique Martinet6e195b02021-11-02 22:16:43 +0900944 c->msize, "9P2000.L");
Sripathi Kodic5a76972010-03-05 18:51:04 +0000945 break;
946 case p9_proto_2000u:
947 req = p9_client_rpc(c, P9_TVERSION, "ds",
Dominique Martinet6e195b02021-11-02 22:16:43 +0900948 c->msize, "9P2000.u");
Sripathi Kodic5a76972010-03-05 18:51:04 +0000949 break;
950 case p9_proto_legacy:
951 req = p9_client_rpc(c, P9_TVERSION, "ds",
Dominique Martinet6e195b02021-11-02 22:16:43 +0900952 c->msize, "9P2000");
Sripathi Kodic5a76972010-03-05 18:51:04 +0000953 break;
954 default:
955 return -EINVAL;
Sripathi Kodic5a76972010-03-05 18:51:04 +0000956 }
957
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500958 if (IS_ERR(req))
959 return PTR_ERR(req);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500960
Dominique Martinet523adb62018-07-30 05:55:19 +0000961 err = p9pdu_readf(&req->rc, c->proto_version, "ds", &msize, &version);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500962 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800963 p9_debug(P9_DEBUG_9P, "version error %d\n", err);
Dominique Martinet523adb62018-07-30 05:55:19 +0000964 trace_9p_protocol_dump(c, &req->rc);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500965 goto error;
966 }
967
Joe Perches5d385152011-11-28 10:40:46 -0800968 p9_debug(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version);
Dominique Martinet6e195b02021-11-02 22:16:43 +0900969 if (!strncmp(version, "9P2000.L", 8)) {
Sripathi Kodi45bc21e2010-03-08 17:33:04 +0000970 c->proto_version = p9_proto_2000L;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900971 } else if (!strncmp(version, "9P2000.u", 8)) {
Sripathi Kodi342fee12010-03-05 18:50:14 +0000972 c->proto_version = p9_proto_2000u;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900973 } else if (!strncmp(version, "9P2000", 6)) {
Sripathi Kodi342fee12010-03-05 18:50:14 +0000974 c->proto_version = p9_proto_legacy;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900975 } else {
Dominique Martinet574d3562018-11-05 09:52:48 +0100976 p9_debug(P9_DEBUG_ERROR,
977 "server returned an unknown version: %s\n", version);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500978 err = -EREMOTEIO;
979 goto error;
980 }
981
Dominique Martinet574d3562018-11-05 09:52:48 +0100982 if (msize < 4096) {
983 p9_debug(P9_DEBUG_ERROR,
984 "server returned a msize < 4096: %d\n", msize);
985 err = -EREMOTEIO;
986 goto error;
987 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500988 if (msize < c->msize)
989 c->msize = msize;
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500990
991error:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500992 kfree(version);
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +0200993 p9_tag_remove(c, req);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500994
995 return err;
996}
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500997
998struct p9_client *p9_client_create(const char *dev_name, char *options)
999{
1000 int err;
1001 struct p9_client *clnt;
Will Deacon50192ab2013-08-21 18:24:47 +01001002 char *client_id;
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -05001003
1004 err = 0;
Dominique Martinet6e195b02021-11-02 22:16:43 +09001005 clnt = kmalloc(sizeof(*clnt), GFP_KERNEL);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001006 if (!clnt)
1007 return ERR_PTR(-ENOMEM);
1008
Tejun Heo72029fe2008-09-24 16:22:23 -05001009 clnt->trans_mod = NULL;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001010 clnt->trans = NULL;
Dominique Martinet91a76be2018-07-30 15:14:37 +09001011 clnt->fcall_cache = NULL;
Will Deacon50192ab2013-08-21 18:24:47 +01001012
1013 client_id = utsname()->nodename;
1014 memcpy(clnt->name, client_id, strlen(client_id) + 1);
1015
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001016 spin_lock_init(&clnt->lock);
Matthew Wilcoxf28cdf02018-07-11 14:02:22 -07001017 idr_init(&clnt->fids);
Matthew Wilcox996d5b42018-07-11 14:02:24 -07001018 idr_init(&clnt->reqs);
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -05001019
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001020 err = parse_opts(options, clnt);
1021 if (err < 0)
Matthew Wilcox996d5b42018-07-11 14:02:24 -07001022 goto free_client;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001023
Abhishek Kulkarnia17d1722009-07-14 13:24:10 -05001024 if (!clnt->trans_mod)
1025 clnt->trans_mod = v9fs_get_default_trans();
1026
Dominique Martinet6e195b02021-11-02 22:16:43 +09001027 if (!clnt->trans_mod) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001028 err = -EPROTONOSUPPORT;
Joe Perches5d385152011-11-28 10:40:46 -08001029 p9_debug(P9_DEBUG_ERROR,
1030 "No transport defined or default transport\n");
Matthew Wilcox996d5b42018-07-11 14:02:24 -07001031 goto free_client;
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -06001032 }
1033
Joe Perches5d385152011-11-28 10:40:46 -08001034 p9_debug(P9_DEBUG_MUX, "clnt %p trans %p msize %d protocol %d\n",
1035 clnt, clnt->trans_mod, clnt->msize, clnt->proto_version);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001036
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001037 err = clnt->trans_mod->create(clnt, dev_name, options);
1038 if (err)
Matthew Wilcoxf28cdf02018-07-11 14:02:22 -07001039 goto put_trans;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001040
Christian Schoenebeck15e27212021-12-30 14:23:18 +01001041 if (clnt->msize > clnt->trans_mod->maxsize) {
Venkateswararao Jujjuri (JV)c9ffb052011-06-29 18:06:33 -07001042 clnt->msize = clnt->trans_mod->maxsize;
Christian Schoenebeck15e27212021-12-30 14:23:18 +01001043 pr_info("Limiting 'msize' to %d as this is the maximum "
1044 "supported by transport %s\n",
1045 clnt->msize, clnt->trans_mod->name
1046 );
1047 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001048
Dominique Martinet574d3562018-11-05 09:52:48 +01001049 if (clnt->msize < 4096) {
1050 p9_debug(P9_DEBUG_ERROR,
1051 "Please specify a msize of at least 4k\n");
1052 err = -EINVAL;
zhengbinbb06c382019-03-13 16:01:37 +08001053 goto close_trans;
Dominique Martinet574d3562018-11-05 09:52:48 +01001054 }
1055
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -05001056 err = p9_client_version(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001057 if (err)
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -06001058 goto close_trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001059
Dominique Martinet91a76be2018-07-30 15:14:37 +09001060 /* P9_HDRSZ + 4 is the smallest packet header we can have that is
1061 * followed by data accessed from userspace by read
1062 */
1063 clnt->fcall_cache =
1064 kmem_cache_create_usercopy("9p-fcall-cache", clnt->msize,
1065 0, 0, P9_HDRSZ + 4,
1066 clnt->msize - (P9_HDRSZ + 4),
1067 NULL);
1068
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001069 return clnt;
1070
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -06001071close_trans:
1072 clnt->trans_mod->close(clnt);
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -06001073put_trans:
1074 v9fs_put_trans(clnt->trans_mod);
1075free_client:
1076 kfree(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001077 return ERR_PTR(err);
1078}
1079EXPORT_SYMBOL(p9_client_create);
1080
1081void p9_client_destroy(struct p9_client *clnt)
1082{
Matthew Wilcoxf28cdf02018-07-11 14:02:22 -07001083 struct p9_fid *fid;
1084 int id;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001085
Joe Perches5d385152011-11-28 10:40:46 -08001086 p9_debug(P9_DEBUG_MUX, "clnt %p\n", clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001087
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001088 if (clnt->trans_mod)
1089 clnt->trans_mod->close(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001090
Tejun Heo72029fe2008-09-24 16:22:23 -05001091 v9fs_put_trans(clnt->trans_mod);
1092
Matthew Wilcoxf28cdf02018-07-11 14:02:22 -07001093 idr_for_each_entry(&clnt->fids, fid, id) {
Joe Perches5d385152011-11-28 10:40:46 -08001094 pr_info("Found fid %d not clunked\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001095 p9_fid_destroy(fid);
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -05001096 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001097
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -05001098 p9_tag_cleanup(clnt);
1099
Dominique Martinet91a76be2018-07-30 15:14:37 +09001100 kmem_cache_destroy(clnt->fcall_cache);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001101 kfree(clnt);
1102}
1103EXPORT_SYMBOL(p9_client_destroy);
1104
1105void p9_client_disconnect(struct p9_client *clnt)
1106{
Joe Perches5d385152011-11-28 10:40:46 -08001107 p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001108 clnt->status = Disconnected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001109}
1110EXPORT_SYMBOL(p9_client_disconnect);
1111
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -05001112void p9_client_begin_disconnect(struct p9_client *clnt)
1113{
Joe Perches5d385152011-11-28 10:40:46 -08001114 p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -05001115 clnt->status = BeginDisconnect;
1116}
1117EXPORT_SYMBOL(p9_client_begin_disconnect);
1118
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001119struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001120 const char *uname, kuid_t n_uname,
1121 const char *aname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001122{
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +05301123 int err = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001124 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001125 struct p9_fid *fid;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001126 struct p9_qid qid;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001127
Joe Perches5d385152011-11-28 10:40:46 -08001128 p9_debug(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
1129 afid ? afid->fid : -1, uname, aname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001130 fid = p9_fid_create(clnt);
Matthew Wilcoxb5303be2018-07-11 14:02:21 -07001131 if (!fid) {
1132 err = -ENOMEM;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001133 goto error;
1134 }
Al Viro21c9f5c2015-04-02 21:47:49 -04001135 fid->uid = n_uname;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001136
Eric W. Biedermanf791f7c2013-01-29 16:09:41 -08001137 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001138 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001139 if (IS_ERR(req)) {
1140 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001141 goto error;
1142 }
1143
Dominique Martinet523adb62018-07-30 05:55:19 +00001144 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", &qid);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001145 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00001146 trace_9p_protocol_dump(clnt, &req->rc);
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001147 p9_tag_remove(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001148 goto error;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001149 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001150
Joe Perches5d385152011-11-28 10:40:46 -08001151 p9_debug(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001152 qid.type, qid.path, qid.version);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001153
1154 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1155
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001156 p9_tag_remove(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001157 return fid;
1158
1159error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001160 if (fid)
1161 p9_fid_destroy(fid);
1162 return ERR_PTR(err);
1163}
1164EXPORT_SYMBOL(p9_client_attach);
1165
Harsh Prateek Borab76225e2011-03-31 15:49:39 +05301166struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001167 const unsigned char * const *wnames, int clone)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001168{
1169 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001170 struct p9_client *clnt;
1171 struct p9_fid *fid;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001172 struct p9_qid *wqids;
1173 struct p9_req_t *req;
Dominique Martinet6e195b02021-11-02 22:16:43 +09001174 u16 nwqids, count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001175
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001176 err = 0;
Latchesar Ionkov62b2be52010-08-24 18:13:59 +00001177 wqids = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001178 clnt = oldfid->clnt;
1179 if (clone) {
1180 fid = p9_fid_create(clnt);
Matthew Wilcoxb5303be2018-07-11 14:02:21 -07001181 if (!fid) {
1182 err = -ENOMEM;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001183 goto error;
1184 }
1185
1186 fid->uid = oldfid->uid;
Dominique Martinet6e195b02021-11-02 22:16:43 +09001187 } else {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001188 fid = oldfid;
Dominique Martinet6e195b02021-11-02 22:16:43 +09001189 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001190
Joe Perches5d385152011-11-28 10:40:46 -08001191 p9_debug(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %ud wname[0] %s\n",
1192 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001193 req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001194 nwname, wnames);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001195 if (IS_ERR(req)) {
1196 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001197 goto error;
1198 }
1199
Dominique Martinet523adb62018-07-30 05:55:19 +00001200 err = p9pdu_readf(&req->rc, clnt->proto_version, "R", &nwqids, &wqids);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001201 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00001202 trace_9p_protocol_dump(clnt, &req->rc);
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001203 p9_tag_remove(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001204 goto clunk_fid;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001205 }
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001206 p9_tag_remove(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001207
Joe Perches5d385152011-11-28 10:40:46 -08001208 p9_debug(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001209
1210 if (nwqids != nwname) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001211 err = -ENOENT;
1212 goto clunk_fid;
1213 }
1214
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001215 for (count = 0; count < nwqids; count++)
Joe Perches5d385152011-11-28 10:40:46 -08001216 p9_debug(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001217 count, wqids[count].type,
1218 wqids[count].path,
1219 wqids[count].version);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001220
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001221 if (nwname)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001222 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001223 else
Eric Van Hensbergen154372e2020-09-23 22:11:43 +08001224 memmove(&fid->qid, &oldfid->qid, sizeof(struct p9_qid));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001225
Latchesar Ionkov62b2be52010-08-24 18:13:59 +00001226 kfree(wqids);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001227 return fid;
1228
1229clunk_fid:
Latchesar Ionkov62b2be52010-08-24 18:13:59 +00001230 kfree(wqids);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001231 p9_client_clunk(fid);
1232 fid = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001233
1234error:
Dominique Martinet6e195b02021-11-02 22:16:43 +09001235 if (fid && fid != oldfid)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001236 p9_fid_destroy(fid);
1237
1238 return ERR_PTR(err);
1239}
1240EXPORT_SYMBOL(p9_client_walk);
1241
1242int p9_client_open(struct p9_fid *fid, int mode)
1243{
1244 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001245 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001246 struct p9_req_t *req;
1247 struct p9_qid qid;
1248 int iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001249
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001250 clnt = fid->clnt;
Joe Perches5d385152011-11-28 10:40:46 -08001251 p9_debug(P9_DEBUG_9P, ">>> %s fid %d mode %d\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001252 p9_is_proto_dotl(clnt) ? "TLOPEN" : "TOPEN", fid->fid, mode);
M. Mohan Kumaref565472010-06-22 19:47:50 +05301253 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001254
1255 if (fid->mode != -1)
1256 return -EINVAL;
1257
M. Mohan Kumaref565472010-06-22 19:47:50 +05301258 if (p9_is_proto_dotl(clnt))
1259 req = p9_client_rpc(clnt, P9_TLOPEN, "dd", fid->fid, mode);
1260 else
1261 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001262 if (IS_ERR(req)) {
1263 err = PTR_ERR(req);
1264 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001265 }
1266
Dominique Martinet523adb62018-07-30 05:55:19 +00001267 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001268 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00001269 trace_9p_protocol_dump(clnt, &req->rc);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001270 goto free_and_error;
1271 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001272
Joe Perches5d385152011-11-28 10:40:46 -08001273 p9_debug(P9_DEBUG_9P, "<<< %s qid %x.%llx.%x iounit %x\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001274 p9_is_proto_dotl(clnt) ? "RLOPEN" : "ROPEN", qid.type,
1275 qid.path, qid.version, iounit);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001276
Eric Van Hensbergen154372e2020-09-23 22:11:43 +08001277 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001278 fid->mode = mode;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001279 fid->iounit = iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001280
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001281free_and_error:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001282 p9_tag_remove(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001283error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001284 return err;
1285}
1286EXPORT_SYMBOL(p9_client_open);
1287
Dominique Martinet6e195b02021-11-02 22:16:43 +09001288int p9_client_create_dotl(struct p9_fid *ofid, const char *name, u32 flags,
1289 u32 mode, kgid_t gid, struct p9_qid *qid)
Venkateswararao Jujjuri (JV)56431352010-06-17 18:27:46 -07001290{
1291 int err = 0;
1292 struct p9_client *clnt;
1293 struct p9_req_t *req;
1294 int iounit;
1295
Joe Perches5d385152011-11-28 10:40:46 -08001296 p9_debug(P9_DEBUG_9P,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001297 ">>> TLCREATE fid %d name %s flags %d mode %d gid %d\n",
1298 ofid->fid, name, flags, mode,
1299 from_kgid(&init_user_ns, gid));
Venkateswararao Jujjuri (JV)56431352010-06-17 18:27:46 -07001300 clnt = ofid->clnt;
1301
1302 if (ofid->mode != -1)
1303 return -EINVAL;
1304
Eric W. Biedermanf791f7c2013-01-29 16:09:41 -08001305 req = p9_client_rpc(clnt, P9_TLCREATE, "dsddg", ofid->fid, name, flags,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001306 mode, gid);
Venkateswararao Jujjuri (JV)56431352010-06-17 18:27:46 -07001307 if (IS_ERR(req)) {
1308 err = PTR_ERR(req);
1309 goto error;
1310 }
1311
Dominique Martinet523adb62018-07-30 05:55:19 +00001312 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", qid, &iounit);
Venkateswararao Jujjuri (JV)56431352010-06-17 18:27:46 -07001313 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00001314 trace_9p_protocol_dump(clnt, &req->rc);
Venkateswararao Jujjuri (JV)56431352010-06-17 18:27:46 -07001315 goto free_and_error;
1316 }
1317
Joe Perches5d385152011-11-28 10:40:46 -08001318 p9_debug(P9_DEBUG_9P, "<<< RLCREATE qid %x.%llx.%x iounit %x\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001319 qid->type, qid->path, qid->version, iounit);
Venkateswararao Jujjuri (JV)56431352010-06-17 18:27:46 -07001320
Eric Van Hensbergen154372e2020-09-23 22:11:43 +08001321 memmove(&ofid->qid, qid, sizeof(struct p9_qid));
Venkateswararao Jujjuri (JV)56431352010-06-17 18:27:46 -07001322 ofid->mode = mode;
1323 ofid->iounit = iounit;
1324
1325free_and_error:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001326 p9_tag_remove(clnt, req);
Venkateswararao Jujjuri (JV)56431352010-06-17 18:27:46 -07001327error:
1328 return err;
1329}
1330EXPORT_SYMBOL(p9_client_create_dotl);
1331
Al Viro7880b432017-01-12 04:01:17 -05001332int p9_client_fcreate(struct p9_fid *fid, const char *name, u32 perm, int mode,
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001333 char *extension)
1334{
1335 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001336 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001337 struct p9_req_t *req;
1338 struct p9_qid qid;
1339 int iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001340
Joe Perches5d385152011-11-28 10:40:46 -08001341 p9_debug(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001342 fid->fid, name, perm, mode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001343 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001344 clnt = fid->clnt;
1345
1346 if (fid->mode != -1)
1347 return -EINVAL;
1348
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001349 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001350 mode, extension);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001351 if (IS_ERR(req)) {
1352 err = PTR_ERR(req);
1353 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001354 }
1355
Dominique Martinet523adb62018-07-30 05:55:19 +00001356 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001357 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00001358 trace_9p_protocol_dump(clnt, &req->rc);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001359 goto free_and_error;
1360 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001361
Joe Perches5d385152011-11-28 10:40:46 -08001362 p9_debug(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001363 qid.type, qid.path, qid.version, iounit);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001364
Eric Van Hensbergen154372e2020-09-23 22:11:43 +08001365 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001366 fid->mode = mode;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001367 fid->iounit = iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001368
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001369free_and_error:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001370 p9_tag_remove(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001371error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001372 return err;
1373}
1374EXPORT_SYMBOL(p9_client_fcreate);
1375
Al Viro7880b432017-01-12 04:01:17 -05001376int p9_client_symlink(struct p9_fid *dfid, const char *name,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001377 const char *symtgt, kgid_t gid, struct p9_qid *qid)
Venkateswararao Jujjuri (JV)50cc42f2010-06-09 15:59:31 -07001378{
1379 int err = 0;
1380 struct p9_client *clnt;
1381 struct p9_req_t *req;
1382
Joe Perches5d385152011-11-28 10:40:46 -08001383 p9_debug(P9_DEBUG_9P, ">>> TSYMLINK dfid %d name %s symtgt %s\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001384 dfid->fid, name, symtgt);
Venkateswararao Jujjuri (JV)50cc42f2010-06-09 15:59:31 -07001385 clnt = dfid->clnt;
1386
Eric W. Biedermanf791f7c2013-01-29 16:09:41 -08001387 req = p9_client_rpc(clnt, P9_TSYMLINK, "dssg", dfid->fid, name, symtgt,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001388 gid);
Venkateswararao Jujjuri (JV)50cc42f2010-06-09 15:59:31 -07001389 if (IS_ERR(req)) {
1390 err = PTR_ERR(req);
1391 goto error;
1392 }
1393
Dominique Martinet523adb62018-07-30 05:55:19 +00001394 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
Venkateswararao Jujjuri (JV)50cc42f2010-06-09 15:59:31 -07001395 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00001396 trace_9p_protocol_dump(clnt, &req->rc);
Venkateswararao Jujjuri (JV)50cc42f2010-06-09 15:59:31 -07001397 goto free_and_error;
1398 }
1399
Joe Perches5d385152011-11-28 10:40:46 -08001400 p9_debug(P9_DEBUG_9P, "<<< RSYMLINK qid %x.%llx.%x\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001401 qid->type, qid->path, qid->version);
Venkateswararao Jujjuri (JV)50cc42f2010-06-09 15:59:31 -07001402
1403free_and_error:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001404 p9_tag_remove(clnt, req);
Venkateswararao Jujjuri (JV)50cc42f2010-06-09 15:59:31 -07001405error:
1406 return err;
1407}
1408EXPORT_SYMBOL(p9_client_symlink);
1409
Al Viro7880b432017-01-12 04:01:17 -05001410int p9_client_link(struct p9_fid *dfid, struct p9_fid *oldfid, const char *newname)
Venkateswararao Jujjuri (JV)652df9a2010-06-03 15:16:59 -07001411{
1412 struct p9_client *clnt;
1413 struct p9_req_t *req;
1414
Joe Perches5d385152011-11-28 10:40:46 -08001415 p9_debug(P9_DEBUG_9P, ">>> TLINK dfid %d oldfid %d newname %s\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001416 dfid->fid, oldfid->fid, newname);
Venkateswararao Jujjuri (JV)652df9a2010-06-03 15:16:59 -07001417 clnt = dfid->clnt;
1418 req = p9_client_rpc(clnt, P9_TLINK, "dds", dfid->fid, oldfid->fid,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001419 newname);
Venkateswararao Jujjuri (JV)652df9a2010-06-03 15:16:59 -07001420 if (IS_ERR(req))
1421 return PTR_ERR(req);
1422
Joe Perches5d385152011-11-28 10:40:46 -08001423 p9_debug(P9_DEBUG_9P, "<<< RLINK\n");
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001424 p9_tag_remove(clnt, req);
Venkateswararao Jujjuri (JV)652df9a2010-06-03 15:16:59 -07001425 return 0;
1426}
1427EXPORT_SYMBOL(p9_client_link);
1428
Venkateswararao Jujjuri (JV)b165d602010-10-22 10:13:12 -07001429int p9_client_fsync(struct p9_fid *fid, int datasync)
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -07001430{
1431 int err;
1432 struct p9_client *clnt;
1433 struct p9_req_t *req;
1434
Joe Perches5d385152011-11-28 10:40:46 -08001435 p9_debug(P9_DEBUG_9P, ">>> TFSYNC fid %d datasync:%d\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001436 fid->fid, datasync);
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -07001437 err = 0;
1438 clnt = fid->clnt;
1439
Venkateswararao Jujjuri (JV)b165d602010-10-22 10:13:12 -07001440 req = p9_client_rpc(clnt, P9_TFSYNC, "dd", fid->fid, datasync);
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -07001441 if (IS_ERR(req)) {
1442 err = PTR_ERR(req);
1443 goto error;
1444 }
1445
Joe Perches5d385152011-11-28 10:40:46 -08001446 p9_debug(P9_DEBUG_9P, "<<< RFSYNC fid %d\n", fid->fid);
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -07001447
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001448 p9_tag_remove(clnt, req);
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -07001449
1450error:
1451 return err;
1452}
1453EXPORT_SYMBOL(p9_client_fsync);
1454
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001455int p9_client_clunk(struct p9_fid *fid)
1456{
1457 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001458 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001459 struct p9_req_t *req;
Jim Garlick208f3c22012-02-26 14:49:57 -06001460 int retries = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001461
Jianyong Wu6636b6d2020-09-23 22:11:46 +08001462 if (!fid || IS_ERR(fid)) {
1463 pr_warn("%s (%d): Trying to clunk with invalid fid\n",
Joe Perches5d385152011-11-28 10:40:46 -08001464 __func__, task_pid_nr(current));
jvrao8e44a082010-08-25 16:27:06 +00001465 dump_stack();
1466 return 0;
1467 }
Dominique Martinetff5e72e2020-11-03 09:35:57 +01001468 if (!refcount_dec_and_test(&fid->count))
Jianyong Wu6636b6d2020-09-23 22:11:46 +08001469 return 0;
jvrao8e44a082010-08-25 16:27:06 +00001470
Jim Garlick208f3c22012-02-26 14:49:57 -06001471again:
Dominique Martinet6e195b02021-11-02 22:16:43 +09001472 p9_debug(P9_DEBUG_9P, ">>> TCLUNK fid %d (try %d)\n",
1473 fid->fid, retries);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001474 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001475 clnt = fid->clnt;
1476
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001477 req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
1478 if (IS_ERR(req)) {
1479 err = PTR_ERR(req);
1480 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001481 }
1482
Joe Perches5d385152011-11-28 10:40:46 -08001483 p9_debug(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001484
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001485 p9_tag_remove(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001486error:
Dominique Martinet6e195b02021-11-02 22:16:43 +09001487 /* Fid is not valid even after a failed clunk
Jim Garlick208f3c22012-02-26 14:49:57 -06001488 * If interrupted, retry once then give up and
1489 * leak fid until umount.
Aneesh Kumar K.V50349902011-07-11 16:40:58 +00001490 */
Jim Garlick208f3c22012-02-26 14:49:57 -06001491 if (err == -ERESTARTSYS) {
1492 if (retries++ == 0)
1493 goto again;
Dominique Martinet6e195b02021-11-02 22:16:43 +09001494 } else {
Jim Garlick208f3c22012-02-26 14:49:57 -06001495 p9_fid_destroy(fid);
Dominique Martinet6e195b02021-11-02 22:16:43 +09001496 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001497 return err;
1498}
1499EXPORT_SYMBOL(p9_client_clunk);
1500
1501int p9_client_remove(struct p9_fid *fid)
1502{
1503 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001504 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001505 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001506
Joe Perches5d385152011-11-28 10:40:46 -08001507 p9_debug(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001508 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001509 clnt = fid->clnt;
1510
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001511 req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
1512 if (IS_ERR(req)) {
1513 err = PTR_ERR(req);
1514 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001515 }
1516
Joe Perches5d385152011-11-28 10:40:46 -08001517 p9_debug(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001518
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001519 p9_tag_remove(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001520error:
Jim Garlick208f3c22012-02-26 14:49:57 -06001521 if (err == -ERESTARTSYS)
1522 p9_client_clunk(fid);
1523 else
1524 p9_fid_destroy(fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001525 return err;
1526}
1527EXPORT_SYMBOL(p9_client_remove);
1528
Aneesh Kumar K.V48e370f2011-06-28 15:41:18 +05301529int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags)
1530{
1531 int err = 0;
1532 struct p9_req_t *req;
1533 struct p9_client *clnt;
1534
Joe Perches5d385152011-11-28 10:40:46 -08001535 p9_debug(P9_DEBUG_9P, ">>> TUNLINKAT fid %d %s %d\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001536 dfid->fid, name, flags);
Aneesh Kumar K.V48e370f2011-06-28 15:41:18 +05301537
1538 clnt = dfid->clnt;
1539 req = p9_client_rpc(clnt, P9_TUNLINKAT, "dsd", dfid->fid, name, flags);
1540 if (IS_ERR(req)) {
1541 err = PTR_ERR(req);
1542 goto error;
1543 }
Joe Perches5d385152011-11-28 10:40:46 -08001544 p9_debug(P9_DEBUG_9P, "<<< RUNLINKAT fid %d %s\n", dfid->fid, name);
Aneesh Kumar K.V48e370f2011-06-28 15:41:18 +05301545
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001546 p9_tag_remove(clnt, req);
Aneesh Kumar K.V48e370f2011-06-28 15:41:18 +05301547error:
1548 return err;
1549}
1550EXPORT_SYMBOL(p9_client_unlinkat);
1551
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001552int
Al Viroe1200fe62015-04-01 23:42:28 -04001553p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001554{
Al Viroe1200fe62015-04-01 23:42:28 -04001555 int total = 0;
Vincent Bernat999b8b82015-08-15 15:49:13 +02001556 *err = 0;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +05301557
Al Viroe1200fe62015-04-01 23:42:28 -04001558 while (iov_iter_count(to)) {
Sergey Alirzaev388f6962020-02-05 23:40:53 +03001559 int count;
Stephen Hemmingerc69f297d2018-07-24 12:29:10 -07001560
Sergey Alirzaev388f6962020-02-05 23:40:53 +03001561 count = p9_client_read_once(fid, offset, to, err);
1562 if (!count || *err)
Al Viroe1200fe62015-04-01 23:42:28 -04001563 break;
Sergey Alirzaev388f6962020-02-05 23:40:53 +03001564 offset += count;
1565 total += count;
Venkateswararao Jujjuri (JV)bb2f8a52011-01-28 17:05:59 -08001566 }
Al Viroe1200fe62015-04-01 23:42:28 -04001567 return total;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001568}
1569EXPORT_SYMBOL(p9_client_read);
1570
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001571int
Sergey Alirzaev388f6962020-02-05 23:40:53 +03001572p9_client_read_once(struct p9_fid *fid, u64 offset, struct iov_iter *to,
1573 int *err)
1574{
1575 struct p9_client *clnt = fid->clnt;
1576 struct p9_req_t *req;
1577 int count = iov_iter_count(to);
1578 int rsize, non_zc = 0;
1579 char *dataptr;
1580
1581 *err = 0;
Dominique Martinet6e195b02021-11-02 22:16:43 +09001582 p9_debug(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %zu\n",
1583 fid->fid, offset, iov_iter_count(to));
Sergey Alirzaev388f6962020-02-05 23:40:53 +03001584
1585 rsize = fid->iounit;
1586 if (!rsize || rsize > clnt->msize - P9_IOHDRSZ)
1587 rsize = clnt->msize - P9_IOHDRSZ;
1588
1589 if (count < rsize)
1590 rsize = count;
1591
1592 /* Don't bother zerocopy for small IO (< 1024) */
1593 if (clnt->trans_mod->zc_request && rsize > 1024) {
1594 /* response header len is 11
1595 * PDU Header(7) + IO Size (4)
1596 */
1597 req = p9_client_zc_rpc(clnt, P9_TREAD, to, NULL, rsize,
1598 0, 11, "dqd", fid->fid,
1599 offset, rsize);
1600 } else {
1601 non_zc = 1;
1602 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset,
1603 rsize);
1604 }
1605 if (IS_ERR(req)) {
1606 *err = PTR_ERR(req);
1607 return 0;
1608 }
1609
1610 *err = p9pdu_readf(&req->rc, clnt->proto_version,
1611 "D", &count, &dataptr);
1612 if (*err) {
1613 trace_9p_protocol_dump(clnt, &req->rc);
1614 p9_tag_remove(clnt, req);
1615 return 0;
1616 }
1617 if (rsize < count) {
1618 pr_err("bogus RREAD count (%d > %d)\n", count, rsize);
1619 count = rsize;
1620 }
1621
1622 p9_debug(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
Sergey Alirzaev388f6962020-02-05 23:40:53 +03001623
1624 if (non_zc) {
1625 int n = copy_to_iter(dataptr, count, to);
1626
1627 if (n != count) {
1628 *err = -EFAULT;
1629 p9_tag_remove(clnt, req);
1630 return n;
1631 }
1632 } else {
1633 iov_iter_advance(to, count);
1634 }
1635 p9_tag_remove(clnt, req);
1636 return count;
1637}
1638EXPORT_SYMBOL(p9_client_read_once);
1639
1640int
Al Viro070b3652015-04-01 20:17:51 -04001641p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001642{
Al Viro070b3652015-04-01 20:17:51 -04001643 struct p9_client *clnt = fid->clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001644 struct p9_req_t *req;
Al Viro070b3652015-04-01 20:17:51 -04001645 int total = 0;
Vincent Bernat999b8b82015-08-15 15:49:13 +02001646 *err = 0;
Al Viro4f3b35c2015-04-01 19:57:53 -04001647
Al Viro070b3652015-04-01 20:17:51 -04001648 p9_debug(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %zd\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001649 fid->fid, offset, iov_iter_count(from));
Al Viro070b3652015-04-01 20:17:51 -04001650
1651 while (iov_iter_count(from)) {
1652 int count = iov_iter_count(from);
1653 int rsize = fid->iounit;
Dominique Martinet6e195b02021-11-02 22:16:43 +09001654
1655 if (!rsize || rsize > clnt->msize - P9_IOHDRSZ)
Al Viro070b3652015-04-01 20:17:51 -04001656 rsize = clnt->msize - P9_IOHDRSZ;
1657
1658 if (count < rsize)
1659 rsize = count;
1660
1661 /* Don't bother zerocopy for small IO (< 1024) */
1662 if (clnt->trans_mod->zc_request && rsize > 1024) {
1663 req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, from, 0,
1664 rsize, P9_ZC_HDR_SZ, "dqd",
1665 fid->fid, offset, rsize);
1666 } else {
1667 req = p9_client_rpc(clnt, P9_TWRITE, "dqV", fid->fid,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001668 offset, rsize, from);
Al Viro070b3652015-04-01 20:17:51 -04001669 }
1670 if (IS_ERR(req)) {
1671 *err = PTR_ERR(req);
1672 break;
1673 }
1674
Dominique Martinet523adb62018-07-30 05:55:19 +00001675 *err = p9pdu_readf(&req->rc, clnt->proto_version, "d", &count);
Al Viro070b3652015-04-01 20:17:51 -04001676 if (*err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00001677 trace_9p_protocol_dump(clnt, &req->rc);
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001678 p9_tag_remove(clnt, req);
Al Viro67e808f2015-07-04 16:11:05 -04001679 break;
Al Viro070b3652015-04-01 20:17:51 -04001680 }
Al Viro0f1db7d2015-07-04 16:17:39 -04001681 if (rsize < count) {
1682 pr_err("bogus RWRITE count (%d > %d)\n", count, rsize);
1683 count = rsize;
1684 }
Al Viro070b3652015-04-01 20:17:51 -04001685
1686 p9_debug(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
1687
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001688 p9_tag_remove(clnt, req);
Al Viro070b3652015-04-01 20:17:51 -04001689 iov_iter_advance(from, count);
1690 total += count;
1691 offset += count;
Al Viro4f3b35c2015-04-01 19:57:53 -04001692 }
Al Viro070b3652015-04-01 20:17:51 -04001693 return total;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001694}
1695EXPORT_SYMBOL(p9_client_write);
1696
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001697struct p9_wstat *p9_client_stat(struct p9_fid *fid)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001698{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001699 int err;
1700 struct p9_client *clnt;
Dominique Martinet6e195b02021-11-02 22:16:43 +09001701 struct p9_wstat *ret;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001702 struct p9_req_t *req;
1703 u16 ignored;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001704
Joe Perches5d385152011-11-28 10:40:46 -08001705 p9_debug(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001706
Dominique Martinet6e195b02021-11-02 22:16:43 +09001707 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001708 if (!ret)
1709 return ERR_PTR(-ENOMEM);
1710
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001711 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001712 clnt = fid->clnt;
1713
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001714 req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
1715 if (IS_ERR(req)) {
1716 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001717 goto error;
1718 }
1719
Dominique Martinet523adb62018-07-30 05:55:19 +00001720 err = p9pdu_readf(&req->rc, clnt->proto_version, "wS", &ignored, ret);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001721 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00001722 trace_9p_protocol_dump(clnt, &req->rc);
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001723 p9_tag_remove(clnt, req);
Abhishek Kulkarnieedfe1c2009-07-14 13:25:41 -05001724 goto error;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001725 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001726
Joe Perches5d385152011-11-28 10:40:46 -08001727 p9_debug(P9_DEBUG_9P,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001728 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1729 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1730 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1731 "<<< uid=%d gid=%d n_muid=%d\n",
1732 ret->size, ret->type, ret->dev, ret->qid.type, ret->qid.path,
1733 ret->qid.version, ret->mode,
1734 ret->atime, ret->mtime, ret->length,
1735 ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
1736 from_kuid(&init_user_ns, ret->n_uid),
1737 from_kgid(&init_user_ns, ret->n_gid),
1738 from_kuid(&init_user_ns, ret->n_muid));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001739
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001740 p9_tag_remove(clnt, req);
Latchesar Ionkov742b11a2009-04-05 16:26:41 -05001741 return ret;
1742
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001743error:
Latchesar Ionkov742b11a2009-04-05 16:26:41 -05001744 kfree(ret);
1745 return ERR_PTR(err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001746}
1747EXPORT_SYMBOL(p9_client_stat);
1748
Sripathi Kodif0853122010-07-12 20:07:23 +05301749struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001750 u64 request_mask)
Sripathi Kodif0853122010-07-12 20:07:23 +05301751{
1752 int err;
1753 struct p9_client *clnt;
Dominique Martinet6e195b02021-11-02 22:16:43 +09001754 struct p9_stat_dotl *ret;
Sripathi Kodif0853122010-07-12 20:07:23 +05301755 struct p9_req_t *req;
1756
Joe Perches5d385152011-11-28 10:40:46 -08001757 p9_debug(P9_DEBUG_9P, ">>> TGETATTR fid %d, request_mask %lld\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001758 fid->fid, request_mask);
Sripathi Kodif0853122010-07-12 20:07:23 +05301759
Dominique Martinet6e195b02021-11-02 22:16:43 +09001760 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
Sripathi Kodif0853122010-07-12 20:07:23 +05301761 if (!ret)
1762 return ERR_PTR(-ENOMEM);
1763
1764 err = 0;
1765 clnt = fid->clnt;
1766
1767 req = p9_client_rpc(clnt, P9_TGETATTR, "dq", fid->fid, request_mask);
1768 if (IS_ERR(req)) {
1769 err = PTR_ERR(req);
1770 goto error;
1771 }
1772
Dominique Martinet523adb62018-07-30 05:55:19 +00001773 err = p9pdu_readf(&req->rc, clnt->proto_version, "A", ret);
Sripathi Kodif0853122010-07-12 20:07:23 +05301774 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00001775 trace_9p_protocol_dump(clnt, &req->rc);
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001776 p9_tag_remove(clnt, req);
Sripathi Kodif0853122010-07-12 20:07:23 +05301777 goto error;
1778 }
1779
Dominique Martinet6e195b02021-11-02 22:16:43 +09001780 p9_debug(P9_DEBUG_9P, "<<< RGETATTR st_result_mask=%lld\n"
1781 "<<< qid=%x.%llx.%x\n"
1782 "<<< st_mode=%8.8x st_nlink=%llu\n"
1783 "<<< st_uid=%d st_gid=%d\n"
1784 "<<< st_rdev=%llx st_size=%llx st_blksize=%llu st_blocks=%llu\n"
1785 "<<< st_atime_sec=%lld st_atime_nsec=%lld\n"
1786 "<<< st_mtime_sec=%lld st_mtime_nsec=%lld\n"
1787 "<<< st_ctime_sec=%lld st_ctime_nsec=%lld\n"
1788 "<<< st_btime_sec=%lld st_btime_nsec=%lld\n"
1789 "<<< st_gen=%lld st_data_version=%lld\n",
1790 ret->st_result_mask,
1791 ret->qid.type, ret->qid.path, ret->qid.version,
1792 ret->st_mode, ret->st_nlink,
1793 from_kuid(&init_user_ns, ret->st_uid),
1794 from_kgid(&init_user_ns, ret->st_gid),
1795 ret->st_rdev, ret->st_size, ret->st_blksize, ret->st_blocks,
1796 ret->st_atime_sec, ret->st_atime_nsec,
1797 ret->st_mtime_sec, ret->st_mtime_nsec,
1798 ret->st_ctime_sec, ret->st_ctime_nsec,
1799 ret->st_btime_sec, ret->st_btime_nsec,
1800 ret->st_gen, ret->st_data_version);
Sripathi Kodif0853122010-07-12 20:07:23 +05301801
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001802 p9_tag_remove(clnt, req);
Sripathi Kodif0853122010-07-12 20:07:23 +05301803 return ret;
1804
1805error:
1806 kfree(ret);
1807 return ERR_PTR(err);
1808}
1809EXPORT_SYMBOL(p9_client_getattr_dotl);
1810
Sripathi Kodi342fee12010-03-05 18:50:14 +00001811static int p9_client_statsize(struct p9_wstat *wst, int proto_version)
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001812{
1813 int ret;
1814
Eric Van Hensbergen9d6939d2010-01-15 19:01:56 -06001815 /* NOTE: size shouldn't include its own length */
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001816 /* size[2] type[2] dev[4] qid[13] */
1817 /* mode[4] atime[4] mtime[4] length[8]*/
1818 /* name[s] uid[s] gid[s] muid[s] */
Dominique Martinet6e195b02021-11-02 22:16:43 +09001819 ret = 2 + 4 + 13 + 4 + 4 + 4 + 8 + 2 + 2 + 2 + 2;
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001820
1821 if (wst->name)
1822 ret += strlen(wst->name);
1823 if (wst->uid)
1824 ret += strlen(wst->uid);
1825 if (wst->gid)
1826 ret += strlen(wst->gid);
1827 if (wst->muid)
1828 ret += strlen(wst->muid);
1829
Dominique Martinet6e195b02021-11-02 22:16:43 +09001830 if (proto_version == p9_proto_2000u ||
1831 proto_version == p9_proto_2000L) {
1832 /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1833 ret += 2 + 4 + 4 + 4;
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001834 if (wst->extension)
1835 ret += strlen(wst->extension);
1836 }
1837
1838 return ret;
1839}
1840
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001841int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1842{
1843 int err;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001844 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001845 struct p9_client *clnt;
1846
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001847 err = 0;
1848 clnt = fid->clnt;
Sripathi Kodi342fee12010-03-05 18:50:14 +00001849 wst->size = p9_client_statsize(wst, clnt->proto_version);
Dominique Martinet6e195b02021-11-02 22:16:43 +09001850 p9_debug(P9_DEBUG_9P, ">>> TWSTAT fid %d\n",
1851 fid->fid);
Joe Perches5d385152011-11-28 10:40:46 -08001852 p9_debug(P9_DEBUG_9P,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001853 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1854 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1855 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1856 " uid=%d gid=%d n_muid=%d\n",
1857 wst->size, wst->type, wst->dev, wst->qid.type,
1858 wst->qid.path, wst->qid.version,
1859 wst->mode, wst->atime, wst->mtime, wst->length,
1860 wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
1861 from_kuid(&init_user_ns, wst->n_uid),
1862 from_kgid(&init_user_ns, wst->n_gid),
1863 from_kuid(&init_user_ns, wst->n_muid));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001864
Dominique Martinet6e195b02021-11-02 22:16:43 +09001865 req = p9_client_rpc(clnt, P9_TWSTAT, "dwS",
1866 fid->fid, wst->size + 2, wst);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001867 if (IS_ERR(req)) {
1868 err = PTR_ERR(req);
1869 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001870 }
1871
Joe Perches5d385152011-11-28 10:40:46 -08001872 p9_debug(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001873
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001874 p9_tag_remove(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001875error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001876 return err;
1877}
1878EXPORT_SYMBOL(p9_client_wstat);
Sripathi Kodibda8e772010-03-25 12:45:30 +00001879
Sripathi Kodi87d78452010-06-18 11:50:10 +05301880int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *p9attr)
1881{
1882 int err;
1883 struct p9_req_t *req;
1884 struct p9_client *clnt;
1885
1886 err = 0;
1887 clnt = fid->clnt;
Joe Perches5d385152011-11-28 10:40:46 -08001888 p9_debug(P9_DEBUG_9P, ">>> TSETATTR fid %d\n", fid->fid);
Dominique Martinet6e195b02021-11-02 22:16:43 +09001889 p9_debug(P9_DEBUG_9P, " valid=%x mode=%x uid=%d gid=%d size=%lld\n",
1890 p9attr->valid, p9attr->mode,
1891 from_kuid(&init_user_ns, p9attr->uid),
1892 from_kgid(&init_user_ns, p9attr->gid),
1893 p9attr->size);
1894 p9_debug(P9_DEBUG_9P, " atime_sec=%lld atime_nsec=%lld\n",
1895 p9attr->atime_sec, p9attr->atime_nsec);
1896 p9_debug(P9_DEBUG_9P, " mtime_sec=%lld mtime_nsec=%lld\n",
1897 p9attr->mtime_sec, p9attr->mtime_nsec);
Sripathi Kodi87d78452010-06-18 11:50:10 +05301898
1899 req = p9_client_rpc(clnt, P9_TSETATTR, "dI", fid->fid, p9attr);
1900
1901 if (IS_ERR(req)) {
1902 err = PTR_ERR(req);
1903 goto error;
1904 }
Joe Perches5d385152011-11-28 10:40:46 -08001905 p9_debug(P9_DEBUG_9P, "<<< RSETATTR fid %d\n", fid->fid);
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001906 p9_tag_remove(clnt, req);
Sripathi Kodi87d78452010-06-18 11:50:10 +05301907error:
1908 return err;
1909}
1910EXPORT_SYMBOL(p9_client_setattr);
1911
Sripathi Kodibda8e772010-03-25 12:45:30 +00001912int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb)
1913{
1914 int err;
1915 struct p9_req_t *req;
1916 struct p9_client *clnt;
1917
1918 err = 0;
1919 clnt = fid->clnt;
1920
Joe Perches5d385152011-11-28 10:40:46 -08001921 p9_debug(P9_DEBUG_9P, ">>> TSTATFS fid %d\n", fid->fid);
Sripathi Kodibda8e772010-03-25 12:45:30 +00001922
1923 req = p9_client_rpc(clnt, P9_TSTATFS, "d", fid->fid);
1924 if (IS_ERR(req)) {
1925 err = PTR_ERR(req);
1926 goto error;
1927 }
1928
Dominique Martinet523adb62018-07-30 05:55:19 +00001929 err = p9pdu_readf(&req->rc, clnt->proto_version, "ddqqqqqqd", &sb->type,
1930 &sb->bsize, &sb->blocks, &sb->bfree, &sb->bavail,
1931 &sb->files, &sb->ffree, &sb->fsid, &sb->namelen);
Sripathi Kodibda8e772010-03-25 12:45:30 +00001932 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00001933 trace_9p_protocol_dump(clnt, &req->rc);
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001934 p9_tag_remove(clnt, req);
Sripathi Kodibda8e772010-03-25 12:45:30 +00001935 goto error;
1936 }
1937
Dominique Martinet6e195b02021-11-02 22:16:43 +09001938 p9_debug(P9_DEBUG_9P,
1939 "<<< RSTATFS fid %d type 0x%x bsize %u blocks %llu bfree %llu bavail %llu files %llu ffree %llu fsid %llu namelen %u\n",
1940 fid->fid, sb->type, sb->bsize, sb->blocks, sb->bfree,
1941 sb->bavail, sb->files, sb->ffree, sb->fsid, sb->namelen);
Sripathi Kodibda8e772010-03-25 12:45:30 +00001942
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001943 p9_tag_remove(clnt, req);
Sripathi Kodibda8e772010-03-25 12:45:30 +00001944error:
1945 return err;
1946}
1947EXPORT_SYMBOL(p9_client_statfs);
Sripathi Kodi4681dbd2010-03-25 12:47:26 +00001948
Aneesh Kumar K.V9e8fb382011-06-28 15:41:16 +05301949int p9_client_rename(struct p9_fid *fid,
1950 struct p9_fid *newdirfid, const char *name)
Sripathi Kodi4681dbd2010-03-25 12:47:26 +00001951{
1952 int err;
1953 struct p9_req_t *req;
1954 struct p9_client *clnt;
1955
1956 err = 0;
1957 clnt = fid->clnt;
1958
Joe Perches5d385152011-11-28 10:40:46 -08001959 p9_debug(P9_DEBUG_9P, ">>> TRENAME fid %d newdirfid %d name %s\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001960 fid->fid, newdirfid->fid, name);
Sripathi Kodi4681dbd2010-03-25 12:47:26 +00001961
1962 req = p9_client_rpc(clnt, P9_TRENAME, "dds", fid->fid,
Dominique Martinet6e195b02021-11-02 22:16:43 +09001963 newdirfid->fid, name);
Sripathi Kodi4681dbd2010-03-25 12:47:26 +00001964 if (IS_ERR(req)) {
1965 err = PTR_ERR(req);
1966 goto error;
1967 }
1968
Joe Perches5d385152011-11-28 10:40:46 -08001969 p9_debug(P9_DEBUG_9P, "<<< RRENAME fid %d\n", fid->fid);
Sripathi Kodi4681dbd2010-03-25 12:47:26 +00001970
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02001971 p9_tag_remove(clnt, req);
Sripathi Kodi4681dbd2010-03-25 12:47:26 +00001972error:
1973 return err;
1974}
1975EXPORT_SYMBOL(p9_client_rename);
1976
Aneesh Kumar K.V9e8fb382011-06-28 15:41:16 +05301977int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name,
1978 struct p9_fid *newdirfid, const char *new_name)
1979{
1980 int err;
1981 struct p9_req_t *req;
1982 struct p9_client *clnt;
1983
1984 err = 0;
1985 clnt = olddirfid->clnt;
1986
Dominique Martinet6e195b02021-11-02 22:16:43 +09001987 p9_debug(P9_DEBUG_9P,
1988 ">>> TRENAMEAT olddirfid %d old name %s newdirfid %d new name %s\n",
1989 olddirfid->fid, old_name, newdirfid->fid, new_name);
Aneesh Kumar K.V9e8fb382011-06-28 15:41:16 +05301990
1991 req = p9_client_rpc(clnt, P9_TRENAMEAT, "dsds", olddirfid->fid,
1992 old_name, newdirfid->fid, new_name);
1993 if (IS_ERR(req)) {
1994 err = PTR_ERR(req);
1995 goto error;
1996 }
1997
Joe Perches5d385152011-11-28 10:40:46 -08001998 p9_debug(P9_DEBUG_9P, "<<< RRENAMEAT newdirfid %d new name %s\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09001999 newdirfid->fid, new_name);
Aneesh Kumar K.V9e8fb382011-06-28 15:41:16 +05302000
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02002001 p9_tag_remove(clnt, req);
Aneesh Kumar K.V9e8fb382011-06-28 15:41:16 +05302002error:
2003 return err;
2004}
2005EXPORT_SYMBOL(p9_client_renameat);
2006
Dominique Martinet6e195b02021-11-02 22:16:43 +09002007/* An xattrwalk without @attr_name gives the fid for the lisxattr namespace
Aneesh Kumar K.V0ef63f32010-05-31 13:22:45 +05302008 */
2009struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid,
Dominique Martinet6e195b02021-11-02 22:16:43 +09002010 const char *attr_name, u64 *attr_size)
Aneesh Kumar K.V0ef63f32010-05-31 13:22:45 +05302011{
2012 int err;
2013 struct p9_req_t *req;
2014 struct p9_client *clnt;
2015 struct p9_fid *attr_fid;
2016
2017 err = 0;
2018 clnt = file_fid->clnt;
2019 attr_fid = p9_fid_create(clnt);
Matthew Wilcoxb5303be2018-07-11 14:02:21 -07002020 if (!attr_fid) {
2021 err = -ENOMEM;
Aneesh Kumar K.V0ef63f32010-05-31 13:22:45 +05302022 goto error;
2023 }
Joe Perches5d385152011-11-28 10:40:46 -08002024 p9_debug(P9_DEBUG_9P,
Dominique Martinet6e195b02021-11-02 22:16:43 +09002025 ">>> TXATTRWALK file_fid %d, attr_fid %d name %s\n",
2026 file_fid->fid, attr_fid->fid, attr_name);
Aneesh Kumar K.V0ef63f32010-05-31 13:22:45 +05302027
2028 req = p9_client_rpc(clnt, P9_TXATTRWALK, "dds",
Dominique Martinet6e195b02021-11-02 22:16:43 +09002029 file_fid->fid, attr_fid->fid, attr_name);
Aneesh Kumar K.V0ef63f32010-05-31 13:22:45 +05302030 if (IS_ERR(req)) {
2031 err = PTR_ERR(req);
2032 goto error;
2033 }
Dominique Martinet523adb62018-07-30 05:55:19 +00002034 err = p9pdu_readf(&req->rc, clnt->proto_version, "q", attr_size);
Aneesh Kumar K.V0ef63f32010-05-31 13:22:45 +05302035 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00002036 trace_9p_protocol_dump(clnt, &req->rc);
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02002037 p9_tag_remove(clnt, req);
Aneesh Kumar K.V0ef63f32010-05-31 13:22:45 +05302038 goto clunk_fid;
2039 }
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02002040 p9_tag_remove(clnt, req);
Joe Perches5d385152011-11-28 10:40:46 -08002041 p9_debug(P9_DEBUG_9P, "<<< RXATTRWALK fid %d size %llu\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09002042 attr_fid->fid, *attr_size);
Aneesh Kumar K.V0ef63f32010-05-31 13:22:45 +05302043 return attr_fid;
2044clunk_fid:
2045 p9_client_clunk(attr_fid);
2046 attr_fid = NULL;
2047error:
Dominique Martinet6e195b02021-11-02 22:16:43 +09002048 if (attr_fid && attr_fid != file_fid)
Aneesh Kumar K.V0ef63f32010-05-31 13:22:45 +05302049 p9_fid_destroy(attr_fid);
2050
2051 return ERR_PTR(err);
2052}
2053EXPORT_SYMBOL_GPL(p9_client_xattrwalk);
2054
Aneesh Kumar K.Veda25e462010-05-31 13:22:50 +05302055int p9_client_xattrcreate(struct p9_fid *fid, const char *name,
Dominique Martinet6e195b02021-11-02 22:16:43 +09002056 u64 attr_size, int flags)
Aneesh Kumar K.Veda25e462010-05-31 13:22:50 +05302057{
2058 int err;
2059 struct p9_req_t *req;
2060 struct p9_client *clnt;
2061
Joe Perches5d385152011-11-28 10:40:46 -08002062 p9_debug(P9_DEBUG_9P,
Dominique Martinet6e195b02021-11-02 22:16:43 +09002063 ">>> TXATTRCREATE fid %d name %s size %llu flag %d\n",
2064 fid->fid, name, attr_size, flags);
Aneesh Kumar K.Veda25e462010-05-31 13:22:50 +05302065 err = 0;
2066 clnt = fid->clnt;
2067 req = p9_client_rpc(clnt, P9_TXATTRCREATE, "dsqd",
Dominique Martinet6e195b02021-11-02 22:16:43 +09002068 fid->fid, name, attr_size, flags);
Aneesh Kumar K.Veda25e462010-05-31 13:22:50 +05302069 if (IS_ERR(req)) {
2070 err = PTR_ERR(req);
2071 goto error;
2072 }
Joe Perches5d385152011-11-28 10:40:46 -08002073 p9_debug(P9_DEBUG_9P, "<<< RXATTRCREATE fid %d\n", fid->fid);
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02002074 p9_tag_remove(clnt, req);
Aneesh Kumar K.Veda25e462010-05-31 13:22:50 +05302075error:
2076 return err;
2077}
2078EXPORT_SYMBOL_GPL(p9_client_xattrcreate);
2079
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002080int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset)
2081{
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +05302082 int err, rsize, non_zc = 0;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002083 struct p9_client *clnt;
2084 struct p9_req_t *req;
2085 char *dataptr;
Al Viro4f3b35c2015-04-01 19:57:53 -04002086 struct kvec kv = {.iov_base = data, .iov_len = count};
2087 struct iov_iter to;
2088
David Howellsaa563d72018-10-20 00:57:56 +01002089 iov_iter_kvec(&to, READ, &kv, 1, count);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002090
Joe Perches5d385152011-11-28 10:40:46 -08002091 p9_debug(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n",
Dominique Martinet6e195b02021-11-02 22:16:43 +09002092 fid->fid, offset, count);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002093
2094 err = 0;
2095 clnt = fid->clnt;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002096
2097 rsize = fid->iounit;
Dominique Martinet6e195b02021-11-02 22:16:43 +09002098 if (!rsize || rsize > clnt->msize - P9_READDIRHDRSZ)
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002099 rsize = clnt->msize - P9_READDIRHDRSZ;
2100
2101 if (count < rsize)
2102 rsize = count;
2103
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +05302104 /* Don't bother zerocopy for small IO (< 1024) */
2105 if (clnt->trans_mod->zc_request && rsize > 1024) {
Dominique Martinet6e195b02021-11-02 22:16:43 +09002106 /* response header len is 11
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +05302107 * PDU Header(7) + IO Size (4)
2108 */
Al Viro4f3b35c2015-04-01 19:57:53 -04002109 req = p9_client_zc_rpc(clnt, P9_TREADDIR, &to, NULL, rsize, 0,
2110 11, "dqd", fid->fid, offset, rsize);
Venkateswararao Jujjuri (JV)2c665232011-02-16 18:43:20 -08002111 } else {
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +05302112 non_zc = 1;
Venkateswararao Jujjuri (JV)2c665232011-02-16 18:43:20 -08002113 req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid,
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +05302114 offset, rsize);
Venkateswararao Jujjuri (JV)2c665232011-02-16 18:43:20 -08002115 }
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002116 if (IS_ERR(req)) {
2117 err = PTR_ERR(req);
2118 goto error;
2119 }
2120
Dominique Martinet523adb62018-07-30 05:55:19 +00002121 err = p9pdu_readf(&req->rc, clnt->proto_version, "D", &count, &dataptr);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002122 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00002123 trace_9p_protocol_dump(clnt, &req->rc);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002124 goto free_and_error;
2125 }
Al Viro71d6ad02017-04-14 17:22:18 -04002126 if (rsize < count) {
2127 pr_err("bogus RREADDIR count (%d > %d)\n", count, rsize);
2128 count = rsize;
2129 }
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002130
Joe Perches5d385152011-11-28 10:40:46 -08002131 p9_debug(P9_DEBUG_9P, "<<< RREADDIR count %d\n", count);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002132
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +05302133 if (non_zc)
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002134 memmove(data, dataptr, count);
2135
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02002136 p9_tag_remove(clnt, req);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002137 return count;
2138
2139free_and_error:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02002140 p9_tag_remove(clnt, req);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +00002141error:
2142 return err;
2143}
2144EXPORT_SYMBOL(p9_client_readdir);
M. Mohan Kumar4b435162010-06-16 14:27:01 +05302145
Al Viro7880b432017-01-12 04:01:17 -05002146int p9_client_mknod_dotl(struct p9_fid *fid, const char *name, int mode,
Dominique Martinet6e195b02021-11-02 22:16:43 +09002147 dev_t rdev, kgid_t gid, struct p9_qid *qid)
M. Mohan Kumar4b435162010-06-16 14:27:01 +05302148{
2149 int err;
2150 struct p9_client *clnt;
2151 struct p9_req_t *req;
2152
2153 err = 0;
2154 clnt = fid->clnt;
Dominique Martinet6e195b02021-11-02 22:16:43 +09002155 p9_debug(P9_DEBUG_9P,
2156 ">>> TMKNOD fid %d name %s mode %d major %d minor %d\n",
2157 fid->fid, name, mode, MAJOR(rdev), MINOR(rdev));
Eric W. Biedermanf791f7c2013-01-29 16:09:41 -08002158 req = p9_client_rpc(clnt, P9_TMKNOD, "dsdddg", fid->fid, name, mode,
Dominique Martinet6e195b02021-11-02 22:16:43 +09002159 MAJOR(rdev), MINOR(rdev), gid);
M. Mohan Kumar4b435162010-06-16 14:27:01 +05302160 if (IS_ERR(req))
2161 return PTR_ERR(req);
2162
Dominique Martinet523adb62018-07-30 05:55:19 +00002163 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
M. Mohan Kumar4b435162010-06-16 14:27:01 +05302164 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00002165 trace_9p_protocol_dump(clnt, &req->rc);
M. Mohan Kumar4b435162010-06-16 14:27:01 +05302166 goto error;
2167 }
Dominique Martinet6e195b02021-11-02 22:16:43 +09002168 p9_debug(P9_DEBUG_9P, "<<< RMKNOD qid %x.%llx.%x\n",
2169 qid->type, qid->path, qid->version);
M. Mohan Kumar4b435162010-06-16 14:27:01 +05302170
2171error:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02002172 p9_tag_remove(clnt, req);
M. Mohan Kumar4b435162010-06-16 14:27:01 +05302173 return err;
M. Mohan Kumar4b435162010-06-16 14:27:01 +05302174}
2175EXPORT_SYMBOL(p9_client_mknod_dotl);
M. Mohan Kumar01a622b2010-06-16 14:27:22 +05302176
Al Viro7880b432017-01-12 04:01:17 -05002177int p9_client_mkdir_dotl(struct p9_fid *fid, const char *name, int mode,
Dominique Martinet6e195b02021-11-02 22:16:43 +09002178 kgid_t gid, struct p9_qid *qid)
M. Mohan Kumar01a622b2010-06-16 14:27:22 +05302179{
2180 int err;
2181 struct p9_client *clnt;
2182 struct p9_req_t *req;
2183
2184 err = 0;
2185 clnt = fid->clnt;
Joe Perches5d385152011-11-28 10:40:46 -08002186 p9_debug(P9_DEBUG_9P, ">>> TMKDIR fid %d name %s mode %d gid %d\n",
Eric W. Biedermanf791f7c2013-01-29 16:09:41 -08002187 fid->fid, name, mode, from_kgid(&init_user_ns, gid));
Dominique Martinet6e195b02021-11-02 22:16:43 +09002188 req = p9_client_rpc(clnt, P9_TMKDIR, "dsdg",
2189 fid->fid, name, mode, gid);
M. Mohan Kumar01a622b2010-06-16 14:27:22 +05302190 if (IS_ERR(req))
2191 return PTR_ERR(req);
2192
Dominique Martinet523adb62018-07-30 05:55:19 +00002193 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
M. Mohan Kumar01a622b2010-06-16 14:27:22 +05302194 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00002195 trace_9p_protocol_dump(clnt, &req->rc);
M. Mohan Kumar01a622b2010-06-16 14:27:22 +05302196 goto error;
2197 }
Joe Perches5d385152011-11-28 10:40:46 -08002198 p9_debug(P9_DEBUG_9P, "<<< RMKDIR qid %x.%llx.%x\n", qid->type,
Dominique Martinet6e195b02021-11-02 22:16:43 +09002199 qid->path, qid->version);
M. Mohan Kumar01a622b2010-06-16 14:27:22 +05302200
2201error:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02002202 p9_tag_remove(clnt, req);
M. Mohan Kumar01a622b2010-06-16 14:27:22 +05302203 return err;
M. Mohan Kumar01a622b2010-06-16 14:27:22 +05302204}
2205EXPORT_SYMBOL(p9_client_mkdir_dotl);
M. Mohan Kumara0990272010-09-27 11:34:24 +05302206
2207int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status)
2208{
2209 int err;
2210 struct p9_client *clnt;
2211 struct p9_req_t *req;
2212
2213 err = 0;
2214 clnt = fid->clnt;
Dominique Martinet6e195b02021-11-02 22:16:43 +09002215 p9_debug(P9_DEBUG_9P,
2216 ">>> TLOCK fid %d type %i flags %d start %lld length %lld proc_id %d client_id %s\n",
2217 fid->fid, flock->type, flock->flags, flock->start,
2218 flock->length, flock->proc_id, flock->client_id);
M. Mohan Kumara0990272010-09-27 11:34:24 +05302219
2220 req = p9_client_rpc(clnt, P9_TLOCK, "dbdqqds", fid->fid, flock->type,
Dominique Martinet6e195b02021-11-02 22:16:43 +09002221 flock->flags, flock->start, flock->length,
2222 flock->proc_id, flock->client_id);
M. Mohan Kumara0990272010-09-27 11:34:24 +05302223
2224 if (IS_ERR(req))
2225 return PTR_ERR(req);
2226
Dominique Martinet523adb62018-07-30 05:55:19 +00002227 err = p9pdu_readf(&req->rc, clnt->proto_version, "b", status);
M. Mohan Kumara0990272010-09-27 11:34:24 +05302228 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00002229 trace_9p_protocol_dump(clnt, &req->rc);
M. Mohan Kumara0990272010-09-27 11:34:24 +05302230 goto error;
2231 }
Joe Perches5d385152011-11-28 10:40:46 -08002232 p9_debug(P9_DEBUG_9P, "<<< RLOCK status %i\n", *status);
M. Mohan Kumara0990272010-09-27 11:34:24 +05302233error:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02002234 p9_tag_remove(clnt, req);
M. Mohan Kumara0990272010-09-27 11:34:24 +05302235 return err;
M. Mohan Kumara0990272010-09-27 11:34:24 +05302236}
2237EXPORT_SYMBOL(p9_client_lock_dotl);
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +05302238
2239int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *glock)
2240{
2241 int err;
2242 struct p9_client *clnt;
2243 struct p9_req_t *req;
2244
2245 err = 0;
2246 clnt = fid->clnt;
Dominique Martinet6e195b02021-11-02 22:16:43 +09002247 p9_debug(P9_DEBUG_9P,
2248 ">>> TGETLOCK fid %d, type %i start %lld length %lld proc_id %d client_id %s\n",
2249 fid->fid, glock->type, glock->start, glock->length,
2250 glock->proc_id, glock->client_id);
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +05302251
Dominique Martinet6e195b02021-11-02 22:16:43 +09002252 req = p9_client_rpc(clnt, P9_TGETLOCK, "dbqqds", fid->fid,
2253 glock->type, glock->start, glock->length,
2254 glock->proc_id, glock->client_id);
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +05302255
2256 if (IS_ERR(req))
2257 return PTR_ERR(req);
2258
Dominique Martinet523adb62018-07-30 05:55:19 +00002259 err = p9pdu_readf(&req->rc, clnt->proto_version, "bqqds", &glock->type,
2260 &glock->start, &glock->length, &glock->proc_id,
2261 &glock->client_id);
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +05302262 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00002263 trace_9p_protocol_dump(clnt, &req->rc);
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +05302264 goto error;
2265 }
Dominique Martinet6e195b02021-11-02 22:16:43 +09002266 p9_debug(P9_DEBUG_9P,
2267 "<<< RGETLOCK type %i start %lld length %lld proc_id %d client_id %s\n",
2268 glock->type, glock->start, glock->length,
2269 glock->proc_id, glock->client_id);
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +05302270error:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02002271 p9_tag_remove(clnt, req);
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +05302272 return err;
2273}
2274EXPORT_SYMBOL(p9_client_getlock_dotl);
M. Mohan Kumar329176cc2010-09-28 19:59:25 +05302275
2276int p9_client_readlink(struct p9_fid *fid, char **target)
2277{
2278 int err;
2279 struct p9_client *clnt;
2280 struct p9_req_t *req;
2281
2282 err = 0;
2283 clnt = fid->clnt;
Joe Perches5d385152011-11-28 10:40:46 -08002284 p9_debug(P9_DEBUG_9P, ">>> TREADLINK fid %d\n", fid->fid);
M. Mohan Kumar329176cc2010-09-28 19:59:25 +05302285
2286 req = p9_client_rpc(clnt, P9_TREADLINK, "d", fid->fid);
2287 if (IS_ERR(req))
2288 return PTR_ERR(req);
2289
Dominique Martinet523adb62018-07-30 05:55:19 +00002290 err = p9pdu_readf(&req->rc, clnt->proto_version, "s", target);
M. Mohan Kumar329176cc2010-09-28 19:59:25 +05302291 if (err) {
Dominique Martinet523adb62018-07-30 05:55:19 +00002292 trace_9p_protocol_dump(clnt, &req->rc);
M. Mohan Kumar329176cc2010-09-28 19:59:25 +05302293 goto error;
2294 }
Joe Perches5d385152011-11-28 10:40:46 -08002295 p9_debug(P9_DEBUG_9P, "<<< RREADLINK target %s\n", *target);
M. Mohan Kumar329176cc2010-09-28 19:59:25 +05302296error:
Tomas Bortoli43cbcbe2018-08-11 16:42:53 +02002297 p9_tag_remove(clnt, req);
M. Mohan Kumar329176cc2010-09-28 19:59:25 +05302298 return err;
2299}
2300EXPORT_SYMBOL(p9_client_readlink);
Matthew Wilcox996d5b42018-07-11 14:02:24 -07002301
2302int __init p9_client_init(void)
2303{
Tomas Bortoli728356d2018-08-14 19:43:42 +02002304 p9_req_cache = KMEM_CACHE(p9_req_t, SLAB_TYPESAFE_BY_RCU);
Matthew Wilcox996d5b42018-07-11 14:02:24 -07002305 return p9_req_cache ? 0 : -ENOMEM;
2306}
2307
2308void __exit p9_client_exit(void)
2309{
2310 kmem_cache_destroy(p9_req_cache);
2311}