libertas: unify various CF-related defines
[safe/jmp/linux-2.6] / fs / fcntl.c
index 2bdaef3..bfd7765 100644 (file)
@@ -9,6 +9,7 @@
 #include <linux/mm.h>
 #include <linux/fs.h>
 #include <linux/file.h>
+#include <linux/fdtable.h>
 #include <linux/capability.h>
 #include <linux/dnotify.h>
 #include <linux/smp_lock.h>
 #include <linux/ptrace.h>
 #include <linux/signal.h>
 #include <linux/rcupdate.h>
+#include <linux/pid_namespace.h>
 
 #include <asm/poll.h>
 #include <asm/siginfo.h>
 #include <asm/uaccess.h>
 
-void fastcall set_close_on_exec(unsigned int fd, int flag)
+void set_close_on_exec(unsigned int fd, int flag)
 {
        struct files_struct *files = current->files;
        struct fdtable *fdt;
@@ -54,14 +56,16 @@ static int get_close_on_exec(unsigned int fd)
  * file_lock held for write.
  */
 
-static int locate_fd(struct files_struct *files, 
-                           struct file *file, unsigned int orig_start)
+static int locate_fd(unsigned int orig_start, int cloexec)
 {
+       struct files_struct *files = current->files;
        unsigned int newfd;
        unsigned int start;
        int error;
        struct fdtable *fdt;
 
+       spin_lock(&files->file_lock);
+
        error = -EINVAL;
        if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
                goto out;
@@ -77,10 +81,9 @@ repeat:
                start = files->next_fd;
 
        newfd = start;
-       if (start < fdt->max_fdset) {
+       if (start < fdt->max_fds)
                newfd = find_next_zero_bit(fdt->open_fds->fds_bits,
-                       fdt->max_fdset, start);
-       }
+                                          fdt->max_fds, start);
        
        error = -EMFILE;
        if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
@@ -97,39 +100,28 @@ repeat:
        if (error)
                goto repeat;
 
-       /*
-        * We reacquired files_lock, so we are safe as long as
-        * we reacquire the fdtable pointer and use it while holding
-        * the lock, no one can free it during that time.
-        */
        if (start <= files->next_fd)
                files->next_fd = newfd + 1;
 
+       FD_SET(newfd, fdt->open_fds);
+       if (cloexec)
+               FD_SET(newfd, fdt->close_on_exec);
+       else
+               FD_CLR(newfd, fdt->close_on_exec);
        error = newfd;
-       
+
 out:
+       spin_unlock(&files->file_lock);
        return error;
 }
 
-static int dupfd(struct file *file, unsigned int start)
+static int dupfd(struct file *file, unsigned int start, int cloexec)
 {
-       struct files_struct * files = current->files;
-       struct fdtable *fdt;
-       int fd;
-
-       spin_lock(&files->file_lock);
-       fd = locate_fd(files, file, start);
-       if (fd >= 0) {
-               /* locate_fd() may have expanded fdtable, load the ptr */
-               fdt = files_fdtable(files);
-               FD_SET(fd, fdt->open_fds);
-               FD_CLR(fd, fdt->close_on_exec);
-               spin_unlock(&files->file_lock);
+       int fd = locate_fd(start, cloexec);
+       if (fd >= 0)
                fd_install(fd, file);
-       } else {
-               spin_unlock(&files->file_lock);
+       else
                fput(file);
-       }
 
        return fd;
 }
@@ -196,7 +188,7 @@ asmlinkage long sys_dup(unsigned int fildes)
        struct file * file = fget(fildes);
 
        if (file)
-               ret = dupfd(file, 0);
+               ret = dupfd(file, 0, 0);
        return ret;
 }
 
@@ -216,7 +208,7 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
 
        /* O_NOATIME can only be set by the owner or superuser */
        if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
-               if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
+               if (!is_owner_or_cap(inode))
                        return -EPERM;
 
        /* required for strict SunOS emulation */
@@ -290,7 +282,7 @@ int f_setown(struct file *filp, unsigned long arg, int force)
                who = -who;
        }
        rcu_read_lock();
-       pid = find_pid(who);
+       pid = find_vpid(who);
        result = __f_setown(filp, pid, type, force);
        rcu_read_unlock();
        return result;
@@ -306,7 +298,7 @@ pid_t f_getown(struct file *filp)
 {
        pid_t pid;
        read_lock(&filp->f_owner.lock);
-       pid = pid_nr(filp->f_owner.pid);
+       pid = pid_vnr(filp->f_owner.pid);
        if (filp->f_owner.pid_type == PIDTYPE_PGID)
                pid = -pid;
        read_unlock(&filp->f_owner.lock);
@@ -320,8 +312,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
 
        switch (cmd) {
        case F_DUPFD:
+       case F_DUPFD_CLOEXEC:
                get_file(filp);
-               err = dupfd(filp, arg);
+               err = dupfd(filp, arg, cmd == F_DUPFD_CLOEXEC);
                break;
        case F_GETFD:
                err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
@@ -639,7 +632,7 @@ EXPORT_SYMBOL(kill_fasync);
 static int __init fasync_init(void)
 {
        fasync_cache = kmem_cache_create("fasync_cache",
-               sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL, NULL);
+               sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
        return 0;
 }