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 | |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame^] | 19 | /** |
| 20 | * vfs_ioctl - call filesystem specific ioctl methods |
| 21 | * @filp: [in] open file to invoke ioctl method on |
| 22 | * @cmd: [in] ioctl command to execute |
| 23 | * @arg: [in/out] command-specific argument for ioctl |
| 24 | * |
| 25 | * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise |
| 26 | * invokes * filesystem specific ->ioctl method. If neither method exists, |
| 27 | * returns -ENOTTY. |
| 28 | * |
| 29 | * Returns 0 on success, -errno on error. |
| 30 | */ |
| 31 | long vfs_ioctl(struct file *filp, unsigned int cmd, |
| 32 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
| 34 | int error = -ENOTTY; |
| 35 | |
| 36 | if (!filp->f_op) |
| 37 | goto out; |
| 38 | |
| 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; |
Andrew Morton | 64d67d2 | 2007-07-15 23:41:02 -0700 | [diff] [blame] | 44 | } else if (filp->f_op->ioctl) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | lock_kernel(); |
Andrew Morton | 64d67d2 | 2007-07-15 23:41:02 -0700 | [diff] [blame] | 46 | error = filp->f_op->ioctl(filp->f_path.dentry->d_inode, |
| 47 | filp, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | unlock_kernel(); |
| 49 | } |
| 50 | |
| 51 | out: |
| 52 | return error; |
| 53 | } |
| 54 | |
| 55 | static int file_ioctl(struct file *filp, unsigned int cmd, |
| 56 | unsigned long arg) |
| 57 | { |
| 58 | int error; |
| 59 | int block; |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 60 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | int __user *p = (int __user *)arg; |
| 62 | |
| 63 | switch (cmd) { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 64 | case FIBMAP: |
| 65 | { |
| 66 | struct address_space *mapping = filp->f_mapping; |
| 67 | int res; |
| 68 | /* do we support this mess? */ |
| 69 | if (!mapping->a_ops->bmap) |
| 70 | return -EINVAL; |
| 71 | if (!capable(CAP_SYS_RAWIO)) |
| 72 | return -EPERM; |
| 73 | error = get_user(block, p); |
| 74 | if (error) |
| 75 | return error; |
| 76 | lock_kernel(); |
| 77 | res = mapping->a_ops->bmap(mapping, block); |
| 78 | unlock_kernel(); |
| 79 | return put_user(res, p); |
| 80 | } |
| 81 | case FIGETBSZ: |
| 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame^] | 87 | return vfs_ioctl(filp, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame^] | 94 | * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | * It's just a simple helper for sys_ioctl and compat_sys_ioctl. |
| 96 | */ |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame^] | 97 | int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, |
| 98 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
| 100 | unsigned int flag; |
| 101 | int on, error = 0; |
| 102 | |
| 103 | switch (cmd) { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 104 | case FIOCLEX: |
| 105 | set_close_on_exec(fd, 1); |
| 106 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 108 | case FIONCLEX: |
| 109 | set_close_on_exec(fd, 0); |
| 110 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 112 | case FIONBIO: |
| 113 | error = get_user(on, (int __user *)arg); |
| 114 | if (error) |
| 115 | break; |
| 116 | flag = O_NONBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | #ifdef __sparc__ |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 118 | /* SunOS compatibility item. */ |
| 119 | if (O_NONBLOCK != O_NDELAY) |
| 120 | flag |= O_NDELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | #endif |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 122 | if (on) |
| 123 | filp->f_flags |= flag; |
| 124 | else |
| 125 | filp->f_flags &= ~flag; |
| 126 | break; |
| 127 | |
| 128 | case FIOASYNC: |
| 129 | error = get_user(on, (int __user *)arg); |
| 130 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | break; |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 132 | flag = on ? FASYNC : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 134 | /* Did FASYNC state change ? */ |
| 135 | if ((flag ^ filp->f_flags) & FASYNC) { |
| 136 | if (filp->f_op && filp->f_op->fasync) { |
| 137 | lock_kernel(); |
| 138 | error = filp->f_op->fasync(fd, filp, on); |
| 139 | unlock_kernel(); |
| 140 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | error = -ENOTTY; |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 142 | } |
| 143 | if (error != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | break; |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 145 | |
| 146 | if (on) |
| 147 | filp->f_flags |= FASYNC; |
| 148 | else |
| 149 | filp->f_flags &= ~FASYNC; |
| 150 | break; |
| 151 | |
| 152 | case FIOQSIZE: |
| 153 | if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) || |
| 154 | S_ISREG(filp->f_path.dentry->d_inode->i_mode) || |
| 155 | S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) { |
| 156 | loff_t res = |
| 157 | inode_get_bytes(filp->f_path.dentry->d_inode); |
| 158 | error = copy_to_user((loff_t __user *)arg, &res, |
| 159 | sizeof(res)) ? -EFAULT : 0; |
| 160 | } else |
| 161 | error = -ENOTTY; |
| 162 | break; |
| 163 | default: |
| 164 | if (S_ISREG(filp->f_path.dentry->d_inode->i_mode)) |
| 165 | error = file_ioctl(filp, cmd, arg); |
| 166 | else |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame^] | 167 | error = vfs_ioctl(filp, cmd, arg); |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 168 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | return error; |
| 171 | } |
| 172 | |
| 173 | asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg) |
| 174 | { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 175 | struct file *filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | int error = -EBADF; |
| 177 | int fput_needed; |
| 178 | |
| 179 | filp = fget_light(fd, &fput_needed); |
| 180 | if (!filp) |
| 181 | goto out; |
| 182 | |
| 183 | error = security_file_ioctl(filp, cmd, arg); |
| 184 | if (error) |
| 185 | goto out_fput; |
| 186 | |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame^] | 187 | error = do_vfs_ioctl(filp, fd, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | out_fput: |
| 189 | fput_light(filp, fput_needed); |
| 190 | out: |
| 191 | return error; |
| 192 | } |