VFS: swap do_ioctl and vfs_ioctl names
[safe/jmp/linux-2.6] / fs / ioctl.c
1 /*
2  *  linux/fs/ioctl.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #include <linux/syscalls.h>
8 #include <linux/mm.h>
9 #include <linux/smp_lock.h>
10 #include <linux/capability.h>
11 #include <linux/file.h>
12 #include <linux/fs.h>
13 #include <linux/security.h>
14 #include <linux/module.h>
15 #include <linux/uaccess.h>
16
17 #include <asm/ioctls.h>
18
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  */
31 long vfs_ioctl(struct file *filp, unsigned int cmd,
32                unsigned long arg)
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;
44         } else if (filp->f_op->ioctl) {
45                 lock_kernel();
46                 error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
47                                           filp, cmd, arg);
48                 unlock_kernel();
49         }
50
51  out:
52         return error;
53 }
54
55 static int file_ioctl(struct file *filp, unsigned int cmd,
56                 unsigned long arg)
57 {
58         int error;
59         int block;
60         struct inode *inode = filp->f_path.dentry->d_inode;
61         int __user *p = (int __user *)arg;
62
63         switch (cmd) {
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);
85         }
86
87         return vfs_ioctl(filp, cmd, arg);
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  *
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.
96  */
97 int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
98              unsigned long arg)
99 {
100         unsigned int flag;
101         int on, error = 0;
102
103         switch (cmd) {
104         case FIOCLEX:
105                 set_close_on_exec(fd, 1);
106                 break;
107
108         case FIONCLEX:
109                 set_close_on_exec(fd, 0);
110                 break;
111
112         case FIONBIO:
113                 error = get_user(on, (int __user *)arg);
114                 if (error)
115                         break;
116                 flag = O_NONBLOCK;
117 #ifdef __sparc__
118                 /* SunOS compatibility item. */
119                 if (O_NONBLOCK != O_NDELAY)
120                         flag |= O_NDELAY;
121 #endif
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)
131                         break;
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
141                                 error = -ENOTTY;
142                 }
143                 if (error != 0)
144                         break;
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
167                         error = vfs_ioctl(filp, cmd, arg);
168                 break;
169         }
170         return error;
171 }
172
173 asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
174 {
175         struct file *filp;
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
187         error = do_vfs_ioctl(filp, fd, cmd, arg);
188  out_fput:
189         fput_light(filp, fput_needed);
190  out:
191         return error;
192 }