blob: e6500cd122584e830f66d8af209ea817004f68dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ioctl.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/syscalls.h>
8#include <linux/mm.h>
9#include <linux/smp_lock.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/file.h>
12#include <linux/fs.h>
13#include <linux/security.h>
14#include <linux/module.h>
Erez Zadokc9845ff2008-02-07 00:13:23 -080015#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/ioctls.h>
18
Erez Zadokdeb21db2008-02-07 00:13:25 -080019/**
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
24 *
25 * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
26 * invokes * filesystem specific ->ioctl method. If neither method exists,
27 * returns -ENOTTY.
28 *
29 * Returns 0 on success, -errno on error.
30 */
31long vfs_ioctl(struct file *filp, unsigned int cmd,
32 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 int error = -ENOTTY;
35
36 if (!filp->f_op)
37 goto out;
38
39 if (filp->f_op->unlocked_ioctl) {
40 error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
41 if (error == -ENOIOCTLCMD)
42 error = -EINVAL;
43 goto out;
Andrew Morton64d67d22007-07-15 23:41:02 -070044 } else if (filp->f_op->ioctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 lock_kernel();
Andrew Morton64d67d22007-07-15 23:41:02 -070046 error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
47 filp, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 unlock_kernel();
49 }
50
51 out:
52 return error;
53}
54
55static int file_ioctl(struct file *filp, unsigned int cmd,
56 unsigned long arg)
57{
58 int error;
59 int block;
Erez Zadokc9845ff2008-02-07 00:13:23 -080060 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int __user *p = (int __user *)arg;
62
63 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -080064 case FIBMAP:
65 {
66 struct address_space *mapping = filp->f_mapping;
67 int res;
68 /* do we support this mess? */
69 if (!mapping->a_ops->bmap)
70 return -EINVAL;
71 if (!capable(CAP_SYS_RAWIO))
72 return -EPERM;
73 error = get_user(block, p);
74 if (error)
75 return error;
76 lock_kernel();
77 res = mapping->a_ops->bmap(mapping, block);
78 unlock_kernel();
79 return put_user(res, p);
80 }
81 case FIGETBSZ:
82 return put_user(inode->i_sb->s_blocksize, p);
83 case FIONREAD:
84 return put_user(i_size_read(inode) - filp->f_pos, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
Erez Zadokdeb21db2008-02-07 00:13:25 -080087 return vfs_ioctl(filp, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/*
91 * When you add any new common ioctls to the switches above and below
92 * please update compat_sys_ioctl() too.
93 *
Erez Zadokdeb21db2008-02-07 00:13:25 -080094 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
96 */
Erez Zadokdeb21db2008-02-07 00:13:25 -080097int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
98 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 unsigned int flag;
101 int on, error = 0;
102
103 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800104 case FIOCLEX:
105 set_close_on_exec(fd, 1);
106 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Erez Zadokc9845ff2008-02-07 00:13:23 -0800108 case FIONCLEX:
109 set_close_on_exec(fd, 0);
110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Erez Zadokc9845ff2008-02-07 00:13:23 -0800112 case FIONBIO:
113 error = get_user(on, (int __user *)arg);
114 if (error)
115 break;
116 flag = O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#ifdef __sparc__
Erez Zadokc9845ff2008-02-07 00:13:23 -0800118 /* SunOS compatibility item. */
119 if (O_NONBLOCK != O_NDELAY)
120 flag |= O_NDELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#endif
Erez Zadokc9845ff2008-02-07 00:13:23 -0800122 if (on)
123 filp->f_flags |= flag;
124 else
125 filp->f_flags &= ~flag;
126 break;
127
128 case FIOASYNC:
129 error = get_user(on, (int __user *)arg);
130 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 break;
Erez Zadokc9845ff2008-02-07 00:13:23 -0800132 flag = on ? FASYNC : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Erez Zadokc9845ff2008-02-07 00:13:23 -0800134 /* Did FASYNC state change ? */
135 if ((flag ^ filp->f_flags) & FASYNC) {
136 if (filp->f_op && filp->f_op->fasync) {
137 lock_kernel();
138 error = filp->f_op->fasync(fd, filp, on);
139 unlock_kernel();
140 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 error = -ENOTTY;
Erez Zadokc9845ff2008-02-07 00:13:23 -0800142 }
143 if (error != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 break;
Erez Zadokc9845ff2008-02-07 00:13:23 -0800145
146 if (on)
147 filp->f_flags |= FASYNC;
148 else
149 filp->f_flags &= ~FASYNC;
150 break;
151
152 case FIOQSIZE:
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)) {
156 loff_t res =
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;
160 } else
161 error = -ENOTTY;
162 break;
163 default:
164 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
165 error = file_ioctl(filp, cmd, arg);
166 else
Erez Zadokdeb21db2008-02-07 00:13:25 -0800167 error = vfs_ioctl(filp, cmd, arg);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170 return error;
171}
172
173asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
174{
Erez Zadokc9845ff2008-02-07 00:13:23 -0800175 struct file *filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 int error = -EBADF;
177 int fput_needed;
178
179 filp = fget_light(fd, &fput_needed);
180 if (!filp)
181 goto out;
182
183 error = security_file_ioctl(filp, cmd, arg);
184 if (error)
185 goto out_fput;
186
Erez Zadokdeb21db2008-02-07 00:13:25 -0800187 error = do_vfs_ioctl(filp, fd, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 out_fput:
189 fput_light(filp, fput_needed);
190 out:
191 return error;
192}