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