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