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