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