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