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>
18 #include <asm/uaccess.h>
19 #include <asm/ioctls.h>
21 #ifdef CONFIG_VSERVER_LEGACY
22 extern int vx_proc_ioctl(struct inode *, struct file *,
23 unsigned int, unsigned long);
26 static int file_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
30 struct inode * inode = filp->f_dentry->d_inode;
31 int __user *p = (int __user *)arg;
36 struct address_space *mapping = filp->f_mapping;
38 /* do we support this mess? */
39 if (!mapping->a_ops->bmap)
41 if (!capable(CAP_SYS_RAWIO))
43 if ((error = get_user(block, p)) != 0)
46 res = mapping->a_ops->bmap(mapping, block);
47 return put_user(res, p);
50 if (inode->i_sb == NULL)
52 return put_user(inode->i_sb->s_blocksize, p);
54 return put_user(i_size_read(inode) - filp->f_pos, p);
56 if (filp->f_op && filp->f_op->ioctl)
57 return filp->f_op->ioctl(inode, filp, cmd, arg);
62 asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
66 int on, error = -EBADF;
72 error = security_file_ioctl(filp, cmd, arg);
81 set_close_on_exec(fd, 1);
85 set_close_on_exec(fd, 0);
89 if ((error = get_user(on, (int __user *)arg)) != 0)
93 /* SunOS compatibility item. */
94 if(O_NONBLOCK != O_NDELAY)
98 filp->f_flags |= flag;
100 filp->f_flags &= ~flag;
104 if ((error = get_user(on, (int __user *)arg)) != 0)
106 flag = on ? FASYNC : 0;
108 /* Did FASYNC state change ? */
109 if ((flag ^ filp->f_flags) & FASYNC) {
110 if (filp->f_op && filp->f_op->fasync)
111 error = filp->f_op->fasync(fd, filp, on);
112 else error = -ENOTTY;
118 filp->f_flags |= FASYNC;
120 filp->f_flags &= ~FASYNC;
124 if (S_ISDIR(filp->f_dentry->d_inode->i_mode) ||
125 S_ISREG(filp->f_dentry->d_inode->i_mode) ||
126 S_ISLNK(filp->f_dentry->d_inode->i_mode)) {
127 loff_t res = inode_get_bytes(filp->f_dentry->d_inode);
128 error = copy_to_user((loff_t __user *)arg, &res, sizeof(res)) ? -EFAULT : 0;
133 #ifdef CONFIG_VSERVER_LEGACY
134 #ifndef CONFIG_INOXID_NONE
136 struct inode *inode = filp->f_dentry->d_inode;
138 /* fixme: if stealth, return -ENOTTY */
140 if (capable(CAP_CONTEXT))
141 error = put_user(inode->i_xid, (int *) arg);
145 struct inode *inode = filp->f_dentry->d_inode;
148 /* fixme: if stealth, return -ENOTTY */
150 if (!capable(CAP_CONTEXT))
153 if (IS_RDONLY(inode))
156 if (!(inode->i_sb->s_flags & MS_TAGXID))
159 if (get_user(xid, (int *) arg))
162 inode->i_xid = (xid & 0xFFFF);
163 inode->i_ctime = CURRENT_TIME;
164 mark_inode_dirty(inode);
171 if (filp->f_dentry->d_inode->i_sb->s_magic == PROC_SUPER_MAGIC)
172 error = vx_proc_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
177 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
178 error = file_ioctl(filp, cmd, arg);
179 else if (filp->f_op && filp->f_op->ioctl)
180 error = filp->f_op->ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
190 * Platforms implementing 32 bit compatibility ioctl handlers in
191 * modules need this exported
194 EXPORT_SYMBOL(sys_ioctl);