blob: 0a7068e30ecb6212c3f1d07c1679b4573a5cb90f [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 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * 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
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070026#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/fs.h>
Al Viro914e2632006-10-18 13:55:46 -040029#include <linux/sched.h>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070030#include <linux/parser.h>
31#include <linux/idr.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050032#include <net/9p/9p.h>
33#include <net/9p/transport.h>
34#include <net/9p/conn.h>
35#include <net/9p/client.h>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070036#include "v9fs.h"
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070037#include "v9fs_vfs.h"
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070038
39/*
40 * Option Parsing (code inspired by NFS code)
41 *
42 */
43
44enum {
45 /* Options that take integer arguments */
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -050046 Opt_debug, Opt_port, Opt_msize, Opt_uid, Opt_gid, Opt_afid,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070047 Opt_rfdno, Opt_wfdno,
48 /* String options */
Eric Van Hensbergen67543e52006-03-25 03:07:29 -080049 Opt_uname, Opt_remotename,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070050 /* Options that take no arguments */
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050051 Opt_legacy, Opt_nodevmap, Opt_unix, Opt_tcp, Opt_fd, Opt_pci,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -060052 /* Cache options */
53 Opt_cache_loose,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070054 /* Error token */
55 Opt_err
56};
57
58static match_table_t tokens = {
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -050059 {Opt_debug, "debug=%x"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070060 {Opt_port, "port=%u"},
61 {Opt_msize, "msize=%u"},
62 {Opt_uid, "uid=%u"},
63 {Opt_gid, "gid=%u"},
64 {Opt_afid, "afid=%u"},
65 {Opt_rfdno, "rfdno=%u"},
66 {Opt_wfdno, "wfdno=%u"},
Eric Van Hensbergen67543e52006-03-25 03:07:29 -080067 {Opt_uname, "uname=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070068 {Opt_remotename, "aname=%s"},
69 {Opt_unix, "proto=unix"},
70 {Opt_tcp, "proto=tcp"},
71 {Opt_fd, "proto=fd"},
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050072#ifdef CONFIG_PCI_9P
73 {Opt_pci, "proto=pci"},
74#endif
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070075 {Opt_tcp, "tcp"},
76 {Opt_unix, "unix"},
77 {Opt_fd, "fd"},
78 {Opt_legacy, "noextend"},
79 {Opt_nodevmap, "nodevmap"},
Eric Van Hensbergene03abc02007-02-11 13:21:39 -060080 {Opt_cache_loose, "cache=loose"},
81 {Opt_cache_loose, "loose"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070082 {Opt_err, NULL}
83};
84
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050085extern struct p9_transport *p9pci_trans_create(void);
86
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070087/*
88 * Parse option string.
89 */
90
91/**
92 * v9fs_parse_options - parse mount options into session structure
93 * @options: options string passed from mount
94 * @v9ses: existing v9fs session information
95 *
96 */
97
98static void v9fs_parse_options(char *options, struct v9fs_session_info *v9ses)
99{
100 char *p;
101 substring_t args[MAX_OPT_ARGS];
102 int option;
103 int ret;
104
105 /* setup defaults */
106 v9ses->port = V9FS_PORT;
107 v9ses->maxdata = 9000;
108 v9ses->proto = PROTO_TCP;
109 v9ses->extended = 1;
110 v9ses->afid = ~0;
111 v9ses->debug = 0;
112 v9ses->rfdno = ~0;
113 v9ses->wfdno = ~0;
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600114 v9ses->cache = 0;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700115
116 if (!options)
117 return;
118
119 while ((p = strsep(&options, ",")) != NULL) {
120 int token;
121 if (!*p)
122 continue;
123 token = match_token(p, tokens, args);
Eric Van Hensbergen67543e52006-03-25 03:07:29 -0800124 if (token < Opt_uname) {
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700125 if ((ret = match_int(&args[0], &option)) < 0) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500126 P9_DPRINTK(P9_DEBUG_ERROR,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700127 "integer field, but no integer?\n");
128 continue;
129 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700130 }
131 switch (token) {
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -0500132 case Opt_debug:
133 v9ses->debug = option;
Eric Van Hensbergen10fa16e2007-07-16 16:02:49 -0500134#ifdef CONFIG_NET_9P_DEBUG
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -0500135 p9_debug_level = option;
Eric Van Hensbergen10fa16e2007-07-16 16:02:49 -0500136#endif
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -0500137 break;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700138 case Opt_port:
139 v9ses->port = option;
140 break;
141 case Opt_msize:
142 v9ses->maxdata = option;
143 break;
144 case Opt_uid:
145 v9ses->uid = option;
146 break;
147 case Opt_gid:
148 v9ses->gid = option;
149 break;
150 case Opt_afid:
151 v9ses->afid = option;
152 break;
153 case Opt_rfdno:
154 v9ses->rfdno = option;
155 break;
156 case Opt_wfdno:
157 v9ses->wfdno = option;
158 break;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700159 case Opt_tcp:
160 v9ses->proto = PROTO_TCP;
161 break;
162 case Opt_unix:
163 v9ses->proto = PROTO_UNIX;
164 break;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500165 case Opt_pci:
166 v9ses->proto = PROTO_PCI;
167 break;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700168 case Opt_fd:
169 v9ses->proto = PROTO_FD;
170 break;
Eric Van Hensbergen67543e52006-03-25 03:07:29 -0800171 case Opt_uname:
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700172 match_strcpy(v9ses->name, &args[0]);
173 break;
174 case Opt_remotename:
175 match_strcpy(v9ses->remotename, &args[0]);
176 break;
177 case Opt_legacy:
178 v9ses->extended = 0;
179 break;
180 case Opt_nodevmap:
181 v9ses->nodev = 1;
182 break;
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600183 case Opt_cache_loose:
184 v9ses->cache = CACHE_LOOSE;
185 break;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700186 default:
187 continue;
188 }
189 }
190}
191
192/**
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700193 * v9fs_session_init - initialize session
194 * @v9ses: session information structure
195 * @dev_name: device being mounted
196 * @data: options
197 *
198 */
199
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500200struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700201 const char *dev_name, char *data)
202{
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700203 int retval = -EINVAL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500204 struct p9_transport *trans;
205 struct p9_fid *fid;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700206
207 v9ses->name = __getname();
208 if (!v9ses->name)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500209 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700210
211 v9ses->remotename = __getname();
212 if (!v9ses->remotename) {
Davi Arnautce44eeb2005-11-07 00:59:36 -0800213 __putname(v9ses->name);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500214 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700215 }
216
217 strcpy(v9ses->name, V9FS_DEFUSER);
218 strcpy(v9ses->remotename, V9FS_DEFANAME);
219
220 v9fs_parse_options(data, v9ses);
221
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700222 switch (v9ses->proto) {
223 case PROTO_TCP:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500224 trans = p9_trans_create_tcp(dev_name, v9ses->port);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700225 break;
226 case PROTO_UNIX:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500227 trans = p9_trans_create_unix(dev_name);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700228 *v9ses->remotename = 0;
229 break;
230 case PROTO_FD:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500231 trans = p9_trans_create_fd(v9ses->rfdno, v9ses->wfdno);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700232 *v9ses->remotename = 0;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700233 break;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500234#ifdef CONFIG_PCI_9P
235 case PROTO_PCI:
236 trans = p9pci_trans_create();
237 *v9ses->remotename = 0;
238 break;
239#endif
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700240 default:
241 printk(KERN_ERR "v9fs: Bad mount protocol %d\n", v9ses->proto);
242 retval = -ENOPROTOOPT;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500243 goto error;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700244 };
245
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500246 if (IS_ERR(trans)) {
247 retval = PTR_ERR(trans);
248 trans = NULL;
249 goto error;
Latchesar Ionkova8e63bf2005-09-22 21:43:51 -0700250 }
251
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500252 v9ses->clnt = p9_client_create(trans, v9ses->maxdata + P9_IOHDRSZ,
253 v9ses->extended);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700254
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500255 if (IS_ERR(v9ses->clnt)) {
256 retval = PTR_ERR(v9ses->clnt);
257 v9ses->clnt = NULL;
258 P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n");
259 goto error;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700260 }
261
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500262 fid = p9_client_attach(v9ses->clnt, NULL, v9ses->name,
263 v9ses->remotename);
264 if (IS_ERR(fid)) {
265 retval = PTR_ERR(fid);
266 fid = NULL;
267 P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n");
268 goto error;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700269 }
270
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500271 return fid;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700272
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500273error:
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700274 v9fs_session_close(v9ses);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500275 return ERR_PTR(retval);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700276}
277
278/**
279 * v9fs_session_close - shutdown a session
280 * @v9ses: session information structure
281 *
282 */
283
284void v9fs_session_close(struct v9fs_session_info *v9ses)
285{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500286 if (v9ses->clnt) {
287 p9_client_destroy(v9ses->clnt);
288 v9ses->clnt = NULL;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800289 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700290
Davi Arnautce44eeb2005-11-07 00:59:36 -0800291 __putname(v9ses->name);
292 __putname(v9ses->remotename);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700293}
294
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700295/**
296 * v9fs_session_cancel - mark transport as disconnected
297 * and cancel all pending requests.
298 */
299void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500300 P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
301 p9_client_disconnect(v9ses->clnt);
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700302}
303
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700304extern int v9fs_error_init(void);
305
306/**
307 * v9fs_init - Initialize module
308 *
309 */
310
311static int __init init_v9fs(void)
312{
Eric Van Hensbergenf94b3472007-01-26 00:57:04 -0800313 printk(KERN_INFO "Installing v9fs 9p2000 file system support\n");
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700314
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500315 return register_filesystem(&v9fs_fs_type);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700316}
317
318/**
319 * v9fs_init - shutdown module
320 *
321 */
322
323static void __exit exit_v9fs(void)
324{
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700325 unregister_filesystem(&v9fs_fs_type);
326}
327
328module_init(init_v9fs)
329module_exit(exit_v9fs)
330
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500331MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700332MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
333MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
334MODULE_LICENSE("GPL");