ckrm_E16
[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/mm.h>
8 #include <linux/smp_lock.h>
9 #include <linux/file.h>
10 #include <linux/fs.h>
11 #include <linux/security.h>
12
13 #include <asm/uaccess.h>
14 #include <asm/ioctls.h>
15
16 static int file_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
17 {
18         int error;
19         int block;
20         struct inode * inode = filp->f_dentry->d_inode;
21         int __user *p = (int __user *)arg;
22
23         switch (cmd) {
24                 case FIBMAP:
25                 {
26                         struct address_space *mapping = filp->f_mapping;
27                         int res;
28                         /* do we support this mess? */
29                         if (!mapping->a_ops->bmap)
30                                 return -EINVAL;
31                         if (!capable(CAP_SYS_RAWIO))
32                                 return -EPERM;
33                         if ((error = get_user(block, p)) != 0)
34                                 return error;
35
36                         res = mapping->a_ops->bmap(mapping, block);
37                         return put_user(res, p);
38                 }
39                 case FIGETBSZ:
40                         if (inode->i_sb == NULL)
41                                 return -EBADF;
42                         return put_user(inode->i_sb->s_blocksize, p);
43                 case FIONREAD:
44                         return put_user(i_size_read(inode) - filp->f_pos, p);
45         }
46         if (filp->f_op && filp->f_op->ioctl)
47                 return filp->f_op->ioctl(inode, filp, cmd, arg);
48         return -ENOTTY;
49 }
50
51
52 asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
53 {       
54         struct file * filp;
55         unsigned int flag;
56         int on, error = -EBADF;
57
58         filp = fget(fd);
59         if (!filp)
60                 goto out;
61
62         error = security_file_ioctl(filp, cmd, arg);
63         if (error) {
64                 fput(filp);
65                 goto out;
66         }
67
68         lock_kernel();
69         switch (cmd) {
70                 case FIOCLEX:
71                         set_close_on_exec(fd, 1);
72                         break;
73
74                 case FIONCLEX:
75                         set_close_on_exec(fd, 0);
76                         break;
77
78                 case FIONBIO:
79                         if ((error = get_user(on, (int __user *)arg)) != 0)
80                                 break;
81                         flag = O_NONBLOCK;
82 #ifdef __sparc__
83                         /* SunOS compatibility item. */
84                         if(O_NONBLOCK != O_NDELAY)
85                                 flag |= O_NDELAY;
86 #endif
87                         if (on)
88                                 filp->f_flags |= flag;
89                         else
90                                 filp->f_flags &= ~flag;
91                         break;
92
93                 case FIOASYNC:
94                         if ((error = get_user(on, (int __user *)arg)) != 0)
95                                 break;
96                         flag = on ? FASYNC : 0;
97
98                         /* Did FASYNC state change ? */
99                         if ((flag ^ filp->f_flags) & FASYNC) {
100                                 if (filp->f_op && filp->f_op->fasync)
101                                         error = filp->f_op->fasync(fd, filp, on);
102                                 else error = -ENOTTY;
103                         }
104                         if (error != 0)
105                                 break;
106
107                         if (on)
108                                 filp->f_flags |= FASYNC;
109                         else
110                                 filp->f_flags &= ~FASYNC;
111                         break;
112
113                 case FIOQSIZE:
114                         if (S_ISDIR(filp->f_dentry->d_inode->i_mode) ||
115                             S_ISREG(filp->f_dentry->d_inode->i_mode) ||
116                             S_ISLNK(filp->f_dentry->d_inode->i_mode)) {
117                                 loff_t res = inode_get_bytes(filp->f_dentry->d_inode);
118                                 error = copy_to_user((loff_t __user *)arg, &res, sizeof(res)) ? -EFAULT : 0;
119                         }
120                         else
121                                 error = -ENOTTY;
122                         break;
123                 default:
124                         error = -ENOTTY;
125                         if (S_ISREG(filp->f_dentry->d_inode->i_mode))
126                                 error = file_ioctl(filp, cmd, arg);
127                         else if (filp->f_op && filp->f_op->ioctl)
128                                 error = filp->f_op->ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
129         }
130         unlock_kernel();
131         fput(filp);
132
133 out:
134         return error;
135 }