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