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