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