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;
29 int __user *p = (int __user *)arg;
34 struct address_space *mapping = filp->f_mapping;
36 /* do we support this mess? */
37 if (!mapping->a_ops->bmap)
39 if (!capable(CAP_SYS_RAWIO))
41 if ((error = get_user(block, p)) != 0)
44 res = mapping->a_ops->bmap(mapping, block);
45 return put_user(res, p);
48 if (inode->i_sb == NULL)
50 return put_user(inode->i_sb->s_blocksize, p);
52 return put_user(i_size_read(inode) - filp->f_pos, p);
54 if (filp->f_op && filp->f_op->ioctl)
55 return filp->f_op->ioctl(inode, filp, cmd, arg);
60 asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
64 int on, error = -EBADF;
70 error = security_file_ioctl(filp, cmd, arg);
79 set_close_on_exec(fd, 1);
83 set_close_on_exec(fd, 0);
87 if ((error = get_user(on, (int __user *)arg)) != 0)
91 /* SunOS compatibility item. */
92 if(O_NONBLOCK != O_NDELAY)
96 filp->f_flags |= flag;
98 filp->f_flags &= ~flag;
102 if ((error = get_user(on, (int __user *)arg)) != 0)
104 flag = on ? FASYNC : 0;
106 /* Did FASYNC state change ? */
107 if ((flag ^ filp->f_flags) & FASYNC) {
108 if (filp->f_op && filp->f_op->fasync)
109 error = filp->f_op->fasync(fd, filp, on);
110 else error = -ENOTTY;
116 filp->f_flags |= FASYNC;
118 filp->f_flags &= ~FASYNC;
122 if (S_ISDIR(filp->f_dentry->d_inode->i_mode) ||
123 S_ISREG(filp->f_dentry->d_inode->i_mode) ||
124 S_ISLNK(filp->f_dentry->d_inode->i_mode)) {
125 loff_t res = inode_get_bytes(filp->f_dentry->d_inode);
126 error = copy_to_user((loff_t __user *)arg, &res, sizeof(res)) ? -EFAULT : 0;
131 #ifdef CONFIG_VSERVER_LEGACY
132 #ifndef CONFIG_INOXID_NONE
134 struct inode *inode = filp->f_dentry->d_inode;
136 /* fixme: if stealth, return -ENOTTY */
138 if (capable(CAP_CONTEXT))
139 error = put_user(inode->i_xid, (int *) arg);
143 struct inode *inode = filp->f_dentry->d_inode;
146 /* fixme: if stealth, return -ENOTTY */
148 if (!capable(CAP_CONTEXT))
151 if (IS_RDONLY(inode))
154 if (!(inode->i_sb->s_flags & MS_TAGXID))
157 if (get_user(xid, (int *) arg))
160 inode->i_xid = (xid & 0xFFFF);
161 inode->i_ctime = CURRENT_TIME;
162 mark_inode_dirty(inode);
169 if (filp->f_dentry->d_inode->i_sb->s_magic == PROC_SUPER_MAGIC)
170 error = vx_proc_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
175 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
176 error = file_ioctl(filp, cmd, arg);
177 else if (filp->f_op && filp->f_op->ioctl)
178 error = filp->f_op->ioctl(filp->f_dentry->d_inode, filp, cmd, arg);