Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 19 | #include <linux/bio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 20 | #include <linux/buffer_head.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/pagemap.h> |
| 23 | #include <linux/highmem.h> |
| 24 | #include <linux/time.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/string.h> |
| 27 | #include <linux/smp_lock.h> |
| 28 | #include <linux/backing-dev.h> |
| 29 | #include <linux/mpage.h> |
| 30 | #include <linux/swap.h> |
| 31 | #include <linux/writeback.h> |
| 32 | #include <linux/statfs.h> |
| 33 | #include <linux/compat.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 34 | #include <linux/bit_spinlock.h> |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 35 | #include <linux/version.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 36 | #include <linux/xattr.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 37 | #include "ctree.h" |
| 38 | #include "disk-io.h" |
| 39 | #include "transaction.h" |
| 40 | #include "btrfs_inode.h" |
| 41 | #include "ioctl.h" |
| 42 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 43 | #include "volumes.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 44 | |
| 45 | struct btrfs_iget_args { |
| 46 | u64 ino; |
| 47 | struct btrfs_root *root; |
| 48 | }; |
| 49 | |
| 50 | static struct inode_operations btrfs_dir_inode_operations; |
| 51 | static struct inode_operations btrfs_symlink_inode_operations; |
| 52 | static struct inode_operations btrfs_dir_ro_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 53 | static struct inode_operations btrfs_special_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 54 | static struct inode_operations btrfs_file_inode_operations; |
| 55 | static struct address_space_operations btrfs_aops; |
| 56 | static struct address_space_operations btrfs_symlink_aops; |
| 57 | static struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 58 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 59 | |
| 60 | static struct kmem_cache *btrfs_inode_cachep; |
| 61 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 62 | struct kmem_cache *btrfs_transaction_cachep; |
| 63 | struct kmem_cache *btrfs_bit_radix_cachep; |
| 64 | struct kmem_cache *btrfs_path_cachep; |
| 65 | |
| 66 | #define S_SHIFT 12 |
| 67 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 68 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 69 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 70 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 71 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 72 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 73 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 74 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 75 | }; |
| 76 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 77 | int btrfs_check_free_space(struct btrfs_root *root, u64 num_required, |
| 78 | int for_del) |
| 79 | { |
| 80 | u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 81 | u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy); |
| 82 | u64 thresh; |
| 83 | int ret = 0; |
| 84 | |
| 85 | if (for_del) |
Chris Mason | f9ef660 | 2008-01-03 09:22:38 -0500 | [diff] [blame] | 86 | thresh = total * 90; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 87 | else |
Chris Mason | f9ef660 | 2008-01-03 09:22:38 -0500 | [diff] [blame] | 88 | thresh = total * 85; |
| 89 | |
| 90 | do_div(thresh, 100); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 91 | |
| 92 | spin_lock(&root->fs_info->delalloc_lock); |
| 93 | if (used + root->fs_info->delalloc_bytes + num_required > thresh) |
| 94 | ret = -ENOSPC; |
| 95 | spin_unlock(&root->fs_info->delalloc_lock); |
| 96 | return ret; |
| 97 | } |
| 98 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 99 | static int cow_file_range(struct inode *inode, u64 start, u64 end) |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 100 | { |
| 101 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 102 | struct btrfs_trans_handle *trans; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 103 | u64 alloc_hint = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 104 | u64 num_bytes; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 105 | u64 cur_alloc_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 106 | u64 blocksize = root->sectorsize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 107 | u64 orig_start = start; |
| 108 | u64 orig_num_bytes; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 109 | struct btrfs_key ins; |
| 110 | int ret; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 111 | |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 112 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 113 | BUG_ON(!trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 114 | btrfs_set_trans_block_group(trans, inode); |
| 115 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 116 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 117 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 118 | ret = btrfs_drop_extents(trans, root, inode, |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 119 | start, start + num_bytes, start, &alloc_hint); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 120 | orig_num_bytes = num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 121 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 122 | if (alloc_hint == EXTENT_MAP_INLINE) |
| 123 | goto out; |
| 124 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 125 | BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 126 | |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 127 | while(num_bytes > 0) { |
| 128 | cur_alloc_size = min(num_bytes, root->fs_info->max_extent); |
| 129 | ret = btrfs_alloc_extent(trans, root, cur_alloc_size, |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 130 | root->sectorsize, |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 131 | root->root_key.objectid, |
| 132 | trans->transid, |
| 133 | inode->i_ino, start, 0, |
| 134 | alloc_hint, (u64)-1, &ins, 1); |
| 135 | if (ret) { |
| 136 | WARN_ON(1); |
| 137 | goto out; |
| 138 | } |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 139 | cur_alloc_size = ins.offset; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 140 | ret = btrfs_insert_file_extent(trans, root, inode->i_ino, |
| 141 | start, ins.objectid, ins.offset, |
| 142 | ins.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 143 | inode->i_blocks += ins.offset >> 9; |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 144 | btrfs_check_file(root, inode); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 145 | if (num_bytes < cur_alloc_size) { |
| 146 | printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes, |
| 147 | cur_alloc_size); |
| 148 | break; |
| 149 | } |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 150 | num_bytes -= cur_alloc_size; |
| 151 | alloc_hint = ins.objectid + ins.offset; |
| 152 | start += cur_alloc_size; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 153 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 154 | btrfs_drop_extent_cache(inode, orig_start, |
| 155 | orig_start + orig_num_bytes - 1); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 156 | btrfs_add_ordered_inode(inode); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 157 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 158 | out: |
| 159 | btrfs_end_transaction(trans, root); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end) |
| 164 | { |
| 165 | u64 extent_start; |
| 166 | u64 extent_end; |
| 167 | u64 bytenr; |
| 168 | u64 cow_end; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 169 | u64 loops = 0; |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 170 | u64 total_fs_bytes; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 171 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 172 | struct extent_buffer *leaf; |
| 173 | int found_type; |
| 174 | struct btrfs_path *path; |
| 175 | struct btrfs_file_extent_item *item; |
| 176 | int ret; |
| 177 | int err; |
| 178 | struct btrfs_key found_key; |
| 179 | |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 180 | total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 181 | path = btrfs_alloc_path(); |
| 182 | BUG_ON(!path); |
| 183 | again: |
| 184 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 185 | inode->i_ino, start, 0); |
| 186 | if (ret < 0) { |
| 187 | btrfs_free_path(path); |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | cow_end = end; |
| 192 | if (ret != 0) { |
| 193 | if (path->slots[0] == 0) |
| 194 | goto not_found; |
| 195 | path->slots[0]--; |
| 196 | } |
| 197 | |
| 198 | leaf = path->nodes[0]; |
| 199 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 200 | struct btrfs_file_extent_item); |
| 201 | |
| 202 | /* are we inside the extent that was found? */ |
| 203 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 204 | found_type = btrfs_key_type(&found_key); |
| 205 | if (found_key.objectid != inode->i_ino || |
| 206 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 207 | goto not_found; |
| 208 | } |
| 209 | |
| 210 | found_type = btrfs_file_extent_type(leaf, item); |
| 211 | extent_start = found_key.offset; |
| 212 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 213 | u64 extent_num_bytes; |
| 214 | |
| 215 | extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item); |
| 216 | extent_end = extent_start + extent_num_bytes; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 217 | err = 0; |
| 218 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 219 | if (loops && start != extent_start) |
| 220 | goto not_found; |
| 221 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 222 | if (start < extent_start || start >= extent_end) |
| 223 | goto not_found; |
| 224 | |
| 225 | cow_end = min(end, extent_end - 1); |
| 226 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 227 | if (bytenr == 0) |
| 228 | goto not_found; |
| 229 | |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 230 | /* |
| 231 | * we may be called by the resizer, make sure we're inside |
| 232 | * the limits of the FS |
| 233 | */ |
| 234 | if (bytenr + extent_num_bytes > total_fs_bytes) |
| 235 | goto not_found; |
| 236 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 237 | if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) { |
| 238 | goto not_found; |
| 239 | } |
| 240 | |
| 241 | start = extent_end; |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 242 | } else { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 243 | goto not_found; |
| 244 | } |
| 245 | loop: |
| 246 | if (start > end) { |
| 247 | btrfs_free_path(path); |
| 248 | return 0; |
| 249 | } |
| 250 | btrfs_release_path(root, path); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 251 | loops++; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 252 | goto again; |
| 253 | |
| 254 | not_found: |
| 255 | cow_file_range(inode, start, cow_end); |
| 256 | start = cow_end + 1; |
| 257 | goto loop; |
| 258 | } |
| 259 | |
| 260 | static int run_delalloc_range(struct inode *inode, u64 start, u64 end) |
| 261 | { |
| 262 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 263 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 264 | mutex_lock(&root->fs_info->fs_mutex); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 265 | if (btrfs_test_opt(root, NODATACOW) || |
| 266 | btrfs_test_flag(inode, NODATACOW)) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 267 | ret = run_delalloc_nocow(inode, start, end); |
| 268 | else |
| 269 | ret = cow_file_range(inode, start, end); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 270 | |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 271 | mutex_unlock(&root->fs_info->fs_mutex); |
| 272 | return ret; |
| 273 | } |
| 274 | |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 275 | int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 276 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 277 | { |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 278 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 279 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 280 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 281 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 282 | root->fs_info->delalloc_bytes += end - start + 1; |
| 283 | spin_unlock(&root->fs_info->delalloc_lock); |
| 284 | } |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 289 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 290 | { |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 291 | if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 292 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 293 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 294 | if (end - start + 1 > root->fs_info->delalloc_bytes) { |
| 295 | printk("warning: delalloc account %Lu %Lu\n", |
| 296 | end - start + 1, root->fs_info->delalloc_bytes); |
| 297 | root->fs_info->delalloc_bytes = 0; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 298 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 299 | } else { |
| 300 | root->fs_info->delalloc_bytes -= end - start + 1; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 301 | BTRFS_I(inode)->delalloc_bytes -= end - start + 1; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 302 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 303 | spin_unlock(&root->fs_info->delalloc_lock); |
| 304 | } |
| 305 | return 0; |
| 306 | } |
| 307 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 308 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
| 309 | size_t size, struct bio *bio) |
| 310 | { |
| 311 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 312 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 313 | u64 logical = bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 314 | u64 length = 0; |
| 315 | u64 map_length; |
| 316 | struct bio_vec *bvec; |
| 317 | int i; |
| 318 | int ret; |
| 319 | |
| 320 | bio_for_each_segment(bvec, bio, i) { |
| 321 | length += bvec->bv_len; |
| 322 | } |
| 323 | map_tree = &root->fs_info->mapping_tree; |
| 324 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 325 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 326 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 327 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 328 | if (map_length < length + size) { |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 329 | return 1; |
| 330 | } |
| 331 | return 0; |
| 332 | } |
| 333 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 334 | int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 335 | int mirror_num) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 336 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 337 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 338 | struct btrfs_trans_handle *trans; |
| 339 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 340 | char *sums = NULL; |
| 341 | |
| 342 | ret = btrfs_csum_one_bio(root, bio, &sums); |
| 343 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 344 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 345 | mutex_lock(&root->fs_info->fs_mutex); |
| 346 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 347 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 348 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 349 | btrfs_csum_file_blocks(trans, root, inode, bio, sums); |
| 350 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 351 | ret = btrfs_end_transaction(trans, root); |
| 352 | BUG_ON(ret); |
| 353 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 354 | |
| 355 | kfree(sums); |
| 356 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 357 | return btrfs_map_bio(root, rw, bio, mirror_num); |
| 358 | } |
| 359 | |
| 360 | int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
| 361 | int mirror_num) |
| 362 | { |
| 363 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 364 | int ret = 0; |
| 365 | |
Chris Mason | 22c5994 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 366 | if (!(rw & (1 << BIO_RW))) { |
| 367 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 368 | BUG_ON(ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 369 | goto mapit; |
| 370 | } |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 371 | |
| 372 | if (btrfs_test_opt(root, NODATASUM) || |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 373 | btrfs_test_flag(inode, NODATASUM)) { |
| 374 | goto mapit; |
| 375 | } |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 376 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 377 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
| 378 | inode, rw, bio, mirror_num, |
| 379 | __btrfs_submit_bio_hook); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 380 | mapit: |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 381 | return btrfs_map_bio(root, rw, bio, mirror_num); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 382 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 383 | |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 384 | int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end) |
| 385 | { |
| 386 | int ret = 0; |
| 387 | struct inode *inode = page->mapping->host; |
| 388 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 389 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 390 | struct btrfs_csum_item *item; |
| 391 | struct btrfs_path *path = NULL; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 392 | u32 csum; |
Chris Mason | 699122f | 2008-04-16 12:59:22 -0400 | [diff] [blame] | 393 | |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 394 | if (btrfs_test_opt(root, NODATASUM) || |
| 395 | btrfs_test_flag(inode, NODATASUM)) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 396 | return 0; |
Chris Mason | 699122f | 2008-04-16 12:59:22 -0400 | [diff] [blame] | 397 | |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 398 | mutex_lock(&root->fs_info->fs_mutex); |
| 399 | path = btrfs_alloc_path(); |
| 400 | item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0); |
| 401 | if (IS_ERR(item)) { |
| 402 | ret = PTR_ERR(item); |
| 403 | /* a csum that isn't present is a preallocated region. */ |
| 404 | if (ret == -ENOENT || ret == -EFBIG) |
| 405 | ret = 0; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 406 | csum = 0; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 407 | printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 408 | goto out; |
| 409 | } |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 410 | read_extent_buffer(path->nodes[0], &csum, (unsigned long)item, |
| 411 | BTRFS_CRC32_SIZE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 412 | set_state_private(io_tree, start, csum); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 413 | out: |
| 414 | if (path) |
| 415 | btrfs_free_path(path); |
| 416 | mutex_unlock(&root->fs_info->fs_mutex); |
| 417 | return ret; |
| 418 | } |
| 419 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 420 | struct io_failure_record { |
| 421 | struct page *page; |
| 422 | u64 start; |
| 423 | u64 len; |
| 424 | u64 logical; |
| 425 | int last_mirror; |
| 426 | }; |
| 427 | |
| 428 | int btrfs_readpage_io_failed_hook(struct bio *failed_bio, |
| 429 | struct page *page, u64 start, u64 end, |
| 430 | struct extent_state *state) |
| 431 | { |
| 432 | struct io_failure_record *failrec = NULL; |
| 433 | u64 private; |
| 434 | struct extent_map *em; |
| 435 | struct inode *inode = page->mapping->host; |
| 436 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 437 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 438 | struct bio *bio; |
| 439 | int num_copies; |
| 440 | int ret; |
| 441 | u64 logical; |
| 442 | |
| 443 | ret = get_state_private(failure_tree, start, &private); |
| 444 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 445 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 446 | if (!failrec) |
| 447 | return -ENOMEM; |
| 448 | failrec->start = start; |
| 449 | failrec->len = end - start + 1; |
| 450 | failrec->last_mirror = 0; |
| 451 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 452 | spin_lock(&em_tree->lock); |
| 453 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 454 | if (em->start > start || em->start + em->len < start) { |
| 455 | free_extent_map(em); |
| 456 | em = NULL; |
| 457 | } |
| 458 | spin_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 459 | |
| 460 | if (!em || IS_ERR(em)) { |
| 461 | kfree(failrec); |
| 462 | return -EIO; |
| 463 | } |
| 464 | logical = start - em->start; |
| 465 | logical = em->block_start + logical; |
| 466 | failrec->logical = logical; |
| 467 | free_extent_map(em); |
| 468 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 469 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 470 | set_state_private(failure_tree, start, |
| 471 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 472 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 473 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 474 | } |
| 475 | num_copies = btrfs_num_copies( |
| 476 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 477 | failrec->logical, failrec->len); |
| 478 | failrec->last_mirror++; |
| 479 | if (!state) { |
| 480 | spin_lock_irq(&BTRFS_I(inode)->io_tree.lock); |
| 481 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 482 | failrec->start, |
| 483 | EXTENT_LOCKED); |
| 484 | if (state && state->start != failrec->start) |
| 485 | state = NULL; |
| 486 | spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock); |
| 487 | } |
| 488 | if (!state || failrec->last_mirror > num_copies) { |
| 489 | set_state_private(failure_tree, failrec->start, 0); |
| 490 | clear_extent_bits(failure_tree, failrec->start, |
| 491 | failrec->start + failrec->len - 1, |
| 492 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 493 | kfree(failrec); |
| 494 | return -EIO; |
| 495 | } |
| 496 | bio = bio_alloc(GFP_NOFS, 1); |
| 497 | bio->bi_private = state; |
| 498 | bio->bi_end_io = failed_bio->bi_end_io; |
| 499 | bio->bi_sector = failrec->logical >> 9; |
| 500 | bio->bi_bdev = failed_bio->bi_bdev; |
| 501 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
| 502 | btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror); |
| 503 | return 0; |
| 504 | } |
| 505 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 506 | int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end, |
| 507 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 508 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 509 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 510 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 511 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 512 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 513 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 514 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 515 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 516 | u32 csum = ~(u32)0; |
Jens Axboe | bbf0d00 | 2007-10-19 09:23:07 -0400 | [diff] [blame] | 517 | unsigned long flags; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 518 | |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 519 | if (btrfs_test_opt(root, NODATASUM) || |
| 520 | btrfs_test_flag(inode, NODATASUM)) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 521 | return 0; |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 522 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 523 | private = state->private; |
| 524 | ret = 0; |
| 525 | } else { |
| 526 | ret = get_state_private(io_tree, start, &private); |
| 527 | } |
Jens Axboe | bbf0d00 | 2007-10-19 09:23:07 -0400 | [diff] [blame] | 528 | local_irq_save(flags); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 529 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 530 | if (ret) { |
| 531 | goto zeroit; |
| 532 | } |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 533 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 534 | btrfs_csum_final(csum, (char *)&csum); |
| 535 | if (csum != private) { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 536 | goto zeroit; |
| 537 | } |
| 538 | kunmap_atomic(kaddr, KM_IRQ0); |
Jens Axboe | bbf0d00 | 2007-10-19 09:23:07 -0400 | [diff] [blame] | 539 | local_irq_restore(flags); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 540 | |
| 541 | /* if the io failure tree for this inode is non-empty, |
| 542 | * check to see if we've recovered from a failed IO |
| 543 | */ |
| 544 | private = 0; |
| 545 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 546 | (u64)-1, 1, EXTENT_DIRTY)) { |
| 547 | u64 private_failure; |
| 548 | struct io_failure_record *failure; |
| 549 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 550 | start, &private_failure); |
| 551 | if (ret == 0) { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 552 | failure = (struct io_failure_record *)(unsigned long) |
| 553 | private_failure; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 554 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 555 | failure->start, 0); |
| 556 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 557 | failure->start, |
| 558 | failure->start + failure->len - 1, |
| 559 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 560 | GFP_NOFS); |
| 561 | kfree(failure); |
| 562 | } |
| 563 | } |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 564 | return 0; |
| 565 | |
| 566 | zeroit: |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 567 | printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n", |
| 568 | page->mapping->host->i_ino, (unsigned long long)start, csum, |
| 569 | private); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 570 | memset(kaddr + offset, 1, end - start + 1); |
| 571 | flush_dcache_page(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 572 | kunmap_atomic(kaddr, KM_IRQ0); |
Jens Axboe | bbf0d00 | 2007-10-19 09:23:07 -0400 | [diff] [blame] | 573 | local_irq_restore(flags); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 574 | if (private == 0) |
| 575 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 576 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 577 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 578 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 579 | void btrfs_read_locked_inode(struct inode *inode) |
| 580 | { |
| 581 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 582 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 583 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 584 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 585 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 586 | struct btrfs_key location; |
| 587 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 588 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 589 | int ret; |
| 590 | |
| 591 | path = btrfs_alloc_path(); |
| 592 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 593 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 594 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 595 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 596 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 597 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 598 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 599 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 600 | leaf = path->nodes[0]; |
| 601 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 602 | struct btrfs_inode_item); |
| 603 | |
| 604 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 605 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 606 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 607 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
| 608 | inode->i_size = btrfs_inode_size(leaf, inode_item); |
| 609 | |
| 610 | tspec = btrfs_inode_atime(inode_item); |
| 611 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 612 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 613 | |
| 614 | tspec = btrfs_inode_mtime(inode_item); |
| 615 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 616 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 617 | |
| 618 | tspec = btrfs_inode_ctime(inode_item); |
| 619 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 620 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 621 | |
| 622 | inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item); |
| 623 | inode->i_generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 624 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 625 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 626 | |
| 627 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 628 | BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info, |
| 629 | alloc_group_block); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 630 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Chris Mason | e52ec0e | 2007-12-21 16:36:24 -0500 | [diff] [blame] | 631 | if (!BTRFS_I(inode)->block_group) { |
| 632 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 633 | NULL, 0, |
| 634 | BTRFS_BLOCK_GROUP_METADATA, 0); |
Chris Mason | e52ec0e | 2007-12-21 16:36:24 -0500 | [diff] [blame] | 635 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 636 | btrfs_free_path(path); |
| 637 | inode_item = NULL; |
| 638 | |
| 639 | mutex_unlock(&root->fs_info->fs_mutex); |
| 640 | |
| 641 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 642 | case S_IFREG: |
| 643 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 644 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 645 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 646 | inode->i_fop = &btrfs_file_operations; |
| 647 | inode->i_op = &btrfs_file_inode_operations; |
| 648 | break; |
| 649 | case S_IFDIR: |
| 650 | inode->i_fop = &btrfs_dir_file_operations; |
| 651 | if (root == root->fs_info->tree_root) |
| 652 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 653 | else |
| 654 | inode->i_op = &btrfs_dir_inode_operations; |
| 655 | break; |
| 656 | case S_IFLNK: |
| 657 | inode->i_op = &btrfs_symlink_inode_operations; |
| 658 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 659 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 660 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 661 | default: |
| 662 | init_special_inode(inode, inode->i_mode, rdev); |
| 663 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 664 | } |
| 665 | return; |
| 666 | |
| 667 | make_bad: |
| 668 | btrfs_release_path(root, path); |
| 669 | btrfs_free_path(path); |
| 670 | mutex_unlock(&root->fs_info->fs_mutex); |
| 671 | make_bad_inode(inode); |
| 672 | } |
| 673 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 674 | static void fill_inode_item(struct extent_buffer *leaf, |
| 675 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 676 | struct inode *inode) |
| 677 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 678 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 679 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
| 680 | btrfs_set_inode_size(leaf, item, inode->i_size); |
| 681 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 682 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 683 | |
| 684 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 685 | inode->i_atime.tv_sec); |
| 686 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 687 | inode->i_atime.tv_nsec); |
| 688 | |
| 689 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 690 | inode->i_mtime.tv_sec); |
| 691 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 692 | inode->i_mtime.tv_nsec); |
| 693 | |
| 694 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 695 | inode->i_ctime.tv_sec); |
| 696 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 697 | inode->i_ctime.tv_nsec); |
| 698 | |
| 699 | btrfs_set_inode_nblocks(leaf, item, inode->i_blocks); |
| 700 | btrfs_set_inode_generation(leaf, item, inode->i_generation); |
| 701 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 702 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 703 | btrfs_set_inode_block_group(leaf, item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 704 | BTRFS_I(inode)->block_group->key.objectid); |
| 705 | } |
| 706 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 707 | int btrfs_update_inode(struct btrfs_trans_handle *trans, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 708 | struct btrfs_root *root, |
| 709 | struct inode *inode) |
| 710 | { |
| 711 | struct btrfs_inode_item *inode_item; |
| 712 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 713 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 714 | int ret; |
| 715 | |
| 716 | path = btrfs_alloc_path(); |
| 717 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 718 | ret = btrfs_lookup_inode(trans, root, path, |
| 719 | &BTRFS_I(inode)->location, 1); |
| 720 | if (ret) { |
| 721 | if (ret > 0) |
| 722 | ret = -ENOENT; |
| 723 | goto failed; |
| 724 | } |
| 725 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 726 | leaf = path->nodes[0]; |
| 727 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 728 | struct btrfs_inode_item); |
| 729 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 730 | fill_inode_item(leaf, inode_item, inode); |
| 731 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 732 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 733 | ret = 0; |
| 734 | failed: |
| 735 | btrfs_release_path(root, path); |
| 736 | btrfs_free_path(path); |
| 737 | return ret; |
| 738 | } |
| 739 | |
| 740 | |
| 741 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, |
| 742 | struct btrfs_root *root, |
| 743 | struct inode *dir, |
| 744 | struct dentry *dentry) |
| 745 | { |
| 746 | struct btrfs_path *path; |
| 747 | const char *name = dentry->d_name.name; |
| 748 | int name_len = dentry->d_name.len; |
| 749 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 750 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 751 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 752 | struct btrfs_key key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 753 | |
| 754 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 755 | if (!path) { |
| 756 | ret = -ENOMEM; |
| 757 | goto err; |
| 758 | } |
| 759 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 760 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 761 | name, name_len, -1); |
| 762 | if (IS_ERR(di)) { |
| 763 | ret = PTR_ERR(di); |
| 764 | goto err; |
| 765 | } |
| 766 | if (!di) { |
| 767 | ret = -ENOENT; |
| 768 | goto err; |
| 769 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 770 | leaf = path->nodes[0]; |
| 771 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 772 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 773 | if (ret) |
| 774 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 775 | btrfs_release_path(root, path); |
| 776 | |
| 777 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 778 | key.objectid, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 779 | if (IS_ERR(di)) { |
| 780 | ret = PTR_ERR(di); |
| 781 | goto err; |
| 782 | } |
| 783 | if (!di) { |
| 784 | ret = -ENOENT; |
| 785 | goto err; |
| 786 | } |
| 787 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 788 | |
| 789 | dentry->d_inode->i_ctime = dir->i_ctime; |
Chris Mason | 76fea00 | 2007-12-13 09:06:01 -0500 | [diff] [blame] | 790 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
| 791 | dentry->d_inode->i_ino, |
| 792 | dentry->d_parent->d_inode->i_ino); |
| 793 | if (ret) { |
| 794 | printk("failed to delete reference to %.*s, " |
| 795 | "inode %lu parent %lu\n", name_len, name, |
| 796 | dentry->d_inode->i_ino, |
| 797 | dentry->d_parent->d_inode->i_ino); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 798 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 799 | err: |
| 800 | btrfs_free_path(path); |
| 801 | if (!ret) { |
| 802 | dir->i_size -= name_len * 2; |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 803 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 804 | btrfs_update_inode(trans, root, dir); |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 805 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 806 | dentry->d_inode->i_nlink--; |
| 807 | #else |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 808 | drop_nlink(dentry->d_inode); |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 809 | #endif |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 810 | ret = btrfs_update_inode(trans, root, dentry->d_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 811 | dir->i_sb->s_dirt = 1; |
| 812 | } |
| 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 817 | { |
| 818 | struct btrfs_root *root; |
| 819 | struct btrfs_trans_handle *trans; |
Chris Mason | 2da98f0 | 2008-01-16 11:44:43 -0500 | [diff] [blame] | 820 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 821 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 822 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 823 | |
| 824 | root = BTRFS_I(dir)->root; |
| 825 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 826 | |
| 827 | ret = btrfs_check_free_space(root, 1, 1); |
| 828 | if (ret) |
| 829 | goto fail; |
| 830 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 831 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 832 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 833 | btrfs_set_trans_block_group(trans, dir); |
| 834 | ret = btrfs_unlink_trans(trans, root, dir, dentry); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 835 | nr = trans->blocks_used; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 836 | |
Chris Mason | 2da98f0 | 2008-01-16 11:44:43 -0500 | [diff] [blame] | 837 | if (inode->i_nlink == 0) { |
| 838 | int found; |
| 839 | /* if the inode isn't linked anywhere, |
| 840 | * we don't need to worry about |
| 841 | * data=ordered |
| 842 | */ |
| 843 | found = btrfs_del_ordered_inode(inode); |
| 844 | if (found == 1) { |
| 845 | atomic_dec(&inode->i_count); |
| 846 | } |
| 847 | } |
| 848 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 849 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 850 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 851 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 852 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 853 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 854 | return ret; |
| 855 | } |
| 856 | |
| 857 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 858 | { |
| 859 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 860 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 861 | int ret; |
| 862 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 863 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 864 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 865 | |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 866 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
| 867 | return -ENOTEMPTY; |
| 868 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 869 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 870 | ret = btrfs_check_free_space(root, 1, 1); |
| 871 | if (ret) |
| 872 | goto fail; |
| 873 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 874 | trans = btrfs_start_transaction(root, 1); |
| 875 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 876 | |
| 877 | /* now the directory is empty */ |
| 878 | err = btrfs_unlink_trans(trans, root, dir, dentry); |
| 879 | if (!err) { |
| 880 | inode->i_size = 0; |
| 881 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 882 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 883 | nr = trans->blocks_used; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 884 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 885 | fail: |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 886 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 887 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 888 | btrfs_throttle(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 889 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 890 | if (ret && !err) |
| 891 | err = ret; |
| 892 | return err; |
| 893 | } |
| 894 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 895 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 896 | * this can truncate away extent items, csum items and directory items. |
| 897 | * It starts at a high offset and removes keys until it can't find |
| 898 | * any higher than i_size. |
| 899 | * |
| 900 | * csum items that cross the new i_size are truncated to the new size |
| 901 | * as well. |
| 902 | */ |
| 903 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
| 904 | struct btrfs_root *root, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 905 | struct inode *inode, |
| 906 | u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 907 | { |
| 908 | int ret; |
| 909 | struct btrfs_path *path; |
| 910 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 911 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 912 | u32 found_type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 913 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 914 | struct btrfs_file_extent_item *fi; |
| 915 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 916 | u64 extent_num_bytes = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 917 | u64 item_end = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 918 | u64 root_gen = 0; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 919 | u64 root_owner = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 920 | int found_extent; |
| 921 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 922 | int pending_del_nr = 0; |
| 923 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 924 | int extent_type = -1; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 925 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 926 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 927 | btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 928 | path = btrfs_alloc_path(); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 929 | path->reada = -1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 930 | BUG_ON(!path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 931 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 932 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 933 | key.objectid = inode->i_ino; |
| 934 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 935 | key.type = (u8)-1; |
| 936 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 937 | btrfs_init_path(path); |
| 938 | search_again: |
| 939 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 940 | if (ret < 0) { |
| 941 | goto error; |
| 942 | } |
| 943 | if (ret > 0) { |
| 944 | BUG_ON(path->slots[0] == 0); |
| 945 | path->slots[0]--; |
| 946 | } |
| 947 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 948 | while(1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 949 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 950 | leaf = path->nodes[0]; |
| 951 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 952 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 953 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 954 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 955 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 956 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 957 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 958 | break; |
| 959 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 960 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 961 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 962 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 963 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 964 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 965 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 966 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 967 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 968 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 969 | struct btrfs_item *item = btrfs_item_nr(leaf, |
| 970 | path->slots[0]); |
| 971 | item_end += btrfs_file_extent_inline_len(leaf, |
| 972 | item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 973 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 974 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 975 | } |
| 976 | if (found_type == BTRFS_CSUM_ITEM_KEY) { |
| 977 | ret = btrfs_csum_truncate(trans, root, path, |
| 978 | inode->i_size); |
| 979 | BUG_ON(ret); |
| 980 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 981 | if (item_end < inode->i_size) { |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 982 | if (found_type == BTRFS_DIR_ITEM_KEY) { |
| 983 | found_type = BTRFS_INODE_ITEM_KEY; |
| 984 | } else if (found_type == BTRFS_EXTENT_ITEM_KEY) { |
| 985 | found_type = BTRFS_CSUM_ITEM_KEY; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 986 | } else if (found_type == BTRFS_EXTENT_DATA_KEY) { |
| 987 | found_type = BTRFS_XATTR_ITEM_KEY; |
| 988 | } else if (found_type == BTRFS_XATTR_ITEM_KEY) { |
| 989 | found_type = BTRFS_INODE_REF_KEY; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 990 | } else if (found_type) { |
| 991 | found_type--; |
| 992 | } else { |
| 993 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 994 | } |
Yan | a61721d | 2007-09-17 11:08:38 -0400 | [diff] [blame] | 995 | btrfs_set_key_type(&key, found_type); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 996 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 997 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 998 | if (found_key.offset >= inode->i_size) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 999 | del_item = 1; |
| 1000 | else |
| 1001 | del_item = 0; |
| 1002 | found_extent = 0; |
| 1003 | |
| 1004 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1005 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 1006 | goto delete; |
| 1007 | |
| 1008 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1009 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1010 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1011 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1012 | u64 orig_num_bytes = |
| 1013 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1014 | extent_num_bytes = inode->i_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1015 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 1016 | extent_num_bytes = extent_num_bytes & |
| 1017 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1018 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 1019 | extent_num_bytes); |
| 1020 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1021 | extent_num_bytes); |
| 1022 | if (extent_start != 0) |
| 1023 | dec_i_blocks(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1024 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1025 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1026 | extent_num_bytes = |
| 1027 | btrfs_file_extent_disk_num_bytes(leaf, |
| 1028 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1029 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1030 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1031 | if (extent_start != 0) { |
| 1032 | found_extent = 1; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1033 | dec_i_blocks(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1034 | } |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1035 | root_gen = btrfs_header_generation(leaf); |
| 1036 | root_owner = btrfs_header_owner(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1037 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1038 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1039 | if (!del_item) { |
| 1040 | u32 newsize = inode->i_size - found_key.offset; |
| 1041 | dec_i_blocks(inode, item_end + 1 - |
| 1042 | found_key.offset - newsize); |
| 1043 | newsize = |
| 1044 | btrfs_file_extent_calc_inline_size(newsize); |
| 1045 | ret = btrfs_truncate_item(trans, root, path, |
| 1046 | newsize, 1); |
| 1047 | BUG_ON(ret); |
| 1048 | } else { |
| 1049 | dec_i_blocks(inode, item_end + 1 - |
| 1050 | found_key.offset); |
| 1051 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1052 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1053 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1054 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1055 | if (!pending_del_nr) { |
| 1056 | /* no pending yet, add ourselves */ |
| 1057 | pending_del_slot = path->slots[0]; |
| 1058 | pending_del_nr = 1; |
| 1059 | } else if (pending_del_nr && |
| 1060 | path->slots[0] + 1 == pending_del_slot) { |
| 1061 | /* hop on the pending chunk */ |
| 1062 | pending_del_nr++; |
| 1063 | pending_del_slot = path->slots[0]; |
| 1064 | } else { |
| 1065 | printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot); |
| 1066 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1067 | } else { |
| 1068 | break; |
| 1069 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1070 | if (found_extent) { |
| 1071 | ret = btrfs_free_extent(trans, root, extent_start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1072 | extent_num_bytes, |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1073 | root_owner, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1074 | root_gen, inode->i_ino, |
| 1075 | found_key.offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1076 | BUG_ON(ret); |
| 1077 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1078 | next: |
| 1079 | if (path->slots[0] == 0) { |
| 1080 | if (pending_del_nr) |
| 1081 | goto del_pending; |
| 1082 | btrfs_release_path(root, path); |
| 1083 | goto search_again; |
| 1084 | } |
| 1085 | |
| 1086 | path->slots[0]--; |
| 1087 | if (pending_del_nr && |
| 1088 | path->slots[0] + 1 != pending_del_slot) { |
| 1089 | struct btrfs_key debug; |
| 1090 | del_pending: |
| 1091 | btrfs_item_key_to_cpu(path->nodes[0], &debug, |
| 1092 | pending_del_slot); |
| 1093 | ret = btrfs_del_items(trans, root, path, |
| 1094 | pending_del_slot, |
| 1095 | pending_del_nr); |
| 1096 | BUG_ON(ret); |
| 1097 | pending_del_nr = 0; |
| 1098 | btrfs_release_path(root, path); |
| 1099 | goto search_again; |
| 1100 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1101 | } |
| 1102 | ret = 0; |
| 1103 | error: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1104 | if (pending_del_nr) { |
| 1105 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 1106 | pending_del_nr); |
| 1107 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1108 | btrfs_release_path(root, path); |
| 1109 | btrfs_free_path(path); |
| 1110 | inode->i_sb->s_dirt = 1; |
| 1111 | return ret; |
| 1112 | } |
| 1113 | |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1114 | static int btrfs_cow_one_page(struct inode *inode, struct page *page, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1115 | size_t zero_start) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1116 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1117 | char *kaddr; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1118 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1119 | u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1120 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1121 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1122 | |
Chris Mason | 190662b | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 1123 | WARN_ON(!PageLocked(page)); |
Christoph Hellwig | b3cfa35 | 2007-09-17 11:25:58 -0400 | [diff] [blame] | 1124 | set_page_extent_mapped(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1125 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1126 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1127 | set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1128 | page_end, GFP_NOFS); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1129 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1130 | if (zero_start != PAGE_CACHE_SIZE) { |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1131 | kaddr = kmap(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1132 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 1133 | flush_dcache_page(page); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1134 | kunmap(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1135 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1136 | set_page_dirty(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1137 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1138 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1139 | return ret; |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * taken from block_truncate_page, but does cow as it zeros out |
| 1144 | * any bytes left in the last page in the file. |
| 1145 | */ |
| 1146 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 1147 | { |
| 1148 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1149 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1150 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1151 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 1152 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 1153 | struct page *page; |
| 1154 | int ret = 0; |
| 1155 | u64 page_start; |
| 1156 | |
| 1157 | if ((offset & (blocksize - 1)) == 0) |
| 1158 | goto out; |
| 1159 | |
| 1160 | ret = -ENOMEM; |
| 1161 | page = grab_cache_page(mapping, index); |
| 1162 | if (!page) |
| 1163 | goto out; |
| 1164 | if (!PageUptodate(page)) { |
| 1165 | ret = btrfs_readpage(NULL, page); |
| 1166 | lock_page(page); |
| 1167 | if (!PageUptodate(page)) { |
| 1168 | ret = -EIO; |
| 1169 | goto out; |
| 1170 | } |
| 1171 | } |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1172 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1173 | |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1174 | ret = btrfs_cow_one_page(inode, page, offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1175 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1176 | unlock_page(page); |
| 1177 | page_cache_release(page); |
| 1178 | out: |
| 1179 | return ret; |
| 1180 | } |
| 1181 | |
| 1182 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 1183 | { |
| 1184 | struct inode *inode = dentry->d_inode; |
| 1185 | int err; |
| 1186 | |
| 1187 | err = inode_change_ok(inode, attr); |
| 1188 | if (err) |
| 1189 | return err; |
| 1190 | |
| 1191 | if (S_ISREG(inode->i_mode) && |
| 1192 | attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) { |
| 1193 | struct btrfs_trans_handle *trans; |
| 1194 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1195 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 1196 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1197 | u64 mask = root->sectorsize - 1; |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1198 | u64 hole_start = (inode->i_size + mask) & ~mask; |
Chris Mason | f392a93 | 2008-01-30 11:54:05 -0500 | [diff] [blame] | 1199 | u64 block_end = (attr->ia_size + mask) & ~mask; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1200 | u64 hole_size; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1201 | u64 alloc_hint = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1202 | |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1203 | if (attr->ia_size <= hole_start) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1204 | goto out; |
| 1205 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1206 | mutex_lock(&root->fs_info->fs_mutex); |
| 1207 | err = btrfs_check_free_space(root, 1, 0); |
| 1208 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1209 | if (err) |
| 1210 | goto fail; |
| 1211 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1212 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 1213 | |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1214 | lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 1215 | hole_size = block_end - hole_start; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1216 | |
| 1217 | mutex_lock(&root->fs_info->fs_mutex); |
| 1218 | trans = btrfs_start_transaction(root, 1); |
| 1219 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 1220 | err = btrfs_drop_extents(trans, root, inode, |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1221 | hole_start, block_end, hole_start, |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 1222 | &alloc_hint); |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 1223 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1224 | if (alloc_hint != EXTENT_MAP_INLINE) { |
| 1225 | err = btrfs_insert_file_extent(trans, root, |
| 1226 | inode->i_ino, |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 1227 | hole_start, 0, 0, |
| 1228 | hole_size); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1229 | btrfs_drop_extent_cache(inode, hole_start, |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1230 | (u64)-1); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 1231 | btrfs_check_file(root, inode); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1232 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1233 | btrfs_end_transaction(trans, root); |
| 1234 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1235 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1236 | if (err) |
| 1237 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1238 | } |
| 1239 | out: |
| 1240 | err = inode_setattr(inode, attr); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1241 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1242 | return err; |
| 1243 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 1244 | |
Chris Mason | 2da98f0 | 2008-01-16 11:44:43 -0500 | [diff] [blame] | 1245 | void btrfs_put_inode(struct inode *inode) |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 1246 | { |
Chris Mason | 2da98f0 | 2008-01-16 11:44:43 -0500 | [diff] [blame] | 1247 | int ret; |
| 1248 | |
| 1249 | if (!BTRFS_I(inode)->ordered_trans) { |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 1250 | return; |
| 1251 | } |
Chris Mason | 2da98f0 | 2008-01-16 11:44:43 -0500 | [diff] [blame] | 1252 | |
| 1253 | if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) || |
| 1254 | mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)) |
| 1255 | return; |
| 1256 | |
| 1257 | ret = btrfs_del_ordered_inode(inode); |
| 1258 | if (ret == 1) { |
| 1259 | atomic_dec(&inode->i_count); |
| 1260 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 1261 | } |
| 1262 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1263 | void btrfs_delete_inode(struct inode *inode) |
| 1264 | { |
| 1265 | struct btrfs_trans_handle *trans; |
| 1266 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1267 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1268 | int ret; |
| 1269 | |
| 1270 | truncate_inode_pages(&inode->i_data, 0); |
| 1271 | if (is_bad_inode(inode)) { |
| 1272 | goto no_delete; |
| 1273 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1274 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1275 | inode->i_size = 0; |
| 1276 | mutex_lock(&root->fs_info->fs_mutex); |
| 1277 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1278 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1279 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1280 | ret = btrfs_truncate_in_trans(trans, root, inode, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1281 | if (ret) |
| 1282 | goto no_delete_lock; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1283 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1284 | nr = trans->blocks_used; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1285 | clear_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1286 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1287 | btrfs_end_transaction(trans, root); |
| 1288 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1289 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1290 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1291 | return; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1292 | |
| 1293 | no_delete_lock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1294 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1295 | btrfs_end_transaction(trans, root); |
| 1296 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1297 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1298 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1299 | no_delete: |
| 1300 | clear_inode(inode); |
| 1301 | } |
| 1302 | |
| 1303 | /* |
| 1304 | * this returns the key found in the dir entry in the location pointer. |
| 1305 | * If no dir entries were found, location->objectid is 0. |
| 1306 | */ |
| 1307 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 1308 | struct btrfs_key *location) |
| 1309 | { |
| 1310 | const char *name = dentry->d_name.name; |
| 1311 | int namelen = dentry->d_name.len; |
| 1312 | struct btrfs_dir_item *di; |
| 1313 | struct btrfs_path *path; |
| 1314 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 1315 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1316 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1317 | if (namelen == 1 && strcmp(name, ".") == 0) { |
| 1318 | location->objectid = dir->i_ino; |
| 1319 | location->type = BTRFS_INODE_ITEM_KEY; |
| 1320 | location->offset = 0; |
| 1321 | return 0; |
| 1322 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1323 | path = btrfs_alloc_path(); |
| 1324 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1325 | |
Chris Mason | 7a72053 | 2007-12-13 09:06:59 -0500 | [diff] [blame] | 1326 | if (namelen == 2 && strcmp(name, "..") == 0) { |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1327 | struct btrfs_key key; |
| 1328 | struct extent_buffer *leaf; |
| 1329 | u32 nritems; |
| 1330 | int slot; |
| 1331 | |
| 1332 | key.objectid = dir->i_ino; |
| 1333 | btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); |
| 1334 | key.offset = 0; |
| 1335 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1336 | BUG_ON(ret == 0); |
| 1337 | ret = 0; |
| 1338 | |
| 1339 | leaf = path->nodes[0]; |
| 1340 | slot = path->slots[0]; |
| 1341 | nritems = btrfs_header_nritems(leaf); |
| 1342 | if (slot >= nritems) |
| 1343 | goto out_err; |
| 1344 | |
| 1345 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 1346 | if (key.objectid != dir->i_ino || |
| 1347 | key.type != BTRFS_INODE_REF_KEY) { |
| 1348 | goto out_err; |
| 1349 | } |
| 1350 | location->objectid = key.offset; |
| 1351 | location->type = BTRFS_INODE_ITEM_KEY; |
| 1352 | location->offset = 0; |
| 1353 | goto out; |
| 1354 | } |
| 1355 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1356 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 1357 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 1358 | if (IS_ERR(di)) |
| 1359 | ret = PTR_ERR(di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1360 | if (!di || IS_ERR(di)) { |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1361 | goto out_err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1362 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1363 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1364 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1365 | btrfs_free_path(path); |
| 1366 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1367 | out_err: |
| 1368 | location->objectid = 0; |
| 1369 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 1374 | * needs to be changed to reflect the root directory of the tree root. This |
| 1375 | * is kind of like crossing a mount point. |
| 1376 | */ |
| 1377 | static int fixup_tree_root_location(struct btrfs_root *root, |
| 1378 | struct btrfs_key *location, |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1379 | struct btrfs_root **sub_root, |
| 1380 | struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1381 | { |
| 1382 | struct btrfs_path *path; |
| 1383 | struct btrfs_root_item *ri; |
| 1384 | |
| 1385 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) |
| 1386 | return 0; |
| 1387 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 1388 | return 0; |
| 1389 | |
| 1390 | path = btrfs_alloc_path(); |
| 1391 | BUG_ON(!path); |
| 1392 | mutex_lock(&root->fs_info->fs_mutex); |
| 1393 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1394 | *sub_root = btrfs_read_fs_root(root->fs_info, location, |
| 1395 | dentry->d_name.name, |
| 1396 | dentry->d_name.len); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1397 | if (IS_ERR(*sub_root)) |
| 1398 | return PTR_ERR(*sub_root); |
| 1399 | |
| 1400 | ri = &(*sub_root)->root_item; |
| 1401 | location->objectid = btrfs_root_dirid(ri); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1402 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 1403 | location->offset = 0; |
| 1404 | |
| 1405 | btrfs_free_path(path); |
| 1406 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 1411 | { |
| 1412 | struct btrfs_iget_args *args = p; |
| 1413 | inode->i_ino = args->ino; |
| 1414 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1415 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1416 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 1417 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1418 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1419 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
| 1420 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 1425 | { |
| 1426 | struct btrfs_iget_args *args = opaque; |
| 1427 | return (args->ino == inode->i_ino && |
| 1428 | args->root == BTRFS_I(inode)->root); |
| 1429 | } |
| 1430 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1431 | struct inode *btrfs_ilookup(struct super_block *s, u64 objectid, |
| 1432 | u64 root_objectid) |
| 1433 | { |
| 1434 | struct btrfs_iget_args args; |
| 1435 | args.ino = objectid; |
| 1436 | args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid); |
| 1437 | |
| 1438 | if (!args.root) |
| 1439 | return NULL; |
| 1440 | |
| 1441 | return ilookup5(s, objectid, btrfs_find_actor, (void *)&args); |
| 1442 | } |
| 1443 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1444 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
| 1445 | struct btrfs_root *root) |
| 1446 | { |
| 1447 | struct inode *inode; |
| 1448 | struct btrfs_iget_args args; |
| 1449 | args.ino = objectid; |
| 1450 | args.root = root; |
| 1451 | |
| 1452 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 1453 | btrfs_init_locked_inode, |
| 1454 | (void *)&args); |
| 1455 | return inode; |
| 1456 | } |
| 1457 | |
| 1458 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 1459 | struct nameidata *nd) |
| 1460 | { |
| 1461 | struct inode * inode; |
| 1462 | struct btrfs_inode *bi = BTRFS_I(dir); |
| 1463 | struct btrfs_root *root = bi->root; |
| 1464 | struct btrfs_root *sub_root = root; |
| 1465 | struct btrfs_key location; |
| 1466 | int ret; |
| 1467 | |
| 1468 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 1469 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1470 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1471 | mutex_lock(&root->fs_info->fs_mutex); |
| 1472 | ret = btrfs_inode_by_name(dir, dentry, &location); |
| 1473 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1474 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1475 | if (ret < 0) |
| 1476 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1477 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1478 | inode = NULL; |
| 1479 | if (location.objectid) { |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1480 | ret = fixup_tree_root_location(root, &location, &sub_root, |
| 1481 | dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1482 | if (ret < 0) |
| 1483 | return ERR_PTR(ret); |
| 1484 | if (ret > 0) |
| 1485 | return ERR_PTR(-ENOENT); |
| 1486 | inode = btrfs_iget_locked(dir->i_sb, location.objectid, |
| 1487 | sub_root); |
| 1488 | if (!inode) |
| 1489 | return ERR_PTR(-EACCES); |
| 1490 | if (inode->i_state & I_NEW) { |
| 1491 | /* the inode and parent dir are two different roots */ |
| 1492 | if (sub_root != root) { |
| 1493 | igrab(inode); |
| 1494 | sub_root->inode = inode; |
| 1495 | } |
| 1496 | BTRFS_I(inode)->root = sub_root; |
| 1497 | memcpy(&BTRFS_I(inode)->location, &location, |
| 1498 | sizeof(location)); |
| 1499 | btrfs_read_locked_inode(inode); |
| 1500 | unlock_new_inode(inode); |
| 1501 | } |
| 1502 | } |
| 1503 | return d_splice_alias(inode, dentry); |
| 1504 | } |
| 1505 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1506 | static unsigned char btrfs_filetype_table[] = { |
| 1507 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 1508 | }; |
| 1509 | |
| 1510 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 1511 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 1512 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1513 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1514 | struct btrfs_item *item; |
| 1515 | struct btrfs_dir_item *di; |
| 1516 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1517 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1518 | struct btrfs_path *path; |
| 1519 | int ret; |
| 1520 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1521 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1522 | int slot; |
| 1523 | int advance; |
| 1524 | unsigned char d_type; |
| 1525 | int over = 0; |
| 1526 | u32 di_cur; |
| 1527 | u32 di_total; |
| 1528 | u32 di_len; |
| 1529 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1530 | char tmp_name[32]; |
| 1531 | char *name_ptr; |
| 1532 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1533 | |
| 1534 | /* FIXME, use a real flag for deciding about the key type */ |
| 1535 | if (root->fs_info->tree_root == root) |
| 1536 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1537 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1538 | /* special case for "." */ |
| 1539 | if (filp->f_pos == 0) { |
| 1540 | over = filldir(dirent, ".", 1, |
| 1541 | 1, inode->i_ino, |
| 1542 | DT_DIR); |
| 1543 | if (over) |
| 1544 | return 0; |
| 1545 | filp->f_pos = 1; |
| 1546 | } |
| 1547 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1548 | mutex_lock(&root->fs_info->fs_mutex); |
| 1549 | key.objectid = inode->i_ino; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1550 | path = btrfs_alloc_path(); |
| 1551 | path->reada = 2; |
| 1552 | |
| 1553 | /* special case for .., just use the back ref */ |
| 1554 | if (filp->f_pos == 1) { |
| 1555 | btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); |
| 1556 | key.offset = 0; |
| 1557 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1558 | BUG_ON(ret == 0); |
| 1559 | leaf = path->nodes[0]; |
| 1560 | slot = path->slots[0]; |
| 1561 | nritems = btrfs_header_nritems(leaf); |
| 1562 | if (slot >= nritems) { |
| 1563 | btrfs_release_path(root, path); |
| 1564 | goto read_dir_items; |
| 1565 | } |
| 1566 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 1567 | btrfs_release_path(root, path); |
| 1568 | if (found_key.objectid != key.objectid || |
| 1569 | found_key.type != BTRFS_INODE_REF_KEY) |
| 1570 | goto read_dir_items; |
| 1571 | over = filldir(dirent, "..", 2, |
| 1572 | 2, found_key.offset, DT_DIR); |
| 1573 | if (over) |
| 1574 | goto nopos; |
| 1575 | filp->f_pos = 2; |
| 1576 | } |
| 1577 | |
| 1578 | read_dir_items: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1579 | btrfs_set_key_type(&key, key_type); |
| 1580 | key.offset = filp->f_pos; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1581 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1582 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1583 | if (ret < 0) |
| 1584 | goto err; |
| 1585 | advance = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1586 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1587 | leaf = path->nodes[0]; |
| 1588 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1589 | slot = path->slots[0]; |
| 1590 | if (advance || slot >= nritems) { |
| 1591 | if (slot >= nritems -1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1592 | ret = btrfs_next_leaf(root, path); |
| 1593 | if (ret) |
| 1594 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1595 | leaf = path->nodes[0]; |
| 1596 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1597 | slot = path->slots[0]; |
| 1598 | } else { |
| 1599 | slot++; |
| 1600 | path->slots[0]++; |
| 1601 | } |
| 1602 | } |
| 1603 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1604 | item = btrfs_item_nr(leaf, slot); |
| 1605 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 1606 | |
| 1607 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1608 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1609 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1610 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1611 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1612 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1613 | |
| 1614 | filp->f_pos = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1615 | advance = 1; |
| 1616 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 1617 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1618 | di_total = btrfs_item_size(leaf, item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1619 | while(di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1620 | struct btrfs_key location; |
| 1621 | |
| 1622 | name_len = btrfs_dir_name_len(leaf, di); |
| 1623 | if (name_len < 32) { |
| 1624 | name_ptr = tmp_name; |
| 1625 | } else { |
| 1626 | name_ptr = kmalloc(name_len, GFP_NOFS); |
| 1627 | BUG_ON(!name_ptr); |
| 1628 | } |
| 1629 | read_extent_buffer(leaf, name_ptr, |
| 1630 | (unsigned long)(di + 1), name_len); |
| 1631 | |
| 1632 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 1633 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1634 | over = filldir(dirent, name_ptr, name_len, |
| 1635 | found_key.offset, |
| 1636 | location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1637 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1638 | |
| 1639 | if (name_ptr != tmp_name) |
| 1640 | kfree(name_ptr); |
| 1641 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1642 | if (over) |
| 1643 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 1644 | di_len = btrfs_dir_name_len(leaf, di) + |
| 1645 | btrfs_dir_data_len(leaf, di) +sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1646 | di_cur += di_len; |
| 1647 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 1648 | } |
| 1649 | } |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 1650 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 1651 | filp->f_pos = INT_LIMIT(typeof(filp->f_pos)); |
| 1652 | else |
| 1653 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1654 | nopos: |
| 1655 | ret = 0; |
| 1656 | err: |
| 1657 | btrfs_release_path(root, path); |
| 1658 | btrfs_free_path(path); |
| 1659 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1660 | return ret; |
| 1661 | } |
| 1662 | |
| 1663 | int btrfs_write_inode(struct inode *inode, int wait) |
| 1664 | { |
| 1665 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1666 | struct btrfs_trans_handle *trans; |
| 1667 | int ret = 0; |
| 1668 | |
| 1669 | if (wait) { |
| 1670 | mutex_lock(&root->fs_info->fs_mutex); |
| 1671 | trans = btrfs_start_transaction(root, 1); |
| 1672 | btrfs_set_trans_block_group(trans, inode); |
| 1673 | ret = btrfs_commit_transaction(trans, root); |
| 1674 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1675 | } |
| 1676 | return ret; |
| 1677 | } |
| 1678 | |
| 1679 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1680 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1681 | * inode changes. But, it is most likely to find the inode in cache. |
| 1682 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 1683 | * to keep or drop this code. |
| 1684 | */ |
| 1685 | void btrfs_dirty_inode(struct inode *inode) |
| 1686 | { |
| 1687 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1688 | struct btrfs_trans_handle *trans; |
| 1689 | |
| 1690 | mutex_lock(&root->fs_info->fs_mutex); |
| 1691 | trans = btrfs_start_transaction(root, 1); |
| 1692 | btrfs_set_trans_block_group(trans, inode); |
| 1693 | btrfs_update_inode(trans, root, inode); |
| 1694 | btrfs_end_transaction(trans, root); |
| 1695 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 1699 | struct btrfs_root *root, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1700 | const char *name, int name_len, |
| 1701 | u64 ref_objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1702 | u64 objectid, |
| 1703 | struct btrfs_block_group_cache *group, |
| 1704 | int mode) |
| 1705 | { |
| 1706 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1707 | struct btrfs_inode_item *inode_item; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1708 | struct btrfs_block_group_cache *new_inode_group; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1709 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1710 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1711 | struct btrfs_inode_ref *ref; |
| 1712 | struct btrfs_key key[2]; |
| 1713 | u32 sizes[2]; |
| 1714 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1715 | int ret; |
| 1716 | int owner; |
| 1717 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1718 | path = btrfs_alloc_path(); |
| 1719 | BUG_ON(!path); |
| 1720 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1721 | inode = new_inode(root->fs_info->sb); |
| 1722 | if (!inode) |
| 1723 | return ERR_PTR(-ENOMEM); |
| 1724 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1725 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 1726 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1727 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1728 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
| 1729 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1730 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1731 | BTRFS_I(inode)->root = root; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1732 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1733 | if (mode & S_IFDIR) |
| 1734 | owner = 0; |
| 1735 | else |
| 1736 | owner = 1; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1737 | new_inode_group = btrfs_find_block_group(root, group, 0, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1738 | BTRFS_BLOCK_GROUP_METADATA, owner); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1739 | if (!new_inode_group) { |
| 1740 | printk("find_block group failed\n"); |
| 1741 | new_inode_group = group; |
| 1742 | } |
| 1743 | BTRFS_I(inode)->block_group = new_inode_group; |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 1744 | BTRFS_I(inode)->flags = 0; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1745 | |
| 1746 | key[0].objectid = objectid; |
| 1747 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 1748 | key[0].offset = 0; |
| 1749 | |
| 1750 | key[1].objectid = objectid; |
| 1751 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 1752 | key[1].offset = ref_objectid; |
| 1753 | |
| 1754 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 1755 | sizes[1] = name_len + sizeof(*ref); |
| 1756 | |
| 1757 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 1758 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1759 | goto fail; |
| 1760 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1761 | if (objectid > root->highest_inode) |
| 1762 | root->highest_inode = objectid; |
| 1763 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1764 | inode->i_uid = current->fsuid; |
| 1765 | inode->i_gid = current->fsgid; |
| 1766 | inode->i_mode = mode; |
| 1767 | inode->i_ino = objectid; |
| 1768 | inode->i_blocks = 0; |
| 1769 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1770 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1771 | struct btrfs_inode_item); |
| 1772 | fill_inode_item(path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1773 | |
| 1774 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 1775 | struct btrfs_inode_ref); |
| 1776 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 1777 | ptr = (unsigned long)(ref + 1); |
| 1778 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 1779 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1780 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 1781 | btrfs_free_path(path); |
| 1782 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1783 | location = &BTRFS_I(inode)->location; |
| 1784 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1785 | location->offset = 0; |
| 1786 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 1787 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1788 | insert_inode_hash(inode); |
| 1789 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1790 | fail: |
| 1791 | btrfs_free_path(path); |
| 1792 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 1796 | { |
| 1797 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 1798 | } |
| 1799 | |
| 1800 | static int btrfs_add_link(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1801 | struct dentry *dentry, struct inode *inode, |
| 1802 | int add_backref) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1803 | { |
| 1804 | int ret; |
| 1805 | struct btrfs_key key; |
| 1806 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 1807 | struct inode *parent_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1808 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1809 | key.objectid = inode->i_ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1810 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 1811 | key.offset = 0; |
| 1812 | |
| 1813 | ret = btrfs_insert_dir_item(trans, root, |
| 1814 | dentry->d_name.name, dentry->d_name.len, |
| 1815 | dentry->d_parent->d_inode->i_ino, |
| 1816 | &key, btrfs_inode_type(inode)); |
| 1817 | if (ret == 0) { |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1818 | if (add_backref) { |
| 1819 | ret = btrfs_insert_inode_ref(trans, root, |
| 1820 | dentry->d_name.name, |
| 1821 | dentry->d_name.len, |
| 1822 | inode->i_ino, |
| 1823 | dentry->d_parent->d_inode->i_ino); |
| 1824 | } |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 1825 | parent_inode = dentry->d_parent->d_inode; |
| 1826 | parent_inode->i_size += dentry->d_name.len * 2; |
| 1827 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1828 | ret = btrfs_update_inode(trans, root, |
| 1829 | dentry->d_parent->d_inode); |
| 1830 | } |
| 1831 | return ret; |
| 1832 | } |
| 1833 | |
| 1834 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1835 | struct dentry *dentry, struct inode *inode, |
| 1836 | int backref) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1837 | { |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1838 | int err = btrfs_add_link(trans, dentry, inode, backref); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1839 | if (!err) { |
| 1840 | d_instantiate(dentry, inode); |
| 1841 | return 0; |
| 1842 | } |
| 1843 | if (err > 0) |
| 1844 | err = -EEXIST; |
| 1845 | return err; |
| 1846 | } |
| 1847 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1848 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 1849 | int mode, dev_t rdev) |
| 1850 | { |
| 1851 | struct btrfs_trans_handle *trans; |
| 1852 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1853 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1854 | int err; |
| 1855 | int drop_inode = 0; |
| 1856 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1857 | unsigned long nr = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1858 | |
| 1859 | if (!new_valid_dev(rdev)) |
| 1860 | return -EINVAL; |
| 1861 | |
| 1862 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1863 | err = btrfs_check_free_space(root, 1, 0); |
| 1864 | if (err) |
| 1865 | goto fail; |
| 1866 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1867 | trans = btrfs_start_transaction(root, 1); |
| 1868 | btrfs_set_trans_block_group(trans, dir); |
| 1869 | |
| 1870 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 1871 | if (err) { |
| 1872 | err = -ENOSPC; |
| 1873 | goto out_unlock; |
| 1874 | } |
| 1875 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1876 | inode = btrfs_new_inode(trans, root, dentry->d_name.name, |
| 1877 | dentry->d_name.len, |
| 1878 | dentry->d_parent->d_inode->i_ino, objectid, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1879 | BTRFS_I(dir)->block_group, mode); |
| 1880 | err = PTR_ERR(inode); |
| 1881 | if (IS_ERR(inode)) |
| 1882 | goto out_unlock; |
| 1883 | |
| 1884 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1885 | err = btrfs_add_nondir(trans, dentry, inode, 0); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1886 | if (err) |
| 1887 | drop_inode = 1; |
| 1888 | else { |
| 1889 | inode->i_op = &btrfs_special_inode_operations; |
| 1890 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1891 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1892 | } |
| 1893 | dir->i_sb->s_dirt = 1; |
| 1894 | btrfs_update_inode_block_group(trans, inode); |
| 1895 | btrfs_update_inode_block_group(trans, dir); |
| 1896 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1897 | nr = trans->blocks_used; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1898 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1899 | fail: |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1900 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1901 | |
| 1902 | if (drop_inode) { |
| 1903 | inode_dec_link_count(inode); |
| 1904 | iput(inode); |
| 1905 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1906 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1907 | btrfs_throttle(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1908 | return err; |
| 1909 | } |
| 1910 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1911 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 1912 | int mode, struct nameidata *nd) |
| 1913 | { |
| 1914 | struct btrfs_trans_handle *trans; |
| 1915 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1916 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1917 | int err; |
| 1918 | int drop_inode = 0; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1919 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1920 | u64 objectid; |
| 1921 | |
| 1922 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1923 | err = btrfs_check_free_space(root, 1, 0); |
| 1924 | if (err) |
| 1925 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1926 | trans = btrfs_start_transaction(root, 1); |
| 1927 | btrfs_set_trans_block_group(trans, dir); |
| 1928 | |
| 1929 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 1930 | if (err) { |
| 1931 | err = -ENOSPC; |
| 1932 | goto out_unlock; |
| 1933 | } |
| 1934 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1935 | inode = btrfs_new_inode(trans, root, dentry->d_name.name, |
| 1936 | dentry->d_name.len, |
| 1937 | dentry->d_parent->d_inode->i_ino, |
| 1938 | objectid, BTRFS_I(dir)->block_group, mode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1939 | err = PTR_ERR(inode); |
| 1940 | if (IS_ERR(inode)) |
| 1941 | goto out_unlock; |
| 1942 | |
| 1943 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1944 | err = btrfs_add_nondir(trans, dentry, inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1945 | if (err) |
| 1946 | drop_inode = 1; |
| 1947 | else { |
| 1948 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 1949 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1950 | inode->i_fop = &btrfs_file_operations; |
| 1951 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1952 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 1953 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1954 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1955 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
| 1956 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1957 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1958 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1959 | } |
| 1960 | dir->i_sb->s_dirt = 1; |
| 1961 | btrfs_update_inode_block_group(trans, inode); |
| 1962 | btrfs_update_inode_block_group(trans, dir); |
| 1963 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1964 | nr = trans->blocks_used; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1965 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1966 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1967 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1968 | |
| 1969 | if (drop_inode) { |
| 1970 | inode_dec_link_count(inode); |
| 1971 | iput(inode); |
| 1972 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1973 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1974 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1975 | return err; |
| 1976 | } |
| 1977 | |
| 1978 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 1979 | struct dentry *dentry) |
| 1980 | { |
| 1981 | struct btrfs_trans_handle *trans; |
| 1982 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1983 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1984 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1985 | int err; |
| 1986 | int drop_inode = 0; |
| 1987 | |
| 1988 | if (inode->i_nlink == 0) |
| 1989 | return -ENOENT; |
| 1990 | |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 1991 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 1992 | inode->i_nlink++; |
| 1993 | #else |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1994 | inc_nlink(inode); |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 1995 | #endif |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1996 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1997 | err = btrfs_check_free_space(root, 1, 0); |
| 1998 | if (err) |
| 1999 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2000 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2001 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2002 | btrfs_set_trans_block_group(trans, dir); |
| 2003 | atomic_inc(&inode->i_count); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2004 | err = btrfs_add_nondir(trans, dentry, inode, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2005 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2006 | if (err) |
| 2007 | drop_inode = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2008 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2009 | dir->i_sb->s_dirt = 1; |
| 2010 | btrfs_update_inode_block_group(trans, dir); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2011 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2012 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2013 | if (err) |
| 2014 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2015 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2016 | nr = trans->blocks_used; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2017 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2018 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2019 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2020 | |
| 2021 | if (drop_inode) { |
| 2022 | inode_dec_link_count(inode); |
| 2023 | iput(inode); |
| 2024 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2025 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2026 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2027 | return err; |
| 2028 | } |
| 2029 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2030 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 2031 | { |
| 2032 | struct inode *inode; |
| 2033 | struct btrfs_trans_handle *trans; |
| 2034 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2035 | int err = 0; |
| 2036 | int drop_on_err = 0; |
| 2037 | u64 objectid; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2038 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2039 | |
| 2040 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2041 | err = btrfs_check_free_space(root, 1, 0); |
| 2042 | if (err) |
| 2043 | goto out_unlock; |
| 2044 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2045 | trans = btrfs_start_transaction(root, 1); |
| 2046 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2047 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2048 | if (IS_ERR(trans)) { |
| 2049 | err = PTR_ERR(trans); |
| 2050 | goto out_unlock; |
| 2051 | } |
| 2052 | |
| 2053 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 2054 | if (err) { |
| 2055 | err = -ENOSPC; |
| 2056 | goto out_unlock; |
| 2057 | } |
| 2058 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2059 | inode = btrfs_new_inode(trans, root, dentry->d_name.name, |
| 2060 | dentry->d_name.len, |
| 2061 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2062 | BTRFS_I(dir)->block_group, S_IFDIR | mode); |
| 2063 | if (IS_ERR(inode)) { |
| 2064 | err = PTR_ERR(inode); |
| 2065 | goto out_fail; |
| 2066 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2067 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2068 | drop_on_err = 1; |
| 2069 | inode->i_op = &btrfs_dir_inode_operations; |
| 2070 | inode->i_fop = &btrfs_dir_file_operations; |
| 2071 | btrfs_set_trans_block_group(trans, inode); |
| 2072 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2073 | inode->i_size = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2074 | err = btrfs_update_inode(trans, root, inode); |
| 2075 | if (err) |
| 2076 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2077 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2078 | err = btrfs_add_link(trans, dentry, inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2079 | if (err) |
| 2080 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2081 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2082 | d_instantiate(dentry, inode); |
| 2083 | drop_on_err = 0; |
| 2084 | dir->i_sb->s_dirt = 1; |
| 2085 | btrfs_update_inode_block_group(trans, inode); |
| 2086 | btrfs_update_inode_block_group(trans, dir); |
| 2087 | |
| 2088 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2089 | nr = trans->blocks_used; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2090 | btrfs_end_transaction(trans, root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2091 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2092 | out_unlock: |
| 2093 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2094 | if (drop_on_err) |
| 2095 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2096 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2097 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2098 | return err; |
| 2099 | } |
| 2100 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2101 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 2102 | struct extent_map *existing, |
| 2103 | struct extent_map *em) |
| 2104 | { |
| 2105 | u64 start_diff; |
| 2106 | u64 new_end; |
| 2107 | int ret = 0; |
| 2108 | int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE; |
| 2109 | |
| 2110 | if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE) |
| 2111 | goto invalid; |
| 2112 | |
| 2113 | if (!real_blocks && em->block_start != existing->block_start) |
| 2114 | goto invalid; |
| 2115 | |
| 2116 | new_end = max(existing->start + existing->len, em->start + em->len); |
| 2117 | |
| 2118 | if (existing->start >= em->start) { |
| 2119 | if (em->start + em->len < existing->start) |
| 2120 | goto invalid; |
| 2121 | |
| 2122 | start_diff = existing->start - em->start; |
| 2123 | if (real_blocks && em->block_start + start_diff != |
| 2124 | existing->block_start) |
| 2125 | goto invalid; |
| 2126 | |
| 2127 | em->len = new_end - em->start; |
| 2128 | |
| 2129 | remove_extent_mapping(em_tree, existing); |
| 2130 | /* free for the tree */ |
| 2131 | free_extent_map(existing); |
| 2132 | ret = add_extent_mapping(em_tree, em); |
| 2133 | |
| 2134 | } else if (em->start > existing->start) { |
| 2135 | |
| 2136 | if (existing->start + existing->len < em->start) |
| 2137 | goto invalid; |
| 2138 | |
| 2139 | start_diff = em->start - existing->start; |
| 2140 | if (real_blocks && existing->block_start + start_diff != |
| 2141 | em->block_start) |
| 2142 | goto invalid; |
| 2143 | |
| 2144 | remove_extent_mapping(em_tree, existing); |
| 2145 | em->block_start = existing->block_start; |
| 2146 | em->start = existing->start; |
| 2147 | em->len = new_end - existing->start; |
| 2148 | free_extent_map(existing); |
| 2149 | |
| 2150 | ret = add_extent_mapping(em_tree, em); |
| 2151 | } else { |
| 2152 | goto invalid; |
| 2153 | } |
| 2154 | return ret; |
| 2155 | |
| 2156 | invalid: |
| 2157 | printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n", |
| 2158 | existing->start, existing->len, existing->block_start, |
| 2159 | em->start, em->len, em->block_start); |
| 2160 | return -EIO; |
| 2161 | } |
| 2162 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2163 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2164 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2165 | int create) |
| 2166 | { |
| 2167 | int ret; |
| 2168 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2169 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2170 | u64 extent_start = 0; |
| 2171 | u64 extent_end = 0; |
| 2172 | u64 objectid = inode->i_ino; |
| 2173 | u32 found_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2174 | struct btrfs_path *path; |
| 2175 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2176 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2177 | struct extent_buffer *leaf; |
| 2178 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2179 | struct extent_map *em = NULL; |
| 2180 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2181 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2182 | struct btrfs_trans_handle *trans = NULL; |
| 2183 | |
| 2184 | path = btrfs_alloc_path(); |
| 2185 | BUG_ON(!path); |
| 2186 | mutex_lock(&root->fs_info->fs_mutex); |
| 2187 | |
| 2188 | again: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2189 | spin_lock(&em_tree->lock); |
| 2190 | em = lookup_extent_mapping(em_tree, start, len); |
| 2191 | spin_unlock(&em_tree->lock); |
| 2192 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2193 | if (em) { |
Chris Mason | 56b453c | 2008-01-03 09:08:27 -0500 | [diff] [blame] | 2194 | if (em->start > start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2195 | printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n", |
| 2196 | start, len, em->start, em->len); |
Chris Mason | 56b453c | 2008-01-03 09:08:27 -0500 | [diff] [blame] | 2197 | WARN_ON(1); |
| 2198 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2199 | if (em->block_start == EXTENT_MAP_INLINE && page) |
| 2200 | free_extent_map(em); |
| 2201 | else |
| 2202 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2203 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2204 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2205 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2206 | err = -ENOMEM; |
| 2207 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2208 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2209 | |
| 2210 | em->start = EXTENT_MAP_HOLE; |
| 2211 | em->len = (u64)-1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2212 | em->bdev = inode->i_sb->s_bdev; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2213 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 2214 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2215 | if (ret < 0) { |
| 2216 | err = ret; |
| 2217 | goto out; |
| 2218 | } |
| 2219 | |
| 2220 | if (ret != 0) { |
| 2221 | if (path->slots[0] == 0) |
| 2222 | goto not_found; |
| 2223 | path->slots[0]--; |
| 2224 | } |
| 2225 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2226 | leaf = path->nodes[0]; |
| 2227 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2228 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2229 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2230 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2231 | found_type = btrfs_key_type(&found_key); |
| 2232 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2233 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 2234 | goto not_found; |
| 2235 | } |
| 2236 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2237 | found_type = btrfs_file_extent_type(leaf, item); |
| 2238 | extent_start = found_key.offset; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2239 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
| 2240 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2241 | btrfs_file_extent_num_bytes(leaf, item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2242 | err = 0; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2243 | if (start < extent_start || start >= extent_end) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2244 | em->start = start; |
| 2245 | if (start < extent_start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2246 | if (start + len <= extent_start) |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2247 | goto not_found; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2248 | em->len = extent_end - extent_start; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2249 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2250 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2251 | } |
| 2252 | goto not_found_em; |
| 2253 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2254 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 2255 | if (bytenr == 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2256 | em->start = extent_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2257 | em->len = extent_end - extent_start; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2258 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2259 | goto insert; |
| 2260 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2261 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 2262 | em->block_start = bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2263 | em->start = extent_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2264 | em->len = extent_end - extent_start; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2265 | goto insert; |
| 2266 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2267 | u64 page_start; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2268 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2269 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 2270 | size_t size; |
| 2271 | size_t extent_offset; |
| 2272 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2273 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2274 | size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf, |
| 2275 | path->slots[0])); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2276 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 2277 | ~((u64)root->sectorsize - 1); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2278 | if (start < extent_start || start >= extent_end) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2279 | em->start = start; |
| 2280 | if (start < extent_start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2281 | if (start + len <= extent_start) |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2282 | goto not_found; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2283 | em->len = extent_end - extent_start; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2284 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2285 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2286 | } |
| 2287 | goto not_found_em; |
| 2288 | } |
| 2289 | em->block_start = EXTENT_MAP_INLINE; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 2290 | |
| 2291 | if (!page) { |
| 2292 | em->start = extent_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2293 | em->len = size; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 2294 | goto out; |
| 2295 | } |
| 2296 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2297 | page_start = page_offset(page) + pg_offset; |
| 2298 | extent_offset = page_start - extent_start; |
| 2299 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 2300 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 2301 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2302 | em->len = (copy_size + root->sectorsize - 1) & |
| 2303 | ~((u64)root->sectorsize - 1); |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 2304 | map = kmap(page); |
| 2305 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2306 | if (create == 0 && !PageUptodate(page)) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2307 | read_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2308 | copy_size); |
| 2309 | flush_dcache_page(page); |
| 2310 | } else if (create && PageUptodate(page)) { |
| 2311 | if (!trans) { |
| 2312 | kunmap(page); |
| 2313 | free_extent_map(em); |
| 2314 | em = NULL; |
| 2315 | btrfs_release_path(root, path); |
| 2316 | trans = btrfs_start_transaction(root, 1); |
| 2317 | goto again; |
| 2318 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2319 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2320 | copy_size); |
| 2321 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2322 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2323 | kunmap(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2324 | set_extent_uptodate(io_tree, em->start, |
| 2325 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2326 | goto insert; |
| 2327 | } else { |
| 2328 | printk("unkknown found_type %d\n", found_type); |
| 2329 | WARN_ON(1); |
| 2330 | } |
| 2331 | not_found: |
| 2332 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2333 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2334 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2335 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2336 | insert: |
| 2337 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2338 | if (em->start > start || extent_map_end(em) <= start) { |
| 2339 | printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2340 | err = -EIO; |
| 2341 | goto out; |
| 2342 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2343 | |
| 2344 | err = 0; |
| 2345 | spin_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2346 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2347 | |
| 2348 | /* it is possible that someone inserted the extent into the tree |
| 2349 | * while we had the lock dropped. It is also possible that |
| 2350 | * an overlapping map exists in the tree |
| 2351 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2352 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2353 | struct extent_map *existing; |
| 2354 | existing = lookup_extent_mapping(em_tree, start, len); |
| 2355 | if (!existing) { |
| 2356 | existing = lookup_extent_mapping(em_tree, em->start, |
| 2357 | em->len); |
| 2358 | if (existing) { |
| 2359 | err = merge_extent_mapping(em_tree, existing, |
| 2360 | em); |
| 2361 | free_extent_map(existing); |
| 2362 | if (err) { |
| 2363 | free_extent_map(em); |
| 2364 | em = NULL; |
| 2365 | } |
| 2366 | } else { |
| 2367 | err = -EIO; |
| 2368 | printk("failing to insert %Lu %Lu\n", |
| 2369 | start, len); |
| 2370 | free_extent_map(em); |
| 2371 | em = NULL; |
| 2372 | } |
| 2373 | } else { |
| 2374 | free_extent_map(em); |
| 2375 | em = existing; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2376 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2377 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2378 | spin_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2379 | out: |
| 2380 | btrfs_free_path(path); |
| 2381 | if (trans) { |
| 2382 | ret = btrfs_end_transaction(trans, root); |
| 2383 | if (!err) |
| 2384 | err = ret; |
| 2385 | } |
| 2386 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2387 | if (err) { |
| 2388 | free_extent_map(em); |
| 2389 | WARN_ON(1); |
| 2390 | return ERR_PTR(err); |
| 2391 | } |
| 2392 | return em; |
| 2393 | } |
| 2394 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 2395 | static int btrfs_get_block(struct inode *inode, sector_t iblock, |
| 2396 | struct buffer_head *bh_result, int create) |
| 2397 | { |
| 2398 | struct extent_map *em; |
| 2399 | u64 start = (u64)iblock << inode->i_blkbits; |
| 2400 | struct btrfs_multi_bio *multi = NULL; |
| 2401 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2402 | u64 len; |
| 2403 | u64 logical; |
| 2404 | u64 map_length; |
| 2405 | int ret = 0; |
| 2406 | |
| 2407 | em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0); |
| 2408 | |
| 2409 | if (!em || IS_ERR(em)) |
| 2410 | goto out; |
| 2411 | |
| 2412 | if (em->start > start || em->start + em->len <= start) |
| 2413 | goto out; |
| 2414 | |
| 2415 | if (em->block_start == EXTENT_MAP_INLINE) { |
| 2416 | ret = -EINVAL; |
| 2417 | goto out; |
| 2418 | } |
| 2419 | |
| 2420 | if (em->block_start == EXTENT_MAP_HOLE || |
| 2421 | em->block_start == EXTENT_MAP_DELALLOC) { |
| 2422 | goto out; |
| 2423 | } |
| 2424 | |
| 2425 | len = em->start + em->len - start; |
| 2426 | len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size))); |
| 2427 | |
| 2428 | logical = start - em->start; |
| 2429 | logical = em->block_start + logical; |
| 2430 | |
| 2431 | map_length = len; |
| 2432 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 2433 | logical, &map_length, &multi, 0); |
| 2434 | BUG_ON(ret); |
| 2435 | bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits; |
| 2436 | bh_result->b_size = min(map_length, len); |
| 2437 | bh_result->b_bdev = multi->stripes[0].dev->bdev; |
| 2438 | set_buffer_mapped(bh_result); |
| 2439 | kfree(multi); |
| 2440 | out: |
| 2441 | free_extent_map(em); |
| 2442 | return ret; |
| 2443 | } |
| 2444 | |
| 2445 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 2446 | const struct iovec *iov, loff_t offset, |
| 2447 | unsigned long nr_segs) |
| 2448 | { |
| 2449 | struct file *file = iocb->ki_filp; |
| 2450 | struct inode *inode = file->f_mapping->host; |
| 2451 | |
| 2452 | if (rw == WRITE) |
| 2453 | return -EINVAL; |
| 2454 | |
| 2455 | return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, |
| 2456 | offset, nr_segs, btrfs_get_block, NULL); |
| 2457 | } |
| 2458 | |
Christoph Hellwig | d396c6f | 2007-09-10 20:02:30 -0400 | [diff] [blame] | 2459 | static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2460 | { |
Christoph Hellwig | d396c6f | 2007-09-10 20:02:30 -0400 | [diff] [blame] | 2461 | return extent_bmap(mapping, iblock, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2462 | } |
| 2463 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2464 | int btrfs_readpage(struct file *file, struct page *page) |
| 2465 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2466 | struct extent_io_tree *tree; |
| 2467 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2468 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2469 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2470 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2471 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 2472 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2473 | struct extent_io_tree *tree; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2474 | |
| 2475 | |
| 2476 | if (current->flags & PF_MEMALLOC) { |
| 2477 | redirty_page_for_writepage(wbc, page); |
| 2478 | unlock_page(page); |
| 2479 | return 0; |
| 2480 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2481 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2482 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 2483 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2484 | |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 2485 | static int btrfs_writepages(struct address_space *mapping, |
| 2486 | struct writeback_control *wbc) |
| 2487 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2488 | struct extent_io_tree *tree; |
| 2489 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 2490 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 2491 | } |
| 2492 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 2493 | static int |
| 2494 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 2495 | struct list_head *pages, unsigned nr_pages) |
| 2496 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2497 | struct extent_io_tree *tree; |
| 2498 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 2499 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 2500 | btrfs_get_extent); |
| 2501 | } |
| 2502 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2503 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2504 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2505 | struct extent_io_tree *tree; |
| 2506 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2507 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2508 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2509 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2510 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2511 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2512 | if (ret == 1) { |
| 2513 | ClearPagePrivate(page); |
| 2514 | set_page_private(page, 0); |
| 2515 | page_cache_release(page); |
| 2516 | } |
| 2517 | return ret; |
| 2518 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2519 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2520 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 2521 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2522 | struct extent_io_tree *tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2523 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2524 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2525 | extent_invalidatepage(tree, page, offset); |
| 2526 | btrfs_releasepage(page, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2527 | } |
| 2528 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2529 | /* |
| 2530 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 2531 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 2532 | * be careful to check for EOF conditions here. We set the page up correctly |
| 2533 | * for a written page which means we get ENOSPC checking when writing into |
| 2534 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 2535 | * support these features. |
| 2536 | * |
| 2537 | * We are not allowed to take the i_mutex here so we have to play games to |
| 2538 | * protect against truncate races as the page could now be beyond EOF. Because |
| 2539 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 2540 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 2541 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 2542 | * unlock the page. |
| 2543 | */ |
| 2544 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page) |
| 2545 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 2546 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2547 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2548 | unsigned long end; |
| 2549 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2550 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2551 | u64 page_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2552 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2553 | mutex_lock(&root->fs_info->fs_mutex); |
| 2554 | ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0); |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 2555 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2556 | if (ret) |
| 2557 | goto out; |
| 2558 | |
| 2559 | ret = -EINVAL; |
| 2560 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2561 | lock_page(page); |
| 2562 | wait_on_page_writeback(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2563 | size = i_size_read(inode); |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 2564 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2565 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2566 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2567 | (page_start > size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2568 | /* page got truncated out from underneath us */ |
| 2569 | goto out_unlock; |
| 2570 | } |
| 2571 | |
| 2572 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2573 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2574 | end = size & ~PAGE_CACHE_MASK; |
| 2575 | else |
| 2576 | end = PAGE_CACHE_SIZE; |
| 2577 | |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2578 | ret = btrfs_cow_one_page(inode, page, end); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2579 | |
| 2580 | out_unlock: |
| 2581 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2582 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2583 | return ret; |
| 2584 | } |
| 2585 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2586 | static void btrfs_truncate(struct inode *inode) |
| 2587 | { |
| 2588 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2589 | int ret; |
| 2590 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2591 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2592 | |
| 2593 | if (!S_ISREG(inode->i_mode)) |
| 2594 | return; |
| 2595 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 2596 | return; |
| 2597 | |
| 2598 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 2599 | |
| 2600 | mutex_lock(&root->fs_info->fs_mutex); |
| 2601 | trans = btrfs_start_transaction(root, 1); |
| 2602 | btrfs_set_trans_block_group(trans, inode); |
| 2603 | |
| 2604 | /* FIXME, add redo link to tree so we don't leak on crash */ |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2605 | ret = btrfs_truncate_in_trans(trans, root, inode, |
| 2606 | BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2607 | btrfs_update_inode(trans, root, inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2608 | nr = trans->blocks_used; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2609 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2610 | ret = btrfs_end_transaction(trans, root); |
| 2611 | BUG_ON(ret); |
| 2612 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2613 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2614 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2615 | } |
| 2616 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2617 | static int noinline create_subvol(struct btrfs_root *root, char *name, |
| 2618 | int namelen) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2619 | { |
| 2620 | struct btrfs_trans_handle *trans; |
| 2621 | struct btrfs_key key; |
| 2622 | struct btrfs_root_item root_item; |
| 2623 | struct btrfs_inode_item *inode_item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2624 | struct extent_buffer *leaf; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2625 | struct btrfs_root *new_root = root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2626 | struct inode *inode; |
| 2627 | struct inode *dir; |
| 2628 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2629 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2630 | u64 objectid; |
| 2631 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2632 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2633 | |
| 2634 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2635 | ret = btrfs_check_free_space(root, 1, 0); |
| 2636 | if (ret) |
| 2637 | goto fail_commit; |
| 2638 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2639 | trans = btrfs_start_transaction(root, 1); |
| 2640 | BUG_ON(!trans); |
| 2641 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2642 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 2643 | 0, &objectid); |
| 2644 | if (ret) |
| 2645 | goto fail; |
| 2646 | |
| 2647 | leaf = __btrfs_alloc_free_block(trans, root, root->leafsize, |
| 2648 | objectid, trans->transid, 0, 0, |
| 2649 | 0, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2650 | if (IS_ERR(leaf)) |
| 2651 | return PTR_ERR(leaf); |
| 2652 | |
| 2653 | btrfs_set_header_nritems(leaf, 0); |
| 2654 | btrfs_set_header_level(leaf, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2655 | btrfs_set_header_bytenr(leaf, leaf->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2656 | btrfs_set_header_generation(leaf, trans->transid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2657 | btrfs_set_header_owner(leaf, objectid); |
| 2658 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2659 | write_extent_buffer(leaf, root->fs_info->fsid, |
| 2660 | (unsigned long)btrfs_header_fsid(leaf), |
| 2661 | BTRFS_FSID_SIZE); |
| 2662 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2663 | |
| 2664 | inode_item = &root_item.inode; |
| 2665 | memset(inode_item, 0, sizeof(*inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2666 | inode_item->generation = cpu_to_le64(1); |
| 2667 | inode_item->size = cpu_to_le64(3); |
| 2668 | inode_item->nlink = cpu_to_le32(1); |
| 2669 | inode_item->nblocks = cpu_to_le64(1); |
| 2670 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2671 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2672 | btrfs_set_root_bytenr(&root_item, leaf->start); |
| 2673 | btrfs_set_root_level(&root_item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2674 | btrfs_set_root_refs(&root_item, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2675 | btrfs_set_root_used(&root_item, 0); |
| 2676 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2677 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
| 2678 | root_item.drop_level = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2679 | |
| 2680 | free_extent_buffer(leaf); |
| 2681 | leaf = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2682 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2683 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 2684 | |
| 2685 | key.objectid = objectid; |
| 2686 | key.offset = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2687 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 2688 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 2689 | &root_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2690 | if (ret) |
| 2691 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2692 | |
| 2693 | /* |
| 2694 | * insert the directory item |
| 2695 | */ |
| 2696 | key.offset = (u64)-1; |
| 2697 | dir = root->fs_info->sb->s_root->d_inode; |
| 2698 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 2699 | name, namelen, dir->i_ino, &key, |
| 2700 | BTRFS_FT_DIR); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2701 | if (ret) |
| 2702 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2703 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2704 | ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, |
| 2705 | name, namelen, objectid, |
| 2706 | root->fs_info->sb->s_root->d_inode->i_ino); |
| 2707 | if (ret) |
| 2708 | goto fail; |
| 2709 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2710 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2711 | if (ret) |
| 2712 | goto fail_commit; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2713 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2714 | new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2715 | BUG_ON(!new_root); |
| 2716 | |
| 2717 | trans = btrfs_start_transaction(new_root, 1); |
| 2718 | BUG_ON(!trans); |
| 2719 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2720 | inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid, |
| 2721 | new_dirid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2722 | BTRFS_I(dir)->block_group, S_IFDIR | 0700); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2723 | if (IS_ERR(inode)) |
| 2724 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2725 | inode->i_op = &btrfs_dir_inode_operations; |
| 2726 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 3408878 | 2007-06-12 11:36:58 -0400 | [diff] [blame] | 2727 | new_root->inode = inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2728 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2729 | ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid, |
| 2730 | new_dirid); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2731 | inode->i_nlink = 1; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2732 | inode->i_size = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2733 | ret = btrfs_update_inode(trans, new_root, inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2734 | if (ret) |
| 2735 | goto fail; |
| 2736 | fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2737 | nr = trans->blocks_used; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2738 | err = btrfs_commit_transaction(trans, new_root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2739 | if (err && !ret) |
| 2740 | ret = err; |
| 2741 | fail_commit: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2742 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2743 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2744 | btrfs_throttle(root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2745 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) |
| 2749 | { |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2750 | struct btrfs_pending_snapshot *pending_snapshot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2751 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2752 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2753 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2754 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2755 | |
| 2756 | if (!root->ref_cows) |
| 2757 | return -EINVAL; |
| 2758 | |
| 2759 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2760 | ret = btrfs_check_free_space(root, 1, 0); |
| 2761 | if (ret) |
| 2762 | goto fail_unlock; |
| 2763 | |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2764 | pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS); |
| 2765 | if (!pending_snapshot) { |
| 2766 | ret = -ENOMEM; |
| 2767 | goto fail_unlock; |
| 2768 | } |
Yan | fb4bc1e | 2008-01-17 11:59:51 -0500 | [diff] [blame] | 2769 | pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2770 | if (!pending_snapshot->name) { |
| 2771 | ret = -ENOMEM; |
| 2772 | kfree(pending_snapshot); |
| 2773 | goto fail_unlock; |
| 2774 | } |
Yan | fb4bc1e | 2008-01-17 11:59:51 -0500 | [diff] [blame] | 2775 | memcpy(pending_snapshot->name, name, namelen); |
| 2776 | pending_snapshot->name[namelen] = '\0'; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2777 | trans = btrfs_start_transaction(root, 1); |
| 2778 | BUG_ON(!trans); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2779 | pending_snapshot->root = root; |
| 2780 | list_add(&pending_snapshot->list, |
| 2781 | &trans->transaction->pending_snapshots); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2782 | ret = btrfs_update_inode(trans, root, root->inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2783 | err = btrfs_commit_transaction(trans, root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2784 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2785 | fail_unlock: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2786 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2787 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2788 | btrfs_throttle(root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2789 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2790 | } |
| 2791 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2792 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2793 | struct file_ra_state *ra, struct file *file, |
| 2794 | pgoff_t offset, pgoff_t last_index) |
| 2795 | { |
| 2796 | pgoff_t req_size; |
| 2797 | |
| 2798 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) |
| 2799 | req_size = last_index - offset + 1; |
| 2800 | offset = page_cache_readahead(mapping, ra, file, offset, req_size); |
| 2801 | return offset; |
| 2802 | #else |
| 2803 | req_size = min(last_index - offset + 1, (pgoff_t)128); |
| 2804 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 2805 | return offset + req_size; |
| 2806 | #endif |
| 2807 | } |
| 2808 | |
| 2809 | int btrfs_defrag_file(struct file *file) { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 2810 | struct inode *inode = fdentry(file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2811 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2812 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2813 | struct page *page; |
| 2814 | unsigned long last_index; |
| 2815 | unsigned long ra_index = 0; |
| 2816 | u64 page_start; |
| 2817 | u64 page_end; |
| 2818 | unsigned long i; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2819 | int ret; |
| 2820 | |
| 2821 | mutex_lock(&root->fs_info->fs_mutex); |
| 2822 | ret = btrfs_check_free_space(root, inode->i_size, 0); |
| 2823 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2824 | if (ret) |
| 2825 | return -ENOSPC; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2826 | |
| 2827 | mutex_lock(&inode->i_mutex); |
| 2828 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 2829 | for (i = 0; i <= last_index; i++) { |
| 2830 | if (i == ra_index) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2831 | ra_index = btrfs_force_ra(inode->i_mapping, |
| 2832 | &file->f_ra, |
| 2833 | file, ra_index, last_index); |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2834 | } |
| 2835 | page = grab_cache_page(inode->i_mapping, i); |
| 2836 | if (!page) |
| 2837 | goto out_unlock; |
| 2838 | if (!PageUptodate(page)) { |
| 2839 | btrfs_readpage(NULL, page); |
| 2840 | lock_page(page); |
| 2841 | if (!PageUptodate(page)) { |
| 2842 | unlock_page(page); |
| 2843 | page_cache_release(page); |
| 2844 | goto out_unlock; |
| 2845 | } |
| 2846 | } |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 2847 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2848 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 2849 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2850 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2851 | set_extent_delalloc(io_tree, page_start, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2852 | page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2853 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2854 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2855 | set_page_dirty(page); |
| 2856 | unlock_page(page); |
| 2857 | page_cache_release(page); |
| 2858 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); |
| 2859 | } |
| 2860 | |
| 2861 | out_unlock: |
| 2862 | mutex_unlock(&inode->i_mutex); |
| 2863 | return 0; |
| 2864 | } |
| 2865 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2866 | static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg) |
| 2867 | { |
| 2868 | u64 new_size; |
| 2869 | u64 old_size; |
| 2870 | struct btrfs_ioctl_vol_args *vol_args; |
| 2871 | struct btrfs_trans_handle *trans; |
| 2872 | char *sizestr; |
| 2873 | int ret = 0; |
| 2874 | int namelen; |
| 2875 | int mod = 0; |
| 2876 | |
| 2877 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 2878 | |
| 2879 | if (!vol_args) |
| 2880 | return -ENOMEM; |
| 2881 | |
| 2882 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 2883 | ret = -EFAULT; |
| 2884 | goto out; |
| 2885 | } |
| 2886 | namelen = strlen(vol_args->name); |
| 2887 | if (namelen > BTRFS_VOL_NAME_MAX) { |
| 2888 | ret = -EINVAL; |
| 2889 | goto out; |
| 2890 | } |
| 2891 | |
| 2892 | sizestr = vol_args->name; |
| 2893 | if (!strcmp(sizestr, "max")) |
| 2894 | new_size = root->fs_info->sb->s_bdev->bd_inode->i_size; |
| 2895 | else { |
| 2896 | if (sizestr[0] == '-') { |
| 2897 | mod = -1; |
| 2898 | sizestr++; |
| 2899 | } else if (sizestr[0] == '+') { |
| 2900 | mod = 1; |
| 2901 | sizestr++; |
| 2902 | } |
| 2903 | new_size = btrfs_parse_size(sizestr); |
| 2904 | if (new_size == 0) { |
| 2905 | ret = -EINVAL; |
| 2906 | goto out; |
| 2907 | } |
| 2908 | } |
| 2909 | |
| 2910 | mutex_lock(&root->fs_info->fs_mutex); |
| 2911 | old_size = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 2912 | |
| 2913 | if (mod < 0) { |
| 2914 | if (new_size > old_size) { |
| 2915 | ret = -EINVAL; |
| 2916 | goto out_unlock; |
| 2917 | } |
| 2918 | new_size = old_size - new_size; |
| 2919 | } else if (mod > 0) { |
| 2920 | new_size = old_size + new_size; |
| 2921 | } |
| 2922 | |
| 2923 | if (new_size < 256 * 1024 * 1024) { |
| 2924 | ret = -EINVAL; |
| 2925 | goto out_unlock; |
| 2926 | } |
| 2927 | if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) { |
| 2928 | ret = -EFBIG; |
| 2929 | goto out_unlock; |
| 2930 | } |
Chris Mason | f9ef660 | 2008-01-03 09:22:38 -0500 | [diff] [blame] | 2931 | |
| 2932 | do_div(new_size, root->sectorsize); |
| 2933 | new_size *= root->sectorsize; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2934 | |
| 2935 | printk("new size is %Lu\n", new_size); |
| 2936 | if (new_size > old_size) { |
| 2937 | trans = btrfs_start_transaction(root, 1); |
| 2938 | ret = btrfs_grow_extent_tree(trans, root, new_size); |
| 2939 | btrfs_commit_transaction(trans, root); |
| 2940 | } else { |
| 2941 | ret = btrfs_shrink_extent_tree(root, new_size); |
| 2942 | } |
| 2943 | |
| 2944 | out_unlock: |
| 2945 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2946 | out: |
| 2947 | kfree(vol_args); |
| 2948 | return ret; |
| 2949 | } |
| 2950 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2951 | static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root, |
| 2952 | void __user *arg) |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2953 | { |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2954 | struct btrfs_ioctl_vol_args *vol_args; |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2955 | struct btrfs_dir_item *di; |
| 2956 | struct btrfs_path *path; |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2957 | u64 root_dirid; |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2958 | int namelen; |
| 2959 | int ret; |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2960 | |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2961 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2962 | |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2963 | if (!vol_args) |
| 2964 | return -ENOMEM; |
| 2965 | |
| 2966 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 2967 | ret = -EFAULT; |
| 2968 | goto out; |
| 2969 | } |
| 2970 | |
| 2971 | namelen = strlen(vol_args->name); |
| 2972 | if (namelen > BTRFS_VOL_NAME_MAX) { |
| 2973 | ret = -EINVAL; |
| 2974 | goto out; |
| 2975 | } |
| 2976 | if (strchr(vol_args->name, '/')) { |
| 2977 | ret = -EINVAL; |
| 2978 | goto out; |
| 2979 | } |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2980 | |
| 2981 | path = btrfs_alloc_path(); |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2982 | if (!path) { |
| 2983 | ret = -ENOMEM; |
| 2984 | goto out; |
| 2985 | } |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2986 | |
| 2987 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
| 2988 | mutex_lock(&root->fs_info->fs_mutex); |
| 2989 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, |
| 2990 | path, root_dirid, |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2991 | vol_args->name, namelen, 0); |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2992 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2993 | btrfs_free_path(path); |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2994 | |
| 2995 | if (di && !IS_ERR(di)) { |
| 2996 | ret = -EEXIST; |
| 2997 | goto out; |
| 2998 | } |
| 2999 | |
| 3000 | if (IS_ERR(di)) { |
| 3001 | ret = PTR_ERR(di); |
| 3002 | goto out; |
| 3003 | } |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 3004 | |
| 3005 | if (root == root->fs_info->tree_root) |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 3006 | ret = create_subvol(root, vol_args->name, namelen); |
| 3007 | else |
| 3008 | ret = create_snapshot(root, vol_args->name, namelen); |
| 3009 | out: |
| 3010 | kfree(vol_args); |
| 3011 | return ret; |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | static int btrfs_ioctl_defrag(struct file *file) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3015 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 3016 | struct inode *inode = fdentry(file)->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3017 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 3018 | |
| 3019 | switch (inode->i_mode & S_IFMT) { |
| 3020 | case S_IFDIR: |
| 3021 | mutex_lock(&root->fs_info->fs_mutex); |
| 3022 | btrfs_defrag_root(root, 0); |
| 3023 | btrfs_defrag_root(root->fs_info->extent_root, 0); |
| 3024 | mutex_unlock(&root->fs_info->fs_mutex); |
| 3025 | break; |
| 3026 | case S_IFREG: |
| 3027 | btrfs_defrag_file(file); |
| 3028 | break; |
| 3029 | } |
| 3030 | |
| 3031 | return 0; |
| 3032 | } |
| 3033 | |
| 3034 | long btrfs_ioctl(struct file *file, unsigned int |
| 3035 | cmd, unsigned long arg) |
| 3036 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 3037 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3038 | |
| 3039 | switch (cmd) { |
| 3040 | case BTRFS_IOC_SNAP_CREATE: |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 3041 | return btrfs_ioctl_snap_create(root, (void __user *)arg); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3042 | case BTRFS_IOC_DEFRAG: |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 3043 | return btrfs_ioctl_defrag(file); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3044 | case BTRFS_IOC_RESIZE: |
| 3045 | return btrfs_ioctl_resize(root, (void __user *)arg); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3046 | } |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 3047 | |
| 3048 | return -ENOTTY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3049 | } |
| 3050 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3051 | /* |
| 3052 | * Called inside transaction, so use GFP_NOFS |
| 3053 | */ |
| 3054 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 3055 | { |
| 3056 | struct btrfs_inode *ei; |
| 3057 | |
| 3058 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 3059 | if (!ei) |
| 3060 | return NULL; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3061 | ei->last_trans = 0; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3062 | ei->ordered_trans = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3063 | return &ei->vfs_inode; |
| 3064 | } |
| 3065 | |
| 3066 | void btrfs_destroy_inode(struct inode *inode) |
| 3067 | { |
| 3068 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 3069 | WARN_ON(inode->i_data.nrpages); |
| 3070 | |
Chris Mason | 8c416c9 | 2008-01-14 15:10:26 -0500 | [diff] [blame] | 3071 | btrfs_drop_extent_cache(inode, 0, (u64)-1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3072 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 3073 | } |
| 3074 | |
Chris Mason | 44ec0b7 | 2007-10-29 10:55:05 -0400 | [diff] [blame] | 3075 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 3076 | static void init_once(struct kmem_cache * cachep, void *foo) |
| 3077 | #else |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3078 | static void init_once(void * foo, struct kmem_cache * cachep, |
| 3079 | unsigned long flags) |
Chris Mason | 44ec0b7 | 2007-10-29 10:55:05 -0400 | [diff] [blame] | 3080 | #endif |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3081 | { |
| 3082 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 3083 | |
| 3084 | inode_init_once(&ei->vfs_inode); |
| 3085 | } |
| 3086 | |
| 3087 | void btrfs_destroy_cachep(void) |
| 3088 | { |
| 3089 | if (btrfs_inode_cachep) |
| 3090 | kmem_cache_destroy(btrfs_inode_cachep); |
| 3091 | if (btrfs_trans_handle_cachep) |
| 3092 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 3093 | if (btrfs_transaction_cachep) |
| 3094 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 3095 | if (btrfs_bit_radix_cachep) |
| 3096 | kmem_cache_destroy(btrfs_bit_radix_cachep); |
| 3097 | if (btrfs_path_cachep) |
| 3098 | kmem_cache_destroy(btrfs_path_cachep); |
| 3099 | } |
| 3100 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3101 | struct kmem_cache *btrfs_cache_create(const char *name, size_t size, |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3102 | unsigned long extra_flags, |
Chris Mason | 44ec0b7 | 2007-10-29 10:55:05 -0400 | [diff] [blame] | 3103 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 3104 | void (*ctor)(struct kmem_cache *, void *) |
| 3105 | #else |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3106 | void (*ctor)(void *, struct kmem_cache *, |
Chris Mason | 44ec0b7 | 2007-10-29 10:55:05 -0400 | [diff] [blame] | 3107 | unsigned long) |
| 3108 | #endif |
| 3109 | ) |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3110 | { |
| 3111 | return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT | |
| 3112 | SLAB_MEM_SPREAD | extra_flags), ctor |
| 3113 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) |
| 3114 | ,NULL |
| 3115 | #endif |
| 3116 | ); |
| 3117 | } |
| 3118 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3119 | int btrfs_init_cachep(void) |
| 3120 | { |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3121 | btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache", |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3122 | sizeof(struct btrfs_inode), |
| 3123 | 0, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3124 | if (!btrfs_inode_cachep) |
| 3125 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3126 | btrfs_trans_handle_cachep = |
| 3127 | btrfs_cache_create("btrfs_trans_handle_cache", |
| 3128 | sizeof(struct btrfs_trans_handle), |
| 3129 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3130 | if (!btrfs_trans_handle_cachep) |
| 3131 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3132 | btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache", |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3133 | sizeof(struct btrfs_transaction), |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3134 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3135 | if (!btrfs_transaction_cachep) |
| 3136 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3137 | btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache", |
Yan | 2322358 | 2007-09-17 11:08:52 -0400 | [diff] [blame] | 3138 | sizeof(struct btrfs_path), |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3139 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3140 | if (!btrfs_path_cachep) |
| 3141 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3142 | btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256, |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3143 | SLAB_DESTROY_BY_RCU, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3144 | if (!btrfs_bit_radix_cachep) |
| 3145 | goto fail; |
| 3146 | return 0; |
| 3147 | fail: |
| 3148 | btrfs_destroy_cachep(); |
| 3149 | return -ENOMEM; |
| 3150 | } |
| 3151 | |
| 3152 | static int btrfs_getattr(struct vfsmount *mnt, |
| 3153 | struct dentry *dentry, struct kstat *stat) |
| 3154 | { |
| 3155 | struct inode *inode = dentry->d_inode; |
| 3156 | generic_fillattr(inode, stat); |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 3157 | stat->blksize = PAGE_CACHE_SIZE; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3158 | stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3159 | return 0; |
| 3160 | } |
| 3161 | |
| 3162 | static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, |
| 3163 | struct inode * new_dir,struct dentry *new_dentry) |
| 3164 | { |
| 3165 | struct btrfs_trans_handle *trans; |
| 3166 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 3167 | struct inode *new_inode = new_dentry->d_inode; |
| 3168 | struct inode *old_inode = old_dentry->d_inode; |
| 3169 | struct timespec ctime = CURRENT_TIME; |
| 3170 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3171 | int ret; |
| 3172 | |
| 3173 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
| 3174 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { |
| 3175 | return -ENOTEMPTY; |
| 3176 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3177 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3178 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3179 | ret = btrfs_check_free_space(root, 1, 0); |
| 3180 | if (ret) |
| 3181 | goto out_unlock; |
| 3182 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3183 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3184 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3185 | btrfs_set_trans_block_group(trans, new_dir); |
| 3186 | path = btrfs_alloc_path(); |
| 3187 | if (!path) { |
| 3188 | ret = -ENOMEM; |
| 3189 | goto out_fail; |
| 3190 | } |
| 3191 | |
| 3192 | old_dentry->d_inode->i_nlink++; |
| 3193 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 3194 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 3195 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3196 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3197 | ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry); |
| 3198 | if (ret) |
| 3199 | goto out_fail; |
| 3200 | |
| 3201 | if (new_inode) { |
| 3202 | new_inode->i_ctime = CURRENT_TIME; |
| 3203 | ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry); |
| 3204 | if (ret) |
| 3205 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3206 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3207 | ret = btrfs_add_link(trans, new_dentry, old_inode, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3208 | if (ret) |
| 3209 | goto out_fail; |
| 3210 | |
| 3211 | out_fail: |
| 3212 | btrfs_free_path(path); |
| 3213 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3214 | out_unlock: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3215 | mutex_unlock(&root->fs_info->fs_mutex); |
| 3216 | return ret; |
| 3217 | } |
| 3218 | |
| 3219 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 3220 | const char *symname) |
| 3221 | { |
| 3222 | struct btrfs_trans_handle *trans; |
| 3223 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 3224 | struct btrfs_path *path; |
| 3225 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3226 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3227 | int err; |
| 3228 | int drop_inode = 0; |
| 3229 | u64 objectid; |
| 3230 | int name_len; |
| 3231 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3232 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3233 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3234 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3235 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3236 | |
| 3237 | name_len = strlen(symname) + 1; |
| 3238 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 3239 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3240 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3241 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3242 | err = btrfs_check_free_space(root, 1, 0); |
| 3243 | if (err) |
| 3244 | goto out_fail; |
| 3245 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3246 | trans = btrfs_start_transaction(root, 1); |
| 3247 | btrfs_set_trans_block_group(trans, dir); |
| 3248 | |
| 3249 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 3250 | if (err) { |
| 3251 | err = -ENOSPC; |
| 3252 | goto out_unlock; |
| 3253 | } |
| 3254 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3255 | inode = btrfs_new_inode(trans, root, dentry->d_name.name, |
| 3256 | dentry->d_name.len, |
| 3257 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3258 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO); |
| 3259 | err = PTR_ERR(inode); |
| 3260 | if (IS_ERR(inode)) |
| 3261 | goto out_unlock; |
| 3262 | |
| 3263 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3264 | err = btrfs_add_nondir(trans, dentry, inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3265 | if (err) |
| 3266 | drop_inode = 1; |
| 3267 | else { |
| 3268 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3269 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3270 | inode->i_fop = &btrfs_file_operations; |
| 3271 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3272 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 3273 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3274 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3275 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
| 3276 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3277 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3278 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3279 | } |
| 3280 | dir->i_sb->s_dirt = 1; |
| 3281 | btrfs_update_inode_block_group(trans, inode); |
| 3282 | btrfs_update_inode_block_group(trans, dir); |
| 3283 | if (drop_inode) |
| 3284 | goto out_unlock; |
| 3285 | |
| 3286 | path = btrfs_alloc_path(); |
| 3287 | BUG_ON(!path); |
| 3288 | key.objectid = inode->i_ino; |
| 3289 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3290 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 3291 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 3292 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 3293 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3294 | if (err) { |
| 3295 | drop_inode = 1; |
| 3296 | goto out_unlock; |
| 3297 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3298 | leaf = path->nodes[0]; |
| 3299 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 3300 | struct btrfs_file_extent_item); |
| 3301 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 3302 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3303 | BTRFS_FILE_EXTENT_INLINE); |
| 3304 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3305 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 3306 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3307 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3308 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3309 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3310 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3311 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3312 | inode->i_size = name_len - 1; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3313 | err = btrfs_update_inode(trans, root, inode); |
| 3314 | if (err) |
| 3315 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3316 | |
| 3317 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3318 | nr = trans->blocks_used; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3319 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3320 | out_fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3321 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3322 | if (drop_inode) { |
| 3323 | inode_dec_link_count(inode); |
| 3324 | iput(inode); |
| 3325 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3326 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3327 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3328 | return err; |
| 3329 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 3330 | |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3331 | static int btrfs_permission(struct inode *inode, int mask, |
| 3332 | struct nameidata *nd) |
| 3333 | { |
| 3334 | if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE)) |
| 3335 | return -EACCES; |
| 3336 | return generic_permission(inode, mask, NULL); |
| 3337 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3338 | |
| 3339 | static struct inode_operations btrfs_dir_inode_operations = { |
| 3340 | .lookup = btrfs_lookup, |
| 3341 | .create = btrfs_create, |
| 3342 | .unlink = btrfs_unlink, |
| 3343 | .link = btrfs_link, |
| 3344 | .mkdir = btrfs_mkdir, |
| 3345 | .rmdir = btrfs_rmdir, |
| 3346 | .rename = btrfs_rename, |
| 3347 | .symlink = btrfs_symlink, |
| 3348 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3349 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 3350 | .setxattr = generic_setxattr, |
| 3351 | .getxattr = generic_getxattr, |
| 3352 | .listxattr = btrfs_listxattr, |
| 3353 | .removexattr = generic_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3354 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3355 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3356 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
| 3357 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3358 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3359 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3360 | static struct file_operations btrfs_dir_file_operations = { |
| 3361 | .llseek = generic_file_llseek, |
| 3362 | .read = generic_read_dir, |
| 3363 | .readdir = btrfs_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 3364 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3365 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 3366 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3367 | #endif |
| 3368 | }; |
| 3369 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3370 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3371 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 3372 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3373 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3374 | .readpage_io_hook = btrfs_readpage_io_hook, |
| 3375 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3376 | .readpage_io_failed_hook = btrfs_readpage_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 3377 | .set_bit_hook = btrfs_set_bit_hook, |
| 3378 | .clear_bit_hook = btrfs_clear_bit_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3379 | }; |
| 3380 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3381 | static struct address_space_operations btrfs_aops = { |
| 3382 | .readpage = btrfs_readpage, |
| 3383 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 3384 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 3385 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3386 | .sync_page = block_sync_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3387 | .bmap = btrfs_bmap, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 3388 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3389 | .invalidatepage = btrfs_invalidatepage, |
| 3390 | .releasepage = btrfs_releasepage, |
| 3391 | .set_page_dirty = __set_page_dirty_nobuffers, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3392 | }; |
| 3393 | |
| 3394 | static struct address_space_operations btrfs_symlink_aops = { |
| 3395 | .readpage = btrfs_readpage, |
| 3396 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 3397 | .invalidatepage = btrfs_invalidatepage, |
| 3398 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3399 | }; |
| 3400 | |
| 3401 | static struct inode_operations btrfs_file_inode_operations = { |
| 3402 | .truncate = btrfs_truncate, |
| 3403 | .getattr = btrfs_getattr, |
| 3404 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 3405 | .setxattr = generic_setxattr, |
| 3406 | .getxattr = generic_getxattr, |
| 3407 | .listxattr = btrfs_listxattr, |
| 3408 | .removexattr = generic_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3409 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3410 | }; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3411 | static struct inode_operations btrfs_special_inode_operations = { |
| 3412 | .getattr = btrfs_getattr, |
| 3413 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3414 | .permission = btrfs_permission, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3415 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3416 | static struct inode_operations btrfs_symlink_inode_operations = { |
| 3417 | .readlink = generic_readlink, |
| 3418 | .follow_link = page_follow_link_light, |
| 3419 | .put_link = page_put_link, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3420 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3421 | }; |