blob: 281a1ed03a04162eadb7a2f5bcd44b28728ed81d [file] [log] [blame]
Thomas Gleixner1f327612019-05-28 09:57:16 -07001// SPDX-License-Identifier: GPL-2.0-only
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -07002/*
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -07003 * This file contains functions assisting in mapping VFS to 9P2000
4 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06005 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -07006 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -07007 */
8
Joe Perches5d385152011-11-28 10:40:46 -08009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070011#include <linux/module.h>
12#include <linux/errno.h>
13#include <linux/fs.h>
Al Viro914e2632006-10-18 13:55:46 -040014#include <linux/sched.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010015#include <linux/cred.h>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070016#include <linux/parser.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David Howellsc4fac912017-07-05 16:25:37 +010018#include <linux/seq_file.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050019#include <net/9p/9p.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050020#include <net/9p/client.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050021#include <net/9p/transport.h>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070022#include "v9fs.h"
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070023#include "v9fs_vfs.h"
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050024#include "cache.h"
25
26static DEFINE_SPINLOCK(v9fs_sessionlist_lock);
27static LIST_HEAD(v9fs_sessionlist);
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +053028struct kmem_cache *v9fs_inode_cache;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070029
30/*
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050031 * Option Parsing (code inspired by NFS code)
32 * NOTE: each transport will parse its own options
33 */
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070034
35enum {
36 /* Options that take integer arguments */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060037 Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070038 /* String options */
Fabian Frederickcb9af412015-06-05 19:09:35 +020039 Opt_uname, Opt_remotename, Opt_cache, Opt_cachetag,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070040 /* Options that take no arguments */
Eric Van Hensbergen6deffc82022-12-18 20:34:45 +000041 Opt_nodevmap, Opt_noxattr, Opt_directio, Opt_ignoreqv,
Latchesar Ionkovba176742007-10-17 14:31:07 -050042 /* Access options */
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -080043 Opt_access, Opt_posixacl,
Dinu-Razvan Chis-Serban5e172f72018-09-05 16:44:12 +090044 /* Lock timeout option */
45 Opt_locktimeout,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070046 /* Error token */
47 Opt_err
48};
49
Steven Whitehousea447c092008-10-13 10:46:57 +010050static const match_table_t tokens = {
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -050051 {Opt_debug, "debug=%x"},
Latchesar Ionkovbd32b822007-10-17 14:31:07 -050052 {Opt_dfltuid, "dfltuid=%u"},
53 {Opt_dfltgid, "dfltgid=%u"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070054 {Opt_afid, "afid=%u"},
Eric Van Hensbergen67543e52006-03-25 03:07:29 -080055 {Opt_uname, "uname=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070056 {Opt_remotename, "aname=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070057 {Opt_nodevmap, "nodevmap"},
Eric Van Hensbergen8142db42022-12-17 17:19:34 +000058 {Opt_noxattr, "noxattr"},
Eric Van Hensbergen6deffc82022-12-18 20:34:45 +000059 {Opt_directio, "directio"},
60 {Opt_ignoreqv, "ignoreqv"},
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050061 {Opt_cache, "cache=%s"},
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050062 {Opt_cachetag, "cachetag=%s"},
Latchesar Ionkovba176742007-10-17 14:31:07 -050063 {Opt_access, "access=%s"},
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -080064 {Opt_posixacl, "posixacl"},
Dinu-Razvan Chis-Serban5e172f72018-09-05 16:44:12 +090065 {Opt_locktimeout, "locktimeout=%u"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070066 {Opt_err, NULL}
67};
68
Prem Karata2dd43b2011-05-06 18:24:18 +053069/* Interpret mount options for cache mode */
70static int get_cache_mode(char *s)
71{
72 int version = -EINVAL;
73
74 if (!strcmp(s, "loose")) {
Eric Van Hensbergen4eb31172023-03-27 01:53:10 +000075 version = CACHE_SC_LOOSE;
Joe Perches5d385152011-11-28 10:40:46 -080076 p9_debug(P9_DEBUG_9P, "Cache mode: loose\n");
Prem Karata2dd43b2011-05-06 18:24:18 +053077 } else if (!strcmp(s, "fscache")) {
Eric Van Hensbergen4eb31172023-03-27 01:53:10 +000078 version = CACHE_SC_FSCACHE;
Joe Perches5d385152011-11-28 10:40:46 -080079 p9_debug(P9_DEBUG_9P, "Cache mode: fscache\n");
Dominique Martinetfb89b452014-01-10 13:44:09 +010080 } else if (!strcmp(s, "mmap")) {
Eric Van Hensbergen4eb31172023-03-27 01:53:10 +000081 version = CACHE_SC_MMAP;
Dominique Martinetfb89b452014-01-10 13:44:09 +010082 p9_debug(P9_DEBUG_9P, "Cache mode: mmap\n");
Eric Van Hensbergend9bc0d12022-12-08 02:40:37 +000083 } else if (!strcmp(s, "readahead")) {
Eric Van Hensbergen4eb31172023-03-27 01:53:10 +000084 version = CACHE_SC_READAHEAD;
Eric Van Hensbergend9bc0d12022-12-08 02:40:37 +000085 p9_debug(P9_DEBUG_9P, "Cache mode: readahead\n");
Prem Karata2dd43b2011-05-06 18:24:18 +053086 } else if (!strcmp(s, "none")) {
Eric Van Hensbergen4eb31172023-03-27 01:53:10 +000087 version = CACHE_SC_NONE;
Joe Perches5d385152011-11-28 10:40:46 -080088 p9_debug(P9_DEBUG_9P, "Cache mode: none\n");
Eric Van Hensbergen4eb31172023-03-27 01:53:10 +000089 } else if (kstrtoint(s, 0, &version) != 0) {
90 version = -EINVAL;
91 pr_info("Unknown Cache mode or invalid value %s\n", s);
92 }
Prem Karata2dd43b2011-05-06 18:24:18 +053093 return version;
94}
95
David Howellsc4fac912017-07-05 16:25:37 +010096/*
97 * Display the mount options in /proc/mounts.
98 */
99int v9fs_show_options(struct seq_file *m, struct dentry *root)
100{
101 struct v9fs_session_info *v9ses = root->d_sb->s_fs_info;
102
103 if (v9ses->debug)
104 seq_printf(m, ",debug=%x", v9ses->debug);
105 if (!uid_eq(v9ses->dfltuid, V9FS_DEFUID))
106 seq_printf(m, ",dfltuid=%u",
107 from_kuid_munged(&init_user_ns, v9ses->dfltuid));
108 if (!gid_eq(v9ses->dfltgid, V9FS_DEFGID))
109 seq_printf(m, ",dfltgid=%u",
110 from_kgid_munged(&init_user_ns, v9ses->dfltgid));
111 if (v9ses->afid != ~0)
112 seq_printf(m, ",afid=%u", v9ses->afid);
113 if (strcmp(v9ses->uname, V9FS_DEFUSER) != 0)
114 seq_printf(m, ",uname=%s", v9ses->uname);
115 if (strcmp(v9ses->aname, V9FS_DEFANAME) != 0)
116 seq_printf(m, ",aname=%s", v9ses->aname);
117 if (v9ses->nodev)
118 seq_puts(m, ",nodevmap");
119 if (v9ses->cache)
Eric Van Hensbergen4eb31172023-03-27 01:53:10 +0000120 seq_printf(m, ",cache=%x", v9ses->cache);
David Howellsc4fac912017-07-05 16:25:37 +0100121#ifdef CONFIG_9P_FSCACHE
Eric Van Hensbergen4eb31172023-03-27 01:53:10 +0000122 if (v9ses->cachetag && (v9ses->cache & CACHE_FSCACHE))
David Howellsc4fac912017-07-05 16:25:37 +0100123 seq_printf(m, ",cachetag=%s", v9ses->cachetag);
124#endif
125
126 switch (v9ses->flags & V9FS_ACCESS_MASK) {
127 case V9FS_ACCESS_USER:
128 seq_puts(m, ",access=user");
129 break;
130 case V9FS_ACCESS_ANY:
131 seq_puts(m, ",access=any");
132 break;
133 case V9FS_ACCESS_CLIENT:
134 seq_puts(m, ",access=client");
135 break;
136 case V9FS_ACCESS_SINGLE:
137 seq_printf(m, ",access=%u",
138 from_kuid_munged(&init_user_ns, v9ses->uid));
139 break;
140 }
141
Eric Van Hensbergen6deffc82022-12-18 20:34:45 +0000142 if (v9ses->flags & V9FS_IGNORE_QV)
143 seq_puts(m, ",ignoreqv");
144 if (v9ses->flags & V9FS_DIRECT_IO)
145 seq_puts(m, ",directio");
David Howellsc4fac912017-07-05 16:25:37 +0100146 if (v9ses->flags & V9FS_POSIX_ACL)
147 seq_puts(m, ",posixacl");
148
Eric Van Hensbergen8142db42022-12-17 17:19:34 +0000149 if (v9ses->flags & V9FS_NO_XATTR)
150 seq_puts(m, ",noxattr");
151
David Howellsc4fac912017-07-05 16:25:37 +0100152 return p9_show_client_options(m, v9ses->clnt);
153}
154
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700155/**
156 * v9fs_parse_options - parse mount options into session structure
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700157 * @v9ses: existing v9fs session information
David Howellsbc868032021-10-04 22:07:22 +0100158 * @opts: The mount option string
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700159 *
Jim Meyeringab312672008-03-06 17:10:28 -0600160 * Return 0 upon success, -ERRNO upon failure.
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700161 */
162
Abhishek Kulkarni4b53e4b2009-08-17 16:42:28 -0500163static int v9fs_parse_options(struct v9fs_session_info *v9ses, char *opts)
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700164{
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600165 char *options, *tmp_options;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700166 substring_t args[MAX_OPT_ARGS];
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500167 char *p;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600168 int option = 0;
Dominique Martinet10c69a02021-11-02 22:23:37 +0900169 char *s;
Jim Meyeringab312672008-03-06 17:10:28 -0600170 int ret = 0;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700171
172 /* setup defaults */
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700173 v9ses->afid = ~0;
174 v9ses->debug = 0;
Prem Karata2dd43b2011-05-06 18:24:18 +0530175 v9ses->cache = CACHE_NONE;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500176#ifdef CONFIG_9P_FSCACHE
177 v9ses->cachetag = NULL;
178#endif
Dinu-Razvan Chis-Serban5e172f72018-09-05 16:44:12 +0900179 v9ses->session_lock_timeout = P9_LOCK_TIMEOUT;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700180
Abhishek Kulkarni4b53e4b2009-08-17 16:42:28 -0500181 if (!opts)
Jim Meyeringab312672008-03-06 17:10:28 -0600182 return 0;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700183
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600184 tmp_options = kstrdup(opts, GFP_KERNEL);
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600185 if (!tmp_options) {
186 ret = -ENOMEM;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500187 goto fail_option_alloc;
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600188 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600189 options = tmp_options;
Jim Meyeringab312672008-03-06 17:10:28 -0600190
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700191 while ((p = strsep(&options, ",")) != NULL) {
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530192 int token, r;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900193
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700194 if (!*p)
195 continue;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900196
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700197 token = match_token(p, tokens, args);
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530198 switch (token) {
199 case Opt_debug:
200 r = match_int(&args[0], &option);
Jim Meyeringab312672008-03-06 17:10:28 -0600201 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800202 p9_debug(P9_DEBUG_ERROR,
203 "integer field, but no integer?\n");
Jim Meyeringab312672008-03-06 17:10:28 -0600204 ret = r;
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700205 } else {
206 v9ses->debug = option;
Eric Van Hensbergen10fa16e2007-07-16 16:02:49 -0500207#ifdef CONFIG_NET_9P_DEBUG
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700208 p9_debug_level = option;
Eric Van Hensbergen10fa16e2007-07-16 16:02:49 -0500209#endif
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700210 }
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -0500211 break;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600212
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500213 case Opt_dfltuid:
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530214 r = match_int(&args[0], &option);
215 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800216 p9_debug(P9_DEBUG_ERROR,
217 "integer field, but no integer?\n");
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530218 ret = r;
219 continue;
220 }
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800221 v9ses->dfltuid = make_kuid(current_user_ns(), option);
222 if (!uid_valid(v9ses->dfltuid)) {
223 p9_debug(P9_DEBUG_ERROR,
224 "uid field, but not a uid?\n");
225 ret = -EINVAL;
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800226 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700227 break;
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500228 case Opt_dfltgid:
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530229 r = match_int(&args[0], &option);
230 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800231 p9_debug(P9_DEBUG_ERROR,
232 "integer field, but no integer?\n");
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530233 ret = r;
234 continue;
235 }
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800236 v9ses->dfltgid = make_kgid(current_user_ns(), option);
237 if (!gid_valid(v9ses->dfltgid)) {
238 p9_debug(P9_DEBUG_ERROR,
239 "gid field, but not a gid?\n");
240 ret = -EINVAL;
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800241 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700242 break;
243 case Opt_afid:
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530244 r = match_int(&args[0], &option);
245 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800246 p9_debug(P9_DEBUG_ERROR,
247 "integer field, but no integer?\n");
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530248 ret = r;
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700249 } else {
250 v9ses->afid = option;
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530251 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700252 break;
Eric Van Hensbergen67543e52006-03-25 03:07:29 -0800253 case Opt_uname:
Jeff Laytone549c132012-09-07 07:55:25 -0400254 kfree(v9ses->uname);
255 v9ses->uname = match_strdup(&args[0]);
256 if (!v9ses->uname) {
257 ret = -ENOMEM;
258 goto free_and_return;
259 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700260 break;
261 case Opt_remotename:
Jeff Laytone549c132012-09-07 07:55:25 -0400262 kfree(v9ses->aname);
263 v9ses->aname = match_strdup(&args[0]);
264 if (!v9ses->aname) {
265 ret = -ENOMEM;
266 goto free_and_return;
267 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700268 break;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700269 case Opt_nodevmap:
270 v9ses->nodev = 1;
271 break;
Eric Van Hensbergen8142db42022-12-17 17:19:34 +0000272 case Opt_noxattr:
273 v9ses->flags |= V9FS_NO_XATTR;
274 break;
Eric Van Hensbergen6deffc82022-12-18 20:34:45 +0000275 case Opt_directio:
276 v9ses->flags |= V9FS_DIRECT_IO;
277 break;
278 case Opt_ignoreqv:
279 v9ses->flags |= V9FS_IGNORE_QV;
280 break;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500281 case Opt_cachetag:
282#ifdef CONFIG_9P_FSCACHE
David Howellsc4fac912017-07-05 16:25:37 +0100283 kfree(v9ses->cachetag);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500284 v9ses->cachetag = match_strdup(&args[0]);
Chengguang Xua25c3652018-04-05 16:19:53 -0700285 if (!v9ses->cachetag) {
286 ret = -ENOMEM;
287 goto free_and_return;
288 }
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500289#endif
290 break;
291 case Opt_cache:
292 s = match_strdup(&args[0]);
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600293 if (!s) {
294 ret = -ENOMEM;
Joe Perches5d385152011-11-28 10:40:46 -0800295 p9_debug(P9_DEBUG_ERROR,
296 "problem allocating copy of cache arg\n");
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600297 goto free_and_return;
298 }
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700299 r = get_cache_mode(s);
300 if (r < 0)
301 ret = r;
302 else
303 v9ses->cache = r;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500304
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500305 kfree(s);
306 break;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500307
308 case Opt_access:
309 s = match_strdup(&args[0]);
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600310 if (!s) {
311 ret = -ENOMEM;
Joe Perches5d385152011-11-28 10:40:46 -0800312 p9_debug(P9_DEBUG_ERROR,
313 "problem allocating copy of access arg\n");
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600314 goto free_and_return;
315 }
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500316
Latchesar Ionkovba176742007-10-17 14:31:07 -0500317 v9ses->flags &= ~V9FS_ACCESS_MASK;
318 if (strcmp(s, "user") == 0)
319 v9ses->flags |= V9FS_ACCESS_USER;
320 else if (strcmp(s, "any") == 0)
321 v9ses->flags |= V9FS_ACCESS_ANY;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530322 else if (strcmp(s, "client") == 0) {
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530323 v9ses->flags |= V9FS_ACCESS_CLIENT;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530324 } else {
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800325 uid_t uid;
Dominique Martinet10c69a02021-11-02 22:23:37 +0900326
Latchesar Ionkovba176742007-10-17 14:31:07 -0500327 v9ses->flags |= V9FS_ACCESS_SINGLE;
Dominique Martinet10c69a02021-11-02 22:23:37 +0900328 r = kstrtouint(s, 10, &uid);
329 if (r) {
330 ret = r;
331 pr_info("Unknown access argument %s: %d\n",
332 s, r);
Prem Karata2dd43b2011-05-06 18:24:18 +0530333 kfree(s);
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700334 continue;
Prem Karata2dd43b2011-05-06 18:24:18 +0530335 }
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800336 v9ses->uid = make_kuid(current_user_ns(), uid);
337 if (!uid_valid(v9ses->uid)) {
338 ret = -EINVAL;
Colin Ian King6baaac02018-07-17 19:14:49 -0700339 pr_info("Unknown uid %s\n", s);
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800340 }
Latchesar Ionkovba176742007-10-17 14:31:07 -0500341 }
Prem Karata2dd43b2011-05-06 18:24:18 +0530342
Adrian Bunk0a976292007-10-23 13:48:50 -0500343 kfree(s);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500344 break;
345
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800346 case Opt_posixacl:
347#ifdef CONFIG_9P_FS_POSIX_ACL
348 v9ses->flags |= V9FS_POSIX_ACL;
349#else
Joe Perches5d385152011-11-28 10:40:46 -0800350 p9_debug(P9_DEBUG_ERROR,
351 "Not defined CONFIG_9P_FS_POSIX_ACL. Ignoring posixacl option\n");
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800352#endif
353 break;
354
Dinu-Razvan Chis-Serban5e172f72018-09-05 16:44:12 +0900355 case Opt_locktimeout:
356 r = match_int(&args[0], &option);
357 if (r < 0) {
358 p9_debug(P9_DEBUG_ERROR,
359 "integer field, but no integer?\n");
360 ret = r;
361 continue;
362 }
363 if (option < 1) {
364 p9_debug(P9_DEBUG_ERROR,
365 "locktimeout must be a greater than zero integer.\n");
366 ret = -EINVAL;
367 continue;
368 }
369 v9ses->session_lock_timeout = (long)option * HZ;
370 break;
371
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700372 default:
373 continue;
374 }
375 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600376
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600377free_and_return:
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600378 kfree(tmp_options);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500379fail_option_alloc:
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600380 return ret;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700381}
382
383/**
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700384 * v9fs_session_init - initialize session
385 * @v9ses: session information structure
386 * @dev_name: device being mounted
387 * @data: options
388 *
389 */
390
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500391struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700392 const char *dev_name, char *data)
393{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500394 struct p9_fid *fid;
Tejun Heo412a19b2015-06-08 14:57:31 +0900395 int rc = -ENOMEM;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700396
Jeff Laytone549c132012-09-07 07:55:25 -0400397 v9ses->uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500398 if (!v9ses->uname)
Tejun Heo412a19b2015-06-08 14:57:31 +0900399 goto err_names;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700400
Jeff Laytone549c132012-09-07 07:55:25 -0400401 v9ses->aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL);
Tejun Heo412a19b2015-06-08 14:57:31 +0900402 if (!v9ses->aname)
403 goto err_names;
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530404 init_rwsem(&v9ses->rename_sem);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700405
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800406 v9ses->uid = INVALID_UID;
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500407 v9ses->dfltuid = V9FS_DEFUID;
408 v9ses->dfltgid = V9FS_DEFGID;
Jim Meyeringab312672008-03-06 17:10:28 -0600409
Abhishek Kulkarni4b53e4b2009-08-17 16:42:28 -0500410 v9ses->clnt = p9_client_create(dev_name, data);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500411 if (IS_ERR(v9ses->clnt)) {
Tejun Heo412a19b2015-06-08 14:57:31 +0900412 rc = PTR_ERR(v9ses->clnt);
Joe Perches5d385152011-11-28 10:40:46 -0800413 p9_debug(P9_DEBUG_ERROR, "problem initializing 9p client\n");
Jan Kara71304fe2017-04-12 12:24:31 +0200414 goto err_names;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700415 }
416
Venkateswararao Jujjuri (JV)6752a1e2011-01-26 16:20:35 -0800417 v9ses->flags = V9FS_ACCESS_USER;
418
419 if (p9_is_proto_dotl(v9ses->clnt)) {
420 v9ses->flags = V9FS_ACCESS_CLIENT;
Sripathi Kodi476ada02010-03-17 17:02:38 +0000421 v9ses->flags |= V9FS_PROTO_2000L;
Venkateswararao Jujjuri (JV)6752a1e2011-01-26 16:20:35 -0800422 } else if (p9_is_proto_dotu(v9ses->clnt)) {
Sripathi Kodi476ada02010-03-17 17:02:38 +0000423 v9ses->flags |= V9FS_PROTO_2000U;
Venkateswararao Jujjuri (JV)6752a1e2011-01-26 16:20:35 -0800424 }
425
426 rc = v9fs_parse_options(v9ses, data);
Tejun Heo412a19b2015-06-08 14:57:31 +0900427 if (rc < 0)
428 goto err_clnt;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500429
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500430 v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600431
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530432 if (!v9fs_proto_dotl(v9ses) &&
433 ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
434 /*
435 * We support ACCESS_CLIENT only for dotl.
436 * Fall back to ACCESS_USER
437 */
438 v9ses->flags &= ~V9FS_ACCESS_MASK;
439 v9ses->flags |= V9FS_ACCESS_USER;
440 }
441 /*FIXME !! */
Latchesar Ionkovba176742007-10-17 14:31:07 -0500442 /* for legacy mode, fall back to V9FS_ACCESS_ANY */
Aneesh Kumar K.V9ffaf632010-06-01 09:26:18 +0000443 if (!(v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses)) &&
Latchesar Ionkovba176742007-10-17 14:31:07 -0500444 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
445
446 v9ses->flags &= ~V9FS_ACCESS_MASK;
447 v9ses->flags |= V9FS_ACCESS_ANY;
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800448 v9ses->uid = INVALID_UID;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500449 }
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800450 if (!v9fs_proto_dotl(v9ses) ||
451 !((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
452 /*
453 * We support ACL checks on clinet only if the protocol is
454 * 9P2000.L and access is V9FS_ACCESS_CLIENT.
455 */
456 v9ses->flags &= ~V9FS_ACL_MASK;
457 }
Latchesar Ionkovba176742007-10-17 14:31:07 -0500458
Eric W. Biedermanf791f7c2013-01-29 16:09:41 -0800459 fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, INVALID_UID,
Latchesar Ionkovba176742007-10-17 14:31:07 -0500460 v9ses->aname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500461 if (IS_ERR(fid)) {
Tejun Heo412a19b2015-06-08 14:57:31 +0900462 rc = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800463 p9_debug(P9_DEBUG_ERROR, "cannot attach\n");
Tejun Heo412a19b2015-06-08 14:57:31 +0900464 goto err_clnt;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700465 }
466
Latchesar Ionkovba176742007-10-17 14:31:07 -0500467 if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
468 fid->uid = v9ses->uid;
469 else
Eric W. Biedermanb4642552013-01-30 11:48:53 -0800470 fid->uid = INVALID_UID;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500471
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500472#ifdef CONFIG_9P_FSCACHE
473 /* register the session for caching */
Eric Van Hensbergen4eb31172023-03-27 01:53:10 +0000474 if (v9ses->cache & CACHE_FSCACHE) {
David Howells24e42e32020-11-18 09:06:42 +0000475 rc = v9fs_cache_session_get_cookie(v9ses, dev_name);
476 if (rc < 0)
477 goto err_clnt;
478 }
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500479#endif
Tejun Heo412a19b2015-06-08 14:57:31 +0900480 spin_lock(&v9fs_sessionlist_lock);
481 list_add(&v9ses->slist, &v9fs_sessionlist);
482 spin_unlock(&v9fs_sessionlist_lock);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500483
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500484 return fid;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700485
Tejun Heo412a19b2015-06-08 14:57:31 +0900486err_clnt:
Chengguang Xua25c3652018-04-05 16:19:53 -0700487#ifdef CONFIG_9P_FSCACHE
488 kfree(v9ses->cachetag);
489#endif
Tejun Heo412a19b2015-06-08 14:57:31 +0900490 p9_client_destroy(v9ses->clnt);
Tejun Heo412a19b2015-06-08 14:57:31 +0900491err_names:
492 kfree(v9ses->uname);
493 kfree(v9ses->aname);
494 return ERR_PTR(rc);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700495}
496
497/**
498 * v9fs_session_close - shutdown a session
499 * @v9ses: session information structure
500 *
501 */
502
503void v9fs_session_close(struct v9fs_session_info *v9ses)
504{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500505 if (v9ses->clnt) {
506 p9_client_destroy(v9ses->clnt);
507 v9ses->clnt = NULL;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800508 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700509
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500510#ifdef CONFIG_9P_FSCACHE
David Howells24e42e32020-11-18 09:06:42 +0000511 fscache_relinquish_volume(v9fs_session_cache(v9ses), NULL, false);
Zheng Bincb0aae02020-06-15 09:21:53 +0800512 kfree(v9ses->cachetag);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500513#endif
Jeff Laytone549c132012-09-07 07:55:25 -0400514 kfree(v9ses->uname);
515 kfree(v9ses->aname);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500516
517 spin_lock(&v9fs_sessionlist_lock);
518 list_del(&v9ses->slist);
519 spin_unlock(&v9fs_sessionlist_lock);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700520}
521
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700522/**
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600523 * v9fs_session_cancel - terminate a session
524 * @v9ses: session to terminate
525 *
526 * mark transport as disconnected and cancel all pending requests.
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700527 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600528
Sohaib Mohamede4eeefb2021-10-01 08:24:54 +0200529void v9fs_session_cancel(struct v9fs_session_info *v9ses)
530{
Joe Perches5d385152011-11-28 10:40:46 -0800531 p9_debug(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500532 p9_client_disconnect(v9ses->clnt);
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700533}
534
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -0500535/**
536 * v9fs_session_begin_cancel - Begin terminate of a session
537 * @v9ses: session to terminate
538 *
539 * After this call we don't allow any request other than clunk.
540 */
541
542void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses)
543{
Joe Perches5d385152011-11-28 10:40:46 -0800544 p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses);
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -0500545 p9_client_begin_disconnect(v9ses->clnt);
546}
547
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500548static struct kobject *v9fs_kobj;
549
550#ifdef CONFIG_9P_FSCACHE
David Howellsbc868032021-10-04 22:07:22 +0100551/*
552 * List caches associated with a session
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500553 */
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500554static ssize_t caches_show(struct kobject *kobj,
555 struct kobj_attribute *attr,
556 char *buf)
557{
558 ssize_t n = 0, count = 0, limit = PAGE_SIZE;
559 struct v9fs_session_info *v9ses;
560
561 spin_lock(&v9fs_sessionlist_lock);
562 list_for_each_entry(v9ses, &v9fs_sessionlist, slist) {
563 if (v9ses->cachetag) {
564 n = snprintf(buf, limit, "%s\n", v9ses->cachetag);
565 if (n < 0) {
566 count = n;
567 break;
568 }
569
570 count += n;
571 limit -= n;
572 }
573 }
574
575 spin_unlock(&v9fs_sessionlist_lock);
576 return count;
577}
578
579static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches);
580#endif /* CONFIG_9P_FSCACHE */
581
582static struct attribute *v9fs_attrs[] = {
583#ifdef CONFIG_9P_FSCACHE
584 &v9fs_attr_cache.attr,
585#endif
586 NULL,
587};
588
Rikard Falkeborn0dae5222021-01-08 23:46:50 +0100589static const struct attribute_group v9fs_attr_group = {
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500590 .attrs = v9fs_attrs,
591};
592
593/**
594 * v9fs_sysfs_init - Initialize the v9fs sysfs interface
595 *
596 */
597
Fabian Frederickbdbeacd2014-06-04 16:06:25 -0700598static int __init v9fs_sysfs_init(void)
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500599{
600 v9fs_kobj = kobject_create_and_add("9p", fs_kobj);
601 if (!v9fs_kobj)
602 return -ENOMEM;
603
604 if (sysfs_create_group(v9fs_kobj, &v9fs_attr_group)) {
605 kobject_put(v9fs_kobj);
606 return -ENOMEM;
607 }
608
609 return 0;
610}
611
612/**
613 * v9fs_sysfs_cleanup - Unregister the v9fs sysfs interface
614 *
615 */
616
617static void v9fs_sysfs_cleanup(void)
618{
619 sysfs_remove_group(v9fs_kobj, &v9fs_attr_group);
620 kobject_put(v9fs_kobj);
621}
622
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530623static void v9fs_inode_init_once(void *foo)
624{
625 struct v9fs_inode *v9inode = (struct v9fs_inode *)foo;
David Howellsbc899ee2021-06-29 22:37:05 +0100626
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000627 memset(&v9inode->qid, 0, sizeof(v9inode->qid));
David Howells874c8ca12022-06-09 21:46:04 +0100628 inode_init_once(&v9inode->netfs.inode);
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530629}
630
631/**
632 * v9fs_init_inode_cache - initialize a cache for 9P
633 * Returns 0 on success.
634 */
635static int v9fs_init_inode_cache(void)
636{
637 v9fs_inode_cache = kmem_cache_create("v9fs_inode_cache",
638 sizeof(struct v9fs_inode),
639 0, (SLAB_RECLAIM_ACCOUNT|
Linus Torvaldsf88c3fb2024-03-12 20:32:19 -0700640 SLAB_ACCOUNT),
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530641 v9fs_inode_init_once);
642 if (!v9fs_inode_cache)
643 return -ENOMEM;
644
645 return 0;
646}
647
648/**
649 * v9fs_destroy_inode_cache - destroy the cache of 9P inode
650 *
651 */
652static void v9fs_destroy_inode_cache(void)
653{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000654 /*
655 * Make sure all delayed rcu free inodes are flushed before we
656 * destroy cache.
657 */
658 rcu_barrier();
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530659 kmem_cache_destroy(v9fs_inode_cache);
660}
661
662static int v9fs_cache_register(void)
663{
664 int ret;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900665
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530666 ret = v9fs_init_inode_cache();
667 if (ret < 0)
668 return ret;
Al Viro8061a6f2013-09-17 08:10:18 -0400669 return ret;
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530670}
671
672static void v9fs_cache_unregister(void)
673{
674 v9fs_destroy_inode_cache();
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530675}
676
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500677/**
678 * init_v9fs - Initialize module
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700679 *
680 */
681
682static int __init init_v9fs(void)
683{
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500684 int err;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900685
Joe Perches5d385152011-11-28 10:40:46 -0800686 pr_info("Installing v9fs 9p2000 file system support\n");
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500687 /* TODO: Setup list of registered trasnport modules */
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500688
689 err = v9fs_cache_register();
690 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800691 pr_err("Failed to register v9fs for caching\n");
Al Viro2226a282012-03-17 02:37:05 -0400692 return err;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500693 }
694
695 err = v9fs_sysfs_init();
696 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800697 pr_err("Failed to register with sysfs\n");
Al Viro2226a282012-03-17 02:37:05 -0400698 goto out_cache;
699 }
700 err = register_filesystem(&v9fs_fs_type);
701 if (err < 0) {
702 pr_err("Failed to register filesystem\n");
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500703 goto out_sysfs_cleanup;
704 }
705
706 return 0;
707
708out_sysfs_cleanup:
709 v9fs_sysfs_cleanup();
710
Al Viro2226a282012-03-17 02:37:05 -0400711out_cache:
712 v9fs_cache_unregister();
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500713
714 return err;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700715}
716
717/**
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500718 * exit_v9fs - shutdown module
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700719 *
720 */
721
722static void __exit exit_v9fs(void)
723{
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500724 v9fs_sysfs_cleanup();
725 v9fs_cache_unregister();
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700726 unregister_filesystem(&v9fs_fs_type);
727}
728
729module_init(init_v9fs)
730module_exit(exit_v9fs)
731
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500732MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700733MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
734MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
Dominique Martinete02be632023-10-26 07:27:52 +0900735MODULE_DESCRIPTION("9P Client File System");
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700736MODULE_LICENSE("GPL");