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