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/uaccess.h>
17 #include <asm/ioctls.h>
20 * vfs_ioctl - call filesystem specific ioctl methods
21 * @filp: [in] open file to invoke ioctl method on
22 * @cmd: [in] ioctl command to execute
23 * @arg: [in/out] command-specific argument for ioctl
25 * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
26 * invokes * filesystem specific ->ioctl method. If neither method exists,
29 * Returns 0 on success, -errno on error.
31 long vfs_ioctl(struct file *filp, unsigned int cmd,
39 if (filp->f_op->unlocked_ioctl) {
40 error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
41 if (error == -ENOIOCTLCMD)
44 } else if (filp->f_op->ioctl) {
46 error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
55 static int file_ioctl(struct file *filp, unsigned int cmd,
60 struct inode *inode = filp->f_path.dentry->d_inode;
61 int __user *p = (int __user *)arg;
66 struct address_space *mapping = filp->f_mapping;
68 /* do we support this mess? */
69 if (!mapping->a_ops->bmap)
71 if (!capable(CAP_SYS_RAWIO))
73 error = get_user(block, p);
77 res = mapping->a_ops->bmap(mapping, block);
79 return put_user(res, p);
82 return put_user(inode->i_sb->s_blocksize, p);
84 return put_user(i_size_read(inode) - filp->f_pos, p);
87 return vfs_ioctl(filp, cmd, arg);
91 * When you add any new common ioctls to the switches above and below
92 * please update compat_sys_ioctl() too.
94 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
95 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
97 int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
105 set_close_on_exec(fd, 1);
109 set_close_on_exec(fd, 0);
113 error = get_user(on, (int __user *)arg);
118 /* SunOS compatibility item. */
119 if (O_NONBLOCK != O_NDELAY)
123 filp->f_flags |= flag;
125 filp->f_flags &= ~flag;
129 error = get_user(on, (int __user *)arg);
132 flag = on ? FASYNC : 0;
134 /* Did FASYNC state change ? */
135 if ((flag ^ filp->f_flags) & FASYNC) {
136 if (filp->f_op && filp->f_op->fasync) {
138 error = filp->f_op->fasync(fd, filp, on);
147 filp->f_flags |= FASYNC;
149 filp->f_flags &= ~FASYNC;
153 if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
154 S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
155 S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
157 inode_get_bytes(filp->f_path.dentry->d_inode);
158 error = copy_to_user((loff_t __user *)arg, &res,
159 sizeof(res)) ? -EFAULT : 0;
164 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
165 error = file_ioctl(filp, cmd, arg);
167 error = vfs_ioctl(filp, cmd, arg);
173 asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
179 filp = fget_light(fd, &fput_needed);
183 error = security_file_ioctl(filp, cmd, arg);
187 error = do_vfs_ioctl(filp, fd, cmd, arg);
189 fput_light(filp, fput_needed);