fs/9p/v9fs.c (v9fs_parse_options): Handle kstrdup and match_strdup failure. Now that...
[linux-2.6/lfs.git] / fs / 9p / v9fs.c
blob5c1ccaf0416c18164b1bf62b477441d4398ef4f3
1 /*
2 * linux/fs/9p/v9fs.c
4 * This file contains functions assisting in mapping VFS to 9P2000
6 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
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.
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
26 #include <linux/module.h>
27 #include <linux/errno.h>
28 #include <linux/fs.h>
29 #include <linux/sched.h>
30 #include <linux/parser.h>
31 #include <linux/idr.h>
32 #include <net/9p/9p.h>
33 #include <net/9p/transport.h>
34 #include <net/9p/client.h>
35 #include "v9fs.h"
36 #include "v9fs_vfs.h"
39 * Option Parsing (code inspired by NFS code)
40 * NOTE: each transport will parse its own options
43 enum {
44 /* Options that take integer arguments */
45 Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid,
46 /* String options */
47 Opt_uname, Opt_remotename, Opt_trans,
48 /* Options that take no arguments */
49 Opt_nodevmap,
50 /* Cache options */
51 Opt_cache_loose,
52 /* Access options */
53 Opt_access,
54 /* Error token */
55 Opt_err
58 static match_table_t tokens = {
59 {Opt_debug, "debug=%x"},
60 {Opt_dfltuid, "dfltuid=%u"},
61 {Opt_dfltgid, "dfltgid=%u"},
62 {Opt_afid, "afid=%u"},
63 {Opt_uname, "uname=%s"},
64 {Opt_remotename, "aname=%s"},
65 {Opt_nodevmap, "nodevmap"},
66 {Opt_cache_loose, "cache=loose"},
67 {Opt_cache_loose, "loose"},
68 {Opt_access, "access=%s"},
69 {Opt_err, NULL}
72 /**
73 * v9fs_parse_options - parse mount options into session structure
74 * @v9ses: existing v9fs session information
76 * Return 0 upon success, -ERRNO upon failure.
79 static int v9fs_parse_options(struct v9fs_session_info *v9ses)
81 char *options;
82 substring_t args[MAX_OPT_ARGS];
83 char *p;
84 int option = 0;
85 char *s, *e;
86 int ret = 0;
88 /* setup defaults */
89 v9ses->afid = ~0;
90 v9ses->debug = 0;
91 v9ses->cache = 0;
93 if (!v9ses->options)
94 return 0;
96 options = kstrdup(v9ses->options, GFP_KERNEL);
97 if (!options) {
98 P9_DPRINTK(P9_DEBUG_ERROR,
99 "failed to allocate copy of option string\n");
100 return -ENOMEM;
103 while ((p = strsep(&options, ",")) != NULL) {
104 int token;
105 if (!*p)
106 continue;
107 token = match_token(p, tokens, args);
108 if (token < Opt_uname) {
109 int r = match_int(&args[0], &option);
110 if (r < 0) {
111 P9_DPRINTK(P9_DEBUG_ERROR,
112 "integer field, but no integer?\n");
113 ret = r;
114 continue;
117 switch (token) {
118 case Opt_debug:
119 v9ses->debug = option;
120 #ifdef CONFIG_NET_9P_DEBUG
121 p9_debug_level = option;
122 #endif
123 break;
125 case Opt_dfltuid:
126 v9ses->dfltuid = option;
127 break;
128 case Opt_dfltgid:
129 v9ses->dfltgid = option;
130 break;
131 case Opt_afid:
132 v9ses->afid = option;
133 break;
134 case Opt_uname:
135 match_strlcpy(v9ses->uname, &args[0], PATH_MAX);
136 break;
137 case Opt_remotename:
138 match_strlcpy(v9ses->aname, &args[0], PATH_MAX);
139 break;
140 case Opt_nodevmap:
141 v9ses->nodev = 1;
142 break;
143 case Opt_cache_loose:
144 v9ses->cache = CACHE_LOOSE;
145 break;
147 case Opt_access:
148 s = match_strdup(&args[0]);
149 if (!s) {
150 P9_DPRINTK(P9_DEBUG_ERROR,
151 "failed to allocate copy"
152 " of option argument\n");
153 ret = -ENOMEM;
154 break;
156 v9ses->flags &= ~V9FS_ACCESS_MASK;
157 if (strcmp(s, "user") == 0)
158 v9ses->flags |= V9FS_ACCESS_USER;
159 else if (strcmp(s, "any") == 0)
160 v9ses->flags |= V9FS_ACCESS_ANY;
161 else {
162 v9ses->flags |= V9FS_ACCESS_SINGLE;
163 v9ses->uid = simple_strtol(s, &e, 10);
164 if (*e != '\0')
165 v9ses->uid = ~0;
167 kfree(s);
168 break;
170 default:
171 continue;
174 kfree(options);
175 return ret;
179 * v9fs_session_init - initialize session
180 * @v9ses: session information structure
181 * @dev_name: device being mounted
182 * @data: options
186 struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
187 const char *dev_name, char *data)
189 int retval = -EINVAL;
190 struct p9_fid *fid;
191 int rc;
193 v9ses->uname = __getname();
194 if (!v9ses->uname)
195 return ERR_PTR(-ENOMEM);
197 v9ses->aname = __getname();
198 if (!v9ses->aname) {
199 __putname(v9ses->uname);
200 return ERR_PTR(-ENOMEM);
203 v9ses->flags = V9FS_EXTENDED | V9FS_ACCESS_USER;
204 strcpy(v9ses->uname, V9FS_DEFUSER);
205 strcpy(v9ses->aname, V9FS_DEFANAME);
206 v9ses->uid = ~0;
207 v9ses->dfltuid = V9FS_DEFUID;
208 v9ses->dfltgid = V9FS_DEFGID;
209 v9ses->options = kstrdup(data, GFP_KERNEL);
210 if (!v9ses->options) {
211 P9_DPRINTK(P9_DEBUG_ERROR,
212 "failed to allocate copy of option string\n");
213 retval = -ENOMEM;
214 goto error;
217 rc = v9fs_parse_options(v9ses);
218 if (rc < 0) {
219 retval = rc;
220 goto error;
223 v9ses->clnt = p9_client_create(dev_name, v9ses->options);
225 if (IS_ERR(v9ses->clnt)) {
226 retval = PTR_ERR(v9ses->clnt);
227 v9ses->clnt = NULL;
228 P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n");
229 goto error;
232 if (!v9ses->clnt->dotu)
233 v9ses->flags &= ~V9FS_EXTENDED;
235 v9ses->maxdata = v9ses->clnt->msize;
237 /* for legacy mode, fall back to V9FS_ACCESS_ANY */
238 if (!v9fs_extended(v9ses) &&
239 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
241 v9ses->flags &= ~V9FS_ACCESS_MASK;
242 v9ses->flags |= V9FS_ACCESS_ANY;
243 v9ses->uid = ~0;
246 fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, ~0,
247 v9ses->aname);
248 if (IS_ERR(fid)) {
249 retval = PTR_ERR(fid);
250 fid = NULL;
251 P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n");
252 goto error;
255 if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
256 fid->uid = v9ses->uid;
257 else
258 fid->uid = ~0;
260 return fid;
262 error:
263 v9fs_session_close(v9ses);
264 return ERR_PTR(retval);
268 * v9fs_session_close - shutdown a session
269 * @v9ses: session information structure
273 void v9fs_session_close(struct v9fs_session_info *v9ses)
275 if (v9ses->clnt) {
276 p9_client_destroy(v9ses->clnt);
277 v9ses->clnt = NULL;
280 __putname(v9ses->uname);
281 __putname(v9ses->aname);
282 kfree(v9ses->options);
286 * v9fs_session_cancel - terminate a session
287 * @v9ses: session to terminate
289 * mark transport as disconnected and cancel all pending requests.
292 void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
293 P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
294 p9_client_disconnect(v9ses->clnt);
297 extern int v9fs_error_init(void);
300 * v9fs_init - Initialize module
304 static int __init init_v9fs(void)
306 printk(KERN_INFO "Installing v9fs 9p2000 file system support\n");
307 /* TODO: Setup list of registered trasnport modules */
308 return register_filesystem(&v9fs_fs_type);
312 * v9fs_init - shutdown module
316 static void __exit exit_v9fs(void)
318 unregister_filesystem(&v9fs_fs_type);
321 module_init(init_v9fs)
322 module_exit(exit_v9fs)
324 MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
325 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
326 MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
327 MODULE_LICENSE("GPL");