blob: e32dd5f7721b7b994fab9615d55431643940e4b0 [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>
17#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
David Howellsc4fac912017-07-05 16:25:37 +010019#include <linux/seq_file.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050020#include <net/9p/9p.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050021#include <net/9p/client.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050022#include <net/9p/transport.h>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070023#include "v9fs.h"
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070024#include "v9fs_vfs.h"
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050025#include "cache.h"
26
27static DEFINE_SPINLOCK(v9fs_sessionlist_lock);
28static LIST_HEAD(v9fs_sessionlist);
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +053029struct kmem_cache *v9fs_inode_cache;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070030
31/*
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050032 * Option Parsing (code inspired by NFS code)
33 * NOTE: each transport will parse its own options
34 */
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070035
36enum {
37 /* Options that take integer arguments */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060038 Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070039 /* String options */
Fabian Frederickcb9af412015-06-05 19:09:35 +020040 Opt_uname, Opt_remotename, Opt_cache, Opt_cachetag,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070041 /* Options that take no arguments */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060042 Opt_nodevmap,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -060043 /* Cache options */
Dominique Martinetfb89b452014-01-10 13:44:09 +010044 Opt_cache_loose, Opt_fscache, Opt_mmap,
Latchesar Ionkovba176742007-10-17 14:31:07 -050045 /* Access options */
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -080046 Opt_access, Opt_posixacl,
Dinu-Razvan Chis-Serban5e172f72018-09-05 16:44:12 +090047 /* Lock timeout option */
48 Opt_locktimeout,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070049 /* Error token */
50 Opt_err
51};
52
Steven Whitehousea447c092008-10-13 10:46:57 +010053static const match_table_t tokens = {
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -050054 {Opt_debug, "debug=%x"},
Latchesar Ionkovbd32b822007-10-17 14:31:07 -050055 {Opt_dfltuid, "dfltuid=%u"},
56 {Opt_dfltgid, "dfltgid=%u"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070057 {Opt_afid, "afid=%u"},
Eric Van Hensbergen67543e52006-03-25 03:07:29 -080058 {Opt_uname, "uname=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070059 {Opt_remotename, "aname=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070060 {Opt_nodevmap, "nodevmap"},
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050061 {Opt_cache, "cache=%s"},
Eric Van Hensbergene03abc02007-02-11 13:21:39 -060062 {Opt_cache_loose, "loose"},
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050063 {Opt_fscache, "fscache"},
Dominique Martinetfb89b452014-01-10 13:44:09 +010064 {Opt_mmap, "mmap"},
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050065 {Opt_cachetag, "cachetag=%s"},
Latchesar Ionkovba176742007-10-17 14:31:07 -050066 {Opt_access, "access=%s"},
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -080067 {Opt_posixacl, "posixacl"},
Dinu-Razvan Chis-Serban5e172f72018-09-05 16:44:12 +090068 {Opt_locktimeout, "locktimeout=%u"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070069 {Opt_err, NULL}
70};
71
David Howellsc4fac912017-07-05 16:25:37 +010072static const char *const v9fs_cache_modes[nr__p9_cache_modes] = {
73 [CACHE_NONE] = "none",
74 [CACHE_MMAP] = "mmap",
75 [CACHE_LOOSE] = "loose",
76 [CACHE_FSCACHE] = "fscache",
77};
78
Prem Karata2dd43b2011-05-06 18:24:18 +053079/* Interpret mount options for cache mode */
80static int get_cache_mode(char *s)
81{
82 int version = -EINVAL;
83
84 if (!strcmp(s, "loose")) {
85 version = CACHE_LOOSE;
Joe Perches5d385152011-11-28 10:40:46 -080086 p9_debug(P9_DEBUG_9P, "Cache mode: loose\n");
Prem Karata2dd43b2011-05-06 18:24:18 +053087 } else if (!strcmp(s, "fscache")) {
88 version = CACHE_FSCACHE;
Joe Perches5d385152011-11-28 10:40:46 -080089 p9_debug(P9_DEBUG_9P, "Cache mode: fscache\n");
Dominique Martinetfb89b452014-01-10 13:44:09 +010090 } else if (!strcmp(s, "mmap")) {
91 version = CACHE_MMAP;
92 p9_debug(P9_DEBUG_9P, "Cache mode: mmap\n");
Prem Karata2dd43b2011-05-06 18:24:18 +053093 } else if (!strcmp(s, "none")) {
94 version = CACHE_NONE;
Joe Perches5d385152011-11-28 10:40:46 -080095 p9_debug(P9_DEBUG_9P, "Cache mode: none\n");
Prem Karata2dd43b2011-05-06 18:24:18 +053096 } else
Joe Perches5d385152011-11-28 10:40:46 -080097 pr_info("Unknown Cache mode %s\n", s);
Prem Karata2dd43b2011-05-06 18:24:18 +053098 return version;
99}
100
David Howellsc4fac912017-07-05 16:25:37 +0100101/*
102 * Display the mount options in /proc/mounts.
103 */
104int v9fs_show_options(struct seq_file *m, struct dentry *root)
105{
106 struct v9fs_session_info *v9ses = root->d_sb->s_fs_info;
107
108 if (v9ses->debug)
109 seq_printf(m, ",debug=%x", v9ses->debug);
110 if (!uid_eq(v9ses->dfltuid, V9FS_DEFUID))
111 seq_printf(m, ",dfltuid=%u",
112 from_kuid_munged(&init_user_ns, v9ses->dfltuid));
113 if (!gid_eq(v9ses->dfltgid, V9FS_DEFGID))
114 seq_printf(m, ",dfltgid=%u",
115 from_kgid_munged(&init_user_ns, v9ses->dfltgid));
116 if (v9ses->afid != ~0)
117 seq_printf(m, ",afid=%u", v9ses->afid);
118 if (strcmp(v9ses->uname, V9FS_DEFUSER) != 0)
119 seq_printf(m, ",uname=%s", v9ses->uname);
120 if (strcmp(v9ses->aname, V9FS_DEFANAME) != 0)
121 seq_printf(m, ",aname=%s", v9ses->aname);
122 if (v9ses->nodev)
123 seq_puts(m, ",nodevmap");
124 if (v9ses->cache)
125 seq_printf(m, ",%s", v9fs_cache_modes[v9ses->cache]);
126#ifdef CONFIG_9P_FSCACHE
127 if (v9ses->cachetag && v9ses->cache == CACHE_FSCACHE)
128 seq_printf(m, ",cachetag=%s", v9ses->cachetag);
129#endif
130
131 switch (v9ses->flags & V9FS_ACCESS_MASK) {
132 case V9FS_ACCESS_USER:
133 seq_puts(m, ",access=user");
134 break;
135 case V9FS_ACCESS_ANY:
136 seq_puts(m, ",access=any");
137 break;
138 case V9FS_ACCESS_CLIENT:
139 seq_puts(m, ",access=client");
140 break;
141 case V9FS_ACCESS_SINGLE:
142 seq_printf(m, ",access=%u",
143 from_kuid_munged(&init_user_ns, v9ses->uid));
144 break;
145 }
146
147 if (v9ses->flags & V9FS_POSIX_ACL)
148 seq_puts(m, ",posixacl");
149
150 return p9_show_client_options(m, v9ses->clnt);
151}
152
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700153/**
154 * v9fs_parse_options - parse mount options into session structure
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700155 * @v9ses: existing v9fs session information
David Howellsbc868032021-10-04 22:07:22 +0100156 * @opts: The mount option string
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700157 *
Jim Meyeringab312672008-03-06 17:10:28 -0600158 * Return 0 upon success, -ERRNO upon failure.
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700159 */
160
Abhishek Kulkarni4b53e4b2009-08-17 16:42:28 -0500161static int v9fs_parse_options(struct v9fs_session_info *v9ses, char *opts)
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700162{
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600163 char *options, *tmp_options;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700164 substring_t args[MAX_OPT_ARGS];
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500165 char *p;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600166 int option = 0;
Dominique Martinet10c69a02021-11-02 22:23:37 +0900167 char *s;
Jim Meyeringab312672008-03-06 17:10:28 -0600168 int ret = 0;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700169
170 /* setup defaults */
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700171 v9ses->afid = ~0;
172 v9ses->debug = 0;
Prem Karata2dd43b2011-05-06 18:24:18 +0530173 v9ses->cache = CACHE_NONE;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500174#ifdef CONFIG_9P_FSCACHE
175 v9ses->cachetag = NULL;
176#endif
Dinu-Razvan Chis-Serban5e172f72018-09-05 16:44:12 +0900177 v9ses->session_lock_timeout = P9_LOCK_TIMEOUT;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700178
Abhishek Kulkarni4b53e4b2009-08-17 16:42:28 -0500179 if (!opts)
Jim Meyeringab312672008-03-06 17:10:28 -0600180 return 0;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700181
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600182 tmp_options = kstrdup(opts, GFP_KERNEL);
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600183 if (!tmp_options) {
184 ret = -ENOMEM;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500185 goto fail_option_alloc;
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600186 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600187 options = tmp_options;
Jim Meyeringab312672008-03-06 17:10:28 -0600188
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700189 while ((p = strsep(&options, ",")) != NULL) {
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530190 int token, r;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900191
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700192 if (!*p)
193 continue;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900194
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700195 token = match_token(p, tokens, args);
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530196 switch (token) {
197 case Opt_debug:
198 r = match_int(&args[0], &option);
Jim Meyeringab312672008-03-06 17:10:28 -0600199 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800200 p9_debug(P9_DEBUG_ERROR,
201 "integer field, but no integer?\n");
Jim Meyeringab312672008-03-06 17:10:28 -0600202 ret = r;
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700203 } else {
204 v9ses->debug = option;
Eric Van Hensbergen10fa16e2007-07-16 16:02:49 -0500205#ifdef CONFIG_NET_9P_DEBUG
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700206 p9_debug_level = option;
Eric Van Hensbergen10fa16e2007-07-16 16:02:49 -0500207#endif
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700208 }
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -0500209 break;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600210
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500211 case Opt_dfltuid:
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530212 r = match_int(&args[0], &option);
213 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800214 p9_debug(P9_DEBUG_ERROR,
215 "integer field, but no integer?\n");
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530216 ret = r;
217 continue;
218 }
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800219 v9ses->dfltuid = make_kuid(current_user_ns(), option);
220 if (!uid_valid(v9ses->dfltuid)) {
221 p9_debug(P9_DEBUG_ERROR,
222 "uid field, but not a uid?\n");
223 ret = -EINVAL;
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800224 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700225 break;
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500226 case Opt_dfltgid:
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530227 r = match_int(&args[0], &option);
228 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800229 p9_debug(P9_DEBUG_ERROR,
230 "integer field, but no integer?\n");
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530231 ret = r;
232 continue;
233 }
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800234 v9ses->dfltgid = make_kgid(current_user_ns(), option);
235 if (!gid_valid(v9ses->dfltgid)) {
236 p9_debug(P9_DEBUG_ERROR,
237 "gid field, but not a gid?\n");
238 ret = -EINVAL;
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800239 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700240 break;
241 case Opt_afid:
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530242 r = match_int(&args[0], &option);
243 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800244 p9_debug(P9_DEBUG_ERROR,
245 "integer field, but no integer?\n");
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530246 ret = r;
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700247 } else {
248 v9ses->afid = option;
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530249 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700250 break;
Eric Van Hensbergen67543e52006-03-25 03:07:29 -0800251 case Opt_uname:
Jeff Laytone549c132012-09-07 07:55:25 -0400252 kfree(v9ses->uname);
253 v9ses->uname = match_strdup(&args[0]);
254 if (!v9ses->uname) {
255 ret = -ENOMEM;
256 goto free_and_return;
257 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700258 break;
259 case Opt_remotename:
Jeff Laytone549c132012-09-07 07:55:25 -0400260 kfree(v9ses->aname);
261 v9ses->aname = match_strdup(&args[0]);
262 if (!v9ses->aname) {
263 ret = -ENOMEM;
264 goto free_and_return;
265 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700266 break;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700267 case Opt_nodevmap:
268 v9ses->nodev = 1;
269 break;
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600270 case Opt_cache_loose:
271 v9ses->cache = CACHE_LOOSE;
272 break;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500273 case Opt_fscache:
274 v9ses->cache = CACHE_FSCACHE;
275 break;
Dominique Martinetfb89b452014-01-10 13:44:09 +0100276 case Opt_mmap:
277 v9ses->cache = CACHE_MMAP;
278 break;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500279 case Opt_cachetag:
280#ifdef CONFIG_9P_FSCACHE
David Howellsc4fac912017-07-05 16:25:37 +0100281 kfree(v9ses->cachetag);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500282 v9ses->cachetag = match_strdup(&args[0]);
Chengguang Xua25c3652018-04-05 16:19:53 -0700283 if (!v9ses->cachetag) {
284 ret = -ENOMEM;
285 goto free_and_return;
286 }
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500287#endif
288 break;
289 case Opt_cache:
290 s = match_strdup(&args[0]);
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600291 if (!s) {
292 ret = -ENOMEM;
Joe Perches5d385152011-11-28 10:40:46 -0800293 p9_debug(P9_DEBUG_ERROR,
294 "problem allocating copy of cache arg\n");
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600295 goto free_and_return;
296 }
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700297 r = get_cache_mode(s);
298 if (r < 0)
299 ret = r;
300 else
301 v9ses->cache = r;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500302
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500303 kfree(s);
304 break;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500305
306 case Opt_access:
307 s = match_strdup(&args[0]);
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600308 if (!s) {
309 ret = -ENOMEM;
Joe Perches5d385152011-11-28 10:40:46 -0800310 p9_debug(P9_DEBUG_ERROR,
311 "problem allocating copy of access arg\n");
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600312 goto free_and_return;
313 }
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500314
Latchesar Ionkovba176742007-10-17 14:31:07 -0500315 v9ses->flags &= ~V9FS_ACCESS_MASK;
316 if (strcmp(s, "user") == 0)
317 v9ses->flags |= V9FS_ACCESS_USER;
318 else if (strcmp(s, "any") == 0)
319 v9ses->flags |= V9FS_ACCESS_ANY;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530320 else if (strcmp(s, "client") == 0) {
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530321 v9ses->flags |= V9FS_ACCESS_CLIENT;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530322 } else {
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800323 uid_t uid;
Dominique Martinet10c69a02021-11-02 22:23:37 +0900324
Latchesar Ionkovba176742007-10-17 14:31:07 -0500325 v9ses->flags |= V9FS_ACCESS_SINGLE;
Dominique Martinet10c69a02021-11-02 22:23:37 +0900326 r = kstrtouint(s, 10, &uid);
327 if (r) {
328 ret = r;
329 pr_info("Unknown access argument %s: %d\n",
330 s, r);
Prem Karata2dd43b2011-05-06 18:24:18 +0530331 kfree(s);
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700332 continue;
Prem Karata2dd43b2011-05-06 18:24:18 +0530333 }
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800334 v9ses->uid = make_kuid(current_user_ns(), uid);
335 if (!uid_valid(v9ses->uid)) {
336 ret = -EINVAL;
Colin Ian King6baaac02018-07-17 19:14:49 -0700337 pr_info("Unknown uid %s\n", s);
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800338 }
Latchesar Ionkovba176742007-10-17 14:31:07 -0500339 }
Prem Karata2dd43b2011-05-06 18:24:18 +0530340
Adrian Bunk0a976292007-10-23 13:48:50 -0500341 kfree(s);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500342 break;
343
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800344 case Opt_posixacl:
345#ifdef CONFIG_9P_FS_POSIX_ACL
346 v9ses->flags |= V9FS_POSIX_ACL;
347#else
Joe Perches5d385152011-11-28 10:40:46 -0800348 p9_debug(P9_DEBUG_ERROR,
349 "Not defined CONFIG_9P_FS_POSIX_ACL. Ignoring posixacl option\n");
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800350#endif
351 break;
352
Dinu-Razvan Chis-Serban5e172f72018-09-05 16:44:12 +0900353 case Opt_locktimeout:
354 r = match_int(&args[0], &option);
355 if (r < 0) {
356 p9_debug(P9_DEBUG_ERROR,
357 "integer field, but no integer?\n");
358 ret = r;
359 continue;
360 }
361 if (option < 1) {
362 p9_debug(P9_DEBUG_ERROR,
363 "locktimeout must be a greater than zero integer.\n");
364 ret = -EINVAL;
365 continue;
366 }
367 v9ses->session_lock_timeout = (long)option * HZ;
368 break;
369
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700370 default:
371 continue;
372 }
373 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600374
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600375free_and_return:
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600376 kfree(tmp_options);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500377fail_option_alloc:
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600378 return ret;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700379}
380
381/**
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700382 * v9fs_session_init - initialize session
383 * @v9ses: session information structure
384 * @dev_name: device being mounted
385 * @data: options
386 *
387 */
388
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500389struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700390 const char *dev_name, char *data)
391{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500392 struct p9_fid *fid;
Tejun Heo412a19b2015-06-08 14:57:31 +0900393 int rc = -ENOMEM;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700394
Jeff Laytone549c132012-09-07 07:55:25 -0400395 v9ses->uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500396 if (!v9ses->uname)
Tejun Heo412a19b2015-06-08 14:57:31 +0900397 goto err_names;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700398
Jeff Laytone549c132012-09-07 07:55:25 -0400399 v9ses->aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL);
Tejun Heo412a19b2015-06-08 14:57:31 +0900400 if (!v9ses->aname)
401 goto err_names;
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530402 init_rwsem(&v9ses->rename_sem);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700403
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800404 v9ses->uid = INVALID_UID;
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500405 v9ses->dfltuid = V9FS_DEFUID;
406 v9ses->dfltgid = V9FS_DEFGID;
Jim Meyeringab312672008-03-06 17:10:28 -0600407
Abhishek Kulkarni4b53e4b2009-08-17 16:42:28 -0500408 v9ses->clnt = p9_client_create(dev_name, data);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500409 if (IS_ERR(v9ses->clnt)) {
Tejun Heo412a19b2015-06-08 14:57:31 +0900410 rc = PTR_ERR(v9ses->clnt);
Joe Perches5d385152011-11-28 10:40:46 -0800411 p9_debug(P9_DEBUG_ERROR, "problem initializing 9p client\n");
Jan Kara71304fe2017-04-12 12:24:31 +0200412 goto err_names;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700413 }
414
Venkateswararao Jujjuri (JV)6752a1e2011-01-26 16:20:35 -0800415 v9ses->flags = V9FS_ACCESS_USER;
416
417 if (p9_is_proto_dotl(v9ses->clnt)) {
418 v9ses->flags = V9FS_ACCESS_CLIENT;
Sripathi Kodi476ada02010-03-17 17:02:38 +0000419 v9ses->flags |= V9FS_PROTO_2000L;
Venkateswararao Jujjuri (JV)6752a1e2011-01-26 16:20:35 -0800420 } else if (p9_is_proto_dotu(v9ses->clnt)) {
Sripathi Kodi476ada02010-03-17 17:02:38 +0000421 v9ses->flags |= V9FS_PROTO_2000U;
Venkateswararao Jujjuri (JV)6752a1e2011-01-26 16:20:35 -0800422 }
423
424 rc = v9fs_parse_options(v9ses, data);
Tejun Heo412a19b2015-06-08 14:57:31 +0900425 if (rc < 0)
426 goto err_clnt;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500427
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500428 v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600429
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530430 if (!v9fs_proto_dotl(v9ses) &&
431 ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
432 /*
433 * We support ACCESS_CLIENT only for dotl.
434 * Fall back to ACCESS_USER
435 */
436 v9ses->flags &= ~V9FS_ACCESS_MASK;
437 v9ses->flags |= V9FS_ACCESS_USER;
438 }
439 /*FIXME !! */
Latchesar Ionkovba176742007-10-17 14:31:07 -0500440 /* for legacy mode, fall back to V9FS_ACCESS_ANY */
Aneesh Kumar K.V9ffaf632010-06-01 09:26:18 +0000441 if (!(v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses)) &&
Latchesar Ionkovba176742007-10-17 14:31:07 -0500442 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
443
444 v9ses->flags &= ~V9FS_ACCESS_MASK;
445 v9ses->flags |= V9FS_ACCESS_ANY;
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800446 v9ses->uid = INVALID_UID;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500447 }
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800448 if (!v9fs_proto_dotl(v9ses) ||
449 !((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
450 /*
451 * We support ACL checks on clinet only if the protocol is
452 * 9P2000.L and access is V9FS_ACCESS_CLIENT.
453 */
454 v9ses->flags &= ~V9FS_ACL_MASK;
455 }
Latchesar Ionkovba176742007-10-17 14:31:07 -0500456
Eric W. Biedermanf791f7c2013-01-29 16:09:41 -0800457 fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, INVALID_UID,
Latchesar Ionkovba176742007-10-17 14:31:07 -0500458 v9ses->aname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500459 if (IS_ERR(fid)) {
Tejun Heo412a19b2015-06-08 14:57:31 +0900460 rc = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800461 p9_debug(P9_DEBUG_ERROR, "cannot attach\n");
Tejun Heo412a19b2015-06-08 14:57:31 +0900462 goto err_clnt;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700463 }
464
Latchesar Ionkovba176742007-10-17 14:31:07 -0500465 if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
466 fid->uid = v9ses->uid;
467 else
Eric W. Biedermanb4642552013-01-30 11:48:53 -0800468 fid->uid = INVALID_UID;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500469
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500470#ifdef CONFIG_9P_FSCACHE
471 /* register the session for caching */
472 v9fs_cache_session_get_cookie(v9ses);
473#endif
Tejun Heo412a19b2015-06-08 14:57:31 +0900474 spin_lock(&v9fs_sessionlist_lock);
475 list_add(&v9ses->slist, &v9fs_sessionlist);
476 spin_unlock(&v9fs_sessionlist_lock);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500477
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500478 return fid;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700479
Tejun Heo412a19b2015-06-08 14:57:31 +0900480err_clnt:
Chengguang Xua25c3652018-04-05 16:19:53 -0700481#ifdef CONFIG_9P_FSCACHE
482 kfree(v9ses->cachetag);
483#endif
Tejun Heo412a19b2015-06-08 14:57:31 +0900484 p9_client_destroy(v9ses->clnt);
Tejun Heo412a19b2015-06-08 14:57:31 +0900485err_names:
486 kfree(v9ses->uname);
487 kfree(v9ses->aname);
488 return ERR_PTR(rc);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700489}
490
491/**
492 * v9fs_session_close - shutdown a session
493 * @v9ses: session information structure
494 *
495 */
496
497void v9fs_session_close(struct v9fs_session_info *v9ses)
498{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500499 if (v9ses->clnt) {
500 p9_client_destroy(v9ses->clnt);
501 v9ses->clnt = NULL;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800502 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700503
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500504#ifdef CONFIG_9P_FSCACHE
Zheng Bincb0aae02020-06-15 09:21:53 +0800505 if (v9ses->fscache)
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500506 v9fs_cache_session_put_cookie(v9ses);
Zheng Bincb0aae02020-06-15 09:21:53 +0800507 kfree(v9ses->cachetag);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500508#endif
Jeff Laytone549c132012-09-07 07:55:25 -0400509 kfree(v9ses->uname);
510 kfree(v9ses->aname);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500511
512 spin_lock(&v9fs_sessionlist_lock);
513 list_del(&v9ses->slist);
514 spin_unlock(&v9fs_sessionlist_lock);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700515}
516
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700517/**
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600518 * v9fs_session_cancel - terminate a session
519 * @v9ses: session to terminate
520 *
521 * mark transport as disconnected and cancel all pending requests.
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700522 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600523
Sohaib Mohamede4eeefb2021-10-01 08:24:54 +0200524void v9fs_session_cancel(struct v9fs_session_info *v9ses)
525{
Joe Perches5d385152011-11-28 10:40:46 -0800526 p9_debug(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500527 p9_client_disconnect(v9ses->clnt);
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700528}
529
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -0500530/**
531 * v9fs_session_begin_cancel - Begin terminate of a session
532 * @v9ses: session to terminate
533 *
534 * After this call we don't allow any request other than clunk.
535 */
536
537void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses)
538{
Joe Perches5d385152011-11-28 10:40:46 -0800539 p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses);
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -0500540 p9_client_begin_disconnect(v9ses->clnt);
541}
542
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700543extern int v9fs_error_init(void);
544
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500545static struct kobject *v9fs_kobj;
546
547#ifdef CONFIG_9P_FSCACHE
David Howellsbc868032021-10-04 22:07:22 +0100548/*
549 * List caches associated with a session
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500550 */
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500551static ssize_t caches_show(struct kobject *kobj,
552 struct kobj_attribute *attr,
553 char *buf)
554{
555 ssize_t n = 0, count = 0, limit = PAGE_SIZE;
556 struct v9fs_session_info *v9ses;
557
558 spin_lock(&v9fs_sessionlist_lock);
559 list_for_each_entry(v9ses, &v9fs_sessionlist, slist) {
560 if (v9ses->cachetag) {
561 n = snprintf(buf, limit, "%s\n", v9ses->cachetag);
562 if (n < 0) {
563 count = n;
564 break;
565 }
566
567 count += n;
568 limit -= n;
569 }
570 }
571
572 spin_unlock(&v9fs_sessionlist_lock);
573 return count;
574}
575
576static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches);
577#endif /* CONFIG_9P_FSCACHE */
578
579static struct attribute *v9fs_attrs[] = {
580#ifdef CONFIG_9P_FSCACHE
581 &v9fs_attr_cache.attr,
582#endif
583 NULL,
584};
585
Rikard Falkeborn0dae5222021-01-08 23:46:50 +0100586static const struct attribute_group v9fs_attr_group = {
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500587 .attrs = v9fs_attrs,
588};
589
590/**
591 * v9fs_sysfs_init - Initialize the v9fs sysfs interface
592 *
593 */
594
Fabian Frederickbdbeacd2014-06-04 16:06:25 -0700595static int __init v9fs_sysfs_init(void)
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500596{
597 v9fs_kobj = kobject_create_and_add("9p", fs_kobj);
598 if (!v9fs_kobj)
599 return -ENOMEM;
600
601 if (sysfs_create_group(v9fs_kobj, &v9fs_attr_group)) {
602 kobject_put(v9fs_kobj);
603 return -ENOMEM;
604 }
605
606 return 0;
607}
608
609/**
610 * v9fs_sysfs_cleanup - Unregister the v9fs sysfs interface
611 *
612 */
613
614static void v9fs_sysfs_cleanup(void)
615{
616 sysfs_remove_group(v9fs_kobj, &v9fs_attr_group);
617 kobject_put(v9fs_kobj);
618}
619
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530620static void v9fs_inode_init_once(void *foo)
621{
622 struct v9fs_inode *v9inode = (struct v9fs_inode *)foo;
623#ifdef CONFIG_9P_FSCACHE
624 v9inode->fscache = NULL;
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530625#endif
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000626 memset(&v9inode->qid, 0, sizeof(v9inode->qid));
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530627 inode_init_once(&v9inode->vfs_inode);
628}
629
630/**
631 * v9fs_init_inode_cache - initialize a cache for 9P
632 * Returns 0 on success.
633 */
634static int v9fs_init_inode_cache(void)
635{
636 v9fs_inode_cache = kmem_cache_create("v9fs_inode_cache",
637 sizeof(struct v9fs_inode),
638 0, (SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800639 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530640 v9fs_inode_init_once);
641 if (!v9fs_inode_cache)
642 return -ENOMEM;
643
644 return 0;
645}
646
647/**
648 * v9fs_destroy_inode_cache - destroy the cache of 9P inode
649 *
650 */
651static void v9fs_destroy_inode_cache(void)
652{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000653 /*
654 * Make sure all delayed rcu free inodes are flushed before we
655 * destroy cache.
656 */
657 rcu_barrier();
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530658 kmem_cache_destroy(v9fs_inode_cache);
659}
660
661static int v9fs_cache_register(void)
662{
663 int ret;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900664
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530665 ret = v9fs_init_inode_cache();
666 if (ret < 0)
667 return ret;
668#ifdef CONFIG_9P_FSCACHE
Al Viro8061a6f2013-09-17 08:10:18 -0400669 ret = fscache_register_netfs(&v9fs_cache_netfs);
670 if (ret < 0)
671 v9fs_destroy_inode_cache();
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530672#endif
Al Viro8061a6f2013-09-17 08:10:18 -0400673 return ret;
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530674}
675
676static void v9fs_cache_unregister(void)
677{
678 v9fs_destroy_inode_cache();
679#ifdef CONFIG_9P_FSCACHE
680 fscache_unregister_netfs(&v9fs_cache_netfs);
681#endif
682}
683
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500684/**
685 * init_v9fs - Initialize module
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700686 *
687 */
688
689static int __init init_v9fs(void)
690{
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500691 int err;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900692
Joe Perches5d385152011-11-28 10:40:46 -0800693 pr_info("Installing v9fs 9p2000 file system support\n");
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500694 /* TODO: Setup list of registered trasnport modules */
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500695
696 err = v9fs_cache_register();
697 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800698 pr_err("Failed to register v9fs for caching\n");
Al Viro2226a282012-03-17 02:37:05 -0400699 return err;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500700 }
701
702 err = v9fs_sysfs_init();
703 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800704 pr_err("Failed to register with sysfs\n");
Al Viro2226a282012-03-17 02:37:05 -0400705 goto out_cache;
706 }
707 err = register_filesystem(&v9fs_fs_type);
708 if (err < 0) {
709 pr_err("Failed to register filesystem\n");
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500710 goto out_sysfs_cleanup;
711 }
712
713 return 0;
714
715out_sysfs_cleanup:
716 v9fs_sysfs_cleanup();
717
Al Viro2226a282012-03-17 02:37:05 -0400718out_cache:
719 v9fs_cache_unregister();
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500720
721 return err;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700722}
723
724/**
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500725 * exit_v9fs - shutdown module
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700726 *
727 */
728
729static void __exit exit_v9fs(void)
730{
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500731 v9fs_sysfs_cleanup();
732 v9fs_cache_unregister();
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700733 unregister_filesystem(&v9fs_fs_type);
734}
735
736module_init(init_v9fs)
737module_exit(exit_v9fs)
738
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500739MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700740MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
741MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
742MODULE_LICENSE("GPL");