4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
9 #include <linux/smp_lock.h>
10 #include <linux/file.h>
12 #include <linux/security.h>
13 #include <linux/module.h>
14 #include <linux/proc_fs.h>
15 #include <linux/vserver/inode.h>
16 #include <linux/vserver/xid.h>
17 #include <linux/module.h>
19 #include <asm/uaccess.h>
20 #include <asm/ioctls.h>
22 #ifdef CONFIG_VSERVER_LEGACY
23 extern int vx_proc_ioctl(struct inode *, struct file *,
24 unsigned int, unsigned long);
27 static int file_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
31 struct inode * inode = filp->f_dentry->d_inode;
32 int __user *p = (int __user *)arg;
37 struct address_space *mapping = filp->f_mapping;
39 /* do we support this mess? */
40 if (!mapping->a_ops->bmap)
42 if (!capable(CAP_SYS_RAWIO))
44 if ((error = get_user(block, p)) != 0)
47 res = mapping->a_ops->bmap(mapping, block);
48 return put_user(res, p);
51 if (inode->i_sb == NULL)
53 return put_user(inode->i_sb->s_blocksize, p);
55 return put_user(i_size_read(inode) - filp->f_pos, p);
57 if (filp->f_op && filp->f_op->ioctl)
58 return filp->f_op->ioctl(inode, filp, cmd, arg);
63 asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
67 int on, error = -EBADF;
73 error = security_file_ioctl(filp, cmd, arg);
82 set_close_on_exec(fd, 1);
86 set_close_on_exec(fd, 0);
90 if ((error = get_user(on, (int __user *)arg)) != 0)
94 /* SunOS compatibility item. */
95 if(O_NONBLOCK != O_NDELAY)
99 filp->f_flags |= flag;
101 filp->f_flags &= ~flag;
105 if ((error = get_user(on, (int __user *)arg)) != 0)
107 flag = on ? FASYNC : 0;
109 /* Did FASYNC state change ? */
110 if ((flag ^ filp->f_flags) & FASYNC) {
111 if (filp->f_op && filp->f_op->fasync)
112 error = filp->f_op->fasync(fd, filp, on);
113 else error = -ENOTTY;
119 filp->f_flags |= FASYNC;
121 filp->f_flags &= ~FASYNC;
125 if (S_ISDIR(filp->f_dentry->d_inode->i_mode) ||
126 S_ISREG(filp->f_dentry->d_inode->i_mode) ||
127 S_ISLNK(filp->f_dentry->d_inode->i_mode)) {
128 loff_t res = inode_get_bytes(filp->f_dentry->d_inode);
129 error = copy_to_user((loff_t __user *)arg, &res, sizeof(res)) ? -EFAULT : 0;
134 #ifdef CONFIG_VSERVER_LEGACY
135 #ifndef CONFIG_INOXID_NONE
137 struct inode *inode = filp->f_dentry->d_inode;
139 /* fixme: if stealth, return -ENOTTY */
141 if (capable(CAP_CONTEXT))
142 error = put_user(inode->i_xid, (int *) arg);
146 struct inode *inode = filp->f_dentry->d_inode;
149 /* fixme: if stealth, return -ENOTTY */
151 if (!capable(CAP_CONTEXT))
154 if (IS_RDONLY(inode))
157 if (!(inode->i_sb->s_flags & MS_TAGXID))
160 if (get_user(xid, (int *) arg))
163 inode->i_xid = (xid & 0xFFFF);
164 inode->i_ctime = CURRENT_TIME;
165 mark_inode_dirty(inode);
172 if (filp->f_dentry->d_inode->i_sb->s_magic == PROC_SUPER_MAGIC)
173 error = vx_proc_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
178 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
179 error = file_ioctl(filp, cmd, arg);
180 else if (filp->f_op && filp->f_op->ioctl)
181 error = filp->f_op->ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
191 * Platforms implementing 32 bit compatibility ioctl handlers in
192 * modules need this exported
195 EXPORT_SYMBOL(sys_ioctl);