blob: 619128b5583702559dae61480262644e3542b3e7 [file] [log] [blame]
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -07001/*
2 * linux/fs/9p/v9fs.c
3 *
4 * This file contains functions assisting in mapping VFS to 9P2000
5 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06006 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -07007 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
8 *
9 * This program is free software; you can redistribute it and/or modify
Eric Van Hensbergen42e8c502006-03-25 03:07:28 -080010 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070012 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
23 *
24 */
25
Joe Perches5d385152011-11-28 10:40:46 -080026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070028#include <linux/module.h>
29#include <linux/errno.h>
30#include <linux/fs.h>
Al Viro914e2632006-10-18 13:55:46 -040031#include <linux/sched.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010032#include <linux/cred.h>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070033#include <linux/parser.h>
34#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
David Howellsc4fac912017-07-05 16:25:37 +010036#include <linux/seq_file.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050037#include <net/9p/9p.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050038#include <net/9p/client.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050039#include <net/9p/transport.h>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070040#include "v9fs.h"
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070041#include "v9fs_vfs.h"
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050042#include "cache.h"
43
44static DEFINE_SPINLOCK(v9fs_sessionlist_lock);
45static LIST_HEAD(v9fs_sessionlist);
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +053046struct kmem_cache *v9fs_inode_cache;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070047
48/*
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050049 * Option Parsing (code inspired by NFS code)
50 * NOTE: each transport will parse its own options
51 */
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070052
53enum {
54 /* Options that take integer arguments */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060055 Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070056 /* String options */
Fabian Frederickcb9af412015-06-05 19:09:35 +020057 Opt_uname, Opt_remotename, Opt_cache, Opt_cachetag,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070058 /* Options that take no arguments */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060059 Opt_nodevmap,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -060060 /* Cache options */
Dominique Martinetfb89b452014-01-10 13:44:09 +010061 Opt_cache_loose, Opt_fscache, Opt_mmap,
Latchesar Ionkovba176742007-10-17 14:31:07 -050062 /* Access options */
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -080063 Opt_access, Opt_posixacl,
Dinu-Razvan Chis-Serban5e172f72018-09-05 16:44:12 +090064 /* Lock timeout option */
65 Opt_locktimeout,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070066 /* Error token */
67 Opt_err
68};
69
Steven Whitehousea447c092008-10-13 10:46:57 +010070static const match_table_t tokens = {
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -050071 {Opt_debug, "debug=%x"},
Latchesar Ionkovbd32b822007-10-17 14:31:07 -050072 {Opt_dfltuid, "dfltuid=%u"},
73 {Opt_dfltgid, "dfltgid=%u"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070074 {Opt_afid, "afid=%u"},
Eric Van Hensbergen67543e52006-03-25 03:07:29 -080075 {Opt_uname, "uname=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070076 {Opt_remotename, "aname=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070077 {Opt_nodevmap, "nodevmap"},
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050078 {Opt_cache, "cache=%s"},
Eric Van Hensbergene03abc02007-02-11 13:21:39 -060079 {Opt_cache_loose, "loose"},
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050080 {Opt_fscache, "fscache"},
Dominique Martinetfb89b452014-01-10 13:44:09 +010081 {Opt_mmap, "mmap"},
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050082 {Opt_cachetag, "cachetag=%s"},
Latchesar Ionkovba176742007-10-17 14:31:07 -050083 {Opt_access, "access=%s"},
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -080084 {Opt_posixacl, "posixacl"},
Dinu-Razvan Chis-Serban5e172f72018-09-05 16:44:12 +090085 {Opt_locktimeout, "locktimeout=%u"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070086 {Opt_err, NULL}
87};
88
David Howellsc4fac912017-07-05 16:25:37 +010089static const char *const v9fs_cache_modes[nr__p9_cache_modes] = {
90 [CACHE_NONE] = "none",
91 [CACHE_MMAP] = "mmap",
92 [CACHE_LOOSE] = "loose",
93 [CACHE_FSCACHE] = "fscache",
94};
95
Prem Karata2dd43b2011-05-06 18:24:18 +053096/* Interpret mount options for cache mode */
97static int get_cache_mode(char *s)
98{
99 int version = -EINVAL;
100
101 if (!strcmp(s, "loose")) {
102 version = CACHE_LOOSE;
Joe Perches5d385152011-11-28 10:40:46 -0800103 p9_debug(P9_DEBUG_9P, "Cache mode: loose\n");
Prem Karata2dd43b2011-05-06 18:24:18 +0530104 } else if (!strcmp(s, "fscache")) {
105 version = CACHE_FSCACHE;
Joe Perches5d385152011-11-28 10:40:46 -0800106 p9_debug(P9_DEBUG_9P, "Cache mode: fscache\n");
Dominique Martinetfb89b452014-01-10 13:44:09 +0100107 } else if (!strcmp(s, "mmap")) {
108 version = CACHE_MMAP;
109 p9_debug(P9_DEBUG_9P, "Cache mode: mmap\n");
Prem Karata2dd43b2011-05-06 18:24:18 +0530110 } else if (!strcmp(s, "none")) {
111 version = CACHE_NONE;
Joe Perches5d385152011-11-28 10:40:46 -0800112 p9_debug(P9_DEBUG_9P, "Cache mode: none\n");
Prem Karata2dd43b2011-05-06 18:24:18 +0530113 } else
Joe Perches5d385152011-11-28 10:40:46 -0800114 pr_info("Unknown Cache mode %s\n", s);
Prem Karata2dd43b2011-05-06 18:24:18 +0530115 return version;
116}
117
David Howellsc4fac912017-07-05 16:25:37 +0100118/*
119 * Display the mount options in /proc/mounts.
120 */
121int v9fs_show_options(struct seq_file *m, struct dentry *root)
122{
123 struct v9fs_session_info *v9ses = root->d_sb->s_fs_info;
124
125 if (v9ses->debug)
126 seq_printf(m, ",debug=%x", v9ses->debug);
127 if (!uid_eq(v9ses->dfltuid, V9FS_DEFUID))
128 seq_printf(m, ",dfltuid=%u",
129 from_kuid_munged(&init_user_ns, v9ses->dfltuid));
130 if (!gid_eq(v9ses->dfltgid, V9FS_DEFGID))
131 seq_printf(m, ",dfltgid=%u",
132 from_kgid_munged(&init_user_ns, v9ses->dfltgid));
133 if (v9ses->afid != ~0)
134 seq_printf(m, ",afid=%u", v9ses->afid);
135 if (strcmp(v9ses->uname, V9FS_DEFUSER) != 0)
136 seq_printf(m, ",uname=%s", v9ses->uname);
137 if (strcmp(v9ses->aname, V9FS_DEFANAME) != 0)
138 seq_printf(m, ",aname=%s", v9ses->aname);
139 if (v9ses->nodev)
140 seq_puts(m, ",nodevmap");
141 if (v9ses->cache)
142 seq_printf(m, ",%s", v9fs_cache_modes[v9ses->cache]);
143#ifdef CONFIG_9P_FSCACHE
144 if (v9ses->cachetag && v9ses->cache == CACHE_FSCACHE)
145 seq_printf(m, ",cachetag=%s", v9ses->cachetag);
146#endif
147
148 switch (v9ses->flags & V9FS_ACCESS_MASK) {
149 case V9FS_ACCESS_USER:
150 seq_puts(m, ",access=user");
151 break;
152 case V9FS_ACCESS_ANY:
153 seq_puts(m, ",access=any");
154 break;
155 case V9FS_ACCESS_CLIENT:
156 seq_puts(m, ",access=client");
157 break;
158 case V9FS_ACCESS_SINGLE:
159 seq_printf(m, ",access=%u",
160 from_kuid_munged(&init_user_ns, v9ses->uid));
161 break;
162 }
163
164 if (v9ses->flags & V9FS_POSIX_ACL)
165 seq_puts(m, ",posixacl");
166
167 return p9_show_client_options(m, v9ses->clnt);
168}
169
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700170/**
171 * v9fs_parse_options - parse mount options into session structure
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700172 * @v9ses: existing v9fs session information
173 *
Jim Meyeringab312672008-03-06 17:10:28 -0600174 * Return 0 upon success, -ERRNO upon failure.
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700175 */
176
Abhishek Kulkarni4b53e4b2009-08-17 16:42:28 -0500177static int v9fs_parse_options(struct v9fs_session_info *v9ses, char *opts)
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700178{
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600179 char *options, *tmp_options;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700180 substring_t args[MAX_OPT_ARGS];
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500181 char *p;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600182 int option = 0;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500183 char *s, *e;
Jim Meyeringab312672008-03-06 17:10:28 -0600184 int ret = 0;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700185
186 /* setup defaults */
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700187 v9ses->afid = ~0;
188 v9ses->debug = 0;
Prem Karata2dd43b2011-05-06 18:24:18 +0530189 v9ses->cache = CACHE_NONE;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500190#ifdef CONFIG_9P_FSCACHE
191 v9ses->cachetag = NULL;
192#endif
Dinu-Razvan Chis-Serban5e172f72018-09-05 16:44:12 +0900193 v9ses->session_lock_timeout = P9_LOCK_TIMEOUT;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700194
Abhishek Kulkarni4b53e4b2009-08-17 16:42:28 -0500195 if (!opts)
Jim Meyeringab312672008-03-06 17:10:28 -0600196 return 0;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700197
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600198 tmp_options = kstrdup(opts, GFP_KERNEL);
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600199 if (!tmp_options) {
200 ret = -ENOMEM;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500201 goto fail_option_alloc;
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600202 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600203 options = tmp_options;
Jim Meyeringab312672008-03-06 17:10:28 -0600204
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700205 while ((p = strsep(&options, ",")) != NULL) {
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530206 int token, r;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700207 if (!*p)
208 continue;
209 token = match_token(p, tokens, args);
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530210 switch (token) {
211 case Opt_debug:
212 r = match_int(&args[0], &option);
Jim Meyeringab312672008-03-06 17:10:28 -0600213 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800214 p9_debug(P9_DEBUG_ERROR,
215 "integer field, but no integer?\n");
Jim Meyeringab312672008-03-06 17:10:28 -0600216 ret = r;
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700217 } else {
218 v9ses->debug = option;
Eric Van Hensbergen10fa16e2007-07-16 16:02:49 -0500219#ifdef CONFIG_NET_9P_DEBUG
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700220 p9_debug_level = option;
Eric Van Hensbergen10fa16e2007-07-16 16:02:49 -0500221#endif
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700222 }
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -0500223 break;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600224
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500225 case Opt_dfltuid:
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530226 r = match_int(&args[0], &option);
227 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800228 p9_debug(P9_DEBUG_ERROR,
229 "integer field, but no integer?\n");
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530230 ret = r;
231 continue;
232 }
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800233 v9ses->dfltuid = make_kuid(current_user_ns(), option);
234 if (!uid_valid(v9ses->dfltuid)) {
235 p9_debug(P9_DEBUG_ERROR,
236 "uid field, but not a uid?\n");
237 ret = -EINVAL;
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800238 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700239 break;
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500240 case Opt_dfltgid:
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530241 r = match_int(&args[0], &option);
242 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800243 p9_debug(P9_DEBUG_ERROR,
244 "integer field, but no integer?\n");
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530245 ret = r;
246 continue;
247 }
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800248 v9ses->dfltgid = make_kgid(current_user_ns(), option);
249 if (!gid_valid(v9ses->dfltgid)) {
250 p9_debug(P9_DEBUG_ERROR,
251 "gid field, but not a gid?\n");
252 ret = -EINVAL;
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800253 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700254 break;
255 case Opt_afid:
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530256 r = match_int(&args[0], &option);
257 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800258 p9_debug(P9_DEBUG_ERROR,
259 "integer field, but no integer?\n");
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530260 ret = r;
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700261 } else {
262 v9ses->afid = option;
Aneesh Kumar K.V4d5077f2011-08-30 12:19:34 +0530263 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700264 break;
Eric Van Hensbergen67543e52006-03-25 03:07:29 -0800265 case Opt_uname:
Jeff Laytone549c132012-09-07 07:55:25 -0400266 kfree(v9ses->uname);
267 v9ses->uname = match_strdup(&args[0]);
268 if (!v9ses->uname) {
269 ret = -ENOMEM;
270 goto free_and_return;
271 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700272 break;
273 case Opt_remotename:
Jeff Laytone549c132012-09-07 07:55:25 -0400274 kfree(v9ses->aname);
275 v9ses->aname = match_strdup(&args[0]);
276 if (!v9ses->aname) {
277 ret = -ENOMEM;
278 goto free_and_return;
279 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700280 break;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700281 case Opt_nodevmap:
282 v9ses->nodev = 1;
283 break;
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600284 case Opt_cache_loose:
285 v9ses->cache = CACHE_LOOSE;
286 break;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500287 case Opt_fscache:
288 v9ses->cache = CACHE_FSCACHE;
289 break;
Dominique Martinetfb89b452014-01-10 13:44:09 +0100290 case Opt_mmap:
291 v9ses->cache = CACHE_MMAP;
292 break;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500293 case Opt_cachetag:
294#ifdef CONFIG_9P_FSCACHE
David Howellsc4fac912017-07-05 16:25:37 +0100295 kfree(v9ses->cachetag);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500296 v9ses->cachetag = match_strdup(&args[0]);
Chengguang Xua25c3652018-04-05 16:19:53 -0700297 if (!v9ses->cachetag) {
298 ret = -ENOMEM;
299 goto free_and_return;
300 }
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500301#endif
302 break;
303 case Opt_cache:
304 s = match_strdup(&args[0]);
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600305 if (!s) {
306 ret = -ENOMEM;
Joe Perches5d385152011-11-28 10:40:46 -0800307 p9_debug(P9_DEBUG_ERROR,
308 "problem allocating copy of cache arg\n");
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600309 goto free_and_return;
310 }
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700311 r = get_cache_mode(s);
312 if (r < 0)
313 ret = r;
314 else
315 v9ses->cache = r;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500316
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500317 kfree(s);
318 break;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500319
320 case Opt_access:
321 s = match_strdup(&args[0]);
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600322 if (!s) {
323 ret = -ENOMEM;
Joe Perches5d385152011-11-28 10:40:46 -0800324 p9_debug(P9_DEBUG_ERROR,
325 "problem allocating copy of access arg\n");
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600326 goto free_and_return;
327 }
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500328
Latchesar Ionkovba176742007-10-17 14:31:07 -0500329 v9ses->flags &= ~V9FS_ACCESS_MASK;
330 if (strcmp(s, "user") == 0)
331 v9ses->flags |= V9FS_ACCESS_USER;
332 else if (strcmp(s, "any") == 0)
333 v9ses->flags |= V9FS_ACCESS_ANY;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530334 else if (strcmp(s, "client") == 0) {
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530335 v9ses->flags |= V9FS_ACCESS_CLIENT;
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530336 } else {
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800337 uid_t uid;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500338 v9ses->flags |= V9FS_ACCESS_SINGLE;
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800339 uid = simple_strtoul(s, &e, 10);
Prem Karata2dd43b2011-05-06 18:24:18 +0530340 if (*e != '\0') {
341 ret = -EINVAL;
Joe Perches5d385152011-11-28 10:40:46 -0800342 pr_info("Unknown access argument %s\n",
343 s);
Prem Karata2dd43b2011-05-06 18:24:18 +0530344 kfree(s);
Chengguang Xu478ae0c2018-06-07 17:05:07 -0700345 continue;
Prem Karata2dd43b2011-05-06 18:24:18 +0530346 }
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800347 v9ses->uid = make_kuid(current_user_ns(), uid);
348 if (!uid_valid(v9ses->uid)) {
349 ret = -EINVAL;
Colin Ian King6baaac02018-07-17 19:14:49 -0700350 pr_info("Unknown uid %s\n", s);
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800351 }
Latchesar Ionkovba176742007-10-17 14:31:07 -0500352 }
Prem Karata2dd43b2011-05-06 18:24:18 +0530353
Adrian Bunk0a976292007-10-23 13:48:50 -0500354 kfree(s);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500355 break;
356
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800357 case Opt_posixacl:
358#ifdef CONFIG_9P_FS_POSIX_ACL
359 v9ses->flags |= V9FS_POSIX_ACL;
360#else
Joe Perches5d385152011-11-28 10:40:46 -0800361 p9_debug(P9_DEBUG_ERROR,
362 "Not defined CONFIG_9P_FS_POSIX_ACL. Ignoring posixacl option\n");
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800363#endif
364 break;
365
Dinu-Razvan Chis-Serban5e172f72018-09-05 16:44:12 +0900366 case Opt_locktimeout:
367 r = match_int(&args[0], &option);
368 if (r < 0) {
369 p9_debug(P9_DEBUG_ERROR,
370 "integer field, but no integer?\n");
371 ret = r;
372 continue;
373 }
374 if (option < 1) {
375 p9_debug(P9_DEBUG_ERROR,
376 "locktimeout must be a greater than zero integer.\n");
377 ret = -EINVAL;
378 continue;
379 }
380 v9ses->session_lock_timeout = (long)option * HZ;
381 break;
382
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700383 default:
384 continue;
385 }
386 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600387
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600388free_and_return:
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600389 kfree(tmp_options);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500390fail_option_alloc:
Eric Van Hensbergenbf2d29c2010-02-08 17:59:34 -0600391 return ret;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700392}
393
394/**
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700395 * v9fs_session_init - initialize session
396 * @v9ses: session information structure
397 * @dev_name: device being mounted
398 * @data: options
399 *
400 */
401
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500402struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700403 const char *dev_name, char *data)
404{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500405 struct p9_fid *fid;
Tejun Heo412a19b2015-06-08 14:57:31 +0900406 int rc = -ENOMEM;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700407
Jeff Laytone549c132012-09-07 07:55:25 -0400408 v9ses->uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500409 if (!v9ses->uname)
Tejun Heo412a19b2015-06-08 14:57:31 +0900410 goto err_names;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700411
Jeff Laytone549c132012-09-07 07:55:25 -0400412 v9ses->aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL);
Tejun Heo412a19b2015-06-08 14:57:31 +0900413 if (!v9ses->aname)
414 goto err_names;
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530415 init_rwsem(&v9ses->rename_sem);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700416
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800417 v9ses->uid = INVALID_UID;
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500418 v9ses->dfltuid = V9FS_DEFUID;
419 v9ses->dfltgid = V9FS_DEFGID;
Jim Meyeringab312672008-03-06 17:10:28 -0600420
Abhishek Kulkarni4b53e4b2009-08-17 16:42:28 -0500421 v9ses->clnt = p9_client_create(dev_name, data);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500422 if (IS_ERR(v9ses->clnt)) {
Tejun Heo412a19b2015-06-08 14:57:31 +0900423 rc = PTR_ERR(v9ses->clnt);
Joe Perches5d385152011-11-28 10:40:46 -0800424 p9_debug(P9_DEBUG_ERROR, "problem initializing 9p client\n");
Jan Kara71304fe2017-04-12 12:24:31 +0200425 goto err_names;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700426 }
427
Venkateswararao Jujjuri (JV)6752a1e2011-01-26 16:20:35 -0800428 v9ses->flags = V9FS_ACCESS_USER;
429
430 if (p9_is_proto_dotl(v9ses->clnt)) {
431 v9ses->flags = V9FS_ACCESS_CLIENT;
Sripathi Kodi476ada02010-03-17 17:02:38 +0000432 v9ses->flags |= V9FS_PROTO_2000L;
Venkateswararao Jujjuri (JV)6752a1e2011-01-26 16:20:35 -0800433 } else if (p9_is_proto_dotu(v9ses->clnt)) {
Sripathi Kodi476ada02010-03-17 17:02:38 +0000434 v9ses->flags |= V9FS_PROTO_2000U;
Venkateswararao Jujjuri (JV)6752a1e2011-01-26 16:20:35 -0800435 }
436
437 rc = v9fs_parse_options(v9ses, data);
Tejun Heo412a19b2015-06-08 14:57:31 +0900438 if (rc < 0)
439 goto err_clnt;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500440
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500441 v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600442
Aneesh Kumar K.V76381a42010-09-28 00:27:41 +0530443 if (!v9fs_proto_dotl(v9ses) &&
444 ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
445 /*
446 * We support ACCESS_CLIENT only for dotl.
447 * Fall back to ACCESS_USER
448 */
449 v9ses->flags &= ~V9FS_ACCESS_MASK;
450 v9ses->flags |= V9FS_ACCESS_USER;
451 }
452 /*FIXME !! */
Latchesar Ionkovba176742007-10-17 14:31:07 -0500453 /* for legacy mode, fall back to V9FS_ACCESS_ANY */
Aneesh Kumar K.V9ffaf632010-06-01 09:26:18 +0000454 if (!(v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses)) &&
Latchesar Ionkovba176742007-10-17 14:31:07 -0500455 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
456
457 v9ses->flags &= ~V9FS_ACCESS_MASK;
458 v9ses->flags |= V9FS_ACCESS_ANY;
Eric W. Biederman76ed23a2013-01-30 11:57:40 -0800459 v9ses->uid = INVALID_UID;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500460 }
Venkateswararao Jujjuri (JV)e782ef72011-01-25 15:40:54 -0800461 if (!v9fs_proto_dotl(v9ses) ||
462 !((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
463 /*
464 * We support ACL checks on clinet only if the protocol is
465 * 9P2000.L and access is V9FS_ACCESS_CLIENT.
466 */
467 v9ses->flags &= ~V9FS_ACL_MASK;
468 }
Latchesar Ionkovba176742007-10-17 14:31:07 -0500469
Eric W. Biedermanf791f7c2013-01-29 16:09:41 -0800470 fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, INVALID_UID,
Latchesar Ionkovba176742007-10-17 14:31:07 -0500471 v9ses->aname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500472 if (IS_ERR(fid)) {
Tejun Heo412a19b2015-06-08 14:57:31 +0900473 rc = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800474 p9_debug(P9_DEBUG_ERROR, "cannot attach\n");
Tejun Heo412a19b2015-06-08 14:57:31 +0900475 goto err_clnt;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700476 }
477
Latchesar Ionkovba176742007-10-17 14:31:07 -0500478 if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
479 fid->uid = v9ses->uid;
480 else
Eric W. Biedermanb4642552013-01-30 11:48:53 -0800481 fid->uid = INVALID_UID;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500482
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500483#ifdef CONFIG_9P_FSCACHE
484 /* register the session for caching */
485 v9fs_cache_session_get_cookie(v9ses);
486#endif
Tejun Heo412a19b2015-06-08 14:57:31 +0900487 spin_lock(&v9fs_sessionlist_lock);
488 list_add(&v9ses->slist, &v9fs_sessionlist);
489 spin_unlock(&v9fs_sessionlist_lock);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500490
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500491 return fid;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700492
Tejun Heo412a19b2015-06-08 14:57:31 +0900493err_clnt:
Chengguang Xua25c3652018-04-05 16:19:53 -0700494#ifdef CONFIG_9P_FSCACHE
495 kfree(v9ses->cachetag);
496#endif
Tejun Heo412a19b2015-06-08 14:57:31 +0900497 p9_client_destroy(v9ses->clnt);
Tejun Heo412a19b2015-06-08 14:57:31 +0900498err_names:
499 kfree(v9ses->uname);
500 kfree(v9ses->aname);
501 return ERR_PTR(rc);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700502}
503
504/**
505 * v9fs_session_close - shutdown a session
506 * @v9ses: session information structure
507 *
508 */
509
510void v9fs_session_close(struct v9fs_session_info *v9ses)
511{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500512 if (v9ses->clnt) {
513 p9_client_destroy(v9ses->clnt);
514 v9ses->clnt = NULL;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800515 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700516
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500517#ifdef CONFIG_9P_FSCACHE
518 if (v9ses->fscache) {
519 v9fs_cache_session_put_cookie(v9ses);
520 kfree(v9ses->cachetag);
521 }
522#endif
Jeff Laytone549c132012-09-07 07:55:25 -0400523 kfree(v9ses->uname);
524 kfree(v9ses->aname);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500525
526 spin_lock(&v9fs_sessionlist_lock);
527 list_del(&v9ses->slist);
528 spin_unlock(&v9fs_sessionlist_lock);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700529}
530
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700531/**
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600532 * v9fs_session_cancel - terminate a session
533 * @v9ses: session to terminate
534 *
535 * mark transport as disconnected and cancel all pending requests.
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700536 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600537
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700538void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
Joe Perches5d385152011-11-28 10:40:46 -0800539 p9_debug(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500540 p9_client_disconnect(v9ses->clnt);
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700541}
542
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -0500543/**
544 * v9fs_session_begin_cancel - Begin terminate of a session
545 * @v9ses: session to terminate
546 *
547 * After this call we don't allow any request other than clunk.
548 */
549
550void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses)
551{
Joe Perches5d385152011-11-28 10:40:46 -0800552 p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses);
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -0500553 p9_client_begin_disconnect(v9ses->clnt);
554}
555
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700556extern int v9fs_error_init(void);
557
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500558static struct kobject *v9fs_kobj;
559
560#ifdef CONFIG_9P_FSCACHE
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700561/**
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500562 * caches_show - list caches associated with a session
563 *
564 * Returns the size of buffer written.
565 */
566
567static ssize_t caches_show(struct kobject *kobj,
568 struct kobj_attribute *attr,
569 char *buf)
570{
571 ssize_t n = 0, count = 0, limit = PAGE_SIZE;
572 struct v9fs_session_info *v9ses;
573
574 spin_lock(&v9fs_sessionlist_lock);
575 list_for_each_entry(v9ses, &v9fs_sessionlist, slist) {
576 if (v9ses->cachetag) {
577 n = snprintf(buf, limit, "%s\n", v9ses->cachetag);
578 if (n < 0) {
579 count = n;
580 break;
581 }
582
583 count += n;
584 limit -= n;
585 }
586 }
587
588 spin_unlock(&v9fs_sessionlist_lock);
589 return count;
590}
591
592static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches);
593#endif /* CONFIG_9P_FSCACHE */
594
595static struct attribute *v9fs_attrs[] = {
596#ifdef CONFIG_9P_FSCACHE
597 &v9fs_attr_cache.attr,
598#endif
599 NULL,
600};
601
602static struct attribute_group v9fs_attr_group = {
603 .attrs = v9fs_attrs,
604};
605
606/**
607 * v9fs_sysfs_init - Initialize the v9fs sysfs interface
608 *
609 */
610
Fabian Frederickbdbeacd2014-06-04 16:06:25 -0700611static int __init v9fs_sysfs_init(void)
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500612{
613 v9fs_kobj = kobject_create_and_add("9p", fs_kobj);
614 if (!v9fs_kobj)
615 return -ENOMEM;
616
617 if (sysfs_create_group(v9fs_kobj, &v9fs_attr_group)) {
618 kobject_put(v9fs_kobj);
619 return -ENOMEM;
620 }
621
622 return 0;
623}
624
625/**
626 * v9fs_sysfs_cleanup - Unregister the v9fs sysfs interface
627 *
628 */
629
630static void v9fs_sysfs_cleanup(void)
631{
632 sysfs_remove_group(v9fs_kobj, &v9fs_attr_group);
633 kobject_put(v9fs_kobj);
634}
635
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530636static void v9fs_inode_init_once(void *foo)
637{
638 struct v9fs_inode *v9inode = (struct v9fs_inode *)foo;
639#ifdef CONFIG_9P_FSCACHE
640 v9inode->fscache = NULL;
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530641#endif
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000642 memset(&v9inode->qid, 0, sizeof(v9inode->qid));
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530643 inode_init_once(&v9inode->vfs_inode);
644}
645
646/**
647 * v9fs_init_inode_cache - initialize a cache for 9P
648 * Returns 0 on success.
649 */
650static int v9fs_init_inode_cache(void)
651{
652 v9fs_inode_cache = kmem_cache_create("v9fs_inode_cache",
653 sizeof(struct v9fs_inode),
654 0, (SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800655 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530656 v9fs_inode_init_once);
657 if (!v9fs_inode_cache)
658 return -ENOMEM;
659
660 return 0;
661}
662
663/**
664 * v9fs_destroy_inode_cache - destroy the cache of 9P inode
665 *
666 */
667static void v9fs_destroy_inode_cache(void)
668{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000669 /*
670 * Make sure all delayed rcu free inodes are flushed before we
671 * destroy cache.
672 */
673 rcu_barrier();
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530674 kmem_cache_destroy(v9fs_inode_cache);
675}
676
677static int v9fs_cache_register(void)
678{
679 int ret;
680 ret = v9fs_init_inode_cache();
681 if (ret < 0)
682 return ret;
683#ifdef CONFIG_9P_FSCACHE
Al Viro8061a6f2013-09-17 08:10:18 -0400684 ret = fscache_register_netfs(&v9fs_cache_netfs);
685 if (ret < 0)
686 v9fs_destroy_inode_cache();
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530687#endif
Al Viro8061a6f2013-09-17 08:10:18 -0400688 return ret;
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530689}
690
691static void v9fs_cache_unregister(void)
692{
693 v9fs_destroy_inode_cache();
694#ifdef CONFIG_9P_FSCACHE
695 fscache_unregister_netfs(&v9fs_cache_netfs);
696#endif
697}
698
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500699/**
700 * init_v9fs - Initialize module
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700701 *
702 */
703
704static int __init init_v9fs(void)
705{
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500706 int err;
Joe Perches5d385152011-11-28 10:40:46 -0800707 pr_info("Installing v9fs 9p2000 file system support\n");
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500708 /* TODO: Setup list of registered trasnport modules */
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500709
710 err = v9fs_cache_register();
711 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800712 pr_err("Failed to register v9fs for caching\n");
Al Viro2226a282012-03-17 02:37:05 -0400713 return err;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500714 }
715
716 err = v9fs_sysfs_init();
717 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800718 pr_err("Failed to register with sysfs\n");
Al Viro2226a282012-03-17 02:37:05 -0400719 goto out_cache;
720 }
721 err = register_filesystem(&v9fs_fs_type);
722 if (err < 0) {
723 pr_err("Failed to register filesystem\n");
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500724 goto out_sysfs_cleanup;
725 }
726
727 return 0;
728
729out_sysfs_cleanup:
730 v9fs_sysfs_cleanup();
731
Al Viro2226a282012-03-17 02:37:05 -0400732out_cache:
733 v9fs_cache_unregister();
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500734
735 return err;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700736}
737
738/**
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500739 * exit_v9fs - shutdown module
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700740 *
741 */
742
743static void __exit exit_v9fs(void)
744{
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500745 v9fs_sysfs_cleanup();
746 v9fs_cache_unregister();
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700747 unregister_filesystem(&v9fs_fs_type);
748}
749
750module_init(init_v9fs)
751module_exit(exit_v9fs)
752
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500753MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700754MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
755MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
756MODULE_LICENSE("GPL");