drm: lindent the drm directory.
[safe/jmp/linux-2.6] / fs / file_table.c
index 03d83cb..86ec8ae 100644 (file)
 #include <linux/fs.h>
 #include <linux/security.h>
 #include <linux/eventpoll.h>
+#include <linux/rcupdate.h>
 #include <linux/mount.h>
 #include <linux/cdev.h>
+#include <linux/fsnotify.h>
 
 /* sysctl tunables... */
 struct files_stat_struct files_stat = {
@@ -52,53 +54,61 @@ void filp_dtor(void * objp, struct kmem_cache_s *cachep, unsigned long dflags)
        spin_unlock_irqrestore(&filp_count_lock, flags);
 }
 
-static inline void file_free(struct file *f)
+static inline void file_free_rcu(struct rcu_head *head)
 {
+       struct file *f =  container_of(head, struct file, f_rcuhead);
        kmem_cache_free(filp_cachep, f);
 }
 
+static inline void file_free(struct file *f)
+{
+       call_rcu(&f->f_rcuhead, file_free_rcu);
+}
+
 /* Find an unused file structure and return a pointer to it.
  * Returns NULL, if there are no more free file structures or
  * we run out of memory.
  */
 struct file *get_empty_filp(void)
 {
-static int old_max;
+       static int old_max;
        struct file * f;
 
        /*
         * Privileged users can go above max_files
         */
-       if (files_stat.nr_files < files_stat.max_files ||
-                               capable(CAP_SYS_ADMIN)) {
-               f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
-               if (f) {
-                       memset(f, 0, sizeof(*f));
-                       if (security_file_alloc(f)) {
-                               file_free(f);
-                               goto fail;
-                       }
-                       eventpoll_init_file(f);
-                       atomic_set(&f->f_count, 1);
-                       f->f_uid = current->fsuid;
-                       f->f_gid = current->fsgid;
-                       rwlock_init(&f->f_owner.lock);
-                       /* f->f_version: 0 */
-                       INIT_LIST_HEAD(&f->f_list);
-                       f->f_maxcount = INT_MAX;
-                       return f;
-               }
-       }
-
+       if (files_stat.nr_files >= files_stat.max_files &&
+                               !capable(CAP_SYS_ADMIN))
+               goto over;
+
+       f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
+       if (f == NULL)
+               goto fail;
+
+       memset(f, 0, sizeof(*f));
+       if (security_file_alloc(f))
+               goto fail_sec;
+
+       eventpoll_init_file(f);
+       atomic_set(&f->f_count, 1);
+       f->f_uid = current->fsuid;
+       f->f_gid = current->fsgid;
+       rwlock_init(&f->f_owner.lock);
+       /* f->f_version: 0 */
+       INIT_LIST_HEAD(&f->f_list);
+       return f;
+
+over:
        /* Ran out of filps - report that */
-       if (files_stat.max_files >= old_max) {
+       if (files_stat.nr_files > old_max) {
                printk(KERN_INFO "VFS: file-max limit %d reached\n",
                                        files_stat.max_files);
-               old_max = files_stat.max_files;
-       } else {
-               /* Big problems... */
-               printk(KERN_WARNING "VFS: filp allocation failed\n");
+               old_max = files_stat.nr_files;
        }
+       goto fail;
+
+fail_sec:
+       file_free(f);
 fail:
        return NULL;
 }
@@ -107,7 +117,7 @@ EXPORT_SYMBOL(get_empty_filp);
 
 void fastcall fput(struct file *file)
 {
-       if (atomic_dec_and_test(&file->f_count))
+       if (rcuref_dec_and_test(&file->f_count))
                __fput(file);
 }
 
@@ -123,6 +133,8 @@ void fastcall __fput(struct file *file)
        struct inode *inode = dentry->d_inode;
 
        might_sleep();
+
+       fsnotify_close(file);
        /*
         * The function eventpoll_release() should be the first called
         * in the file cleanup chain.
@@ -151,11 +163,17 @@ struct file fastcall *fget(unsigned int fd)
        struct file *file;
        struct files_struct *files = current->files;
 
-       spin_lock(&files->file_lock);
+       rcu_read_lock();
        file = fcheck_files(files, fd);
-       if (file)
-               get_file(file);
-       spin_unlock(&files->file_lock);
+       if (file) {
+               if (!rcuref_inc_lf(&file->f_count)) {
+                       /* File object ref couldn't be taken */
+                       rcu_read_unlock();
+                       return NULL;
+               }
+       }
+       rcu_read_unlock();
+
        return file;
 }
 
@@ -177,21 +195,25 @@ struct file fastcall *fget_light(unsigned int fd, int *fput_needed)
        if (likely((atomic_read(&files->count) == 1))) {
                file = fcheck_files(files, fd);
        } else {
-               spin_lock(&files->file_lock);
+               rcu_read_lock();
                file = fcheck_files(files, fd);
                if (file) {
-                       get_file(file);
-                       *fput_needed = 1;
+                       if (rcuref_inc_lf(&file->f_count))
+                               *fput_needed = 1;
+                       else
+                               /* Didn't get the reference, someone's freed */
+                               file = NULL;
                }
-               spin_unlock(&files->file_lock);
+               rcu_read_unlock();
        }
+
        return file;
 }
 
 
 void put_filp(struct file *file)
 {
-       if (atomic_dec_and_test(&file->f_count)) {
+       if (rcuref_dec_and_test(&file->f_count)) {
                security_file_free(file);
                file_kill(file);
                file_free(file);
@@ -252,4 +274,5 @@ void __init files_init(unsigned long mempages)
        files_stat.max_files = n; 
        if (files_stat.max_files < NR_FILE)
                files_stat.max_files = NR_FILE;
+       files_defer_init();
 }