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>
17 #include <asm/uaccess.h>
18 #include <asm/ioctls.h>
20 static long do_ioctl(struct file *filp, unsigned int cmd,
28 if (filp->f_op->unlocked_ioctl) {
29 error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
30 if (error == -ENOIOCTLCMD)
33 } else if (filp->f_op->ioctl) {
35 error = filp->f_op->ioctl(filp->f_dentry->d_inode,
44 static int file_ioctl(struct file *filp, unsigned int cmd,
49 struct inode * inode = filp->f_dentry->d_inode;
50 int __user *p = (int __user *)arg;
55 struct address_space *mapping = filp->f_mapping;
57 /* do we support this mess? */
58 if (!mapping->a_ops->bmap)
60 if (!capable(CAP_SYS_RAWIO))
62 if ((error = get_user(block, p)) != 0)
66 res = mapping->a_ops->bmap(mapping, block);
68 return put_user(res, p);
71 if (inode->i_sb == NULL)
73 return put_user(inode->i_sb->s_blocksize, p);
75 return put_user(i_size_read(inode) - filp->f_pos, p);
78 return do_ioctl(filp, cmd, arg);
82 * When you add any new common ioctls to the switches above and below
83 * please update compat_sys_ioctl() too.
85 * vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
86 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
88 int vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, unsigned long arg)
95 set_close_on_exec(fd, 1);
99 set_close_on_exec(fd, 0);
103 if ((error = get_user(on, (int __user *)arg)) != 0)
107 /* SunOS compatibility item. */
108 if(O_NONBLOCK != O_NDELAY)
112 filp->f_flags |= flag;
114 filp->f_flags &= ~flag;
118 if ((error = get_user(on, (int __user *)arg)) != 0)
120 flag = on ? FASYNC : 0;
122 /* Did FASYNC state change ? */
123 if ((flag ^ filp->f_flags) & FASYNC) {
124 if (filp->f_op && filp->f_op->fasync) {
126 error = filp->f_op->fasync(fd, filp, on);
129 else error = -ENOTTY;
135 filp->f_flags |= FASYNC;
137 filp->f_flags &= ~FASYNC;
141 if (S_ISDIR(filp->f_dentry->d_inode->i_mode) ||
142 S_ISREG(filp->f_dentry->d_inode->i_mode) ||
143 S_ISLNK(filp->f_dentry->d_inode->i_mode)) {
144 loff_t res = inode_get_bytes(filp->f_dentry->d_inode);
145 error = copy_to_user((loff_t __user *)arg, &res, sizeof(res)) ? -EFAULT : 0;
151 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
152 error = file_ioctl(filp, cmd, arg);
154 error = do_ioctl(filp, cmd, arg);
160 asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
166 filp = fget_light(fd, &fput_needed);
170 error = security_file_ioctl(filp, cmd, arg);
174 error = vfs_ioctl(filp, fd, cmd, arg);
176 fput_light(filp, fput_needed);
182 * Platforms implementing 32 bit compatibility ioctl handlers in
183 * modules need this exported
186 EXPORT_SYMBOL(sys_ioctl);