4 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/smp_lock.h>
9 #include <linux/file.h>
11 #include <linux/security.h>
12 #include <linux/proc_fs.h>
13 #include <linux/vserver/inode.h>
14 #include <linux/vserver/xid.h>
16 #include <asm/uaccess.h>
17 #include <asm/ioctls.h>
19 #ifdef CONFIG_VSERVER_LEGACY
20 extern int vx_proc_ioctl(struct inode *, struct file *,
21 unsigned int, unsigned long);
24 static int file_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
28 struct inode * inode = filp->f_dentry->d_inode;
33 struct address_space *mapping = filp->f_mapping;
35 /* do we support this mess? */
36 if (!mapping->a_ops->bmap)
38 if (!capable(CAP_SYS_RAWIO))
40 if ((error = get_user(block, (int *) arg)) != 0)
43 res = mapping->a_ops->bmap(mapping, block);
44 return put_user(res, (int *) arg);
47 if (inode->i_sb == NULL)
49 return put_user(inode->i_sb->s_blocksize, (int *) arg);
51 return put_user(i_size_read(inode) - filp->f_pos, (int *) arg);
53 if (filp->f_op && filp->f_op->ioctl)
54 return filp->f_op->ioctl(inode, filp, cmd, arg);
59 asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
63 int on, error = -EBADF;
69 error = security_file_ioctl(filp, cmd, arg);
78 set_close_on_exec(fd, 1);
82 set_close_on_exec(fd, 0);
86 if ((error = get_user(on, (int __user *)arg)) != 0)
90 /* SunOS compatibility item. */
91 if(O_NONBLOCK != O_NDELAY)
95 filp->f_flags |= flag;
97 filp->f_flags &= ~flag;
101 if ((error = get_user(on, (int __user *)arg)) != 0)
103 flag = on ? FASYNC : 0;
105 /* Did FASYNC state change ? */
106 if ((flag ^ filp->f_flags) & FASYNC) {
107 if (filp->f_op && filp->f_op->fasync)
108 error = filp->f_op->fasync(fd, filp, on);
109 else error = -ENOTTY;
115 filp->f_flags |= FASYNC;
117 filp->f_flags &= ~FASYNC;
121 if (S_ISDIR(filp->f_dentry->d_inode->i_mode) ||
122 S_ISREG(filp->f_dentry->d_inode->i_mode) ||
123 S_ISLNK(filp->f_dentry->d_inode->i_mode)) {
124 loff_t res = inode_get_bytes(filp->f_dentry->d_inode);
125 error = copy_to_user((loff_t __user *)arg, &res, sizeof(res)) ? -EFAULT : 0;
130 #ifdef CONFIG_VSERVER_LEGACY
131 #ifndef CONFIG_INOXID_NONE
133 struct inode *inode = filp->f_dentry->d_inode;
135 /* fixme: if stealth, return -ENOTTY */
137 if (capable(CAP_CONTEXT))
138 error = put_user(inode->i_xid, (int *) arg);
142 struct inode *inode = filp->f_dentry->d_inode;
145 /* fixme: if stealth, return -ENOTTY */
147 if (!capable(CAP_CONTEXT))
150 if (IS_RDONLY(inode))
153 if (!(inode->i_sb->s_flags & MS_TAGXID))
156 if (get_user(xid, (int *) arg))
159 inode->i_xid = (xid & 0xFFFF);
160 inode->i_ctime = CURRENT_TIME;
161 mark_inode_dirty(inode);
168 if (filp->f_dentry->d_inode->i_sb->s_magic == PROC_SUPER_MAGIC)
169 error = vx_proc_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
174 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
175 error = file_ioctl(filp, cmd, arg);
176 else if (filp->f_op && filp->f_op->ioctl)
177 error = filp->f_op->ioctl(filp->f_dentry->d_inode, filp, cmd, arg);