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