4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
8 #include <linux/syscalls.h>
10 #include <linux/smp_lock.h>
11 #include <linux/capability.h>
12 #include <linux/file.h>
14 #include <linux/security.h>
15 #include <linux/module.h>
16 #include <linux/proc_fs.h>
17 #include <linux/vserver/inode.h>
18 #include <linux/vserver/xid.h>
20 #include <asm/uaccess.h>
21 #include <asm/ioctls.h>
24 #ifdef CONFIG_VSERVER_LEGACY
25 extern int vx_proc_ioctl(struct inode *, struct file *,
26 unsigned int, unsigned long);
29 static long do_ioctl(struct file *filp, unsigned int cmd,
37 if (filp->f_op->unlocked_ioctl) {
38 error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
39 if (error == -ENOIOCTLCMD)
42 } else if (filp->f_op->ioctl) {
44 error = filp->f_op->ioctl(filp->f_dentry->d_inode,
53 static int file_ioctl(struct file *filp, unsigned int cmd,
58 struct inode * inode = filp->f_dentry->d_inode;
59 int __user *p = (int __user *)arg;
64 struct address_space *mapping = filp->f_mapping;
66 /* do we support this mess? */
67 if (!mapping->a_ops->bmap)
69 if (!capable(CAP_SYS_RAWIO))
71 if ((error = get_user(block, p)) != 0)
75 res = mapping->a_ops->bmap(mapping, block);
77 return put_user(res, p);
80 if (inode->i_sb == NULL)
82 return put_user(inode->i_sb->s_blocksize, p);
84 return put_user(i_size_read(inode) - filp->f_pos, p);
87 return do_ioctl(filp, cmd, arg);
91 * When you add any new common ioctls to the switches above and below
92 * please update compat_sys_ioctl() too.
94 * vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
95 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
97 int vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, unsigned long arg)
104 set_close_on_exec(fd, 1);
108 set_close_on_exec(fd, 0);
112 if ((error = get_user(on, (int __user *)arg)) != 0)
116 /* SunOS compatibility item. */
117 if(O_NONBLOCK != O_NDELAY)
121 filp->f_flags |= flag;
123 filp->f_flags &= ~flag;
127 if ((error = get_user(on, (int __user *)arg)) != 0)
129 flag = on ? FASYNC : 0;
131 /* Did FASYNC state change ? */
132 if ((flag ^ filp->f_flags) & FASYNC) {
133 if (filp->f_op && filp->f_op->fasync) {
135 error = filp->f_op->fasync(fd, filp, on);
138 else error = -ENOTTY;
144 filp->f_flags |= FASYNC;
146 filp->f_flags &= ~FASYNC;
150 if (S_ISDIR(filp->f_dentry->d_inode->i_mode) ||
151 S_ISREG(filp->f_dentry->d_inode->i_mode) ||
152 S_ISLNK(filp->f_dentry->d_inode->i_mode)) {
153 loff_t res = inode_get_bytes(filp->f_dentry->d_inode);
154 error = copy_to_user((loff_t __user *)arg, &res, sizeof(res)) ? -EFAULT : 0;
159 #ifdef CONFIG_VSERVER_LEGACY
160 #ifndef CONFIG_INOXID_NONE
162 struct inode *inode = filp->f_dentry->d_inode;
164 /* fixme: if stealth, return -ENOTTY */
166 if (capable(CAP_CONTEXT))
167 error = put_user(inode->i_xid, (int __user *) arg);
171 struct inode *inode = filp->f_dentry->d_inode;
174 /* fixme: if stealth, return -ENOTTY */
176 if (!capable(CAP_CONTEXT))
179 if (IS_RDONLY(inode))
182 if (!(inode->i_sb->s_flags & MS_TAGXID))
185 if (get_user(xid, (int __user *) arg))
188 inode->i_xid = (xid & 0xFFFF);
189 inode->i_ctime = CURRENT_TIME;
190 mark_inode_dirty(inode);
197 if (filp->f_dentry->d_inode->i_sb->s_magic == PROC_SUPER_MAGIC)
198 error = vx_proc_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
202 * These cmds needed for PLK - don't lose them!
207 * Verify that this filp is a file object,
208 * not (say) a socket.
211 if (S_ISREG(filp->f_dentry->d_inode->i_mode) ||
212 S_ISDIR(filp->f_dentry->d_inode->i_mode))
213 error = vc_iattr_ioctl(filp->f_dentry,
218 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
219 error = file_ioctl(filp, cmd, arg);
221 error = do_ioctl(filp, cmd, arg);
227 asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
233 filp = fget_light(fd, &fput_needed);
237 error = security_file_ioctl(filp, cmd, arg);
241 error = vfs_ioctl(filp, fd, cmd, arg);
243 fput_light(filp, fput_needed);
249 * Platforms implementing 32 bit compatibility ioctl handlers in
250 * modules need this exported
253 EXPORT_SYMBOL(sys_ioctl);