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