]>
Commit | Line | Data |
---|---|---|
1f327613 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
9e82cf6a | 2 | /* |
9e82cf6a EVH |
3 | * This file contains functions assisting in mapping VFS to 9P2000 |
4 | * | |
8a0dc95f | 5 | * Copyright (C) 2004-2008 by Eric Van Hensbergen <[email protected]> |
9e82cf6a | 6 | * Copyright (C) 2002 by Ron Minnich <[email protected]> |
9e82cf6a EVH |
7 | */ |
8 | ||
5d385153 JP |
9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
10 | ||
9e82cf6a EVH |
11 | #include <linux/module.h> |
12 | #include <linux/errno.h> | |
13 | #include <linux/fs.h> | |
914e2637 | 14 | #include <linux/sched.h> |
5b825c3a | 15 | #include <linux/cred.h> |
9e82cf6a EVH |
16 | #include <linux/parser.h> |
17 | #include <linux/idr.h> | |
5a0e3ad6 | 18 | #include <linux/slab.h> |
c4fac910 | 19 | #include <linux/seq_file.h> |
bd238fb4 | 20 | #include <net/9p/9p.h> |
bd238fb4 | 21 | #include <net/9p/client.h> |
8b81ef58 | 22 | #include <net/9p/transport.h> |
9e82cf6a | 23 | #include "v9fs.h" |
9e82cf6a | 24 | #include "v9fs_vfs.h" |
60e78d2c AK |
25 | #include "cache.h" |
26 | ||
27 | static DEFINE_SPINLOCK(v9fs_sessionlist_lock); | |
28 | static LIST_HEAD(v9fs_sessionlist); | |
a78ce05d | 29 | struct kmem_cache *v9fs_inode_cache; |
9e82cf6a EVH |
30 | |
31 | /* | |
60e78d2c AK |
32 | * Option Parsing (code inspired by NFS code) |
33 | * NOTE: each transport will parse its own options | |
34 | */ | |
9e82cf6a EVH |
35 | |
36 | enum { | |
37 | /* Options that take integer arguments */ | |
8a0dc95f | 38 | Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid, |
9e82cf6a | 39 | /* String options */ |
cb9af418 | 40 | Opt_uname, Opt_remotename, Opt_cache, Opt_cachetag, |
9e82cf6a | 41 | /* Options that take no arguments */ |
8a0dc95f | 42 | Opt_nodevmap, |
e03abc0c | 43 | /* Cache options */ |
fb89b45c | 44 | Opt_cache_loose, Opt_fscache, Opt_mmap, |
ba17674f | 45 | /* Access options */ |
e782ef71 | 46 | Opt_access, Opt_posixacl, |
5e172f75 DRCS |
47 | /* Lock timeout option */ |
48 | Opt_locktimeout, | |
9e82cf6a EVH |
49 | /* Error token */ |
50 | Opt_err | |
51 | }; | |
52 | ||
a447c093 | 53 | static const match_table_t tokens = { |
9e2f6688 | 54 | {Opt_debug, "debug=%x"}, |
bd32b82d LI |
55 | {Opt_dfltuid, "dfltuid=%u"}, |
56 | {Opt_dfltgid, "dfltgid=%u"}, | |
9e82cf6a | 57 | {Opt_afid, "afid=%u"}, |
67543e50 | 58 | {Opt_uname, "uname=%s"}, |
9e82cf6a | 59 | {Opt_remotename, "aname=%s"}, |
9e82cf6a | 60 | {Opt_nodevmap, "nodevmap"}, |
60e78d2c | 61 | {Opt_cache, "cache=%s"}, |
e03abc0c | 62 | {Opt_cache_loose, "loose"}, |
60e78d2c | 63 | {Opt_fscache, "fscache"}, |
fb89b45c | 64 | {Opt_mmap, "mmap"}, |
60e78d2c | 65 | {Opt_cachetag, "cachetag=%s"}, |
ba17674f | 66 | {Opt_access, "access=%s"}, |
e782ef71 | 67 | {Opt_posixacl, "posixacl"}, |
5e172f75 | 68 | {Opt_locktimeout, "locktimeout=%u"}, |
9e82cf6a EVH |
69 | {Opt_err, NULL} |
70 | }; | |
71 | ||
c4fac910 DH |
72 | static 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 | ||
a2dd43bb PK |
79 | /* Interpret mount options for cache mode */ |
80 | static int get_cache_mode(char *s) | |
81 | { | |
82 | int version = -EINVAL; | |
83 | ||
84 | if (!strcmp(s, "loose")) { | |
85 | version = CACHE_LOOSE; | |
5d385153 | 86 | p9_debug(P9_DEBUG_9P, "Cache mode: loose\n"); |
a2dd43bb PK |
87 | } else if (!strcmp(s, "fscache")) { |
88 | version = CACHE_FSCACHE; | |
5d385153 | 89 | p9_debug(P9_DEBUG_9P, "Cache mode: fscache\n"); |
fb89b45c DM |
90 | } else if (!strcmp(s, "mmap")) { |
91 | version = CACHE_MMAP; | |
92 | p9_debug(P9_DEBUG_9P, "Cache mode: mmap\n"); | |
a2dd43bb PK |
93 | } else if (!strcmp(s, "none")) { |
94 | version = CACHE_NONE; | |
5d385153 | 95 | p9_debug(P9_DEBUG_9P, "Cache mode: none\n"); |
a2dd43bb | 96 | } else |
5d385153 | 97 | pr_info("Unknown Cache mode %s\n", s); |
a2dd43bb PK |
98 | return version; |
99 | } | |
100 | ||
c4fac910 DH |
101 | /* |
102 | * Display the mount options in /proc/mounts. | |
103 | */ | |
104 | int 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 | ||
9e82cf6a EVH |
153 | /** |
154 | * v9fs_parse_options - parse mount options into session structure | |
9e82cf6a | 155 | * @v9ses: existing v9fs session information |
bc868036 | 156 | * @opts: The mount option string |
9e82cf6a | 157 | * |
ab31267d | 158 | * Return 0 upon success, -ERRNO upon failure. |
9e82cf6a EVH |
159 | */ |
160 | ||
4b53e4b5 | 161 | static int v9fs_parse_options(struct v9fs_session_info *v9ses, char *opts) |
9e82cf6a | 162 | { |
d8c8a9e3 | 163 | char *options, *tmp_options; |
9e82cf6a | 164 | substring_t args[MAX_OPT_ARGS]; |
a80d923e | 165 | char *p; |
8a0dc95f | 166 | int option = 0; |
10c69a0d | 167 | char *s; |
ab31267d | 168 | int ret = 0; |
9e82cf6a EVH |
169 | |
170 | /* setup defaults */ | |
9e82cf6a EVH |
171 | v9ses->afid = ~0; |
172 | v9ses->debug = 0; | |
a2dd43bb | 173 | v9ses->cache = CACHE_NONE; |
60e78d2c AK |
174 | #ifdef CONFIG_9P_FSCACHE |
175 | v9ses->cachetag = NULL; | |
176 | #endif | |
5e172f75 | 177 | v9ses->session_lock_timeout = P9_LOCK_TIMEOUT; |
9e82cf6a | 178 | |
4b53e4b5 | 179 | if (!opts) |
ab31267d | 180 | return 0; |
9e82cf6a | 181 | |
d8c8a9e3 | 182 | tmp_options = kstrdup(opts, GFP_KERNEL); |
bf2d29c6 EVH |
183 | if (!tmp_options) { |
184 | ret = -ENOMEM; | |
60e78d2c | 185 | goto fail_option_alloc; |
bf2d29c6 | 186 | } |
d8c8a9e3 | 187 | options = tmp_options; |
ab31267d | 188 | |
9e82cf6a | 189 | while ((p = strsep(&options, ",")) != NULL) { |
4d5077f1 | 190 | int token, r; |
6e195b0f | 191 | |
9e82cf6a EVH |
192 | if (!*p) |
193 | continue; | |
6e195b0f | 194 | |
9e82cf6a | 195 | token = match_token(p, tokens, args); |
4d5077f1 AK |
196 | switch (token) { |
197 | case Opt_debug: | |
198 | r = match_int(&args[0], &option); | |
ab31267d | 199 | if (r < 0) { |
5d385153 JP |
200 | p9_debug(P9_DEBUG_ERROR, |
201 | "integer field, but no integer?\n"); | |
ab31267d | 202 | ret = r; |
478ae0ca CX |
203 | } else { |
204 | v9ses->debug = option; | |
10fa16e7 | 205 | #ifdef CONFIG_NET_9P_DEBUG |
478ae0ca | 206 | p9_debug_level = option; |
10fa16e7 | 207 | #endif |
478ae0ca | 208 | } |
9e2f6688 | 209 | break; |
8a0dc95f | 210 | |
bd32b82d | 211 | case Opt_dfltuid: |
4d5077f1 AK |
212 | r = match_int(&args[0], &option); |
213 | if (r < 0) { | |
5d385153 JP |
214 | p9_debug(P9_DEBUG_ERROR, |
215 | "integer field, but no integer?\n"); | |
4d5077f1 AK |
216 | ret = r; |
217 | continue; | |
218 | } | |
76ed23a5 EB |
219 | 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; | |
76ed23a5 | 224 | } |
9e82cf6a | 225 | break; |
bd32b82d | 226 | case Opt_dfltgid: |
4d5077f1 AK |
227 | r = match_int(&args[0], &option); |
228 | if (r < 0) { | |
5d385153 JP |
229 | p9_debug(P9_DEBUG_ERROR, |
230 | "integer field, but no integer?\n"); | |
4d5077f1 AK |
231 | ret = r; |
232 | continue; | |
233 | } | |
76ed23a5 EB |
234 | 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; | |
76ed23a5 | 239 | } |
9e82cf6a EVH |
240 | break; |
241 | case Opt_afid: | |
4d5077f1 AK |
242 | r = match_int(&args[0], &option); |
243 | if (r < 0) { | |
5d385153 JP |
244 | p9_debug(P9_DEBUG_ERROR, |
245 | "integer field, but no integer?\n"); | |
4d5077f1 | 246 | ret = r; |
478ae0ca CX |
247 | } else { |
248 | v9ses->afid = option; | |
4d5077f1 | 249 | } |
9e82cf6a | 250 | break; |
67543e50 | 251 | case Opt_uname: |
e549c133 JL |
252 | kfree(v9ses->uname); |
253 | v9ses->uname = match_strdup(&args[0]); | |
254 | if (!v9ses->uname) { | |
255 | ret = -ENOMEM; | |
256 | goto free_and_return; | |
257 | } | |
9e82cf6a EVH |
258 | break; |
259 | case Opt_remotename: | |
e549c133 JL |
260 | kfree(v9ses->aname); |
261 | v9ses->aname = match_strdup(&args[0]); | |
262 | if (!v9ses->aname) { | |
263 | ret = -ENOMEM; | |
264 | goto free_and_return; | |
265 | } | |
9e82cf6a | 266 | break; |
9e82cf6a EVH |
267 | case Opt_nodevmap: |
268 | v9ses->nodev = 1; | |
269 | break; | |
e03abc0c EVH |
270 | case Opt_cache_loose: |
271 | v9ses->cache = CACHE_LOOSE; | |
272 | break; | |
60e78d2c AK |
273 | case Opt_fscache: |
274 | v9ses->cache = CACHE_FSCACHE; | |
275 | break; | |
fb89b45c DM |
276 | case Opt_mmap: |
277 | v9ses->cache = CACHE_MMAP; | |
278 | break; | |
60e78d2c AK |
279 | case Opt_cachetag: |
280 | #ifdef CONFIG_9P_FSCACHE | |
c4fac910 | 281 | kfree(v9ses->cachetag); |
60e78d2c | 282 | v9ses->cachetag = match_strdup(&args[0]); |
a25c3657 CX |
283 | if (!v9ses->cachetag) { |
284 | ret = -ENOMEM; | |
285 | goto free_and_return; | |
286 | } | |
60e78d2c AK |
287 | #endif |
288 | break; | |
289 | case Opt_cache: | |
290 | s = match_strdup(&args[0]); | |
bf2d29c6 EVH |
291 | if (!s) { |
292 | ret = -ENOMEM; | |
5d385153 JP |
293 | p9_debug(P9_DEBUG_ERROR, |
294 | "problem allocating copy of cache arg\n"); | |
bf2d29c6 EVH |
295 | goto free_and_return; |
296 | } | |
478ae0ca CX |
297 | r = get_cache_mode(s); |
298 | if (r < 0) | |
299 | ret = r; | |
300 | else | |
301 | v9ses->cache = r; | |
60e78d2c | 302 | |
60e78d2c AK |
303 | kfree(s); |
304 | break; | |
ba17674f LI |
305 | |
306 | case Opt_access: | |
307 | s = match_strdup(&args[0]); | |
bf2d29c6 EVH |
308 | if (!s) { |
309 | ret = -ENOMEM; | |
5d385153 JP |
310 | p9_debug(P9_DEBUG_ERROR, |
311 | "problem allocating copy of access arg\n"); | |
bf2d29c6 EVH |
312 | goto free_and_return; |
313 | } | |
60e78d2c | 314 | |
ba17674f LI |
315 | 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; | |
76381a42 | 320 | else if (strcmp(s, "client") == 0) { |
76381a42 | 321 | v9ses->flags |= V9FS_ACCESS_CLIENT; |
76381a42 | 322 | } else { |
76ed23a5 | 323 | uid_t uid; |
10c69a0d | 324 | |
ba17674f | 325 | v9ses->flags |= V9FS_ACCESS_SINGLE; |
10c69a0d DM |
326 | r = kstrtouint(s, 10, &uid); |
327 | if (r) { | |
328 | ret = r; | |
329 | pr_info("Unknown access argument %s: %d\n", | |
330 | s, r); | |
a2dd43bb | 331 | kfree(s); |
478ae0ca | 332 | continue; |
a2dd43bb | 333 | } |
76ed23a5 EB |
334 | v9ses->uid = make_kuid(current_user_ns(), uid); |
335 | if (!uid_valid(v9ses->uid)) { | |
336 | ret = -EINVAL; | |
6baaac09 | 337 | pr_info("Unknown uid %s\n", s); |
76ed23a5 | 338 | } |
ba17674f | 339 | } |
a2dd43bb | 340 | |
0a976297 | 341 | kfree(s); |
ba17674f LI |
342 | break; |
343 | ||
e782ef71 VJJ |
344 | case Opt_posixacl: |
345 | #ifdef CONFIG_9P_FS_POSIX_ACL | |
346 | v9ses->flags |= V9FS_POSIX_ACL; | |
347 | #else | |
5d385153 JP |
348 | p9_debug(P9_DEBUG_ERROR, |
349 | "Not defined CONFIG_9P_FS_POSIX_ACL. Ignoring posixacl option\n"); | |
e782ef71 VJJ |
350 | #endif |
351 | break; | |
352 | ||
5e172f75 DRCS |
353 | 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 | ||
9e82cf6a EVH |
370 | default: |
371 | continue; | |
372 | } | |
373 | } | |
d8c8a9e3 | 374 | |
bf2d29c6 | 375 | free_and_return: |
d8c8a9e3 | 376 | kfree(tmp_options); |
60e78d2c | 377 | fail_option_alloc: |
bf2d29c6 | 378 | return ret; |
9e82cf6a EVH |
379 | } |
380 | ||
9e82cf6a EVH |
381 | /** |
382 | * v9fs_session_init - initialize session | |
383 | * @v9ses: session information structure | |
384 | * @dev_name: device being mounted | |
385 | * @data: options | |
386 | * | |
387 | */ | |
388 | ||
bd238fb4 | 389 | struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses, |
9e82cf6a EVH |
390 | const char *dev_name, char *data) |
391 | { | |
bd238fb4 | 392 | struct p9_fid *fid; |
412a19b6 | 393 | int rc = -ENOMEM; |
9e82cf6a | 394 | |
e549c133 | 395 | v9ses->uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL); |
ba17674f | 396 | if (!v9ses->uname) |
412a19b6 | 397 | goto err_names; |
9e82cf6a | 398 | |
e549c133 | 399 | v9ses->aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL); |
412a19b6 TH |
400 | if (!v9ses->aname) |
401 | goto err_names; | |
a534c8d1 | 402 | init_rwsem(&v9ses->rename_sem); |
9e82cf6a | 403 | |
76ed23a5 | 404 | v9ses->uid = INVALID_UID; |
bd32b82d LI |
405 | v9ses->dfltuid = V9FS_DEFUID; |
406 | v9ses->dfltgid = V9FS_DEFGID; | |
ab31267d | 407 | |
4b53e4b5 | 408 | v9ses->clnt = p9_client_create(dev_name, data); |
bd238fb4 | 409 | if (IS_ERR(v9ses->clnt)) { |
412a19b6 | 410 | rc = PTR_ERR(v9ses->clnt); |
5d385153 | 411 | p9_debug(P9_DEBUG_ERROR, "problem initializing 9p client\n"); |
71304feb | 412 | goto err_names; |
9e82cf6a EVH |
413 | } |
414 | ||
6752a1eb VJJ |
415 | v9ses->flags = V9FS_ACCESS_USER; |
416 | ||
417 | if (p9_is_proto_dotl(v9ses->clnt)) { | |
418 | v9ses->flags = V9FS_ACCESS_CLIENT; | |
476ada04 | 419 | v9ses->flags |= V9FS_PROTO_2000L; |
6752a1eb | 420 | } else if (p9_is_proto_dotu(v9ses->clnt)) { |
476ada04 | 421 | v9ses->flags |= V9FS_PROTO_2000U; |
6752a1eb VJJ |
422 | } |
423 | ||
424 | rc = v9fs_parse_options(v9ses, data); | |
412a19b6 TH |
425 | if (rc < 0) |
426 | goto err_clnt; | |
ba17674f | 427 | |
fbedadc1 | 428 | v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ; |
8a0dc95f | 429 | |
76381a42 AK |
430 | 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 !! */ | |
ba17674f | 440 | /* for legacy mode, fall back to V9FS_ACCESS_ANY */ |
9ffaf63e | 441 | if (!(v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses)) && |
ba17674f LI |
442 | ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) { |
443 | ||
444 | v9ses->flags &= ~V9FS_ACCESS_MASK; | |
445 | v9ses->flags |= V9FS_ACCESS_ANY; | |
76ed23a5 | 446 | v9ses->uid = INVALID_UID; |
ba17674f | 447 | } |
e782ef71 VJJ |
448 | 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 | } | |
ba17674f | 456 | |
f791f7c5 | 457 | fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, INVALID_UID, |
ba17674f | 458 | v9ses->aname); |
bd238fb4 | 459 | if (IS_ERR(fid)) { |
412a19b6 | 460 | rc = PTR_ERR(fid); |
5d385153 | 461 | p9_debug(P9_DEBUG_ERROR, "cannot attach\n"); |
412a19b6 | 462 | goto err_clnt; |
9e82cf6a EVH |
463 | } |
464 | ||
ba17674f LI |
465 | if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE) |
466 | fid->uid = v9ses->uid; | |
467 | else | |
b4642556 | 468 | fid->uid = INVALID_UID; |
ba17674f | 469 | |
60e78d2c AK |
470 | #ifdef CONFIG_9P_FSCACHE |
471 | /* register the session for caching */ | |
24e42e32 DH |
472 | if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) { |
473 | rc = v9fs_cache_session_get_cookie(v9ses, dev_name); | |
474 | if (rc < 0) | |
475 | goto err_clnt; | |
476 | } | |
60e78d2c | 477 | #endif |
412a19b6 TH |
478 | spin_lock(&v9fs_sessionlist_lock); |
479 | list_add(&v9ses->slist, &v9fs_sessionlist); | |
480 | spin_unlock(&v9fs_sessionlist_lock); | |
60e78d2c | 481 | |
bd238fb4 | 482 | return fid; |
9e82cf6a | 483 | |
412a19b6 | 484 | err_clnt: |
a25c3657 CX |
485 | #ifdef CONFIG_9P_FSCACHE |
486 | kfree(v9ses->cachetag); | |
487 | #endif | |
412a19b6 | 488 | p9_client_destroy(v9ses->clnt); |
412a19b6 TH |
489 | err_names: |
490 | kfree(v9ses->uname); | |
491 | kfree(v9ses->aname); | |
492 | return ERR_PTR(rc); | |
9e82cf6a EVH |
493 | } |
494 | ||
495 | /** | |
496 | * v9fs_session_close - shutdown a session | |
497 | * @v9ses: session information structure | |
498 | * | |
499 | */ | |
500 | ||
501 | void v9fs_session_close(struct v9fs_session_info *v9ses) | |
502 | { | |
bd238fb4 LI |
503 | if (v9ses->clnt) { |
504 | p9_client_destroy(v9ses->clnt); | |
505 | v9ses->clnt = NULL; | |
3cf6429a | 506 | } |
9e82cf6a | 507 | |
60e78d2c | 508 | #ifdef CONFIG_9P_FSCACHE |
24e42e32 | 509 | fscache_relinquish_volume(v9fs_session_cache(v9ses), NULL, false); |
cb0aae0e | 510 | kfree(v9ses->cachetag); |
60e78d2c | 511 | #endif |
e549c133 JL |
512 | kfree(v9ses->uname); |
513 | kfree(v9ses->aname); | |
60e78d2c AK |
514 | |
515 | spin_lock(&v9fs_sessionlist_lock); | |
516 | list_del(&v9ses->slist); | |
517 | spin_unlock(&v9fs_sessionlist_lock); | |
9e82cf6a EVH |
518 | } |
519 | ||
322b329a | 520 | /** |
ee443996 EVH |
521 | * v9fs_session_cancel - terminate a session |
522 | * @v9ses: session to terminate | |
523 | * | |
524 | * mark transport as disconnected and cancel all pending requests. | |
322b329a | 525 | */ |
ee443996 | 526 | |
e4eeefba SM |
527 | void v9fs_session_cancel(struct v9fs_session_info *v9ses) |
528 | { | |
5d385153 | 529 | p9_debug(P9_DEBUG_ERROR, "cancel session %p\n", v9ses); |
bd238fb4 | 530 | p9_client_disconnect(v9ses->clnt); |
322b329a EVH |
531 | } |
532 | ||
6d96d3ab AK |
533 | /** |
534 | * v9fs_session_begin_cancel - Begin terminate of a session | |
535 | * @v9ses: session to terminate | |
536 | * | |
537 | * After this call we don't allow any request other than clunk. | |
538 | */ | |
539 | ||
540 | void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses) | |
541 | { | |
5d385153 | 542 | p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses); |
6d96d3ab AK |
543 | p9_client_begin_disconnect(v9ses->clnt); |
544 | } | |
545 | ||
9e82cf6a EVH |
546 | extern int v9fs_error_init(void); |
547 | ||
60e78d2c AK |
548 | static struct kobject *v9fs_kobj; |
549 | ||
550 | #ifdef CONFIG_9P_FSCACHE | |
bc868036 DH |
551 | /* |
552 | * List caches associated with a session | |
60e78d2c | 553 | */ |
60e78d2c AK |
554 | static 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 | ||
579 | static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches); | |
580 | #endif /* CONFIG_9P_FSCACHE */ | |
581 | ||
582 | static struct attribute *v9fs_attrs[] = { | |
583 | #ifdef CONFIG_9P_FSCACHE | |
584 | &v9fs_attr_cache.attr, | |
585 | #endif | |
586 | NULL, | |
587 | }; | |
588 | ||
0dae5228 | 589 | static const struct attribute_group v9fs_attr_group = { |
60e78d2c AK |
590 | .attrs = v9fs_attrs, |
591 | }; | |
592 | ||
593 | /** | |
594 | * v9fs_sysfs_init - Initialize the v9fs sysfs interface | |
595 | * | |
596 | */ | |
597 | ||
bdbeacde | 598 | static int __init v9fs_sysfs_init(void) |
60e78d2c AK |
599 | { |
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 | ||
617 | static void v9fs_sysfs_cleanup(void) | |
618 | { | |
619 | sysfs_remove_group(v9fs_kobj, &v9fs_attr_group); | |
620 | kobject_put(v9fs_kobj); | |
621 | } | |
622 | ||
a78ce05d AK |
623 | static void v9fs_inode_init_once(void *foo) |
624 | { | |
625 | struct v9fs_inode *v9inode = (struct v9fs_inode *)foo; | |
626 | #ifdef CONFIG_9P_FSCACHE | |
627 | v9inode->fscache = NULL; | |
a78ce05d | 628 | #endif |
fd2421f5 | 629 | memset(&v9inode->qid, 0, sizeof(v9inode->qid)); |
a78ce05d AK |
630 | inode_init_once(&v9inode->vfs_inode); |
631 | } | |
632 | ||
633 | /** | |
634 | * v9fs_init_inode_cache - initialize a cache for 9P | |
635 | * Returns 0 on success. | |
636 | */ | |
637 | static int v9fs_init_inode_cache(void) | |
638 | { | |
639 | v9fs_inode_cache = kmem_cache_create("v9fs_inode_cache", | |
640 | sizeof(struct v9fs_inode), | |
641 | 0, (SLAB_RECLAIM_ACCOUNT| | |
5d097056 | 642 | SLAB_MEM_SPREAD|SLAB_ACCOUNT), |
a78ce05d AK |
643 | v9fs_inode_init_once); |
644 | if (!v9fs_inode_cache) | |
645 | return -ENOMEM; | |
646 | ||
647 | return 0; | |
648 | } | |
649 | ||
650 | /** | |
651 | * v9fs_destroy_inode_cache - destroy the cache of 9P inode | |
652 | * | |
653 | */ | |
654 | static void v9fs_destroy_inode_cache(void) | |
655 | { | |
8c0a8537 KS |
656 | /* |
657 | * Make sure all delayed rcu free inodes are flushed before we | |
658 | * destroy cache. | |
659 | */ | |
660 | rcu_barrier(); | |
a78ce05d AK |
661 | kmem_cache_destroy(v9fs_inode_cache); |
662 | } | |
663 | ||
664 | static int v9fs_cache_register(void) | |
665 | { | |
666 | int ret; | |
6e195b0f | 667 | |
a78ce05d AK |
668 | ret = v9fs_init_inode_cache(); |
669 | if (ret < 0) | |
670 | return ret; | |
8061a6fa | 671 | return ret; |
a78ce05d AK |
672 | } |
673 | ||
674 | static void v9fs_cache_unregister(void) | |
675 | { | |
676 | v9fs_destroy_inode_cache(); | |
a78ce05d AK |
677 | } |
678 | ||
60e78d2c AK |
679 | /** |
680 | * init_v9fs - Initialize module | |
9e82cf6a EVH |
681 | * |
682 | */ | |
683 | ||
684 | static int __init init_v9fs(void) | |
685 | { | |
60e78d2c | 686 | int err; |
6e195b0f | 687 | |
5d385153 | 688 | pr_info("Installing v9fs 9p2000 file system support\n"); |
a80d923e | 689 | /* TODO: Setup list of registered trasnport modules */ |
60e78d2c AK |
690 | |
691 | err = v9fs_cache_register(); | |
692 | if (err < 0) { | |
5d385153 | 693 | pr_err("Failed to register v9fs for caching\n"); |
2226a288 | 694 | return err; |
60e78d2c AK |
695 | } |
696 | ||
697 | err = v9fs_sysfs_init(); | |
698 | if (err < 0) { | |
5d385153 | 699 | pr_err("Failed to register with sysfs\n"); |
2226a288 AV |
700 | goto out_cache; |
701 | } | |
702 | err = register_filesystem(&v9fs_fs_type); | |
703 | if (err < 0) { | |
704 | pr_err("Failed to register filesystem\n"); | |
60e78d2c AK |
705 | goto out_sysfs_cleanup; |
706 | } | |
707 | ||
708 | return 0; | |
709 | ||
710 | out_sysfs_cleanup: | |
711 | v9fs_sysfs_cleanup(); | |
712 | ||
2226a288 AV |
713 | out_cache: |
714 | v9fs_cache_unregister(); | |
60e78d2c AK |
715 | |
716 | return err; | |
9e82cf6a EVH |
717 | } |
718 | ||
719 | /** | |
60e78d2c | 720 | * exit_v9fs - shutdown module |
9e82cf6a EVH |
721 | * |
722 | */ | |
723 | ||
724 | static void __exit exit_v9fs(void) | |
725 | { | |
60e78d2c AK |
726 | v9fs_sysfs_cleanup(); |
727 | v9fs_cache_unregister(); | |
9e82cf6a EVH |
728 | unregister_filesystem(&v9fs_fs_type); |
729 | } | |
730 | ||
731 | module_init(init_v9fs) | |
732 | module_exit(exit_v9fs) | |
733 | ||
bd238fb4 | 734 | MODULE_AUTHOR("Latchesar Ionkov <[email protected]>"); |
9e82cf6a EVH |
735 | MODULE_AUTHOR("Eric Van Hensbergen <[email protected]>"); |
736 | MODULE_AUTHOR("Ron Minnich <[email protected]>"); | |
737 | MODULE_LICENSE("GPL"); |