[PATCH] fuse: scramble lock owner ID
[pandora-kernel.git] / fs / fuse / inode.c
index 879e6fb..5ceb8bd 100644 (file)
@@ -1,6 +1,6 @@
 /*
   FUSE: Filesystem in Userspace
-  Copyright (C) 2001-2005  Miklos Szeredi <miklos@szeredi.hu>
+  Copyright (C) 2001-2006  Miklos Szeredi <miklos@szeredi.hu>
 
   This program can be distributed under the terms of the GNU GPL.
   See the file COPYING.
 #include <linux/pagemap.h>
 #include <linux/slab.h>
 #include <linux/file.h>
-#include <linux/mount.h>
 #include <linux/seq_file.h>
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/parser.h>
 #include <linux/statfs.h>
+#include <linux/random.h>
 
 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
 MODULE_DESCRIPTION("Filesystem in Userspace");
 MODULE_LICENSE("GPL");
 
-spinlock_t fuse_lock;
 static kmem_cache_t *fuse_inode_cachep;
-static struct subsystem connections_subsys;
-
-struct fuse_conn_attr {
-       struct attribute attr;
-       ssize_t (*show)(struct fuse_conn *, char *);
-       ssize_t (*store)(struct fuse_conn *, const char *, size_t);
-};
+struct list_head fuse_conn_list;
+DEFINE_MUTEX(fuse_mutex);
 
 #define FUSE_SUPER_MAGIC 0x65735546
 
@@ -105,6 +99,14 @@ static void fuse_clear_inode(struct inode *inode)
        }
 }
 
+static int fuse_remount_fs(struct super_block *sb, int *flags, char *data)
+{
+       if (*flags & MS_MANDLOCK)
+               return -EINVAL;
+
+       return 0;
+}
+
 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
 {
        if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
@@ -205,20 +207,19 @@ static void fuse_put_super(struct super_block *sb)
 {
        struct fuse_conn *fc = get_fuse_conn_super(sb);
 
-       down_write(&fc->sbput_sem);
-       while (!list_empty(&fc->background))
-               fuse_release_background(list_entry(fc->background.next,
-                                                  struct fuse_req, bg_entry));
-
-       spin_lock(&fuse_lock);
-       fc->mounted = 0;
+       spin_lock(&fc->lock);
        fc->connected = 0;
-       spin_unlock(&fuse_lock);
-       up_write(&fc->sbput_sem);
+       fc->blocked = 0;
+       spin_unlock(&fc->lock);
        /* Flush all readers on this fs */
+       kill_fasync(&fc->fasync, SIGIO, POLL_IN);
        wake_up_all(&fc->waitq);
-       kobject_del(&fc->kobj);
-       kobject_put(&fc->kobj);
+       wake_up_all(&fc->blocked_waitq);
+       mutex_lock(&fuse_mutex);
+       list_del(&fc->entry);
+       fuse_ctl_remove_conn(fc);
+       mutex_unlock(&fuse_mutex);
+       fuse_conn_put(fc);
 }
 
 static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
@@ -235,16 +236,17 @@ static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr
        /* fsid is left zero */
 }
 
-static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
+static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
 {
+       struct super_block *sb = dentry->d_sb;
        struct fuse_conn *fc = get_fuse_conn_super(sb);
        struct fuse_req *req;
        struct fuse_statfs_out outarg;
        int err;
 
-        req = fuse_get_request(fc);
-       if (!req)
-               return -EINTR;
+       req = fuse_get_req(fc);
+       if (IS_ERR(req))
+               return PTR_ERR(req);
 
        memset(&outarg, 0, sizeof(outarg));
        req->in.numargs = 0;
@@ -367,81 +369,40 @@ static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
        return 0;
 }
 
