]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ioctl.c
VFS: swap do_ioctl and vfs_ioctl names
[net-next-2.6.git] / fs / ioctl.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/ioctl.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
1da177e4
LT
7#include <linux/syscalls.h>
8#include <linux/mm.h>
9#include <linux/smp_lock.h>
16f7e0fe 10#include <linux/capability.h>
1da177e4
LT
11#include <linux/file.h>
12#include <linux/fs.h>
13#include <linux/security.h>
14#include <linux/module.h>
c9845ff1 15#include <linux/uaccess.h>
1da177e4 16
1da177e4
LT
17#include <asm/ioctls.h>
18
deb21db7
EZ
19/**
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)
1da177e4
LT
33{
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;
64d67d21 44 } else if (filp->f_op->ioctl) {
1da177e4 45 lock_kernel();
64d67d21
AM
46 error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
47 filp, cmd, arg);
1da177e4
LT
48 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;
c9845ff1 60 struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4
LT
61 int __user *p = (int __user *)arg;
62
63 switch (cmd) {
c9845ff1
EZ
64 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);
1da177e4
LT
85 }
86
deb21db7 87 return vfs_ioctl(filp, cmd, arg);
1da177e4
LT
88}
89
90/*
91 * When you add any new common ioctls to the switches above and below
92 * please update compat_sys_ioctl() too.
93 *
deb21db7 94 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
1da177e4
LT
95 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
96 */
deb21db7
EZ
97int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
98 unsigned long arg)
1da177e4
LT
99{
100 unsigned int flag;
101 int on, error = 0;
102
103 switch (cmd) {
c9845ff1
EZ
104 case FIOCLEX:
105 set_close_on_exec(fd, 1);
106 break;
1da177e4 107
c9845ff1
EZ
108 case FIONCLEX:
109 set_close_on_exec(fd, 0);
110 break;
1da177e4 111
c9845ff1
EZ
112 case FIONBIO:
113 error = get_user(on, (int __user *)arg);
114 if (error)
115 break;
116 flag = O_NONBLOCK;
1da177e4 117#ifdef __sparc__
c9845ff1
EZ
118 /* SunOS compatibility item. */
119 if (O_NONBLOCK != O_NDELAY)
120 flag |= O_NDELAY;
1da177e4 121#endif
c9845ff1
EZ
122 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)
1da177e4 131 break;
c9845ff1
EZ
132 flag = on ? FASYNC : 0;
133
134 /* 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
1da177e4 141 error = -ENOTTY;
c9845ff1
EZ
142 }
143 if (error != 0)
1da177e4 144 break;
c9845ff1
EZ
145
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
deb21db7 167 error = vfs_ioctl(filp, cmd, arg);
c9845ff1 168 break;
1da177e4
LT
169 }
170 return error;
171}
172
173asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
174{
c9845ff1 175 struct file *filp;
1da177e4
LT
176 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
deb21db7 187 error = do_vfs_ioctl(filp, fd, cmd, arg);
1da177e4
LT
188 out_fput:
189 fput_light(filp, fput_needed);
190 out:
191 return error;
192}