Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ext2/ioctl.c |
| 3 | * |
| 4 | * Copyright (C) 1993, 1994, 1995 |
| 5 | * Remy Card (card@masi.ibp.fr) |
| 6 | * Laboratoire MASI - Institut Blaise Pascal |
| 7 | * Universite Pierre et Marie Curie (Paris VI) |
| 8 | */ |
| 9 | |
| 10 | #include "ext2.h" |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 11 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/time.h> |
| 13 | #include <linux/sched.h> |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 14 | #include <linux/compat.h> |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 15 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/current.h> |
| 17 | #include <asm/uaccess.h> |
| 18 | |
| 19 | |
Andi Kleen | 14f9f7b | 2008-02-06 01:40:10 -0800 | [diff] [blame] | 20 | long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 22 | struct inode *inode = file_inode(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | struct ext2_inode_info *ei = EXT2_I(inode); |
| 24 | unsigned int flags; |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 25 | unsigned short rsv_window_size; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 26 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg); |
| 29 | |
| 30 | switch (cmd) { |
| 31 | case EXT2_IOC_GETFLAGS: |
Jan Kara | 4f99ed6 | 2007-05-08 00:31:04 -0700 | [diff] [blame] | 32 | ext2_get_inode_flags(ei); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | flags = ei->i_flags & EXT2_FL_USER_VISIBLE; |
| 34 | return put_user(flags, (int __user *) arg); |
| 35 | case EXT2_IOC_SETFLAGS: { |
| 36 | unsigned int oldflags; |
| 37 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 38 | ret = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 39 | if (ret) |
| 40 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 42 | if (!inode_owner_or_capable(inode)) { |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 43 | ret = -EACCES; |
| 44 | goto setflags_out; |
| 45 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 47 | if (get_user(flags, (int __user *) arg)) { |
| 48 | ret = -EFAULT; |
| 49 | goto setflags_out; |
| 50 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Duane Griffin | ef8b646 | 2009-01-07 18:07:21 -0800 | [diff] [blame] | 52 | flags = ext2_mask_flags(inode->i_mode, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 54 | inode_lock(inode); |
Jan Kara | e47776a | 2007-11-14 16:58:56 -0800 | [diff] [blame] | 55 | /* Is it quota file? Do not allow user to mess with it */ |
| 56 | if (IS_NOQUOTA(inode)) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 57 | inode_unlock(inode); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 58 | ret = -EPERM; |
| 59 | goto setflags_out; |
Jan Kara | e47776a | 2007-11-14 16:58:56 -0800 | [diff] [blame] | 60 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | oldflags = ei->i_flags; |
| 62 | |
| 63 | /* |
| 64 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by |
| 65 | * the relevant capability. |
| 66 | * |
| 67 | * This test looks nicer. Thanks to Pauline Middelink |
| 68 | */ |
| 69 | if ((flags ^ oldflags) & (EXT2_APPEND_FL | EXT2_IMMUTABLE_FL)) { |
Andrew Morton | 93f210d | 2006-12-06 20:37:33 -0800 | [diff] [blame] | 70 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 71 | inode_unlock(inode); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 72 | ret = -EPERM; |
| 73 | goto setflags_out; |
Andrew Morton | 93f210d | 2006-12-06 20:37:33 -0800 | [diff] [blame] | 74 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | flags = flags & EXT2_FL_USER_MODIFIABLE; |
| 78 | flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE; |
| 79 | ei->i_flags = flags; |
| 80 | |
| 81 | ext2_set_inode_flags(inode); |
| 82 | inode->i_ctime = CURRENT_TIME_SEC; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 83 | inode_unlock(inode); |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | mark_inode_dirty(inode); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 86 | setflags_out: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 87 | mnt_drop_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 88 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | case EXT2_IOC_GETVERSION: |
| 91 | return put_user(inode->i_generation, (int __user *) arg); |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 92 | case EXT2_IOC_SETVERSION: { |
| 93 | __u32 generation; |
| 94 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 95 | if (!inode_owner_or_capable(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return -EPERM; |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 97 | ret = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 98 | if (ret) |
| 99 | return ret; |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 100 | if (get_user(generation, (int __user *) arg)) { |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 101 | ret = -EFAULT; |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 102 | goto setversion_out; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 103 | } |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 104 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 105 | inode_lock(inode); |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 106 | inode->i_ctime = CURRENT_TIME_SEC; |
| 107 | inode->i_generation = generation; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 108 | inode_unlock(inode); |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 109 | |
| 110 | mark_inode_dirty(inode); |
| 111 | setversion_out: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 112 | mnt_drop_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 113 | return ret; |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 114 | } |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 115 | case EXT2_IOC_GETRSVSZ: |
| 116 | if (test_opt(inode->i_sb, RESERVATION) |
| 117 | && S_ISREG(inode->i_mode) |
| 118 | && ei->i_block_alloc_info) { |
| 119 | rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size; |
| 120 | return put_user(rsv_window_size, (int __user *)arg); |
| 121 | } |
| 122 | return -ENOTTY; |
| 123 | case EXT2_IOC_SETRSVSZ: { |
| 124 | |
| 125 | if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode)) |
| 126 | return -ENOTTY; |
| 127 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 128 | if (!inode_owner_or_capable(inode)) |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 129 | return -EACCES; |
| 130 | |
| 131 | if (get_user(rsv_window_size, (int __user *)arg)) |
| 132 | return -EFAULT; |
| 133 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 134 | ret = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 135 | if (ret) |
| 136 | return ret; |
| 137 | |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 138 | if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS) |
| 139 | rsv_window_size = EXT2_MAX_RESERVE_BLOCKS; |
| 140 | |
| 141 | /* |
| 142 | * need to allocate reservation structure for this inode |
| 143 | * before set the window size |
| 144 | */ |
| 145 | /* |
| 146 | * XXX What lock should protect the rsv_goal_size? |
| 147 | * Accessed in ext2_get_block only. ext3 uses i_truncate. |
| 148 | */ |
| 149 | mutex_lock(&ei->truncate_mutex); |
| 150 | if (!ei->i_block_alloc_info) |
| 151 | ext2_init_block_alloc_info(inode); |
| 152 | |
| 153 | if (ei->i_block_alloc_info){ |
| 154 | struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node; |
| 155 | rsv->rsv_goal_size = rsv_window_size; |
| 156 | } |
| 157 | mutex_unlock(&ei->truncate_mutex); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 158 | mnt_drop_write_file(filp); |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | default: |
| 162 | return -ENOTTY; |
| 163 | } |
| 164 | } |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 165 | |
| 166 | #ifdef CONFIG_COMPAT |
| 167 | long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 168 | { |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 169 | /* These are just misnamed, they actually get/put from/to user an int */ |
| 170 | switch (cmd) { |
| 171 | case EXT2_IOC32_GETFLAGS: |
| 172 | cmd = EXT2_IOC_GETFLAGS; |
| 173 | break; |
| 174 | case EXT2_IOC32_SETFLAGS: |
| 175 | cmd = EXT2_IOC_SETFLAGS; |
| 176 | break; |
| 177 | case EXT2_IOC32_GETVERSION: |
| 178 | cmd = EXT2_IOC_GETVERSION; |
| 179 | break; |
| 180 | case EXT2_IOC32_SETVERSION: |
| 181 | cmd = EXT2_IOC_SETVERSION; |
| 182 | break; |
| 183 | default: |
| 184 | return -ENOIOCTLCMD; |
| 185 | } |
Andi Kleen | 14f9f7b | 2008-02-06 01:40:10 -0800 | [diff] [blame] | 186 | return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 187 | } |
| 188 | #endif |