-static void fuse_conn_release(struct kobject *kobj)
-{
-       struct fuse_conn *fc = get_fuse_conn_kobj(kobj);
-
-       while (!list_empty(&fc->unused_list)) {
-               struct fuse_req *req;
-               req = list_entry(fc->unused_list.next, struct fuse_req, list);
-               list_del(&req->list);
-               fuse_request_free(req);
-       }
-       kfree(fc);
-}
-
 static struct fuse_conn *new_conn(void)
 {
        struct fuse_conn *fc;
 
        fc = kzalloc(sizeof(*fc), GFP_KERNEL);
        if (fc) {
-               int i;
+               spin_lock_init(&fc->lock);
+               atomic_set(&fc->count, 1);
                init_waitqueue_head(&fc->waitq);
+               init_waitqueue_head(&fc->blocked_waitq);
                INIT_LIST_HEAD(&fc->pending);
                INIT_LIST_HEAD(&fc->processing);
                INIT_LIST_HEAD(&fc->io);
-               INIT_LIST_HEAD(&fc->unused_list);
-               INIT_LIST_HEAD(&fc->background);
-               sema_init(&fc->outstanding_sem, 1); /* One for INIT */
-               init_rwsem(&fc->sbput_sem);
-               kobj_set_kset_s(fc, connections_subsys);
-               kobject_init(&fc->kobj);
+               INIT_LIST_HEAD(&fc->interrupts);
                atomic_set(&fc->num_waiting, 0);
-               for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
-                       struct fuse_req *req = fuse_request_alloc();
-                       if (!req) {
-                               kobject_put(&fc->kobj);
-                               return NULL;
-                       }
-                       list_add(&req->list, &fc->unused_list);
-               }
                fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
                fc->bdi.unplug_io_fn = default_unplug_io_fn;
                fc->reqctr = 0;
+               fc->blocked = 1;
+               get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
        }
        return fc;
 }
 
-static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
+void fuse_conn_put(struct fuse_conn *fc)
 {
-       struct fuse_conn *fc;
-       int err;
-
-       err = -EINVAL;
-       if (file->f_op != &fuse_dev_operations)
-               goto out_err;
-
-       err = -ENOMEM;
-       fc = new_conn();
-       if (!fc)
-               goto out_err;
-
-       spin_lock(&fuse_lock);
-       err = -EINVAL;
-       if (file->private_data)
-               goto out_unlock;
+       if (atomic_dec_and_test(&fc->count))
+               kfree(fc);
+}
 
-       kobject_get(&fc->kobj);
-       file->private_data = fc;
-       spin_unlock(&fuse_lock);
+struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
+{
+       atomic_inc(&fc->count);
        return fc;
-
- out_unlock:
-       spin_unlock(&fuse_lock);
-       kobject_put(&fc->kobj);
- out_err:
-       return ERR_PTR(err);
 }
 
 static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
@@ -459,6 +420,7 @@ static struct super_operations fuse_super_operations = {
        .destroy_inode  = fuse_destroy_inode,
        .read_inode     = fuse_read_inode,
        .clear_inode    = fuse_clear_inode,
+       .remount_fs     = fuse_remount_fs,
        .put_super      = fuse_put_super,
        .umount_begin   = fuse_umount_begin,
        .statfs         = fuse_statfs,
@@ -467,7 +429,6 @@ static struct super_operations fuse_super_operations = {
 
 static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
 {
-       int i;
        struct fuse_init_out *arg = &req->misc.init_out;
 
        if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
@@ -479,35 +440,30 @@ static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
                        ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
                        if (arg->flags & FUSE_ASYNC_READ)
                                fc->async_read = 1;
-               } else
+                       if (!(arg->flags & FUSE_POSIX_LOCKS))
+                               fc->no_lock = 1;
+               } else {
                        ra_pages = fc->max_read / PAGE_CACHE_SIZE;
+                       fc->no_lock = 1;
+               }
 
                fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
                fc->minor = arg->minor;
                fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
        }
-
-       /* After INIT reply is received other requests can go
-          out.  So do (FUSE_MAX_OUTSTANDING - 1) number of
-          up()s on outstanding_sem.  The last up() is done in
-          fuse_putback_request() */
-       for (i = 1; i < FUSE_MAX_OUTSTANDING; i++)
-               up(&fc->outstanding_sem);
-
        fuse_put_request(fc, req);
