Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ioctl.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/syscalls.h> |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/smp_lock.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 10 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/file.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/security.h> |
| 14 | #include <linux/module.h> |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 15 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/ioctls.h> |
| 18 | |
| 19 | static long do_ioctl(struct file *filp, unsigned int cmd, |
| 20 | unsigned long arg) |
| 21 | { |
| 22 | int error = -ENOTTY; |
| 23 | |
| 24 | if (!filp->f_op) |
| 25 | goto out; |
| 26 | |
| 27 | if (filp->f_op->unlocked_ioctl) { |
| 28 | error = filp->f_op->unlocked_ioctl(filp, cmd, arg); |
| 29 | if (error == -ENOIOCTLCMD) |
| 30 | error = -EINVAL; |
| 31 | goto out; |
Andrew Morton | 64d67d2 | 2007-07-15 23:41:02 -0700 | [diff] [blame] | 32 | } else if (filp->f_op->ioctl) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | lock_kernel(); |
Andrew Morton | 64d67d2 | 2007-07-15 23:41:02 -0700 | [diff] [blame] | 34 | error = filp->f_op->ioctl(filp->f_path.dentry->d_inode, |
| 35 | filp, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | unlock_kernel(); |
| 37 | } |
| 38 | |
| 39 | out: |
| 40 | return error; |
| 41 | } |
| 42 | |
| 43 | static int file_ioctl(struct file *filp, unsigned int cmd, |
| 44 | unsigned long arg) |
| 45 | { |
| 46 | int error; |
| 47 | int block; |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 48 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | int __user *p = (int __user *)arg; |
| 50 | |
| 51 | switch (cmd) { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 52 | case FIBMAP: |
| 53 | { |
| 54 | struct address_space *mapping = filp->f_mapping; |
| 55 | int res; |
| 56 | /* do we support this mess? */ |
| 57 | if (!mapping->a_ops->bmap) |
| 58 | return -EINVAL; |
| 59 | if (!capable(CAP_SYS_RAWIO)) |
| 60 | return -EPERM; |
| 61 | error = get_user(block, p); |
| 62 | if (error) |
| 63 | return error; |
| 64 | lock_kernel(); |
| 65 | res = mapping->a_ops->bmap(mapping, block); |
| 66 | unlock_kernel(); |
| 67 | return put_user(res, p); |
| 68 | } |
| 69 | case FIGETBSZ: |
| 70 | return put_user(inode->i_sb->s_blocksize, p); |
| 71 | case FIONREAD: |
| 72 | return put_user(i_size_read(inode) - filp->f_pos, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | return do_ioctl(filp, cmd, arg); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * When you add any new common ioctls to the switches above and below |
| 80 | * please update compat_sys_ioctl() too. |
| 81 | * |
| 82 | * vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d. |
| 83 | * It's just a simple helper for sys_ioctl and compat_sys_ioctl. |
| 84 | */ |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 85 | int vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, |
| 86 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
| 88 | unsigned int flag; |
| 89 | int on, error = 0; |
| 90 | |
| 91 | switch (cmd) { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 92 | case FIOCLEX: |
| 93 | set_close_on_exec(fd, 1); |
| 94 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 96 | case FIONCLEX: |
| 97 | set_close_on_exec(fd, 0); |
| 98 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 100 | case FIONBIO: |
| 101 | error = get_user(on, (int __user *)arg); |
| 102 | if (error) |
| 103 | break; |
| 104 | flag = O_NONBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | #ifdef __sparc__ |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 106 | /* SunOS compatibility item. */ |
| 107 | if (O_NONBLOCK != O_NDELAY) |
| 108 | flag |= O_NDELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | #endif |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 110 | if (on) |
| 111 | filp->f_flags |= flag; |
| 112 | else |
| 113 | filp->f_flags &= ~flag; |
| 114 | break; |
| 115 | |
| 116 | case FIOASYNC: |
| 117 | error = get_user(on, (int __user *)arg); |
| 118 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | break; |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 120 | flag = on ? FASYNC : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 122 | /* Did FASYNC state change ? */ |
| 123 | if ((flag ^ filp->f_flags) & FASYNC) { |
| 124 | if (filp->f_op && filp->f_op->fasync) { |
| 125 | lock_kernel(); |
| 126 | error = filp->f_op->fasync(fd, filp, on); |
| 127 | unlock_kernel(); |
| 128 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | error = -ENOTTY; |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 130 | } |
| 131 | if (error != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | break; |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 133 | |
| 134 | if (on) |
| 135 | filp->f_flags |= FASYNC; |
| 136 | else |
| 137 | filp->f_flags &= ~FASYNC; |
| 138 | break; |
| 139 | |
| 140 | case FIOQSIZE: |
| 141 | if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) || |
| 142 | S_ISREG(filp->f_path.dentry->d_inode->i_mode) || |
| 143 | S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) { |
| 144 | loff_t res = |
| 145 | inode_get_bytes(filp->f_path.dentry->d_inode); |
| 146 | error = copy_to_user((loff_t __user *)arg, &res, |
| 147 | sizeof(res)) ? -EFAULT : 0; |
| 148 | } else |
| 149 | error = -ENOTTY; |
| 150 | break; |
| 151 | default: |
| 152 | if (S_ISREG(filp->f_path.dentry->d_inode->i_mode)) |
| 153 | error = file_ioctl(filp, cmd, arg); |
| 154 | else |
| 155 | error = do_ioctl(filp, cmd, arg); |
| 156 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | return error; |
| 159 | } |
| 160 | |
| 161 | asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg) |
| 162 | { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame^] | 163 | struct file *filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | int error = -EBADF; |
| 165 | int fput_needed; |
| 166 | |
| 167 | filp = fget_light(fd, &fput_needed); |
| 168 | if (!filp) |
| 169 | goto out; |
| 170 | |
| 171 | error = security_file_ioctl(filp, cmd, arg); |
| 172 | if (error) |
| 173 | goto out_fput; |
| 174 | |
| 175 | error = vfs_ioctl(filp, fd, cmd, arg); |
| 176 | out_fput: |
| 177 | fput_light(filp, fput_needed); |
| 178 | out: |
| 179 | return error; |
| 180 | } |