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