blob: 43a6fc0db8a7ec981f73cd65a8c128a3fa68118a [file] [log] [blame]
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredid7133112006-04-10 22:54:55 -07003 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/file.h>
14#include <linux/mount.h>
15#include <linux/seq_file.h>
16#include <linux/init.h>
17#include <linux/module.h>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070018#include <linux/parser.h>
19#include <linux/statfs.h>
20
21MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
22MODULE_DESCRIPTION("Filesystem in Userspace");
23MODULE_LICENSE("GPL");
24
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070025static kmem_cache_t *fuse_inode_cachep;
Miklos Szeredif543f252006-01-16 22:14:35 -080026static struct subsystem connections_subsys;
27
28struct fuse_conn_attr {
29 struct attribute attr;
30 ssize_t (*show)(struct fuse_conn *, char *);
31 ssize_t (*store)(struct fuse_conn *, const char *, size_t);
32};
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070033
34#define FUSE_SUPER_MAGIC 0x65735546
35
36struct fuse_mount_data {
37 int fd;
38 unsigned rootmode;
39 unsigned user_id;
Miklos Szeredi87729a52005-09-09 13:10:34 -070040 unsigned group_id;
Miklos Szeredi5a533682005-09-09 13:10:34 -070041 unsigned fd_present : 1;
42 unsigned rootmode_present : 1;
43 unsigned user_id_present : 1;
44 unsigned group_id_present : 1;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -070045 unsigned flags;
Miklos Szeredidb50b962005-09-09 13:10:33 -070046 unsigned max_read;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070047};
48
49static struct inode *fuse_alloc_inode(struct super_block *sb)
50{
51 struct inode *inode;
52 struct fuse_inode *fi;
53
54 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
55 if (!inode)
56 return NULL;
57
58 fi = get_fuse_inode(inode);
59 fi->i_time = jiffies - 1;
60 fi->nodeid = 0;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070061 fi->nlookup = 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -070062 fi->forget_req = fuse_request_alloc();
63 if (!fi->forget_req) {
64 kmem_cache_free(fuse_inode_cachep, inode);
65 return NULL;
66 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070067
68 return inode;
69}
70
71static void fuse_destroy_inode(struct inode *inode)
72{
Miklos Szeredie5e55582005-09-09 13:10:28 -070073 struct fuse_inode *fi = get_fuse_inode(inode);
74 if (fi->forget_req)
75 fuse_request_free(fi->forget_req);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070076 kmem_cache_free(fuse_inode_cachep, inode);
77}
78
79static void fuse_read_inode(struct inode *inode)
80{
81 /* No op */
82}
83
Miklos Szeredie5e55582005-09-09 13:10:28 -070084void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070085 unsigned long nodeid, u64 nlookup)
Miklos Szeredie5e55582005-09-09 13:10:28 -070086{
87 struct fuse_forget_in *inarg = &req->misc.forget_in;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070088 inarg->nlookup = nlookup;
Miklos Szeredie5e55582005-09-09 13:10:28 -070089 req->in.h.opcode = FUSE_FORGET;
90 req->in.h.nodeid = nodeid;
91 req->in.numargs = 1;
92 req->in.args[0].size = sizeof(struct fuse_forget_in);
93 req->in.args[0].value = inarg;
94 request_send_noreply(fc, req);
95}
96
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070097static void fuse_clear_inode(struct inode *inode)
98{
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -070099 if (inode->i_sb->s_flags & MS_ACTIVE) {
100 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700101 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700102 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700103 fi->forget_req = NULL;
104 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700105}
106
107void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
108{
109 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
110 invalidate_inode_pages(inode->i_mapping);
111
112 inode->i_ino = attr->ino;
113 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
114 inode->i_nlink = attr->nlink;
115 inode->i_uid = attr->uid;
116 inode->i_gid = attr->gid;
117 i_size_write(inode, attr->size);
118 inode->i_blksize = PAGE_CACHE_SIZE;
119 inode->i_blocks = attr->blocks;
120 inode->i_atime.tv_sec = attr->atime;
121 inode->i_atime.tv_nsec = attr->atimensec;
122 inode->i_mtime.tv_sec = attr->mtime;
123 inode->i_mtime.tv_nsec = attr->mtimensec;
124 inode->i_ctime.tv_sec = attr->ctime;
125 inode->i_ctime.tv_nsec = attr->ctimensec;
126}
127
128static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
129{
130 inode->i_mode = attr->mode & S_IFMT;
131 i_size_write(inode, attr->size);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700132 if (S_ISREG(inode->i_mode)) {
133 fuse_init_common(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700134 fuse_init_file_inode(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700135 } else if (S_ISDIR(inode->i_mode))
136 fuse_init_dir(inode);
137 else if (S_ISLNK(inode->i_mode))
138 fuse_init_symlink(inode);
139 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
140 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
141 fuse_init_common(inode);
142 init_special_inode(inode, inode->i_mode,
143 new_decode_dev(attr->rdev));
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800144 } else
145 BUG();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700146}
147
148static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
149{
150 unsigned long nodeid = *(unsigned long *) _nodeidp;
151 if (get_node_id(inode) == nodeid)
152 return 1;
153 else
154 return 0;
155}
156
157static int fuse_inode_set(struct inode *inode, void *_nodeidp)
158{
159 unsigned long nodeid = *(unsigned long *) _nodeidp;
160 get_fuse_inode(inode)->nodeid = nodeid;
161 return 0;
162}
163
164struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700165 int generation, struct fuse_attr *attr)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700166{
167 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700168 struct fuse_inode *fi;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700169 struct fuse_conn *fc = get_fuse_conn_super(sb);
170 int retried = 0;
171
172 retry:
173 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
174 if (!inode)
175 return NULL;
176
177 if ((inode->i_state & I_NEW)) {
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700178 inode->i_flags |= S_NOATIME|S_NOCMTIME;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700179 inode->i_generation = generation;
180 inode->i_data.backing_dev_info = &fc->bdi;
181 fuse_init_inode(inode, attr);
182 unlock_new_inode(inode);
183 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
184 BUG_ON(retried);
185 /* Inode has changed type, any I/O on the old should fail */
186 make_bad_inode(inode);
187 iput(inode);
188 retried = 1;
189 goto retry;
190 }
191
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700192 fi = get_fuse_inode(inode);
193 fi->nlookup ++;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700194 fuse_change_attributes(inode, attr);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700195 return inode;
196}
197
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800198static void fuse_umount_begin(struct super_block *sb)
199{
200 fuse_abort_conn(get_fuse_conn_super(sb));
201}
202
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700203static void fuse_put_super(struct super_block *sb)
204{
205 struct fuse_conn *fc = get_fuse_conn_super(sb);
206
Miklos Szeredid7133112006-04-10 22:54:55 -0700207 spin_lock(&fc->lock);
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800208 fc->connected = 0;
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200209 while (!list_empty(&fc->background)) {
210 struct fuse_req *req = list_entry(fc->background.next,
211 struct fuse_req, bg_entry);
212 struct inode *inode = req->inode;
213 struct inode *inode2 = req->inode2;
214
215 /* File would hold a reference to vfsmount */
216 BUG_ON(req->file);
217 req->inode = NULL;
218 req->inode2 = NULL;
219 fuse_remove_background(fc, req);
220
221 spin_unlock(&fc->lock);
222 iput(inode);
223 iput(inode2);
224 spin_lock(&fc->lock);
225 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700226 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700227 /* Flush all readers on this fs */
Jeff Dike385a17b2006-04-10 22:54:52 -0700228 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700229 wake_up_all(&fc->waitq);
Miklos Szeredif543f252006-01-16 22:14:35 -0800230 kobject_del(&fc->kobj);
231 kobject_put(&fc->kobj);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700232}
233
Miklos Szeredie5e55582005-09-09 13:10:28 -0700234static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
235{
236 stbuf->f_type = FUSE_SUPER_MAGIC;
237 stbuf->f_bsize = attr->bsize;
Miklos Szeredide5f1202006-01-06 00:19:37 -0800238 stbuf->f_frsize = attr->frsize;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700239 stbuf->f_blocks = attr->blocks;
240 stbuf->f_bfree = attr->bfree;
241 stbuf->f_bavail = attr->bavail;
242 stbuf->f_files = attr->files;
243 stbuf->f_ffree = attr->ffree;
244 stbuf->f_namelen = attr->namelen;
245 /* fsid is left zero */
246}
247
248static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
249{
250 struct fuse_conn *fc = get_fuse_conn_super(sb);
251 struct fuse_req *req;
252 struct fuse_statfs_out outarg;
253 int err;
254
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700255 req = fuse_get_req(fc);
256 if (IS_ERR(req))
257 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700258
Miklos Szeredide5f1202006-01-06 00:19:37 -0800259 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700260 req->in.numargs = 0;
261 req->in.h.opcode = FUSE_STATFS;
262 req->out.numargs = 1;
Miklos Szeredide5f1202006-01-06 00:19:37 -0800263 req->out.args[0].size =
264 fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700265 req->out.args[0].value = &outarg;
266 request_send(fc, req);
267 err = req->out.h.error;
268 if (!err)
269 convert_fuse_statfs(buf, &outarg.st);
270 fuse_put_request(fc, req);
271 return err;
272}
273
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700274enum {
275 OPT_FD,
276 OPT_ROOTMODE,
277 OPT_USER_ID,
Miklos Szeredi87729a52005-09-09 13:10:34 -0700278 OPT_GROUP_ID,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700279 OPT_DEFAULT_PERMISSIONS,
280 OPT_ALLOW_OTHER,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700281 OPT_MAX_READ,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700282 OPT_ERR
283};
284
285static match_table_t tokens = {
286 {OPT_FD, "fd=%u"},
287 {OPT_ROOTMODE, "rootmode=%o"},
288 {OPT_USER_ID, "user_id=%u"},
Miklos Szeredi87729a52005-09-09 13:10:34 -0700289 {OPT_GROUP_ID, "group_id=%u"},
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700290 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
291 {OPT_ALLOW_OTHER, "allow_other"},
Miklos Szeredidb50b962005-09-09 13:10:33 -0700292 {OPT_MAX_READ, "max_read=%u"},
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700293 {OPT_ERR, NULL}
294};
295
296static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
297{
298 char *p;
299 memset(d, 0, sizeof(struct fuse_mount_data));
Miklos Szeredidb50b962005-09-09 13:10:33 -0700300 d->max_read = ~0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700301
302 while ((p = strsep(&opt, ",")) != NULL) {
303 int token;
304 int value;
305 substring_t args[MAX_OPT_ARGS];
306 if (!*p)
307 continue;
308
309 token = match_token(p, tokens, args);
310 switch (token) {
311 case OPT_FD:
312 if (match_int(&args[0], &value))
313 return 0;
314 d->fd = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700315 d->fd_present = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700316 break;
317
318 case OPT_ROOTMODE:
319 if (match_octal(&args[0], &value))
320 return 0;
321 d->rootmode = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700322 d->rootmode_present = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700323 break;
324
325 case OPT_USER_ID:
326 if (match_int(&args[0], &value))
327 return 0;
328 d->user_id = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700329 d->user_id_present = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700330 break;
331
Miklos Szeredi87729a52005-09-09 13:10:34 -0700332 case OPT_GROUP_ID:
333 if (match_int(&args[0], &value))
334 return 0;
335 d->group_id = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700336 d->group_id_present = 1;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700337 break;
338
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700339 case OPT_DEFAULT_PERMISSIONS:
340 d->flags |= FUSE_DEFAULT_PERMISSIONS;
341 break;
342
343 case OPT_ALLOW_OTHER:
344 d->flags |= FUSE_ALLOW_OTHER;
345 break;
346
Miklos Szeredidb50b962005-09-09 13:10:33 -0700347 case OPT_MAX_READ:
348 if (match_int(&args[0], &value))
349 return 0;
350 d->max_read = value;
351 break;
352
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700353 default:
354 return 0;
355 }
356 }
Miklos Szeredi5a533682005-09-09 13:10:34 -0700357
358 if (!d->fd_present || !d->rootmode_present ||
359 !d->user_id_present || !d->group_id_present)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700360 return 0;
361
362 return 1;
363}
364
365static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
366{
367 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
368
369 seq_printf(m, ",user_id=%u", fc->user_id);
Miklos Szeredi87729a52005-09-09 13:10:34 -0700370 seq_printf(m, ",group_id=%u", fc->group_id);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700371 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
372 seq_puts(m, ",default_permissions");
373 if (fc->flags & FUSE_ALLOW_OTHER)
374 seq_puts(m, ",allow_other");
Miklos Szeredidb50b962005-09-09 13:10:33 -0700375 if (fc->max_read != ~0)
376 seq_printf(m, ",max_read=%u", fc->max_read);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700377 return 0;
378}
379
Miklos Szeredif543f252006-01-16 22:14:35 -0800380static void fuse_conn_release(struct kobject *kobj)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700381{
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700382 kfree(get_fuse_conn_kobj(kobj));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700383}
384
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700385static struct fuse_conn *new_conn(void)
386{
387 struct fuse_conn *fc;
388
Miklos Szeredi6383bda2006-01-16 22:14:29 -0800389 fc = kzalloc(sizeof(*fc), GFP_KERNEL);
Miklos Szeredif543f252006-01-16 22:14:35 -0800390 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700391 spin_lock_init(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700392 init_waitqueue_head(&fc->waitq);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700393 init_waitqueue_head(&fc->blocked_waitq);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700394 INIT_LIST_HEAD(&fc->pending);
395 INIT_LIST_HEAD(&fc->processing);
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800396 INIT_LIST_HEAD(&fc->io);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700397 INIT_LIST_HEAD(&fc->background);
Miklos Szeredif543f252006-01-16 22:14:35 -0800398 kobj_set_kset_s(fc, connections_subsys);
399 kobject_init(&fc->kobj);
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800400 atomic_set(&fc->num_waiting, 0);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700401 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
402 fc->bdi.unplug_io_fn = default_unplug_io_fn;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700403 fc->reqctr = 0;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700404 fc->blocked = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700405 }
406 return fc;
407}
408
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700409static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
410{
411 struct fuse_attr attr;
412 memset(&attr, 0, sizeof(attr));
413
414 attr.mode = mode;
415 attr.ino = FUSE_ROOT_ID;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700416 return fuse_iget(sb, 1, 0, &attr);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700417}
418
419static struct super_operations fuse_super_operations = {
420 .alloc_inode = fuse_alloc_inode,
421 .destroy_inode = fuse_destroy_inode,
422 .read_inode = fuse_read_inode,
423 .clear_inode = fuse_clear_inode,
424 .put_super = fuse_put_super,
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800425 .umount_begin = fuse_umount_begin,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700426 .statfs = fuse_statfs,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700427 .show_options = fuse_show_options,
428};
429
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800430static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
431{
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800432 struct fuse_init_out *arg = &req->misc.init_out;
433
434 if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
435 fc->conn_error = 1;
436 else {
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800437 unsigned long ra_pages;
438
439 if (arg->minor >= 6) {
440 ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
441 if (arg->flags & FUSE_ASYNC_READ)
442 fc->async_read = 1;
443 } else
444 ra_pages = fc->max_read / PAGE_CACHE_SIZE;
445
446 fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800447 fc->minor = arg->minor;
448 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
449 }
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800450 fuse_put_request(fc, req);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700451 fc->blocked = 0;
452 wake_up_all(&fc->blocked_waitq);
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800453}
454
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700455static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800456{
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800457 struct fuse_init_in *arg = &req->misc.init_in;
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800458
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800459 arg->major = FUSE_KERNEL_VERSION;
460 arg->minor = FUSE_KERNEL_MINOR_VERSION;
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800461 arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
462 arg->flags |= FUSE_ASYNC_READ;
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800463 req->in.h.opcode = FUSE_INIT;
464 req->in.numargs = 1;
465 req->in.args[0].size = sizeof(*arg);
466 req->in.args[0].value = arg;
467 req->out.numargs = 1;
468 /* Variable length arguement used for backward compatibility
469 with interface version < 7.5. Rest of init_out is zeroed
470 by do_get_request(), so a short reply is not a problem */
471 req->out.argvar = 1;
472 req->out.args[0].size = sizeof(struct fuse_init_out);
473 req->out.args[0].value = &req->misc.init_out;
474 req->end = process_init_reply;
475 request_send_background(fc, req);
476}
477
Miklos Szeredif543f252006-01-16 22:14:35 -0800478static unsigned long long conn_id(void)
479{
Miklos Szeredi0720b312006-04-10 22:54:55 -0700480 /* BKL is held for ->get_sb() */
Miklos Szeredif543f252006-01-16 22:14:35 -0800481 static unsigned long long ctr = 1;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700482 return ctr++;
Miklos Szeredif543f252006-01-16 22:14:35 -0800483}
484
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700485static int fuse_fill_super(struct super_block *sb, void *data, int silent)
486{
487 struct fuse_conn *fc;
488 struct inode *root;
489 struct fuse_mount_data d;
490 struct file *file;
Miklos Szeredif543f252006-01-16 22:14:35 -0800491 struct dentry *root_dentry;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700492 struct fuse_req *init_req;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700493 int err;
494
495 if (!parse_fuse_opt((char *) data, &d))
496 return -EINVAL;
497
498 sb->s_blocksize = PAGE_CACHE_SIZE;
499 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
500 sb->s_magic = FUSE_SUPER_MAGIC;
501 sb->s_op = &fuse_super_operations;
502 sb->s_maxbytes = MAX_LFS_FILESIZE;
503
504 file = fget(d.fd);
505 if (!file)
506 return -EINVAL;
507
Miklos Szeredi0720b312006-04-10 22:54:55 -0700508 if (file->f_op != &fuse_dev_operations)
509 return -EINVAL;
510
511 /* Setting file->private_data can't race with other mount()
512 instances, since BKL is held for ->get_sb() */
513 if (file->private_data)
514 return -EINVAL;
515
516 fc = new_conn();
517 if (!fc)
518 return -ENOMEM;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700519
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700520 fc->flags = d.flags;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700521 fc->user_id = d.user_id;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700522 fc->group_id = d.group_id;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700523 fc->max_read = d.max_read;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700524
Miklos Szeredif543f252006-01-16 22:14:35 -0800525 /* Used by get_root_inode() */
526 sb->s_fs_info = fc;
527
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700528 err = -ENOMEM;
529 root = get_root_inode(sb, d.rootmode);
Miklos Szeredif543f252006-01-16 22:14:35 -0800530 if (!root)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700531 goto err;
532
Miklos Szeredif543f252006-01-16 22:14:35 -0800533 root_dentry = d_alloc_root(root);
534 if (!root_dentry) {
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700535 iput(root);
536 goto err;
537 }
Miklos Szeredif543f252006-01-16 22:14:35 -0800538
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700539 init_req = fuse_request_alloc();
540 if (!init_req)
541 goto err_put_root;
542
Miklos Szeredif543f252006-01-16 22:14:35 -0800543 err = kobject_set_name(&fc->kobj, "%llu", conn_id());
544 if (err)
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700545 goto err_free_req;
Miklos Szeredif543f252006-01-16 22:14:35 -0800546
547 err = kobject_add(&fc->kobj);
548 if (err)
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700549 goto err_free_req;
Miklos Szeredif543f252006-01-16 22:14:35 -0800550
551 sb->s_root = root_dentry;
Miklos Szeredif543f252006-01-16 22:14:35 -0800552 fc->connected = 1;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700553 kobject_get(&fc->kobj);
554 file->private_data = fc;
555 /*
556 * atomic_dec_and_test() in fput() provides the necessary
557 * memory barrier for file->private_data to be visible on all
558 * CPUs after this
559 */
560 fput(file);
Miklos Szeredif543f252006-01-16 22:14:35 -0800561
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700562 fuse_send_init(fc, init_req);
Miklos Szeredif543f252006-01-16 22:14:35 -0800563
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700564 return 0;
565
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700566 err_free_req:
567 fuse_request_free(init_req);
Miklos Szeredif543f252006-01-16 22:14:35 -0800568 err_put_root:
569 dput(root_dentry);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700570 err:
Miklos Szeredi0720b312006-04-10 22:54:55 -0700571 fput(file);
Miklos Szeredif543f252006-01-16 22:14:35 -0800572 kobject_put(&fc->kobj);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700573 return err;
574}
575
576static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
577 int flags, const char *dev_name,
578 void *raw_data)
579{
580 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
581}
582
583static struct file_system_type fuse_fs_type = {
584 .owner = THIS_MODULE,
585 .name = "fuse",
586 .get_sb = fuse_get_sb,
587 .kill_sb = kill_anon_super,
588};
589
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800590static ssize_t fuse_conn_waiting_show(struct fuse_conn *fc, char *page)
591{
592 return sprintf(page, "%i\n", atomic_read(&fc->num_waiting));
593}
594
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800595static ssize_t fuse_conn_abort_store(struct fuse_conn *fc, const char *page,
596 size_t count)
597{
598 fuse_abort_conn(fc);
599 return count;
600}
601
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800602static struct fuse_conn_attr fuse_conn_waiting =
603 __ATTR(waiting, 0400, fuse_conn_waiting_show, NULL);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800604static struct fuse_conn_attr fuse_conn_abort =
605 __ATTR(abort, 0600, NULL, fuse_conn_abort_store);
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800606
Miklos Szeredif543f252006-01-16 22:14:35 -0800607static struct attribute *fuse_conn_attrs[] = {
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800608 &fuse_conn_waiting.attr,
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800609 &fuse_conn_abort.attr,
Miklos Szeredif543f252006-01-16 22:14:35 -0800610 NULL,
611};
612
613static ssize_t fuse_conn_attr_show(struct kobject *kobj,
614 struct attribute *attr,
615 char *page)
616{
617 struct fuse_conn_attr *fca =
618 container_of(attr, struct fuse_conn_attr, attr);
619
620 if (fca->show)
621 return fca->show(get_fuse_conn_kobj(kobj), page);
622 else
623 return -EACCES;
624}
625
626static ssize_t fuse_conn_attr_store(struct kobject *kobj,
627 struct attribute *attr,
628 const char *page, size_t count)
629{
630 struct fuse_conn_attr *fca =
631 container_of(attr, struct fuse_conn_attr, attr);
632
633 if (fca->store)
634 return fca->store(get_fuse_conn_kobj(kobj), page, count);
635 else
636 return -EACCES;
637}
638
639static struct sysfs_ops fuse_conn_sysfs_ops = {
640 .show = &fuse_conn_attr_show,
641 .store = &fuse_conn_attr_store,
642};
643
644static struct kobj_type ktype_fuse_conn = {
645 .release = fuse_conn_release,
646 .sysfs_ops = &fuse_conn_sysfs_ops,
647 .default_attrs = fuse_conn_attrs,
648};
649
650static decl_subsys(fuse, NULL, NULL);
651static decl_subsys(connections, &ktype_fuse_conn, NULL);
652
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700653static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
654 unsigned long flags)
655{
656 struct inode * inode = foo;
657
658 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
659 SLAB_CTOR_CONSTRUCTOR)
660 inode_init_once(inode);
661}
662
663static int __init fuse_fs_init(void)
664{
665 int err;
666
667 err = register_filesystem(&fuse_fs_type);
668 if (err)
669 printk("fuse: failed to register filesystem\n");
670 else {
671 fuse_inode_cachep = kmem_cache_create("fuse_inode",
672 sizeof(struct fuse_inode),
673 0, SLAB_HWCACHE_ALIGN,
674 fuse_inode_init_once, NULL);
675 if (!fuse_inode_cachep) {
676 unregister_filesystem(&fuse_fs_type);
677 err = -ENOMEM;
678 }
679 }
680
681 return err;
682}
683
684static void fuse_fs_cleanup(void)
685{
686 unregister_filesystem(&fuse_fs_type);
687 kmem_cache_destroy(fuse_inode_cachep);
688}
689
Miklos Szeredif543f252006-01-16 22:14:35 -0800690static int fuse_sysfs_init(void)
691{
692 int err;
693
694 kset_set_kset_s(&fuse_subsys, fs_subsys);
695 err = subsystem_register(&fuse_subsys);
696 if (err)
697 goto out_err;
698
699 kset_set_kset_s(&connections_subsys, fuse_subsys);
700 err = subsystem_register(&connections_subsys);
701 if (err)
702 goto out_fuse_unregister;
703
704 return 0;
705
706 out_fuse_unregister:
707 subsystem_unregister(&fuse_subsys);
708 out_err:
709 return err;
710}
711
712static void fuse_sysfs_cleanup(void)
713{
714 subsystem_unregister(&connections_subsys);
715 subsystem_unregister(&fuse_subsys);
716}
717
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700718static int __init fuse_init(void)
719{
720 int res;
721
722 printk("fuse init (API version %i.%i)\n",
723 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
724
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700725 res = fuse_fs_init();
726 if (res)
727 goto err;
728
Miklos Szeredi334f4852005-09-09 13:10:27 -0700729 res = fuse_dev_init();
730 if (res)
731 goto err_fs_cleanup;
732
Miklos Szeredif543f252006-01-16 22:14:35 -0800733 res = fuse_sysfs_init();
734 if (res)
735 goto err_dev_cleanup;
736
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700737 return 0;
738
Miklos Szeredif543f252006-01-16 22:14:35 -0800739 err_dev_cleanup:
740 fuse_dev_cleanup();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700741 err_fs_cleanup:
742 fuse_fs_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700743 err:
744 return res;
745}
746
747static void __exit fuse_exit(void)
748{
749 printk(KERN_DEBUG "fuse exit\n");
750
Miklos Szeredif543f252006-01-16 22:14:35 -0800751 fuse_sysfs_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700752 fuse_fs_cleanup();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700753 fuse_dev_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700754}
755
756module_init(fuse_init);
757module_exit(fuse_exit);