+       fc->blocked = 0;
+       wake_up_all(&fc->blocked_waitq);
 }
 
-static void fuse_send_init(struct fuse_conn *fc)
+static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
 {
-       /* This is called from fuse_read_super() so there's guaranteed
-          to be exactly one request available */
-       struct fuse_req *req = fuse_get_request(fc);
        struct fuse_init_in *arg = &req->misc.init_in;
 
        arg->major = FUSE_KERNEL_VERSION;
        arg->minor = FUSE_KERNEL_MINOR_VERSION;
        arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
-       arg->flags |= FUSE_ASYNC_READ;
+       arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS;
        req->in.h.opcode = FUSE_INIT;
        req->in.numargs = 1;
        req->in.args[0].size = sizeof(*arg);
@@ -523,14 +479,10 @@ static void fuse_send_init(struct fuse_conn *fc)
        request_send_background(fc, req);
 }
 
-static unsigned long long conn_id(void)
+static u64 conn_id(void)
 {
-       static unsigned long long ctr = 1;
-       unsigned long long val;
-       spin_lock(&fuse_lock);
-       val = ctr++;
-       spin_unlock(&fuse_lock);
-       return val;
+       static u64 ctr = 1;
+       return ctr++;
 }
 
 static int fuse_fill_super(struct super_block *sb, void *data, int silent)
@@ -540,8 +492,12 @@ static int fuse_fill_super(struct super_block *sb, void *data, int silent)
        struct fuse_mount_data d;
        struct file *file;
        struct dentry *root_dentry;
+       struct fuse_req *init_req;
        int err;
 
+       if (sb->s_flags & MS_MANDLOCK)
+               return -EINVAL;
+
        if (!parse_fuse_opt((char *) data, &d))
                return -EINVAL;
 
@@ -555,10 +511,12 @@ static int fuse_fill_super(struct super_block *sb, void *data, int silent)
        if (!file)
                return -EINVAL;
 
-       fc = get_conn(file, sb);
-       fput(file);
-       if (IS_ERR(fc))
-               return PTR_ERR(fc);
+       if (file->f_op != &fuse_dev_operations)
+               return -EINVAL;
+
+       fc = new_conn();
+       if (!fc)
+               return -ENOMEM;
 
        fc->flags = d.flags;
        fc->user_id = d.user_id;
@@ -579,36 +537,52 @@ static int fuse_fill_super(struct super_block *sb, void *data, int silent)
                goto err;
        }
 
-       err = kobject_set_name(&fc->kobj, "%llu", conn_id());
-       if (err)
+       init_req = fuse_request_alloc();
+       if (!init_req)
                goto err_put_root;
 
-       err = kobject_add(&fc->kobj);
+       mutex_lock(&fuse_mutex);
+       err = -EINVAL;
+       if (file->private_data)
+               goto err_unlock;
+
+       fc->id = conn_id();
+       err = fuse_ctl_add_conn(fc);
        if (err)
-               goto err_put_root;
+               goto err_unlock;
 
+       list_add_tail(&fc->entry, &fuse_conn_list);
        sb->s_root = root_dentry;
-       spin_lock(&fuse_lock);
-       fc->mounted = 1;
        fc->connected = 1;
-       spin_unlock(&fuse_lock);
+       file->private_data = fuse_conn_get(fc);
+       mutex_unlock(&fuse_mutex);
+       /*
+        * atomic_dec_and_test() in fput() provides the necessary
+        * memory barrier for file->private_data to be visible on all
+        * CPUs after this
+        */
+       fput(file);
 
-       fuse_send_init(fc);
+       fuse_send_init(fc, init_req);
 
        return 0;
 
+ err_unlock:
+       mutex_unlock(&fuse_mutex);
+       fuse_request_free(init_req);
  err_put_root:
        dput(root_dentry);
  err:
-       kobject_put(&fc->kobj);
+       fput(file);
+       fuse_conn_put(fc);
        return err;
 }
 
-static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
-                                      int flags, const char *dev_name,
-                                      void *raw_data)
+static int fuse_get_sb(struct file_system_type *fs_type,
+                      int flags, const char *dev_name,
+                      void *raw_data, struct vfsmount *mnt)
 {
-       return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
+       return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt);
 }
 
 static struct file_system_type fuse_fs_type = {
@@ -618,68 +592,8 @@ static struct file_system_type fuse_fs_type = {
        .kill_sb        = kill_anon_super,
 };
 
-static ssize_t fuse_conn_waiting_show(struct fuse_conn *fc, char *page)
-{
-       return sprintf(page, "%i\n", atomic_read(&fc->num_waiting));
-}
-
-static ssize_t fuse_conn_abort_store(struct fuse_conn *fc, const char *page,
-                                    size_t count)
-{
-       fuse_abort_conn(fc);
-       return count;
-}
-
-static struct fuse_conn_attr fuse_conn_waiting =
-       __ATTR(waiting, 0400, fuse_conn_waiting_show, NULL);
-static struct fuse_conn_attr fuse_conn_abort =
-       __ATTR(abort, 0600, NULL, fuse_conn_abort_store);
-
-static struct attribute *fuse_conn_attrs[] = {
-       &fuse_conn_waiting.attr,
-       &fuse_conn_abort.attr,
-       NULL,
-};
-
-static ssize_t fuse_conn_attr_show(struct kobject *kobj,
-                                  struct attribute *attr,
-                                  char *page)
-{
-       struct fuse_conn_attr *fca =
-               container_of(attr, struct fuse_conn_attr, attr);
-
-       if (fca->show)
-               return fca->show(get_fuse_conn_kobj(kobj), page);
-       else
-               return -EACCES;
-}
-
-static ssize_t fuse_conn_attr_store(struct kobject *kobj,
-                                   struct attribute *attr,
-                                   const char *page, size_t count)
-{
-       struct fuse_conn_attr *fca =
-               container_of(attr, struct fuse_conn_attr, attr);
-
-       if (fca->store)
-               return fca->store(get_fuse_conn_kobj(kobj), page, count);
-       else
-               return -EACCES;
-}
-
-static struct sysfs_ops fuse_conn_sysfs_ops = {
-       .show   = &fuse_conn_attr_show,
-       .store  = &fuse_conn_attr_store,
-};
-
-static struct kobj_type ktype_fuse_conn = {
-       .release        = fuse_conn_release,
-       .sysfs_ops      = &fuse_conn_sysfs_ops,
-       .default_attrs  = fuse_conn_attrs,
-};
-
 static decl_subsys(fuse, NULL, NULL);
-static decl_subsys(connections, &ktype_fuse_conn, NULL);
+static decl_subsys(connections, NULL, NULL);
 
 static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
                                 unsigned long flags)
@@ -753,7 +667,7 @@ static int __init fuse_init(void)
        printk("fuse init (API version %i.%i)\n",
               FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
 
-       spin_lock_init(&fuse_lock);
+       INIT_LIST_HEAD(&fuse_conn_list);
        res = fuse_fs_init();
        if (res)
                goto err;
@@ -766,8 +680,14 @@ static int __init fuse_init(void)
        if (res)
                goto err_dev_cleanup;
 
+       res = fuse_ctl_init();
+       if (res)
+               goto err_sysfs_cleanup;
+
        return 0;
 
+ err_sysfs_cleanup:
+       fuse_sysfs_cleanup();
  err_dev_cleanup:
        fuse_dev_cleanup();
  err_fs_cleanup:
@@ -780,6 +700,7 @@ static void __exit fuse_exit(void)
 {
        printk(KERN_DEBUG "fuse exit\n");
 
+       fuse_ctl_cleanup();
        fuse_sysfs_cleanup();
        fuse_fs_cleanup();
        fuse_dev_cleanup();