Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / 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/proc_fs.h>
16 #include <linux/vserver/inode.h>
17 #include <linux/vserver/xid.h>
18
19 #include <asm/uaccess.h>
20 #include <asm/ioctls.h>
21
22
23 #ifdef  CONFIG_VSERVER_LEGACY
24 extern int vx_proc_ioctl(struct inode *, struct file *,
25         unsigned int, unsigned long);
26 #endif
27
28 static long do_ioctl(struct file *filp, unsigned int cmd,
29                 unsigned long arg)
30 {
31         int error = -ENOTTY;
32
33         if (!filp->f_op)
34                 goto out;
35
36         if (filp->f_op->unlocked_ioctl) {
37                 error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
38                 if (error == -ENOIOCTLCMD)
39                         error = -EINVAL;
40                 goto out;
41         } else if (filp->f_op->ioctl) {
42                 lock_kernel();
43                 error = filp->f_op->ioctl(filp->f_dentry->d_inode,
44                                           filp, cmd, arg);
45                 unlock_kernel();
46         }
47
48  out:
49         return error;
50 }
51
52 static int file_ioctl(struct file *filp, unsigned int cmd,
53                 unsigned long arg)
54 {
55         int error;
56         int block;
57         struct inode * inode = filp->f_dentry->d_inode;
58         int __user *p = (int __user *)arg;
59
60         switch (cmd) {
61                 case FIBMAP:
62                 {
63                         struct address_space *mapping = filp->f_mapping;
64                         int res;
65                         /* do we support this mess? */
66                         if (!mapping->a_ops->bmap)
67                                 return -EINVAL;
68                         if (!capable(CAP_SYS_RAWIO))
69                                 return -EPERM;
70                         if ((error = get_user(block, p)) != 0)
71                                 return error;
72
73                         lock_kernel();
74                         res = mapping->a_ops->bmap(mapping, block);
75                         unlock_kernel();
76                         return put_user(res, p);
77                 }
78                 case FIGETBSZ:
79                         if (inode->i_sb == NULL)
80                                 return -EBADF;
81                         return put_user(inode->i_sb->s_blocksize, p);
82                 case FIONREAD:
83                         return put_user(i_size_read(inode) - filp->f_pos, p);
84         }
85
86         return do_ioctl(filp, cmd, arg);
87 }
88
89 /*
90  * When you add any new common ioctls to the switches above and below
91  * please update compat_sys_ioctl() too.
92  *
93  * vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
94  * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
95  */
96 int vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, unsigned long arg)
97 {
98         unsigned int flag;
99         int on, error = 0;
100
101         switch (cmd) {
102                 case FIOCLEX:
103                         set_close_on_exec(fd, 1);
104                         break;
105
106                 case FIONCLEX:
107                         set_close_on_exec(fd, 0);
108                         break;
109
110                 case FIONBIO:
111                         if ((error = get_user(on, (int __user *)arg)) != 0)
112                                 break;
113                         flag = O_NONBLOCK;
114 #ifdef __sparc__
115                         /* SunOS compatibility item. */
116                         if(O_NONBLOCK != O_NDELAY)
117                                 flag |= O_NDELAY;
118 #endif
119                         if (on)
120                                 filp->f_flags |= flag;
121                         else
122                                 filp->f_flags &= ~flag;
123                         break;
124
125                 case FIOASYNC:
126                         if ((error = get_user(on, (int __user *)arg)) != 0)
127                                 break;
128                         flag = on ? FASYNC : 0;
129
130                         /* Did FASYNC state change ? */
131                         if ((flag ^ filp->f_flags) & FASYNC) {
132                                 if (filp->f_op && filp->f_op->fasync) {
133                                         lock_kernel();
134                                         error = filp->f_op->fasync(fd, filp, on);
135                                         unlock_kernel();
136                                 }
137                                 else error = -ENOTTY;
138                         }
139                         if (error != 0)
140                                 break;
141
142                         if (on)
143                                 filp->f_flags |= FASYNC;
144                         else
145                                 filp->f_flags &= ~FASYNC;
146                         break;
147
148                 case FIOQSIZE:
149                         if (S_ISDIR(filp->f_dentry->d_inode->i_mode) ||
150                             S_ISREG(filp->f_dentry->d_inode->i_mode) ||
151                             S_ISLNK(filp->f_dentry->d_inode->i_mode)) {
152                                 loff_t res = inode_get_bytes(filp->f_dentry->d_inode);
153                                 error = copy_to_user((loff_t __user *)arg, &res, sizeof(res)) ? -EFAULT : 0;
154                         }
155                         else
156                                 error = -ENOTTY;
157                         break;
158 #ifdef  CONFIG_VSERVER_LEGACY
159 #ifndef CONFIG_INOXID_NONE
160                 case FIOC_GETXID: {
161                         struct inode *inode = filp->f_dentry->d_inode;
162
163                         /* fixme: if stealth, return -ENOTTY */
164                         error = -EPERM;
165                         if (capable(CAP_CONTEXT))
166                                 error = put_user(inode->i_xid, (int __user *) arg);
167                         break;
168                 }
169                 case FIOC_SETXID: {
170                         struct inode *inode = filp->f_dentry->d_inode;
171                         int xid;
172
173                         /* fixme: if stealth, return -ENOTTY */
174                         error = -EPERM;
175                         if (!capable(CAP_CONTEXT))
176                                 break;
177                         error = -EROFS;
178                         if (IS_RDONLY(inode))
179                                 break;
180                         error = -ENOSYS;
181                         if (!(inode->i_sb->s_flags & MS_TAGXID))
182                                 break;
183                         error = -EFAULT;
184                         if (get_user(xid, (int __user *) arg))
185                                 break;
186                         error = 0;
187                         inode->i_xid = (xid & 0xFFFF);
188                         inode->i_ctime = CURRENT_TIME;
189                         mark_inode_dirty(inode);
190                         break;
191                 }
192 #endif
193                 case FIOC_GETXFLG:
194                 case FIOC_SETXFLG:
195                         error = -ENOTTY;
196                         if (filp->f_dentry->d_inode->i_sb->s_magic == PROC_SUPER_MAGIC)
197                                 error = vx_proc_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
198                         break;
199 #endif
200                 default:
201                         if (S_ISREG(filp->f_dentry->d_inode->i_mode))
202                                 error = file_ioctl(filp, cmd, arg);
203                         else
204                                 error = do_ioctl(filp, cmd, arg);
205                         break;
206         }
207         return error;
208 }
209
210 asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
211 {
212         struct file * filp;
213         int error = -EBADF;
214         int fput_needed;
215
216         filp = fget_light(fd, &fput_needed);
217         if (!filp)
218                 goto out;
219
220         error = security_file_ioctl(filp, cmd, arg);
221         if (error)
222                 goto out_fput;
223
224         error = vfs_ioctl(filp, fd, cmd, arg);
225  out_fput:
226         fput_light(filp, fput_needed);
227  out:
228         return error;
229 }
230
231 /*
232  * Platforms implementing 32 bit compatibility ioctl handlers in
233  * modules need this exported
234  */
235 #ifdef CONFIG_COMPAT
236 EXPORT_SYMBOL(sys_ioctl);
237 #endif