Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/ext2/ioctl.c |
| 4 | * |
| 5 | * Copyright (C) 1993, 1994, 1995 |
| 6 | * Remy Card (card@masi.ibp.fr) |
| 7 | * Laboratoire MASI - Institut Blaise Pascal |
| 8 | * Universite Pierre et Marie Curie (Paris VI) |
| 9 | */ |
| 10 | |
| 11 | #include "ext2.h" |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 12 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/time.h> |
| 14 | #include <linux/sched.h> |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 15 | #include <linux/compat.h> |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 16 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/current.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 18 | #include <linux/uaccess.h> |
Miklos Szeredi | aba405e | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 19 | #include <linux/fileattr.h> |
| 20 | |
| 21 | int ext2_fileattr_get(struct dentry *dentry, struct fileattr *fa) |
| 22 | { |
| 23 | struct ext2_inode_info *ei = EXT2_I(d_inode(dentry)); |
| 24 | |
| 25 | fileattr_fill_flags(fa, ei->i_flags & EXT2_FL_USER_VISIBLE); |
| 26 | |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | int ext2_fileattr_set(struct user_namespace *mnt_userns, |
| 31 | struct dentry *dentry, struct fileattr *fa) |
| 32 | { |
| 33 | struct inode *inode = d_inode(dentry); |
| 34 | struct ext2_inode_info *ei = EXT2_I(inode); |
| 35 | |
| 36 | if (fileattr_has_fsx(fa)) |
| 37 | return -EOPNOTSUPP; |
| 38 | |
| 39 | /* Is it quota file? Do not allow user to mess with it */ |
| 40 | if (IS_NOQUOTA(inode)) |
| 41 | return -EPERM; |
| 42 | |
| 43 | ei->i_flags = (ei->i_flags & ~EXT2_FL_USER_MODIFIABLE) | |
| 44 | (fa->flags & EXT2_FL_USER_MODIFIABLE); |
| 45 | |
| 46 | ext2_set_inode_flags(inode); |
| 47 | inode->i_ctime = current_time(inode); |
| 48 | mark_inode_dirty(inode); |
| 49 | |
| 50 | return 0; |
| 51 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | |
Andi Kleen | 14f9f7b | 2008-02-06 01:40:10 -0800 | [diff] [blame] | 54 | long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 56 | struct inode *inode = file_inode(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | struct ext2_inode_info *ei = EXT2_I(inode); |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 58 | unsigned short rsv_window_size; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 59 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg); |
| 62 | |
| 63 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | case EXT2_IOC_GETVERSION: |
| 65 | return put_user(inode->i_generation, (int __user *) arg); |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 66 | case EXT2_IOC_SETVERSION: { |
| 67 | __u32 generation; |
| 68 | |
Christian Brauner | 21cb47b | 2021-01-21 14:19:25 +0100 | [diff] [blame] | 69 | if (!inode_owner_or_capable(&init_user_ns, inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | return -EPERM; |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 71 | ret = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 72 | if (ret) |
| 73 | return ret; |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 74 | if (get_user(generation, (int __user *) arg)) { |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 75 | ret = -EFAULT; |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 76 | goto setversion_out; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 77 | } |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 78 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 79 | inode_lock(inode); |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 80 | inode->i_ctime = current_time(inode); |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 81 | inode->i_generation = generation; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 82 | inode_unlock(inode); |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 83 | |
| 84 | mark_inode_dirty(inode); |
| 85 | setversion_out: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 86 | mnt_drop_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 87 | return ret; |
Djalal Harouni | 34b0784 | 2012-01-09 15:58:37 +0100 | [diff] [blame] | 88 | } |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 89 | case EXT2_IOC_GETRSVSZ: |
| 90 | if (test_opt(inode->i_sb, RESERVATION) |
| 91 | && S_ISREG(inode->i_mode) |
| 92 | && ei->i_block_alloc_info) { |
| 93 | rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size; |
| 94 | return put_user(rsv_window_size, (int __user *)arg); |
| 95 | } |
| 96 | return -ENOTTY; |
| 97 | case EXT2_IOC_SETRSVSZ: { |
| 98 | |
| 99 | if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode)) |
| 100 | return -ENOTTY; |
| 101 | |
Christian Brauner | 21cb47b | 2021-01-21 14:19:25 +0100 | [diff] [blame] | 102 | if (!inode_owner_or_capable(&init_user_ns, inode)) |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 103 | return -EACCES; |
| 104 | |
| 105 | if (get_user(rsv_window_size, (int __user *)arg)) |
| 106 | return -EFAULT; |
| 107 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 108 | ret = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 109 | if (ret) |
| 110 | return ret; |
| 111 | |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 112 | if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS) |
| 113 | rsv_window_size = EXT2_MAX_RESERVE_BLOCKS; |
| 114 | |
| 115 | /* |
| 116 | * need to allocate reservation structure for this inode |
| 117 | * before set the window size |
| 118 | */ |
| 119 | /* |
| 120 | * XXX What lock should protect the rsv_goal_size? |
| 121 | * Accessed in ext2_get_block only. ext3 uses i_truncate. |
| 122 | */ |
| 123 | mutex_lock(&ei->truncate_mutex); |
| 124 | if (!ei->i_block_alloc_info) |
| 125 | ext2_init_block_alloc_info(inode); |
| 126 | |
| 127 | if (ei->i_block_alloc_info){ |
| 128 | struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node; |
| 129 | rsv->rsv_goal_size = rsv_window_size; |
Chengguang Xu | acd1f04 | 2019-10-23 21:56:43 +0800 | [diff] [blame] | 130 | } else { |
| 131 | ret = -ENOMEM; |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 132 | } |
Chengguang Xu | acd1f04 | 2019-10-23 21:56:43 +0800 | [diff] [blame] | 133 | |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 134 | mutex_unlock(&ei->truncate_mutex); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 135 | mnt_drop_write_file(filp); |
Chengguang Xu | acd1f04 | 2019-10-23 21:56:43 +0800 | [diff] [blame] | 136 | return ret; |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | default: |
| 139 | return -ENOTTY; |
| 140 | } |
| 141 | } |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 142 | |
| 143 | #ifdef CONFIG_COMPAT |
| 144 | long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 145 | { |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 146 | /* These are just misnamed, they actually get/put from/to user an int */ |
| 147 | switch (cmd) { |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 148 | case EXT2_IOC32_GETVERSION: |
| 149 | cmd = EXT2_IOC_GETVERSION; |
| 150 | break; |
| 151 | case EXT2_IOC32_SETVERSION: |
| 152 | cmd = EXT2_IOC_SETVERSION; |
| 153 | break; |
| 154 | default: |
| 155 | return -ENOIOCTLCMD; |
| 156 | } |
Andi Kleen | 14f9f7b | 2008-02-06 01:40:10 -0800 | [diff] [blame] | 157 | return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 158 | } |
| 159 | #endif |