ACPI: thinkpad-acpi: handle HKEY event 6030
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ioctl.c
blobb9a59a4889082d692cad00acdbb1350cf1b5071f
1 /*
2 * linux/fs/ioctl.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
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>
17 #include <asm/ioctls.h>
19 /**
20 * vfs_ioctl - call filesystem specific ioctl methods
21 * @filp: open file to invoke ioctl method on
22 * @cmd: ioctl command to execute
23 * @arg: command-specific argument for ioctl
25 * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
26 * invokes filesystem specific ->ioctl method. If neither method exists,
27 * returns -ENOTTY.
29 * Returns 0 on success, -errno on error.
31 static long vfs_ioctl(struct file *filp, unsigned int cmd,
32 unsigned long arg)
34 int error = -ENOTTY;
36 if (!filp->f_op)
37 goto out;
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();
51 out:
52 return error;
55 static int ioctl_fibmap(struct file *filp, int __user *p)
57 struct address_space *mapping = filp->f_mapping;
58 int res, block;
60 /* do we support this mess? */
61 if (!mapping->a_ops->bmap)
62 return -EINVAL;
63 if (!capable(CAP_SYS_RAWIO))
64 return -EPERM;
65 res = get_user(block, p);
66 if (res)
67 return res;
68 lock_kernel();
69 res = mapping->a_ops->bmap(mapping, block);
70 unlock_kernel();
71 return put_user(res, p);
74 static int file_ioctl(struct file *filp, unsigned int cmd,
75 unsigned long arg)
77 struct inode *inode = filp->f_path.dentry->d_inode;
78 int __user *p = (int __user *)arg;
80 switch (cmd) {
81 case FIBMAP:
82 return ioctl_fibmap(filp, p);
83 case FIGETBSZ:
84 return put_user(inode->i_sb->s_blocksize, p);
85 case FIONREAD:
86 return put_user(i_size_read(inode) - filp->f_pos, p);
89 return vfs_ioctl(filp, cmd, arg);
92 static int ioctl_fionbio(struct file *filp, int __user *argp)
94 unsigned int flag;
95 int on, error;
97 error = get_user(on, argp);
98 if (error)
99 return error;
100 flag = O_NONBLOCK;
101 #ifdef __sparc__
102 /* SunOS compatibility item. */
103 if (O_NONBLOCK != O_NDELAY)
104 flag |= O_NDELAY;
105 #endif
106 if (on)
107 filp->f_flags |= flag;
108 else
109 filp->f_flags &= ~flag;
110 return error;
113 static int ioctl_fioasync(unsigned int fd, struct file *filp,
114 int __user *argp)
116 unsigned int flag;
117 int on, error;
119 error = get_user(on, argp);
120 if (error)
121 return error;
122 flag = on ? FASYNC : 0;
124 /* Did FASYNC state change ? */
125 if ((flag ^ filp->f_flags) & FASYNC) {
126 if (filp->f_op && filp->f_op->fasync)
127 error = filp->f_op->fasync(fd, filp, on);
128 else
129 error = -ENOTTY;
131 if (error)
132 return error;
134 if (on)
135 filp->f_flags |= FASYNC;
136 else
137 filp->f_flags &= ~FASYNC;
138 return error;
142 * When you add any new common ioctls to the switches above and below
143 * please update compat_sys_ioctl() too.
145 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
146 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
148 int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
149 unsigned long arg)
151 int error = 0;
152 int __user *argp = (int __user *)arg;
154 switch (cmd) {
155 case FIOCLEX:
156 set_close_on_exec(fd, 1);
157 break;
159 case FIONCLEX:
160 set_close_on_exec(fd, 0);
161 break;
163 case FIONBIO:
164 /* BKL needed to avoid races tweaking f_flags */
165 lock_kernel();
166 error = ioctl_fionbio(filp, argp);
167 unlock_kernel();
168 break;
170 case FIOASYNC:
171 /* BKL needed to avoid races tweaking f_flags */
172 lock_kernel();
173 error = ioctl_fioasync(fd, filp, argp);
174 unlock_kernel();
175 break;
177 case FIOQSIZE:
178 if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
179 S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
180 S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
181 loff_t res =
182 inode_get_bytes(filp->f_path.dentry->d_inode);
183 error = copy_to_user((loff_t __user *)arg, &res,
184 sizeof(res)) ? -EFAULT : 0;
185 } else
186 error = -ENOTTY;
187 break;
188 default:
189 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
190 error = file_ioctl(filp, cmd, arg);
191 else
192 error = vfs_ioctl(filp, cmd, arg);
193 break;
195 return error;
198 SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
200 struct file *filp;
201 int error = -EBADF;
202 int fput_needed;
204 filp = fget_light(fd, &fput_needed);
205 if (!filp)
206 goto out;
208 error = security_file_ioctl(filp, cmd, arg);
209 if (error)
210 goto out_fput;
212 error = do_vfs_ioctl(filp, fd, cmd, arg);
213 out_fput:
214 fput_light(filp, fput_needed);
215 out:
216 return error;