4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/syscalls.h>
9 #include <linux/smp_lock.h>
10 #include <linux/capability.h>
11 #include <linux/file.h>
13 #include <linux/security.h>
14 #include <linux/module.h>
15 #include <linux/proc_fs.h>
16 #include <linux/vserver/inode.h>
17 #include <linux/vserver/xid.h>
19 #include <asm/uaccess.h>
20 #include <asm/ioctls.h>
23 #ifdef CONFIG_VSERVER_LEGACY
24 extern int vx_proc_ioctl(struct inode *, struct file *,
25 unsigned int, unsigned long);
28 static long do_ioctl(struct file *filp, unsigned int cmd,
36 if (filp->f_op->unlocked_ioctl) {
37 error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
38 if (error == -ENOIOCTLCMD)
41 } else if (filp->f_op->ioctl) {
43 error = filp->f_op->ioctl(filp->f_dentry->d_inode,
52 static int file_ioctl(struct file *filp, unsigned int cmd,
57 struct inode * inode = filp->f_dentry->d_inode;
58 int __user *p = (int __user *)arg;
63 struct address_space *mapping = filp->f_mapping;
65 /* do we support this mess? */
66 if (!mapping->a_ops->bmap)
68 if (!capable(CAP_SYS_RAWIO))
70 if ((error = get_user(block, p)) != 0)
74 res = mapping->a_ops->bmap(mapping, block);
76 return put_user(res, p);
79 if (inode->i_sb == NULL)
81 return put_user(inode->i_sb->s_blocksize, p);
83 return put_user(i_size_read(inode) - filp->f_pos, p);
86 return do_ioctl(filp, cmd, arg);
90 * When you add any new common ioctls to the switches above and below
91 * please update compat_sys_ioctl() too.
93 * vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
94 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
96 int vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, unsigned long arg)
103 set_close_on_exec(fd, 1);
107 set_close_on_exec(fd, 0);
111 if ((error = get_user(on, (int __user *)arg)) != 0)
115 /* SunOS compatibility item. */
116 if(O_NONBLOCK != O_NDELAY)
120 filp->f_flags |= flag;
122 filp->f_flags &= ~flag;
126 if ((error = get_user(on, (int __user *)arg)) != 0)
128 flag = on ? FASYNC : 0;
130 /* Did FASYNC state change ? */
131 if ((flag ^ filp->f_flags) & FASYNC) {
132 if (filp->f_op && filp->f_op->fasync) {
134 error = filp->f_op->fasync(fd, filp, on);
137 else error = -ENOTTY;
143 filp->f_flags |= FASYNC;
145 filp->f_flags &= ~FASYNC;
149 if (S_ISDIR(filp->f_dentry->d_inode->i_mode) ||
150 S_ISREG(filp->f_dentry->d_inode->i_mode) ||
151 S_ISLNK(filp->f_dentry->d_inode->i_mode)) {
152 loff_t res = inode_get_bytes(filp->f_dentry->d_inode);
153 error = copy_to_user((loff_t __user *)arg, &res, sizeof(res)) ? -EFAULT : 0;
158 #ifdef CONFIG_VSERVER_LEGACY
159 #ifndef CONFIG_INOXID_NONE
161 struct inode *inode = filp->f_dentry->d_inode;
163 /* fixme: if stealth, return -ENOTTY */
165 if (capable(CAP_CONTEXT))
166 error = put_user(inode->i_xid, (int __user *) arg);
170 struct inode *inode = filp->f_dentry->d_inode;
173 /* fixme: if stealth, return -ENOTTY */
175 if (!capable(CAP_CONTEXT))
178 if (IS_RDONLY(inode))
181 if (!(inode->i_sb->s_flags & MS_TAGXID))
184 if (get_user(xid, (int __user *) arg))
187 inode->i_xid = (xid & 0xFFFF);
188 inode->i_ctime = CURRENT_TIME;
189 mark_inode_dirty(inode);
196 if (filp->f_dentry->d_inode->i_sb->s_magic == PROC_SUPER_MAGIC)
197 error = vx_proc_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
201 * These cmds needed for PLK - don't lose them!
206 * Verify that this filp is a file object,
207 * not (say) a socket.
210 if (S_ISREG(filp->f_dentry->d_inode->i_mode) ||
211 S_ISDIR(filp->f_dentry->d_inode->i_mode))
212 error = vc_iattr_ioctl(filp->f_dentry,
217 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
218 error = file_ioctl(filp, cmd, arg);
220 error = do_ioctl(filp, cmd, arg);
226 asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
232 filp = fget_light(fd, &fput_needed);
236 error = security_file_ioctl(filp, cmd, arg);
240 error = vfs_ioctl(filp, fd, cmd, arg);
242 fput_light(filp, fput_needed);
248 * Platforms implementing 32 bit compatibility ioctl handlers in
249 * modules need this exported
252 EXPORT_SYMBOL(sys_ioctl);