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