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> |
| 7 | #include <linux/reiserfs_fs.h> |
| 8 | #include <linux/time.h> |
| 9 | #include <asm/uaccess.h> |
| 10 | #include <linux/pagemap.h> |
| 11 | #include <linux/smp_lock.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 | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 14 | static int reiserfs_unpack(struct inode *inode, struct file *filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | ** reiserfs_ioctl - handler for ioctl for inode |
| 18 | ** supported commands: |
| 19 | ** 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect |
| 20 | ** and prevent packing file (argument arg has to be non-zero) |
| 21 | ** 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION |
| 22 | ** 3) That's all for a while ... |
| 23 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 24 | int reiserfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, |
| 25 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
| 27 | unsigned int flags; |
| 28 | |
| 29 | switch (cmd) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 30 | case REISERFS_IOC_UNPACK: |
| 31 | if (S_ISREG(inode->i_mode)) { |
| 32 | if (arg) |
| 33 | return reiserfs_unpack(inode, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | else |
| 35 | return 0; |
| 36 | } else |
| 37 | return -ENOTTY; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 38 | /* following two cases are taken from fs/ext2/ioctl.c by Remy |
| 39 | Card (card@masi.ibp.fr) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | case REISERFS_IOC_GETFLAGS: |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 41 | if (!reiserfs_attrs(inode->i_sb)) |
Jeff Mahoney | 869eb76 | 2005-06-29 18:52:28 -0400 | [diff] [blame] | 42 | return -ENOTTY; |
| 43 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 44 | flags = REISERFS_I(inode)->i_attrs; |
| 45 | i_attrs_to_sd_attrs(inode, (__u16 *) & flags); |
| 46 | return put_user(flags, (int __user *)arg); |
| 47 | case REISERFS_IOC_SETFLAGS:{ |
| 48 | if (!reiserfs_attrs(inode->i_sb)) |
| 49 | return -ENOTTY; |
Jeff Mahoney | 869eb76 | 2005-06-29 18:52:28 -0400 | [diff] [blame] | 50 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 51 | if (IS_RDONLY(inode)) |
| 52 | return -EROFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 54 | if (!is_owner_or_cap(inode)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 55 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 57 | if (get_user(flags, (int __user *)arg)) |
| 58 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 60 | if (((flags ^ REISERFS_I(inode)-> |
| 61 | i_attrs) & (REISERFS_IMMUTABLE_FL | |
| 62 | REISERFS_APPEND_FL)) |
| 63 | && !capable(CAP_LINUX_IMMUTABLE)) |
| 64 | return -EPERM; |
| 65 | |
| 66 | if ((flags & REISERFS_NOTAIL_FL) && |
| 67 | S_ISREG(inode->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | int result; |
| 69 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 70 | result = reiserfs_unpack(inode, filp); |
| 71 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return result; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 73 | } |
| 74 | sd_attrs_to_i_attrs(flags, inode); |
| 75 | REISERFS_I(inode)->i_attrs = flags; |
| 76 | inode->i_ctime = CURRENT_TIME_SEC; |
| 77 | mark_inode_dirty(inode); |
| 78 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | case REISERFS_IOC_GETVERSION: |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 81 | return put_user(inode->i_generation, (int __user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | case REISERFS_IOC_SETVERSION: |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 83 | if (!is_owner_or_cap(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return -EPERM; |
| 85 | if (IS_RDONLY(inode)) |
| 86 | return -EROFS; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 87 | if (get_user(inode->i_generation, (int __user *)arg)) |
| 88 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | inode->i_ctime = CURRENT_TIME_SEC; |
| 90 | mark_inode_dirty(inode); |
| 91 | return 0; |
| 92 | default: |
| 93 | return -ENOTTY; |
| 94 | } |
| 95 | } |
| 96 | |
David Howells | 52b499c | 2006-08-29 19:06:18 +0100 | [diff] [blame] | 97 | #ifdef CONFIG_COMPAT |
| 98 | long reiserfs_compat_ioctl(struct file *file, unsigned int cmd, |
| 99 | unsigned long arg) |
| 100 | { |
Josef Sipek | 1fc5adb | 2006-12-08 02:37:33 -0800 | [diff] [blame] | 101 | struct inode *inode = file->f_path.dentry->d_inode; |
David Howells | 52b499c | 2006-08-29 19:06:18 +0100 | [diff] [blame] | 102 | int ret; |
| 103 | |
| 104 | /* These are just misnamed, they actually get/put from/to user an int */ |
| 105 | switch (cmd) { |
| 106 | case REISERFS_IOC32_UNPACK: |
| 107 | cmd = REISERFS_IOC_UNPACK; |
| 108 | break; |
| 109 | case REISERFS_IOC32_GETFLAGS: |
| 110 | cmd = REISERFS_IOC_GETFLAGS; |
| 111 | break; |
| 112 | case REISERFS_IOC32_SETFLAGS: |
| 113 | cmd = REISERFS_IOC_SETFLAGS; |
| 114 | break; |
| 115 | case REISERFS_IOC32_GETVERSION: |
| 116 | cmd = REISERFS_IOC_GETVERSION; |
| 117 | break; |
| 118 | case REISERFS_IOC32_SETVERSION: |
| 119 | cmd = REISERFS_IOC_SETVERSION; |
| 120 | break; |
| 121 | default: |
| 122 | return -ENOIOCTLCMD; |
| 123 | } |
| 124 | lock_kernel(); |
| 125 | ret = reiserfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg)); |
| 126 | unlock_kernel(); |
| 127 | return ret; |
| 128 | } |
| 129 | #endif |
| 130 | |
Vladimir Saveliev | ba9d8ce | 2007-10-16 01:25:14 -0700 | [diff] [blame] | 131 | int reiserfs_commit_write(struct file *f, struct page *page, |
| 132 | unsigned from, unsigned to); |
| 133 | int reiserfs_prepare_write(struct file *f, struct page *page, |
| 134 | unsigned from, unsigned to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | /* |
| 136 | ** reiserfs_unpack |
| 137 | ** Function try to convert tail from direct item into indirect. |
| 138 | ** It set up nopack attribute in the REISERFS_I(inode)->nopack |
| 139 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 140 | static int reiserfs_unpack(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 142 | int retval = 0; |
| 143 | int index; |
| 144 | struct page *page; |
| 145 | struct address_space *mapping; |
| 146 | unsigned long write_from; |
| 147 | unsigned long blocksize = inode->i_sb->s_blocksize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 149 | if (inode->i_size == 0) { |
| 150 | REISERFS_I(inode)->i_flags |= i_nopack_mask; |
| 151 | return 0; |
| 152 | } |
| 153 | /* ioctl already done */ |
| 154 | if (REISERFS_I(inode)->i_flags & i_nopack_mask) { |
| 155 | return 0; |
| 156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 158 | /* we need to make sure nobody is changing the file size beneath |
| 159 | ** us |
| 160 | */ |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 161 | mutex_lock(&inode->i_mutex); |
Chris Mason | b5f3953 | 2006-08-05 12:15:08 -0700 | [diff] [blame] | 162 | reiserfs_write_lock(inode->i_sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 163 | |
| 164 | write_from = inode->i_size & (blocksize - 1); |
| 165 | /* if we are on a block boundary, we are already unpacked. */ |
| 166 | if (write_from == 0) { |
| 167 | REISERFS_I(inode)->i_flags |= i_nopack_mask; |
| 168 | goto out; |
| 169 | } |
| 170 | |
| 171 | /* we unpack by finding the page with the tail, and calling |
| 172 | ** reiserfs_prepare_write on that page. This will force a |
| 173 | ** reiserfs_get_block to unpack the tail for us. |
| 174 | */ |
| 175 | index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 176 | mapping = inode->i_mapping; |
| 177 | page = grab_cache_page(mapping, index); |
| 178 | retval = -ENOMEM; |
| 179 | if (!page) { |
| 180 | goto out; |
| 181 | } |
Vladimir Saveliev | ba9d8ce | 2007-10-16 01:25:14 -0700 | [diff] [blame] | 182 | retval = reiserfs_prepare_write(NULL, page, write_from, write_from); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 183 | if (retval) |
| 184 | goto out_unlock; |
| 185 | |
| 186 | /* conversion can change page contents, must flush */ |
| 187 | flush_dcache_page(page); |
Vladimir Saveliev | ba9d8ce | 2007-10-16 01:25:14 -0700 | [diff] [blame] | 188 | retval = reiserfs_commit_write(NULL, page, write_from, write_from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | REISERFS_I(inode)->i_flags |= i_nopack_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 191 | out_unlock: |
| 192 | unlock_page(page); |
| 193 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 195 | out: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 196 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 197 | reiserfs_write_unlock(inode->i_sb); |
| 198 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |