]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - fs/file_table.c
fs: cleanup files_lock locking
[net-next-2.6.git] / fs / file_table.c
index b8a0bb63cbd7c877cd69ac3b445c385ed2e60ba3..6f0e62ecfdddfd29bba313e1387ab06656acf0e5 100644 (file)
@@ -32,8 +32,7 @@ struct files_stat_struct files_stat = {
        .max_files = NR_FILE
 };
 
-/* public. Not pretty! */
-__cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
+static __cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
 
 /* SLAB cache for file structures */
 static struct kmem_cache *filp_cachep __read_mostly;
@@ -230,15 +229,6 @@ static void __fput(struct file *file)
        might_sleep();
 
        fsnotify_close(file);
-
-       /*
-        * fsnotify_create_event may have taken one or more references on this
-        * file.  If it did so it left one reference for us to drop to make sure
-        * its calls to fput could not prematurely destroy the file.
-        */
-       if (atomic_long_read(&file->f_count))
-               return fput(file);
-
        /*
         * The function eventpoll_release() should be the first called
         * in the file cleanup chain.
@@ -258,7 +248,7 @@ static void __fput(struct file *file)
                cdev_put(inode->i_cdev);
        fops_put(file->f_op);
        put_pid(file->f_owner.pid);
-       file_kill(file);
+       file_sb_list_del(file);
        if (file->f_mode & FMODE_WRITE)
                drop_file_write_access(file);
        file->f_path.dentry = NULL;
@@ -298,11 +288,20 @@ struct file *fget(unsigned int fd)
 EXPORT_SYMBOL(fget);
 
 /*
- * Lightweight file lookup - no refcnt increment if fd table isn't shared. 
- * You can use this only if it is guranteed that the current task already 
- * holds a refcnt to that file. That check has to be done at fget() only
- * and a flag is returned to be passed to the corresponding fput_light().
- * There must not be a cloning between an fget_light/fput_light pair.
+ * Lightweight file lookup - no refcnt increment if fd table isn't shared.
+ *
+ * You can use this instead of fget if you satisfy all of the following
+ * conditions:
+ * 1) You must call fput_light before exiting the syscall and returning control
+ *    to userspace (i.e. you cannot remember the returned struct file * after
+ *    returning to userspace).
+ * 2) You must not call filp_close on the returned struct file * in between
+ *    calls to fget_light and fput_light.
+ * 3) You must not clone the current task in between the calls to fget_light
+ *    and fput_light.
+ *
+ * The fput_needed flag returned by fget_light should be passed to the
+ * corresponding fput_light.
  */
 struct file *fget_light(unsigned int fd, int *fput_needed)
 {
@@ -328,31 +327,29 @@ struct file *fget_light(unsigned int fd, int *fput_needed)
        return file;
 }
 
-
 void put_filp(struct file *file)
 {
        if (atomic_long_dec_and_test(&file->f_count)) {
                security_file_free(file);
-               file_kill(file);
+               file_sb_list_del(file);
                file_free(file);
        }
 }
 
-void file_move(struct file *file, struct list_head *list)
+void file_sb_list_add(struct file *file, struct super_block *sb)
 {
-       if (!list)
-               return;
-       file_list_lock();
-       list_move(&file->f_u.fu_list, list);
-       file_list_unlock();
+       spin_lock(&files_lock);
+       BUG_ON(!list_empty(&file->f_u.fu_list));
+       list_add(&file->f_u.fu_list, &sb->s_files);
+       spin_unlock(&files_lock);
 }
 
-void file_kill(struct file *file)
+void file_sb_list_del(struct file *file)
 {
        if (!list_empty(&file->f_u.fu_list)) {
-               file_list_lock();
+               spin_lock(&files_lock);
                list_del_init(&file->f_u.fu_list);
-               file_list_unlock();
+               spin_unlock(&files_lock);
        }
 }
 
@@ -361,7 +358,7 @@ int fs_may_remount_ro(struct super_block *sb)
        struct file *file;
 
        /* Check that no files are currently opened for writing. */
-       file_list_lock();
+       spin_lock(&files_lock);
        list_for_each_entry(file, &sb->s_files, f_u.fu_list) {
                struct inode *inode = file->f_path.dentry->d_inode;
 
@@ -373,10 +370,10 @@ int fs_may_remount_ro(struct super_block *sb)
                if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
                        goto too_bad;
        }
-       file_list_unlock();
+       spin_unlock(&files_lock);
        return 1; /* Tis' cool bro. */
 too_bad:
-       file_list_unlock();
+       spin_unlock(&files_lock);
        return 0;
 }
 
@@ -392,7 +389,7 @@ void mark_files_ro(struct super_block *sb)
        struct file *f;
 
 retry:
-       file_list_lock();
+       spin_lock(&files_lock);
        list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
                struct vfsmount *mnt;
                if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
@@ -408,16 +405,13 @@ retry:
                        continue;
                file_release_write(f);
                mnt = mntget(f->f_path.mnt);
-               file_list_unlock();
-               /*
-                * This can sleep, so we can't hold
-                * the file_list_lock() spinlock.
-                */
+               /* This can sleep, so we can't hold the spinlock. */
+               spin_unlock(&files_lock);
                mnt_drop_write(mnt);
                mntput(mnt);
                goto retry;
        }
-       file_list_unlock();
+       spin_unlock(&files_lock);
 }
 
 void __init files_init(unsigned long mempages)