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