fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / ioctl.c
index 12f3e71..0335d8d 100644 (file)
@@ -4,28 +4,57 @@
  *  Copyright (C) 1991, 1992  Linus Torvalds
  */
 
+#include <linux/syscalls.h>
 #include <linux/mm.h>
 #include <linux/smp_lock.h>
+#include <linux/capability.h>
 #include <linux/file.h>
 #include <linux/fs.h>
 #include <linux/security.h>
+#include <linux/module.h>
 #include <linux/proc_fs.h>
 #include <linux/vserver/inode.h>
-#include <linux/vserver/xid.h>
+#include <linux/vs_tag.h>
 
 #include <asm/uaccess.h>
 #include <asm/ioctls.h>
 
-#ifdef CONFIG_VSERVER_LEGACY           
+
+#ifdef CONFIG_VSERVER_LEGACY
 extern int vx_proc_ioctl(struct inode *, struct file *,
        unsigned int, unsigned long);
 #endif
 
-static int file_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
+static long do_ioctl(struct file *filp, unsigned int cmd,
+               unsigned long arg)
+{
+       int error = -ENOTTY;
+
+       if (!filp->f_op)
+               goto out;
+
+       if (filp->f_op->unlocked_ioctl) {
+               error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
+               if (error == -ENOIOCTLCMD)
+                       error = -EINVAL;
+               goto out;
+       } else if (filp->f_op->ioctl) {
+               lock_kernel();
+               error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
+                                         filp, cmd, arg);
+               unlock_kernel();
+       }
+
+ out:
+       return error;
+}
+
+static int file_ioctl(struct file *filp, unsigned int cmd,
+               unsigned long arg)
 {
        int error;
        int block;
-       struct inode * inode = filp->f_dentry->d_inode;
+       struct inode * inode = filp->f_path.dentry->d_inode;
        int __user *p = (int __user *)arg;
 
        switch (cmd) {
@@ -41,7 +70,9 @@ static int file_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
                        if ((error = get_user(block, p)) != 0)
                                return error;
 
+                       lock_kernel();
                        res = mapping->a_ops->bmap(mapping, block);
+                       unlock_kernel();
                        return put_user(res, p);
                }
                case FIGETBSZ:
@@ -51,29 +82,22 @@ static int file_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
                case FIONREAD:
                        return put_user(i_size_read(inode) - filp->f_pos, p);
        }
-       if (filp->f_op && filp->f_op->ioctl)
-               return filp->f_op->ioctl(inode, filp, cmd, arg);
-       return -ENOTTY;
-}
 
+       return do_ioctl(filp, cmd, arg);
+}
 
-asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
-{      
-       struct file * filp;
+/*
+ * When you add any new common ioctls to the switches above and below
+ * please update compat_sys_ioctl() too.
+ *
+ * vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
+ * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
+ */
+int vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, unsigned long arg)
+{
        unsigned int flag;
-       int on, error = -EBADF;
-
-       filp = fget(fd);
-       if (!filp)
-               goto out;
+       int on, error = 0;
 
-       error = security_file_ioctl(filp, cmd, arg);
-       if (error) {
-                fput(filp);
-                goto out;
-        }
-
-       lock_kernel();
        switch (cmd) {
                case FIOCLEX:
                        set_close_on_exec(fd, 1);
@@ -105,8 +129,11 @@ asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
 
                        /* Did FASYNC state change ? */
                        if ((flag ^ filp->f_flags) & FASYNC) {
-                               if (filp->f_op && filp->f_op->fasync)
+                               if (filp->f_op && filp->f_op->fasync) {
+                                       lock_kernel();
                                        error = filp->f_op->fasync(fd, filp, on);
+                                       unlock_kernel();
+                               }
                                else error = -ENOTTY;
                        }
                        if (error != 0)
@@ -119,29 +146,29 @@ asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
                        break;
 
                case FIOQSIZE:
-                       if (S_ISDIR(filp->f_dentry->d_inode->i_mode) ||
-                           S_ISREG(filp->f_dentry->d_inode->i_mode) ||
-                           S_ISLNK(filp->f_dentry->d_inode->i_mode)) {
-                               loff_t res = inode_get_bytes(filp->f_dentry->d_inode);
+                       if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
+                           S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
+                           S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
+                               loff_t res = inode_get_bytes(filp->f_path.dentry->d_inode);
                                error = copy_to_user((loff_t __user *)arg, &res, sizeof(res)) ? -EFAULT : 0;
                        }
                        else
                                error = -ENOTTY;
                        break;
-#ifdef CONFIG_VSERVER_LEGACY           
-#ifndef CONFIG_INOXID_NONE
-               case FIOC_GETXID: {
+#ifdef CONFIG_VSERVER_LEGACY
+#ifndef CONFIG_TAGGING_NONE
+               case FIOC_GETTAG: {
                        struct inode *inode = filp->f_dentry->d_inode;
 
                        /* fixme: if stealth, return -ENOTTY */
                        error = -EPERM;
                        if (capable(CAP_CONTEXT))
-                               error = put_user(inode->i_xid, (int *) arg);
+                               error = put_user(inode->i_tag, (int __user *) arg);
                        break;
                }
-               case FIOC_SETXID: {
+               case FIOC_SETTAG: {
                        struct inode *inode = filp->f_dentry->d_inode;
-                       int xid;
+                       int tag;
 
                        /* fixme: if stealth, return -ENOTTY */
                        error = -EPERM;
@@ -151,16 +178,16 @@ asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
                        if (IS_RDONLY(inode))
                                break;
                        error = -ENOSYS;
-                       if (!(inode->i_sb->s_flags & MS_TAGXID))
+                       if (!(inode->i_sb->s_flags & MS_TAGGED))
                                break;
                        error = -EFAULT;
-                       if (get_user(xid, (int *) arg))
+                       if (get_user(tag, (int __user *) arg))
                                break;
                        error = 0;
-                       inode->i_xid = (xid & 0xFFFF);
+                       inode->i_tag = (tag & 0xFFFF);
                        inode->i_ctime = CURRENT_TIME;
                        mark_inode_dirty(inode);
-                       break;  
+                       break;
                }
 #endif
                case FIOC_GETXFLG:
@@ -171,15 +198,40 @@ asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
                        break;
 #endif
                default:
-                       error = -ENOTTY;
-                       if (S_ISREG(filp->f_dentry->d_inode->i_mode))
+                       if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
                                error = file_ioctl(filp, cmd, arg);
-                       else if (filp->f_op && filp->f_op->ioctl)
-                               error = filp->f_op->ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
+                       else
+                               error = do_ioctl(filp, cmd, arg);
+                       break;
        }
-       unlock_kernel();
-       fput(filp);
+       return error;
+}
 
-out:
+asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
+{
+       struct file * filp;
+       int error = -EBADF;
+       int fput_needed;
+
+       filp = fget_light(fd, &fput_needed);
+       if (!filp)
+               goto out;
+
+       error = security_file_ioctl(filp, cmd, arg);
+       if (error)
+               goto out_fput;
+
+       error = vfs_ioctl(filp, fd, cmd, arg);
+ out_fput:
+       fput_light(filp, fput_needed);
+ out:
        return error;
 }
+
+/*
+ * Platforms implementing 32 bit compatibility ioctl handlers in
+ * modules need this exported
+ */
+#ifdef CONFIG_COMPAT
+EXPORT_SYMBOL(sys_ioctl);
+#endif