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> |
| 10 | #include <asm/uaccess.h> |
| 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 |
| 18 | * and prevent packing file (argument arg has to be non-zero) |
| 19 | * 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION |
| 20 | * 3) That's all for a while ... |
| 21 | */ |
Frederic Weisbecker | 205cb37 | 2009-10-14 23:22:17 +0200 | [diff] [blame] | 22 | long reiserfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 24 | struct inode *inode = file_inode(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | unsigned int flags; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 26 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 28 | reiserfs_write_lock(inode->i_sb); |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | switch (cmd) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 31 | case REISERFS_IOC_UNPACK: |
| 32 | if (S_ISREG(inode->i_mode)) { |
| 33 | if (arg) |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 34 | err = reiserfs_unpack(inode, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | } else |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 36 | err = -ENOTTY; |
| 37 | break; |
| 38 | /* |
| 39 | * following two cases are taken from fs/ext2/ioctl.c by Remy |
| 40 | * Card (card@masi.ibp.fr) |
| 41 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | case REISERFS_IOC_GETFLAGS: |
Frederic Weisbecker | ac78a07 | 2009-10-14 23:08:43 +0200 | [diff] [blame] | 43 | if (!reiserfs_attrs(inode->i_sb)) { |
| 44 | err = -ENOTTY; |
| 45 | break; |
| 46 | } |
Jeff Mahoney | 869eb76 | 2005-06-29 18:52:28 -0400 | [diff] [blame] | 47 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 48 | flags = REISERFS_I(inode)->i_attrs; |
| 49 | i_attrs_to_sd_attrs(inode, (__u16 *) & flags); |
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; |
| 96 | inode->i_ctime = CURRENT_TIME_SEC; |
| 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | inode->i_ctime = CURRENT_TIME_SEC; |
| 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 | { |
David Howells | 52b499c | 2006-08-29 19:06:18 +0100 | [diff] [blame] | 135 | /* These are just misnamed, they actually get/put from/to user an int */ |
| 136 | switch (cmd) { |
| 137 | case REISERFS_IOC32_UNPACK: |
| 138 | cmd = REISERFS_IOC_UNPACK; |
| 139 | break; |
| 140 | case REISERFS_IOC32_GETFLAGS: |
| 141 | cmd = REISERFS_IOC_GETFLAGS; |
| 142 | break; |
| 143 | case REISERFS_IOC32_SETFLAGS: |
| 144 | cmd = REISERFS_IOC_SETFLAGS; |
| 145 | break; |
| 146 | case REISERFS_IOC32_GETVERSION: |
| 147 | cmd = REISERFS_IOC_GETVERSION; |
| 148 | break; |
| 149 | case REISERFS_IOC32_SETVERSION: |
| 150 | cmd = REISERFS_IOC_SETVERSION; |
| 151 | break; |
| 152 | default: |
| 153 | return -ENOIOCTLCMD; |
| 154 | } |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 155 | |
Frederic Weisbecker | 205cb37 | 2009-10-14 23:22:17 +0200 | [diff] [blame] | 156 | return reiserfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
David Howells | 52b499c | 2006-08-29 19:06:18 +0100 | [diff] [blame] | 157 | } |
| 158 | #endif |
| 159 | |
Vladimir Saveliev | ba9d8ce | 2007-10-16 01:25:14 -0700 | [diff] [blame] | 160 | int reiserfs_commit_write(struct file *f, struct page *page, |
| 161 | unsigned from, unsigned to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | /* |
| 163 | ** reiserfs_unpack |
| 164 | ** Function try to convert tail from direct item into indirect. |
| 165 | ** It set up nopack attribute in the REISERFS_I(inode)->nopack |
| 166 | */ |
Jan Kara | d5dee5c | 2008-04-28 02:16:23 -0700 | [diff] [blame] | 167 | int reiserfs_unpack(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 169 | int retval = 0; |
Frederic Weisbecker | 9d8117e | 2010-09-30 15:15:38 -0700 | [diff] [blame] | 170 | int depth; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 171 | int index; |
| 172 | struct page *page; |
| 173 | struct address_space *mapping; |
| 174 | unsigned long write_from; |
| 175 | unsigned long blocksize = inode->i_sb->s_blocksize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 177 | if (inode->i_size == 0) { |
| 178 | REISERFS_I(inode)->i_flags |= i_nopack_mask; |
| 179 | return 0; |
| 180 | } |
| 181 | /* ioctl already done */ |
| 182 | if (REISERFS_I(inode)->i_flags & i_nopack_mask) { |
| 183 | return 0; |
| 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Frederic Weisbecker | 9d8117e | 2010-09-30 15:15:38 -0700 | [diff] [blame] | 186 | depth = reiserfs_write_lock_once(inode->i_sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -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 */ |
| 189 | reiserfs_mutex_lock_safe(&inode->i_mutex, inode->i_sb); |
| 190 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 191 | write_from = inode->i_size & (blocksize - 1); |
| 192 | /* if we are on a block boundary, we are already unpacked. */ |
| 193 | if (write_from == 0) { |
| 194 | REISERFS_I(inode)->i_flags |= i_nopack_mask; |
| 195 | goto out; |
| 196 | } |
| 197 | |
| 198 | /* we unpack by finding the page with the tail, and calling |
Christoph Hellwig | ebdec24 | 2010-10-06 10:47:23 +0200 | [diff] [blame] | 199 | ** __reiserfs_write_begin on that page. This will force a |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 200 | ** reiserfs_get_block to unpack the tail for us. |
| 201 | */ |
| 202 | index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 203 | mapping = inode->i_mapping; |
| 204 | page = grab_cache_page(mapping, index); |
| 205 | retval = -ENOMEM; |
| 206 | if (!page) { |
| 207 | goto out; |
| 208 | } |
Christoph Hellwig | ebdec24 | 2010-10-06 10:47:23 +0200 | [diff] [blame] | 209 | retval = __reiserfs_write_begin(page, write_from, 0); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 210 | if (retval) |
| 211 | goto out_unlock; |
| 212 | |
| 213 | /* conversion can change page contents, must flush */ |
| 214 | flush_dcache_page(page); |
Vladimir Saveliev | ba9d8ce | 2007-10-16 01:25:14 -0700 | [diff] [blame] | 215 | retval = reiserfs_commit_write(NULL, page, write_from, write_from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | REISERFS_I(inode)->i_flags |= i_nopack_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 218 | out_unlock: |
| 219 | unlock_page(page); |
| 220 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 222 | out: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 223 | mutex_unlock(&inode->i_mutex); |
Frederic Weisbecker | 9d8117e | 2010-09-30 15:15:38 -0700 | [diff] [blame] | 224 | reiserfs_write_unlock_once(inode->i_sb, depth); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 225 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |