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> |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 16 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/current.h> |
| 18 | #include <asm/uaccess.h> |
| 19 | |
| 20 | |
Andi Kleen | 14f9f7b | 2008-02-06 01:40:10 -0800 | [diff] [blame] | 21 | long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { |
Andi Kleen | 14f9f7b | 2008-02-06 01:40:10 -0800 | [diff] [blame] | 23 | struct inode *inode = filp->f_dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | struct ext2_inode_info *ei = EXT2_I(inode); |
| 25 | unsigned int flags; |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 26 | unsigned short rsv_window_size; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 27 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg); |
| 30 | |
| 31 | switch (cmd) { |
| 32 | case EXT2_IOC_GETFLAGS: |
Jan Kara | 4f99ed6 | 2007-05-08 00:31:04 -0700 | [diff] [blame] | 33 | ext2_get_inode_flags(ei); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | flags = ei->i_flags & EXT2_FL_USER_VISIBLE; |
| 35 | return put_user(flags, (int __user *) arg); |
| 36 | case EXT2_IOC_SETFLAGS: { |
| 37 | unsigned int oldflags; |
| 38 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 39 | ret = mnt_want_write(filp->f_path.mnt); |
| 40 | if (ret) |
| 41 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 43 | if (!is_owner_or_cap(inode)) { |
| 44 | ret = -EACCES; |
| 45 | goto setflags_out; |
| 46 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 48 | if (get_user(flags, (int __user *) arg)) { |
| 49 | ret = -EFAULT; |
| 50 | goto setflags_out; |
| 51 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | if (!S_ISDIR(inode->i_mode)) |
| 54 | flags &= ~EXT2_DIRSYNC_FL; |
| 55 | |
Andrew Morton | 93f210d | 2006-12-06 20:37:33 -0800 | [diff] [blame] | 56 | mutex_lock(&inode->i_mutex); |
Jan Kara | e47776a | 2007-11-14 16:58:56 -0800 | [diff] [blame] | 57 | /* Is it quota file? Do not allow user to mess with it */ |
| 58 | if (IS_NOQUOTA(inode)) { |
| 59 | mutex_unlock(&inode->i_mutex); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 60 | ret = -EPERM; |
| 61 | goto setflags_out; |
Jan Kara | e47776a | 2007-11-14 16:58:56 -0800 | [diff] [blame] | 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | oldflags = ei->i_flags; |
| 64 | |
| 65 | /* |
| 66 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by |
| 67 | * the relevant capability. |
| 68 | * |
| 69 | * This test looks nicer. Thanks to Pauline Middelink |
| 70 | */ |
| 71 | if ((flags ^ oldflags) & (EXT2_APPEND_FL | EXT2_IMMUTABLE_FL)) { |
Andrew Morton | 93f210d | 2006-12-06 20:37:33 -0800 | [diff] [blame] | 72 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 73 | mutex_unlock(&inode->i_mutex); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 74 | ret = -EPERM; |
| 75 | goto setflags_out; |
Andrew Morton | 93f210d | 2006-12-06 20:37:33 -0800 | [diff] [blame] | 76 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | flags = flags & EXT2_FL_USER_MODIFIABLE; |
| 80 | flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE; |
| 81 | ei->i_flags = flags; |
Andrew Morton | 93f210d | 2006-12-06 20:37:33 -0800 | [diff] [blame] | 82 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | ext2_set_inode_flags(inode); |
| 85 | inode->i_ctime = CURRENT_TIME_SEC; |
| 86 | mark_inode_dirty(inode); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 87 | setflags_out: |
| 88 | mnt_drop_write(filp->f_path.mnt); |
| 89 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | case EXT2_IOC_GETVERSION: |
| 92 | return put_user(inode->i_generation, (int __user *) arg); |
| 93 | case EXT2_IOC_SETVERSION: |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 94 | if (!is_owner_or_cap(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | return -EPERM; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 96 | ret = mnt_want_write(filp->f_path.mnt); |
| 97 | if (ret) |
| 98 | return ret; |
| 99 | if (get_user(inode->i_generation, (int __user *) arg)) { |
| 100 | ret = -EFAULT; |
| 101 | } else { |
| 102 | inode->i_ctime = CURRENT_TIME_SEC; |
| 103 | mark_inode_dirty(inode); |
| 104 | } |
| 105 | mnt_drop_write(filp->f_path.mnt); |
| 106 | return ret; |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 107 | case EXT2_IOC_GETRSVSZ: |
| 108 | if (test_opt(inode->i_sb, RESERVATION) |
| 109 | && S_ISREG(inode->i_mode) |
| 110 | && ei->i_block_alloc_info) { |
| 111 | rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size; |
| 112 | return put_user(rsv_window_size, (int __user *)arg); |
| 113 | } |
| 114 | return -ENOTTY; |
| 115 | case EXT2_IOC_SETRSVSZ: { |
| 116 | |
| 117 | if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode)) |
| 118 | return -ENOTTY; |
| 119 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 120 | if (!is_owner_or_cap(inode)) |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 121 | return -EACCES; |
| 122 | |
| 123 | if (get_user(rsv_window_size, (int __user *)arg)) |
| 124 | return -EFAULT; |
| 125 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 126 | ret = mnt_want_write(filp->f_path.mnt); |
| 127 | if (ret) |
| 128 | return ret; |
| 129 | |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 130 | if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS) |
| 131 | rsv_window_size = EXT2_MAX_RESERVE_BLOCKS; |
| 132 | |
| 133 | /* |
| 134 | * need to allocate reservation structure for this inode |
| 135 | * before set the window size |
| 136 | */ |
| 137 | /* |
| 138 | * XXX What lock should protect the rsv_goal_size? |
| 139 | * Accessed in ext2_get_block only. ext3 uses i_truncate. |
| 140 | */ |
| 141 | mutex_lock(&ei->truncate_mutex); |
| 142 | if (!ei->i_block_alloc_info) |
| 143 | ext2_init_block_alloc_info(inode); |
| 144 | |
| 145 | if (ei->i_block_alloc_info){ |
| 146 | struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node; |
| 147 | rsv->rsv_goal_size = rsv_window_size; |
| 148 | } |
| 149 | mutex_unlock(&ei->truncate_mutex); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 150 | mnt_drop_write(filp->f_path.mnt); |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 151 | return 0; |
| 152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | default: |
| 154 | return -ENOTTY; |
| 155 | } |
| 156 | } |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 157 | |
| 158 | #ifdef CONFIG_COMPAT |
| 159 | long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 160 | { |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 161 | /* These are just misnamed, they actually get/put from/to user an int */ |
| 162 | switch (cmd) { |
| 163 | case EXT2_IOC32_GETFLAGS: |
| 164 | cmd = EXT2_IOC_GETFLAGS; |
| 165 | break; |
| 166 | case EXT2_IOC32_SETFLAGS: |
| 167 | cmd = EXT2_IOC_SETFLAGS; |
| 168 | break; |
| 169 | case EXT2_IOC32_GETVERSION: |
| 170 | cmd = EXT2_IOC_GETVERSION; |
| 171 | break; |
| 172 | case EXT2_IOC32_SETVERSION: |
| 173 | cmd = EXT2_IOC_SETVERSION; |
| 174 | break; |
| 175 | default: |
| 176 | return -ENOIOCTLCMD; |
| 177 | } |
Andi Kleen | 14f9f7b | 2008-02-06 01:40:10 -0800 | [diff] [blame] | 178 | return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 179 | } |
| 180 | #endif |