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