]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - fs/fuse/inode.c
[PATCH] fuse: extend semantics of connected flag
[thirdparty/kernel/linux.git] / fs / fuse / inode.c
CommitLineData
d8a5ba45
MS
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
4
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>
d8a5ba45
MS
18#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
25spinlock_t fuse_lock;
26static kmem_cache_t *fuse_inode_cachep;
d8a5ba45
MS
27
28#define FUSE_SUPER_MAGIC 0x65735546
29
30struct fuse_mount_data {
31 int fd;
32 unsigned rootmode;
33 unsigned user_id;
87729a55 34 unsigned group_id;
5a533682
MS
35 unsigned fd_present : 1;
36 unsigned rootmode_present : 1;
37 unsigned user_id_present : 1;
38 unsigned group_id_present : 1;
1e9a4ed9 39 unsigned flags;
db50b96c 40 unsigned max_read;
d8a5ba45
MS
41};
42
43static struct inode *fuse_alloc_inode(struct super_block *sb)
44{
45 struct inode *inode;
46 struct fuse_inode *fi;
47
48 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
49 if (!inode)
50 return NULL;
51
52 fi = get_fuse_inode(inode);
53 fi->i_time = jiffies - 1;
54 fi->nodeid = 0;
9e6268db 55 fi->nlookup = 0;
e5e5558e
MS
56 fi->forget_req = fuse_request_alloc();
57 if (!fi->forget_req) {
58 kmem_cache_free(fuse_inode_cachep, inode);
59 return NULL;
60 }
d8a5ba45
MS
61
62 return inode;
63}
64
65static void fuse_destroy_inode(struct inode *inode)
66{
e5e5558e
MS
67 struct fuse_inode *fi = get_fuse_inode(inode);
68 if (fi->forget_req)
69 fuse_request_free(fi->forget_req);
d8a5ba45
MS
70 kmem_cache_free(fuse_inode_cachep, inode);
71}
72
73static void fuse_read_inode(struct inode *inode)
74{
75 /* No op */
76}
77
e5e5558e 78void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
9e6268db 79 unsigned long nodeid, u64 nlookup)
e5e5558e
MS
80{
81 struct fuse_forget_in *inarg = &req->misc.forget_in;
9e6268db 82 inarg->nlookup = nlookup;
e5e5558e
MS
83 req->in.h.opcode = FUSE_FORGET;
84 req->in.h.nodeid = nodeid;
85 req->in.numargs = 1;
86 req->in.args[0].size = sizeof(struct fuse_forget_in);
87 req->in.args[0].value = inarg;
88 request_send_noreply(fc, req);
89}
90
d8a5ba45
MS
91static void fuse_clear_inode(struct inode *inode)
92{
1e9a4ed9
MS
93 if (inode->i_sb->s_flags & MS_ACTIVE) {
94 struct fuse_conn *fc = get_fuse_conn(inode);
e5e5558e 95 struct fuse_inode *fi = get_fuse_inode(inode);
9e6268db 96 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
e5e5558e
MS
97 fi->forget_req = NULL;
98 }
d8a5ba45
MS
99}
100
101void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
102{
103 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
104 invalidate_inode_pages(inode->i_mapping);
105
106 inode->i_ino = attr->ino;
107 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
108 inode->i_nlink = attr->nlink;
109 inode->i_uid = attr->uid;
110 inode->i_gid = attr->gid;
111 i_size_write(inode, attr->size);
112 inode->i_blksize = PAGE_CACHE_SIZE;
113 inode->i_blocks = attr->blocks;
114 inode->i_atime.tv_sec = attr->atime;
115 inode->i_atime.tv_nsec = attr->atimensec;
116 inode->i_mtime.tv_sec = attr->mtime;
117 inode->i_mtime.tv_nsec = attr->mtimensec;
118 inode->i_ctime.tv_sec = attr->ctime;
119 inode->i_ctime.tv_nsec = attr->ctimensec;
120}
121
122static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
123{
124 inode->i_mode = attr->mode & S_IFMT;
125 i_size_write(inode, attr->size);
e5e5558e
MS
126 if (S_ISREG(inode->i_mode)) {
127 fuse_init_common(inode);
b6aeaded 128 fuse_init_file_inode(inode);
e5e5558e
MS
129 } else if (S_ISDIR(inode->i_mode))
130 fuse_init_dir(inode);
131 else if (S_ISLNK(inode->i_mode))
132 fuse_init_symlink(inode);
133 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
134 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
135 fuse_init_common(inode);
136 init_special_inode(inode, inode->i_mode,
137 new_decode_dev(attr->rdev));
39ee059a
MS
138 } else
139 BUG();
d8a5ba45
MS
140}
141
142static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
143{
144 unsigned long nodeid = *(unsigned long *) _nodeidp;
145 if (get_node_id(inode) == nodeid)
146 return 1;
147 else
148 return 0;
149}
150
151static int fuse_inode_set(struct inode *inode, void *_nodeidp)
152{
153 unsigned long nodeid = *(unsigned long *) _nodeidp;
154 get_fuse_inode(inode)->nodeid = nodeid;
155 return 0;
156}
157
158struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
9e6268db 159 int generation, struct fuse_attr *attr)
d8a5ba45
MS
160{
161 struct inode *inode;
9e6268db 162 struct fuse_inode *fi;
d8a5ba45
MS
163 struct fuse_conn *fc = get_fuse_conn_super(sb);
164 int retried = 0;
165
166 retry:
167 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
168 if (!inode)
169 return NULL;
170
171 if ((inode->i_state & I_NEW)) {
b36c31ba 172 inode->i_flags |= S_NOATIME|S_NOCMTIME;
d8a5ba45
MS
173 inode->i_generation = generation;
174 inode->i_data.backing_dev_info = &fc->bdi;
175 fuse_init_inode(inode, attr);
176 unlock_new_inode(inode);
177 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
178 BUG_ON(retried);
179 /* Inode has changed type, any I/O on the old should fail */
180 make_bad_inode(inode);
181 iput(inode);
182 retried = 1;
183 goto retry;
184 }
185
9e6268db
MS
186 fi = get_fuse_inode(inode);
187 fi->nlookup ++;
d8a5ba45 188 fuse_change_attributes(inode, attr);
d8a5ba45
MS
189 return inode;
190}
191
192static void fuse_put_super(struct super_block *sb)
193{
194 struct fuse_conn *fc = get_fuse_conn_super(sb);
195
1e9a4ed9
MS
196 down_write(&fc->sbput_sem);
197 while (!list_empty(&fc->background))
198 fuse_release_background(list_entry(fc->background.next,
199 struct fuse_req, bg_entry));
200
d8a5ba45 201 spin_lock(&fuse_lock);
1e9a4ed9 202 fc->mounted = 0;
9ba7cbba 203 fc->connected = 0;
334f485d
MS
204 /* Flush all readers on this fs */
205 wake_up_all(&fc->waitq);
1e9a4ed9 206 up_write(&fc->sbput_sem);
d8a5ba45 207 fuse_release_conn(fc);
d8a5ba45
MS
208 spin_unlock(&fuse_lock);
209}
210
e5e5558e
MS
211static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
212{
213 stbuf->f_type = FUSE_SUPER_MAGIC;
214 stbuf->f_bsize = attr->bsize;
de5f1202 215 stbuf->f_frsize = attr->frsize;
e5e5558e
MS
216 stbuf->f_blocks = attr->blocks;
217 stbuf->f_bfree = attr->bfree;
218 stbuf->f_bavail = attr->bavail;
219 stbuf->f_files = attr->files;
220 stbuf->f_ffree = attr->ffree;
221 stbuf->f_namelen = attr->namelen;
222 /* fsid is left zero */
223}
224
225static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
226{
227 struct fuse_conn *fc = get_fuse_conn_super(sb);
228 struct fuse_req *req;
229 struct fuse_statfs_out outarg;
230 int err;
231
232 req = fuse_get_request(fc);
233 if (!req)
7c352bdf 234 return -EINTR;
e5e5558e 235
de5f1202 236 memset(&outarg, 0, sizeof(outarg));
e5e5558e
MS
237 req->in.numargs = 0;
238 req->in.h.opcode = FUSE_STATFS;
239 req->out.numargs = 1;
de5f1202
MS
240 req->out.args[0].size =
241 fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
e5e5558e
MS
242 req->out.args[0].value = &outarg;
243 request_send(fc, req);
244 err = req->out.h.error;
245 if (!err)
246 convert_fuse_statfs(buf, &outarg.st);
247 fuse_put_request(fc, req);
248 return err;
249}
250
d8a5ba45
MS
251enum {
252 OPT_FD,
253 OPT_ROOTMODE,
254 OPT_USER_ID,
87729a55 255 OPT_GROUP_ID,
d8a5ba45
MS
256 OPT_DEFAULT_PERMISSIONS,
257 OPT_ALLOW_OTHER,
db50b96c 258 OPT_MAX_READ,
d8a5ba45
MS
259 OPT_ERR
260};
261
262static match_table_t tokens = {
263 {OPT_FD, "fd=%u"},
264 {OPT_ROOTMODE, "rootmode=%o"},
265 {OPT_USER_ID, "user_id=%u"},
87729a55 266 {OPT_GROUP_ID, "group_id=%u"},
d8a5ba45
MS
267 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
268 {OPT_ALLOW_OTHER, "allow_other"},
db50b96c 269 {OPT_MAX_READ, "max_read=%u"},
d8a5ba45
MS
270 {OPT_ERR, NULL}
271};
272
273static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
274{
275 char *p;
276 memset(d, 0, sizeof(struct fuse_mount_data));
db50b96c 277 d->max_read = ~0;
d8a5ba45
MS
278
279 while ((p = strsep(&opt, ",")) != NULL) {
280 int token;
281 int value;
282 substring_t args[MAX_OPT_ARGS];
283 if (!*p)
284 continue;
285
286 token = match_token(p, tokens, args);
287 switch (token) {
288 case OPT_FD:
289 if (match_int(&args[0], &value))
290 return 0;
291 d->fd = value;
5a533682 292 d->fd_present = 1;
d8a5ba45
MS
293 break;
294
295 case OPT_ROOTMODE:
296 if (match_octal(&args[0], &value))
297 return 0;
298 d->rootmode = value;
5a533682 299 d->rootmode_present = 1;
d8a5ba45
MS
300 break;
301
302 case OPT_USER_ID:
303 if (match_int(&args[0], &value))
304 return 0;
305 d->user_id = value;
5a533682 306 d->user_id_present = 1;
d8a5ba45
MS
307 break;
308
87729a55
MS
309 case OPT_GROUP_ID:
310 if (match_int(&args[0], &value))
311 return 0;
312 d->group_id = value;
5a533682 313 d->group_id_present = 1;
87729a55
MS
314 break;
315
1e9a4ed9
MS
316 case OPT_DEFAULT_PERMISSIONS:
317 d->flags |= FUSE_DEFAULT_PERMISSIONS;
318 break;
319
320 case OPT_ALLOW_OTHER:
321 d->flags |= FUSE_ALLOW_OTHER;
322 break;
323
db50b96c
MS
324 case OPT_MAX_READ:
325 if (match_int(&args[0], &value))
326 return 0;
327 d->max_read = value;
328 break;
329
d8a5ba45
MS
330 default:
331 return 0;
332 }
333 }
5a533682
MS
334
335 if (!d->fd_present || !d->rootmode_present ||
336 !d->user_id_present || !d->group_id_present)
d8a5ba45
MS
337 return 0;
338
339 return 1;
340}
341
342static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
343{
344 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
345
346 seq_printf(m, ",user_id=%u", fc->user_id);
87729a55 347 seq_printf(m, ",group_id=%u", fc->group_id);
1e9a4ed9
MS
348 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
349 seq_puts(m, ",default_permissions");
350 if (fc->flags & FUSE_ALLOW_OTHER)
351 seq_puts(m, ",allow_other");
db50b96c
MS
352 if (fc->max_read != ~0)
353 seq_printf(m, ",max_read=%u", fc->max_read);
d8a5ba45
MS
354 return 0;
355}
356
334f485d 357static void free_conn(struct fuse_conn *fc)
d8a5ba45 358{
334f485d
MS
359 while (!list_empty(&fc->unused_list)) {
360 struct fuse_req *req;
361 req = list_entry(fc->unused_list.next, struct fuse_req, list);
362 list_del(&req->list);
363 fuse_request_free(req);
364 }
d8a5ba45
MS
365 kfree(fc);
366}
367
334f485d
MS
368/* Must be called with the fuse lock held */
369void fuse_release_conn(struct fuse_conn *fc)
370{
1e9a4ed9
MS
371 fc->count--;
372 if (!fc->count)
334f485d
MS
373 free_conn(fc);
374}
375
d8a5ba45
MS
376static struct fuse_conn *new_conn(void)
377{
378 struct fuse_conn *fc;
379
6383bdaa 380 fc = kzalloc(sizeof(*fc), GFP_KERNEL);
d8a5ba45 381 if (fc != NULL) {
334f485d 382 int i;
334f485d
MS
383 init_waitqueue_head(&fc->waitq);
384 INIT_LIST_HEAD(&fc->pending);
385 INIT_LIST_HEAD(&fc->processing);
d77a1d5b 386 INIT_LIST_HEAD(&fc->io);
334f485d 387 INIT_LIST_HEAD(&fc->unused_list);
1e9a4ed9 388 INIT_LIST_HEAD(&fc->background);
6383bdaa 389 sema_init(&fc->outstanding_sem, 1); /* One for INIT */
1e9a4ed9 390 init_rwsem(&fc->sbput_sem);
334f485d
MS
391 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
392 struct fuse_req *req = fuse_request_alloc();
393 if (!req) {
394 free_conn(fc);
395 return NULL;
396 }
397 list_add(&req->list, &fc->unused_list);
398 }
d8a5ba45
MS
399 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
400 fc->bdi.unplug_io_fn = default_unplug_io_fn;
334f485d 401 fc->reqctr = 0;
d8a5ba45
MS
402 }
403 return fc;
404}
405
406static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
407{
408 struct fuse_conn *fc;
409
334f485d
MS
410 if (file->f_op != &fuse_dev_operations)
411 return ERR_PTR(-EINVAL);
d8a5ba45
MS
412 fc = new_conn();
413 if (fc == NULL)
334f485d 414 return ERR_PTR(-ENOMEM);
d8a5ba45 415 spin_lock(&fuse_lock);
334f485d
MS
416 if (file->private_data) {
417 free_conn(fc);
418 fc = ERR_PTR(-EINVAL);
419 } else {
420 file->private_data = fc;
6383bdaa 421 sb->s_fs_info = fc;
1e9a4ed9
MS
422 fc->mounted = 1;
423 fc->connected = 1;
424 fc->count = 2;
334f485d 425 }
d8a5ba45
MS
426 spin_unlock(&fuse_lock);
427 return fc;
428}
429
430static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
431{
432 struct fuse_attr attr;
433 memset(&attr, 0, sizeof(attr));
434
435 attr.mode = mode;
436 attr.ino = FUSE_ROOT_ID;
9e6268db 437 return fuse_iget(sb, 1, 0, &attr);
d8a5ba45
MS
438}
439
440static struct super_operations fuse_super_operations = {
441 .alloc_inode = fuse_alloc_inode,
442 .destroy_inode = fuse_destroy_inode,
443 .read_inode = fuse_read_inode,
444 .clear_inode = fuse_clear_inode,
445 .put_super = fuse_put_super,
e5e5558e 446 .statfs = fuse_statfs,
d8a5ba45
MS
447 .show_options = fuse_show_options,
448};
449
d8a5ba45
MS
450static int fuse_fill_super(struct super_block *sb, void *data, int silent)
451{
452 struct fuse_conn *fc;
453 struct inode *root;
454 struct fuse_mount_data d;
455 struct file *file;
456 int err;
457
458 if (!parse_fuse_opt((char *) data, &d))
459 return -EINVAL;
460
461 sb->s_blocksize = PAGE_CACHE_SIZE;
462 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
463 sb->s_magic = FUSE_SUPER_MAGIC;
464 sb->s_op = &fuse_super_operations;
465 sb->s_maxbytes = MAX_LFS_FILESIZE;
466
467 file = fget(d.fd);
468 if (!file)
469 return -EINVAL;
470
471 fc = get_conn(file, sb);
472 fput(file);
334f485d
MS
473 if (IS_ERR(fc))
474 return PTR_ERR(fc);
d8a5ba45 475
1e9a4ed9 476 fc->flags = d.flags;
d8a5ba45 477 fc->user_id = d.user_id;
87729a55 478 fc->group_id = d.group_id;
db50b96c
MS
479 fc->max_read = d.max_read;
480 if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages)
481 fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE;
d8a5ba45 482
d8a5ba45
MS
483 err = -ENOMEM;
484 root = get_root_inode(sb, d.rootmode);
485 if (root == NULL)
486 goto err;
487
488 sb->s_root = d_alloc_root(root);
489 if (!sb->s_root) {
490 iput(root);
491 goto err;
492 }
334f485d 493 fuse_send_init(fc);
d8a5ba45
MS
494 return 0;
495
496 err:
497 spin_lock(&fuse_lock);
d8a5ba45
MS
498 fuse_release_conn(fc);
499 spin_unlock(&fuse_lock);
d8a5ba45
MS
500 return err;
501}
502
503static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
504 int flags, const char *dev_name,
505 void *raw_data)
506{
507 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
508}
509
510static struct file_system_type fuse_fs_type = {
511 .owner = THIS_MODULE,
512 .name = "fuse",
513 .get_sb = fuse_get_sb,
514 .kill_sb = kill_anon_super,
515};
516
517static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
518 unsigned long flags)
519{
520 struct inode * inode = foo;
521
522 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
523 SLAB_CTOR_CONSTRUCTOR)
524 inode_init_once(inode);
525}
526
527static int __init fuse_fs_init(void)
528{
529 int err;
530
531 err = register_filesystem(&fuse_fs_type);
532 if (err)
533 printk("fuse: failed to register filesystem\n");
534 else {
535 fuse_inode_cachep = kmem_cache_create("fuse_inode",
536 sizeof(struct fuse_inode),
537 0, SLAB_HWCACHE_ALIGN,
538 fuse_inode_init_once, NULL);
539 if (!fuse_inode_cachep) {
540 unregister_filesystem(&fuse_fs_type);
541 err = -ENOMEM;
542 }
543 }
544
545 return err;
546}
547
548static void fuse_fs_cleanup(void)
549{
550 unregister_filesystem(&fuse_fs_type);
551 kmem_cache_destroy(fuse_inode_cachep);
552}
553
554static int __init fuse_init(void)
555{
556 int res;
557
558 printk("fuse init (API version %i.%i)\n",
559 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
560
561 spin_lock_init(&fuse_lock);
562 res = fuse_fs_init();
563 if (res)
564 goto err;
565
334f485d
MS
566 res = fuse_dev_init();
567 if (res)
568 goto err_fs_cleanup;
569
d8a5ba45
MS
570 return 0;
571
334f485d
MS
572 err_fs_cleanup:
573 fuse_fs_cleanup();
d8a5ba45
MS
574 err:
575 return res;
576}
577
578static void __exit fuse_exit(void)
579{
580 printk(KERN_DEBUG "fuse exit\n");
581
582 fuse_fs_cleanup();
334f485d 583 fuse_dev_cleanup();
d8a5ba45
MS
584}
585
586module_init(fuse_init);
587module_exit(fuse_exit);