Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README |
| 3 | */ |
| 4 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 5 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/fs.h> |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 7 | #include <linux/mount.h> |
Al Viro | f466c6f | 2012-03-17 01:16:43 -0400 | [diff] [blame] | 8 | #include "reiserfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/time.h> |
Fabian Frederick | 17093991 | 2014-08-08 14:21:12 -0700 | [diff] [blame] | 10 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/pagemap.h> |
David Howells | 52b499c | 2006-08-29 19:06:18 +0100 | [diff] [blame] | 12 | #include <linux/compat.h> |
Miklos Szeredi | 03eb606 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 13 | #include <linux/fileattr.h> |
| 14 | |
| 15 | int reiserfs_fileattr_get(struct dentry *dentry, struct fileattr *fa) |
| 16 | { |
| 17 | struct inode *inode = d_inode(dentry); |
| 18 | |
| 19 | if (!reiserfs_attrs(inode->i_sb)) |
| 20 | return -ENOTTY; |
| 21 | |
| 22 | fileattr_fill_flags(fa, REISERFS_I(inode)->i_attrs); |
| 23 | |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | int reiserfs_fileattr_set(struct user_namespace *mnt_userns, |
| 28 | struct dentry *dentry, struct fileattr *fa) |
| 29 | { |
| 30 | struct inode *inode = d_inode(dentry); |
| 31 | unsigned int flags = fa->flags; |
| 32 | int err; |
| 33 | |
| 34 | reiserfs_write_lock(inode->i_sb); |
| 35 | |
| 36 | err = -ENOTTY; |
| 37 | if (!reiserfs_attrs(inode->i_sb)) |
| 38 | goto unlock; |
| 39 | |
| 40 | err = -EOPNOTSUPP; |
| 41 | if (fileattr_has_fsx(fa)) |
| 42 | goto unlock; |
| 43 | |
| 44 | /* |
| 45 | * Is it quota file? Do not allow user to mess with it |
| 46 | */ |
| 47 | err = -EPERM; |
| 48 | if (IS_NOQUOTA(inode)) |
| 49 | goto unlock; |
| 50 | |
| 51 | if ((flags & REISERFS_NOTAIL_FL) && S_ISREG(inode->i_mode)) { |
| 52 | err = reiserfs_unpack(inode); |
| 53 | if (err) |
| 54 | goto unlock; |
| 55 | } |
| 56 | sd_attrs_to_i_attrs(flags, inode); |
| 57 | REISERFS_I(inode)->i_attrs = flags; |
| 58 | inode->i_ctime = current_time(inode); |
| 59 | mark_inode_dirty(inode); |
| 60 | err = 0; |
| 61 | unlock: |
| 62 | reiserfs_write_unlock(inode->i_sb); |
| 63 | |
| 64 | return err; |
| 65 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 68 | * reiserfs_ioctl - handler for ioctl for inode |
| 69 | * supported commands: |
| 70 | * 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 71 | * and prevent packing file (argument arg has t |
| 72 | * be non-zero) |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 73 | * 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION |
| 74 | * 3) That's all for a while ... |
| 75 | */ |
Frederic Weisbecker | 205cb37 | 2009-10-14 23:22:17 +0200 | [diff] [blame] | 76 | long reiserfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 78 | struct inode *inode = file_inode(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 79 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 81 | reiserfs_write_lock(inode->i_sb); |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | switch (cmd) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 84 | case REISERFS_IOC_UNPACK: |
| 85 | if (S_ISREG(inode->i_mode)) { |
| 86 | if (arg) |
Miklos Szeredi | 03eb606 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 87 | err = reiserfs_unpack(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } else |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 89 | err = -ENOTTY; |
| 90 | break; |
| 91 | /* |
| 92 | * following two cases are taken from fs/ext2/ioctl.c by Remy |
| 93 | * Card (card@masi.ibp.fr) |
| 94 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | case REISERFS_IOC_GETVERSION: |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 96 | err = put_user(inode->i_generation, (int __user *)arg); |
| 97 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | case REISERFS_IOC_SETVERSION: |
Christian Brauner | 21cb47b | 2021-01-21 14:19:25 +0100 | [diff] [blame] | 99 | if (!inode_owner_or_capable(&init_user_ns, inode)) { |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 100 | err = -EPERM; |
| 101 | break; |
Jiri Slaby | e0baec1 | 2010-01-06 23:09:50 +0100 | [diff] [blame] | 102 | } |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 103 | err = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 104 | if (err) |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 105 | break; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 106 | if (get_user(inode->i_generation, (int __user *)arg)) { |
| 107 | err = -EFAULT; |
| 108 | goto setversion_out; |
| 109 | } |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 110 | inode->i_ctime = current_time(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | mark_inode_dirty(inode); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 112 | setversion_out: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 113 | mnt_drop_write_file(filp); |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 114 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | default: |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 116 | err = -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 118 | |
| 119 | reiserfs_write_unlock(inode->i_sb); |
| 120 | |
| 121 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
David Howells | 52b499c | 2006-08-29 19:06:18 +0100 | [diff] [blame] | 124 | #ifdef CONFIG_COMPAT |
| 125 | long reiserfs_compat_ioctl(struct file *file, unsigned int cmd, |
| 126 | unsigned long arg) |
| 127 | { |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 128 | /* |
| 129 | * These are just misnamed, they actually |
| 130 | * get/put from/to user an int |
| 131 | */ |
David Howells | 52b499c | 2006-08-29 19:06:18 +0100 | [diff] [blame] | 132 | switch (cmd) { |
| 133 | case REISERFS_IOC32_UNPACK: |
| 134 | cmd = REISERFS_IOC_UNPACK; |
| 135 | break; |
David Howells | 52b499c | 2006-08-29 19:06:18 +0100 | [diff] [blame] | 136 | case REISERFS_IOC32_GETVERSION: |
| 137 | cmd = REISERFS_IOC_GETVERSION; |
| 138 | break; |
| 139 | case REISERFS_IOC32_SETVERSION: |
| 140 | cmd = REISERFS_IOC_SETVERSION; |
| 141 | break; |
| 142 | default: |
| 143 | return -ENOIOCTLCMD; |
| 144 | } |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 145 | |
Frederic Weisbecker | 205cb37 | 2009-10-14 23:22:17 +0200 | [diff] [blame] | 146 | return reiserfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
David Howells | 52b499c | 2006-08-29 19:06:18 +0100 | [diff] [blame] | 147 | } |
| 148 | #endif |
| 149 | |
Vladimir Saveliev | ba9d8ce | 2007-10-16 01:25:14 -0700 | [diff] [blame] | 150 | int reiserfs_commit_write(struct file *f, struct page *page, |
| 151 | unsigned from, unsigned to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 153 | * reiserfs_unpack |
| 154 | * Function try to convert tail from direct item into indirect. |
| 155 | * It set up nopack attribute in the REISERFS_I(inode)->nopack |
| 156 | */ |
Miklos Szeredi | 03eb606 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 157 | int reiserfs_unpack(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 159 | int retval = 0; |
| 160 | int index; |
| 161 | struct page *page; |
| 162 | struct address_space *mapping; |
| 163 | unsigned long write_from; |
| 164 | unsigned long blocksize = inode->i_sb->s_blocksize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 166 | if (inode->i_size == 0) { |
| 167 | REISERFS_I(inode)->i_flags |= i_nopack_mask; |
| 168 | return 0; |
| 169 | } |
| 170 | /* ioctl already done */ |
| 171 | if (REISERFS_I(inode)->i_flags & i_nopack_mask) { |
| 172 | return 0; |
| 173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Frederic Weisbecker | da90587 | 2010-11-24 12:57:15 -0800 | [diff] [blame] | 175 | /* we need to make sure nobody is changing the file size beneath us */ |
Colin Ian King | 5404e7e | 2020-04-06 20:11:46 -0700 | [diff] [blame] | 176 | { |
| 177 | int depth = reiserfs_write_unlock_nested(inode->i_sb); |
| 178 | |
| 179 | inode_lock(inode); |
| 180 | reiserfs_write_lock_nested(inode->i_sb, depth); |
| 181 | } |
Frederic Weisbecker | da90587 | 2010-11-24 12:57:15 -0800 | [diff] [blame] | 182 | |
Jeff Mahoney | 278f667 | 2013-08-08 17:34:46 -0400 | [diff] [blame] | 183 | reiserfs_write_lock(inode->i_sb); |
| 184 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 185 | write_from = inode->i_size & (blocksize - 1); |
| 186 | /* if we are on a block boundary, we are already unpacked. */ |
| 187 | if (write_from == 0) { |
| 188 | REISERFS_I(inode)->i_flags |= i_nopack_mask; |
| 189 | goto out; |
| 190 | } |
| 191 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 192 | /* |
| 193 | * we unpack by finding the page with the tail, and calling |
| 194 | * __reiserfs_write_begin on that page. This will force a |
| 195 | * reiserfs_get_block to unpack the tail for us. |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 196 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 197 | index = inode->i_size >> PAGE_SHIFT; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 198 | mapping = inode->i_mapping; |
| 199 | page = grab_cache_page(mapping, index); |
| 200 | retval = -ENOMEM; |
| 201 | if (!page) { |
| 202 | goto out; |
| 203 | } |
Christoph Hellwig | ebdec24 | 2010-10-06 10:47:23 +0200 | [diff] [blame] | 204 | retval = __reiserfs_write_begin(page, write_from, 0); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 205 | if (retval) |
| 206 | goto out_unlock; |
| 207 | |
| 208 | /* conversion can change page contents, must flush */ |
| 209 | flush_dcache_page(page); |
Vladimir Saveliev | ba9d8ce | 2007-10-16 01:25:14 -0700 | [diff] [blame] | 210 | retval = reiserfs_commit_write(NULL, page, write_from, write_from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | REISERFS_I(inode)->i_flags |= i_nopack_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Jeff Mahoney | cf776a7 | 2014-04-23 10:00:41 -0400 | [diff] [blame] | 213 | out_unlock: |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 214 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 215 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Jeff Mahoney | cf776a7 | 2014-04-23 10:00:41 -0400 | [diff] [blame] | 217 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 218 | inode_unlock(inode); |
Jeff Mahoney | 278f667 | 2013-08-08 17:34:46 -0400 | [diff] [blame] | 219 | reiserfs_write_unlock(inode->i_sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 220 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |