Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
| 19 | #include <linux/sched.h> |
| 20 | #include "ctree.h" |
| 21 | #include "transaction.h" |
| 22 | #include "disk-io.h" |
| 23 | #include "locking.h" |
| 24 | #include "print-tree.h" |
| 25 | #include "compat.h" |
| 26 | |
| 27 | /* magic values for the inode_only field in btrfs_log_inode: |
| 28 | * |
| 29 | * LOG_INODE_ALL means to log everything |
| 30 | * LOG_INODE_EXISTS means to log just enough to recreate the inode |
| 31 | * during log replay |
| 32 | */ |
| 33 | #define LOG_INODE_ALL 0 |
| 34 | #define LOG_INODE_EXISTS 1 |
| 35 | |
| 36 | /* |
| 37 | * stages for the tree walking. The first |
| 38 | * stage (0) is to only pin down the blocks we find |
| 39 | * the second stage (1) is to make sure that all the inodes |
| 40 | * we find in the log are created in the subvolume. |
| 41 | * |
| 42 | * The last stage is to deal with directories and links and extents |
| 43 | * and all the other fun semantics |
| 44 | */ |
| 45 | #define LOG_WALK_PIN_ONLY 0 |
| 46 | #define LOG_WALK_REPLAY_INODES 1 |
| 47 | #define LOG_WALK_REPLAY_ALL 2 |
| 48 | |
| 49 | static int __btrfs_log_inode(struct btrfs_trans_handle *trans, |
| 50 | struct btrfs_root *root, struct inode *inode, |
| 51 | int inode_only); |
| 52 | |
| 53 | /* |
| 54 | * tree logging is a special write ahead log used to make sure that |
| 55 | * fsyncs and O_SYNCs can happen without doing full tree commits. |
| 56 | * |
| 57 | * Full tree commits are expensive because they require commonly |
| 58 | * modified blocks to be recowed, creating many dirty pages in the |
| 59 | * extent tree an 4x-6x higher write load than ext3. |
| 60 | * |
| 61 | * Instead of doing a tree commit on every fsync, we use the |
| 62 | * key ranges and transaction ids to find items for a given file or directory |
| 63 | * that have changed in this transaction. Those items are copied into |
| 64 | * a special tree (one per subvolume root), that tree is written to disk |
| 65 | * and then the fsync is considered complete. |
| 66 | * |
| 67 | * After a crash, items are copied out of the log-tree back into the |
| 68 | * subvolume tree. Any file data extents found are recorded in the extent |
| 69 | * allocation tree, and the log-tree freed. |
| 70 | * |
| 71 | * The log tree is read three times, once to pin down all the extents it is |
| 72 | * using in ram and once, once to create all the inodes logged in the tree |
| 73 | * and once to do all the other items. |
| 74 | */ |
| 75 | |
| 76 | /* |
| 77 | * btrfs_add_log_tree adds a new per-subvolume log tree into the |
| 78 | * tree of log tree roots. This must be called with a tree log transaction |
| 79 | * running (see start_log_trans). |
| 80 | */ |
| 81 | int btrfs_add_log_tree(struct btrfs_trans_handle *trans, |
| 82 | struct btrfs_root *root) |
| 83 | { |
| 84 | struct btrfs_key key; |
| 85 | struct btrfs_root_item root_item; |
| 86 | struct btrfs_inode_item *inode_item; |
| 87 | struct extent_buffer *leaf; |
| 88 | struct btrfs_root *new_root = root; |
| 89 | int ret; |
| 90 | u64 objectid = root->root_key.objectid; |
| 91 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 92 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 93 | BTRFS_TREE_LOG_OBJECTID, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 94 | trans->transid, 0, 0, 0); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 95 | if (IS_ERR(leaf)) { |
| 96 | ret = PTR_ERR(leaf); |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | btrfs_set_header_nritems(leaf, 0); |
| 101 | btrfs_set_header_level(leaf, 0); |
| 102 | btrfs_set_header_bytenr(leaf, leaf->start); |
| 103 | btrfs_set_header_generation(leaf, trans->transid); |
| 104 | btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); |
| 105 | |
| 106 | write_extent_buffer(leaf, root->fs_info->fsid, |
| 107 | (unsigned long)btrfs_header_fsid(leaf), |
| 108 | BTRFS_FSID_SIZE); |
| 109 | btrfs_mark_buffer_dirty(leaf); |
| 110 | |
| 111 | inode_item = &root_item.inode; |
| 112 | memset(inode_item, 0, sizeof(*inode_item)); |
| 113 | inode_item->generation = cpu_to_le64(1); |
| 114 | inode_item->size = cpu_to_le64(3); |
| 115 | inode_item->nlink = cpu_to_le32(1); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 116 | inode_item->nbytes = cpu_to_le64(root->leafsize); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 117 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
| 118 | |
| 119 | btrfs_set_root_bytenr(&root_item, leaf->start); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame^] | 120 | btrfs_set_root_generation(&root_item, trans->transid); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 121 | btrfs_set_root_level(&root_item, 0); |
| 122 | btrfs_set_root_refs(&root_item, 0); |
| 123 | btrfs_set_root_used(&root_item, 0); |
| 124 | |
| 125 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
| 126 | root_item.drop_level = 0; |
| 127 | |
| 128 | btrfs_tree_unlock(leaf); |
| 129 | free_extent_buffer(leaf); |
| 130 | leaf = NULL; |
| 131 | |
| 132 | btrfs_set_root_dirid(&root_item, 0); |
| 133 | |
| 134 | key.objectid = BTRFS_TREE_LOG_OBJECTID; |
| 135 | key.offset = objectid; |
| 136 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 137 | ret = btrfs_insert_root(trans, root->fs_info->log_root_tree, &key, |
| 138 | &root_item); |
| 139 | if (ret) |
| 140 | goto fail; |
| 141 | |
| 142 | new_root = btrfs_read_fs_root_no_radix(root->fs_info->log_root_tree, |
| 143 | &key); |
| 144 | BUG_ON(!new_root); |
| 145 | |
| 146 | WARN_ON(root->log_root); |
| 147 | root->log_root = new_root; |
| 148 | |
| 149 | /* |
| 150 | * log trees do not get reference counted because they go away |
| 151 | * before a real commit is actually done. They do store pointers |
| 152 | * to file data extents, and those reference counts still get |
| 153 | * updated (along with back refs to the log tree). |
| 154 | */ |
| 155 | new_root->ref_cows = 0; |
| 156 | new_root->last_trans = trans->transid; |
| 157 | fail: |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * start a sub transaction and setup the log tree |
| 163 | * this increments the log tree writer count to make the people |
| 164 | * syncing the tree wait for us to finish |
| 165 | */ |
| 166 | static int start_log_trans(struct btrfs_trans_handle *trans, |
| 167 | struct btrfs_root *root) |
| 168 | { |
| 169 | int ret; |
| 170 | mutex_lock(&root->fs_info->tree_log_mutex); |
| 171 | if (!root->fs_info->log_root_tree) { |
| 172 | ret = btrfs_init_log_root_tree(trans, root->fs_info); |
| 173 | BUG_ON(ret); |
| 174 | } |
| 175 | if (!root->log_root) { |
| 176 | ret = btrfs_add_log_tree(trans, root); |
| 177 | BUG_ON(ret); |
| 178 | } |
| 179 | atomic_inc(&root->fs_info->tree_log_writers); |
| 180 | root->fs_info->tree_log_batch++; |
| 181 | mutex_unlock(&root->fs_info->tree_log_mutex); |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * returns 0 if there was a log transaction running and we were able |
| 187 | * to join, or returns -ENOENT if there were not transactions |
| 188 | * in progress |
| 189 | */ |
| 190 | static int join_running_log_trans(struct btrfs_root *root) |
| 191 | { |
| 192 | int ret = -ENOENT; |
| 193 | |
| 194 | smp_mb(); |
| 195 | if (!root->log_root) |
| 196 | return -ENOENT; |
| 197 | |
| 198 | mutex_lock(&root->fs_info->tree_log_mutex); |
| 199 | if (root->log_root) { |
| 200 | ret = 0; |
| 201 | atomic_inc(&root->fs_info->tree_log_writers); |
| 202 | root->fs_info->tree_log_batch++; |
| 203 | } |
| 204 | mutex_unlock(&root->fs_info->tree_log_mutex); |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * indicate we're done making changes to the log tree |
| 210 | * and wake up anyone waiting to do a sync |
| 211 | */ |
| 212 | static int end_log_trans(struct btrfs_root *root) |
| 213 | { |
| 214 | atomic_dec(&root->fs_info->tree_log_writers); |
| 215 | smp_mb(); |
| 216 | if (waitqueue_active(&root->fs_info->tree_log_wait)) |
| 217 | wake_up(&root->fs_info->tree_log_wait); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /* |
| 223 | * the walk control struct is used to pass state down the chain when |
| 224 | * processing the log tree. The stage field tells us which part |
| 225 | * of the log tree processing we are currently doing. The others |
| 226 | * are state fields used for that specific part |
| 227 | */ |
| 228 | struct walk_control { |
| 229 | /* should we free the extent on disk when done? This is used |
| 230 | * at transaction commit time while freeing a log tree |
| 231 | */ |
| 232 | int free; |
| 233 | |
| 234 | /* should we write out the extent buffer? This is used |
| 235 | * while flushing the log tree to disk during a sync |
| 236 | */ |
| 237 | int write; |
| 238 | |
| 239 | /* should we wait for the extent buffer io to finish? Also used |
| 240 | * while flushing the log tree to disk for a sync |
| 241 | */ |
| 242 | int wait; |
| 243 | |
| 244 | /* pin only walk, we record which extents on disk belong to the |
| 245 | * log trees |
| 246 | */ |
| 247 | int pin; |
| 248 | |
| 249 | /* what stage of the replay code we're currently in */ |
| 250 | int stage; |
| 251 | |
| 252 | /* the root we are currently replaying */ |
| 253 | struct btrfs_root *replay_dest; |
| 254 | |
| 255 | /* the trans handle for the current replay */ |
| 256 | struct btrfs_trans_handle *trans; |
| 257 | |
| 258 | /* the function that gets used to process blocks we find in the |
| 259 | * tree. Note the extent_buffer might not be up to date when it is |
| 260 | * passed in, and it must be checked or read if you need the data |
| 261 | * inside it |
| 262 | */ |
| 263 | int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb, |
| 264 | struct walk_control *wc, u64 gen); |
| 265 | }; |
| 266 | |
| 267 | /* |
| 268 | * process_func used to pin down extents, write them or wait on them |
| 269 | */ |
| 270 | static int process_one_buffer(struct btrfs_root *log, |
| 271 | struct extent_buffer *eb, |
| 272 | struct walk_control *wc, u64 gen) |
| 273 | { |
| 274 | if (wc->pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 275 | mutex_lock(&log->fs_info->pinned_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 276 | btrfs_update_pinned_extents(log->fs_info->extent_root, |
| 277 | eb->start, eb->len, 1); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 278 | mutex_unlock(&log->fs_info->pinned_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (btrfs_buffer_uptodate(eb, gen)) { |
| 282 | if (wc->write) |
| 283 | btrfs_write_tree_block(eb); |
| 284 | if (wc->wait) |
| 285 | btrfs_wait_tree_block_writeback(eb); |
| 286 | } |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Item overwrite used by replay and tree logging. eb, slot and key all refer |
| 292 | * to the src data we are copying out. |
| 293 | * |
| 294 | * root is the tree we are copying into, and path is a scratch |
| 295 | * path for use in this function (it should be released on entry and |
| 296 | * will be released on exit). |
| 297 | * |
| 298 | * If the key is already in the destination tree the existing item is |
| 299 | * overwritten. If the existing item isn't big enough, it is extended. |
| 300 | * If it is too large, it is truncated. |
| 301 | * |
| 302 | * If the key isn't in the destination yet, a new item is inserted. |
| 303 | */ |
| 304 | static noinline int overwrite_item(struct btrfs_trans_handle *trans, |
| 305 | struct btrfs_root *root, |
| 306 | struct btrfs_path *path, |
| 307 | struct extent_buffer *eb, int slot, |
| 308 | struct btrfs_key *key) |
| 309 | { |
| 310 | int ret; |
| 311 | u32 item_size; |
| 312 | u64 saved_i_size = 0; |
| 313 | int save_old_i_size = 0; |
| 314 | unsigned long src_ptr; |
| 315 | unsigned long dst_ptr; |
| 316 | int overwrite_root = 0; |
| 317 | |
| 318 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 319 | overwrite_root = 1; |
| 320 | |
| 321 | item_size = btrfs_item_size_nr(eb, slot); |
| 322 | src_ptr = btrfs_item_ptr_offset(eb, slot); |
| 323 | |
| 324 | /* look for the key in the destination tree */ |
| 325 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 326 | if (ret == 0) { |
| 327 | char *src_copy; |
| 328 | char *dst_copy; |
| 329 | u32 dst_size = btrfs_item_size_nr(path->nodes[0], |
| 330 | path->slots[0]); |
| 331 | if (dst_size != item_size) |
| 332 | goto insert; |
| 333 | |
| 334 | if (item_size == 0) { |
| 335 | btrfs_release_path(root, path); |
| 336 | return 0; |
| 337 | } |
| 338 | dst_copy = kmalloc(item_size, GFP_NOFS); |
| 339 | src_copy = kmalloc(item_size, GFP_NOFS); |
| 340 | |
| 341 | read_extent_buffer(eb, src_copy, src_ptr, item_size); |
| 342 | |
| 343 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
| 344 | read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, |
| 345 | item_size); |
| 346 | ret = memcmp(dst_copy, src_copy, item_size); |
| 347 | |
| 348 | kfree(dst_copy); |
| 349 | kfree(src_copy); |
| 350 | /* |
| 351 | * they have the same contents, just return, this saves |
| 352 | * us from cowing blocks in the destination tree and doing |
| 353 | * extra writes that may not have been done by a previous |
| 354 | * sync |
| 355 | */ |
| 356 | if (ret == 0) { |
| 357 | btrfs_release_path(root, path); |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | } |
| 362 | insert: |
| 363 | btrfs_release_path(root, path); |
| 364 | /* try to insert the key into the destination tree */ |
| 365 | ret = btrfs_insert_empty_item(trans, root, path, |
| 366 | key, item_size); |
| 367 | |
| 368 | /* make sure any existing item is the correct size */ |
| 369 | if (ret == -EEXIST) { |
| 370 | u32 found_size; |
| 371 | found_size = btrfs_item_size_nr(path->nodes[0], |
| 372 | path->slots[0]); |
| 373 | if (found_size > item_size) { |
| 374 | btrfs_truncate_item(trans, root, path, item_size, 1); |
| 375 | } else if (found_size < item_size) { |
| 376 | ret = btrfs_del_item(trans, root, |
| 377 | path); |
| 378 | BUG_ON(ret); |
| 379 | |
| 380 | btrfs_release_path(root, path); |
| 381 | ret = btrfs_insert_empty_item(trans, |
| 382 | root, path, key, item_size); |
| 383 | BUG_ON(ret); |
| 384 | } |
| 385 | } else if (ret) { |
| 386 | BUG(); |
| 387 | } |
| 388 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], |
| 389 | path->slots[0]); |
| 390 | |
| 391 | /* don't overwrite an existing inode if the generation number |
| 392 | * was logged as zero. This is done when the tree logging code |
| 393 | * is just logging an inode to make sure it exists after recovery. |
| 394 | * |
| 395 | * Also, don't overwrite i_size on directories during replay. |
| 396 | * log replay inserts and removes directory items based on the |
| 397 | * state of the tree found in the subvolume, and i_size is modified |
| 398 | * as it goes |
| 399 | */ |
| 400 | if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) { |
| 401 | struct btrfs_inode_item *src_item; |
| 402 | struct btrfs_inode_item *dst_item; |
| 403 | |
| 404 | src_item = (struct btrfs_inode_item *)src_ptr; |
| 405 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 406 | |
| 407 | if (btrfs_inode_generation(eb, src_item) == 0) |
| 408 | goto no_copy; |
| 409 | |
| 410 | if (overwrite_root && |
| 411 | S_ISDIR(btrfs_inode_mode(eb, src_item)) && |
| 412 | S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { |
| 413 | save_old_i_size = 1; |
| 414 | saved_i_size = btrfs_inode_size(path->nodes[0], |
| 415 | dst_item); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | copy_extent_buffer(path->nodes[0], eb, dst_ptr, |
| 420 | src_ptr, item_size); |
| 421 | |
| 422 | if (save_old_i_size) { |
| 423 | struct btrfs_inode_item *dst_item; |
| 424 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 425 | btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); |
| 426 | } |
| 427 | |
| 428 | /* make sure the generation is filled in */ |
| 429 | if (key->type == BTRFS_INODE_ITEM_KEY) { |
| 430 | struct btrfs_inode_item *dst_item; |
| 431 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 432 | if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { |
| 433 | btrfs_set_inode_generation(path->nodes[0], dst_item, |
| 434 | trans->transid); |
| 435 | } |
| 436 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 437 | |
| 438 | if (overwrite_root && |
| 439 | key->type == BTRFS_EXTENT_DATA_KEY) { |
| 440 | int extent_type; |
| 441 | struct btrfs_file_extent_item *fi; |
| 442 | |
| 443 | fi = (struct btrfs_file_extent_item *)dst_ptr; |
| 444 | extent_type = btrfs_file_extent_type(path->nodes[0], fi); |
| 445 | if (extent_type == BTRFS_FILE_EXTENT_REG) { |
| 446 | struct btrfs_key ins; |
| 447 | ins.objectid = btrfs_file_extent_disk_bytenr( |
| 448 | path->nodes[0], fi); |
| 449 | ins.offset = btrfs_file_extent_disk_num_bytes( |
| 450 | path->nodes[0], fi); |
| 451 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 452 | |
| 453 | /* |
| 454 | * is this extent already allocated in the extent |
| 455 | * allocation tree? If so, just add a reference |
| 456 | */ |
| 457 | ret = btrfs_lookup_extent(root, ins.objectid, |
| 458 | ins.offset); |
| 459 | if (ret == 0) { |
| 460 | ret = btrfs_inc_extent_ref(trans, root, |
| 461 | ins.objectid, ins.offset, |
| 462 | path->nodes[0]->start, |
| 463 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 464 | trans->transid, key->objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 465 | } else { |
| 466 | /* |
| 467 | * insert the extent pointer in the extent |
| 468 | * allocation tree |
| 469 | */ |
| 470 | ret = btrfs_alloc_logged_extent(trans, root, |
| 471 | path->nodes[0]->start, |
| 472 | root->root_key.objectid, |
| 473 | trans->transid, key->objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 474 | &ins); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 475 | BUG_ON(ret); |
| 476 | } |
| 477 | } |
| 478 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 479 | no_copy: |
| 480 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 481 | btrfs_release_path(root, path); |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * simple helper to read an inode off the disk from a given root |
| 487 | * This can only be called for subvolume roots and not for the log |
| 488 | */ |
| 489 | static noinline struct inode *read_one_inode(struct btrfs_root *root, |
| 490 | u64 objectid) |
| 491 | { |
| 492 | struct inode *inode; |
| 493 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); |
| 494 | if (inode->i_state & I_NEW) { |
| 495 | BTRFS_I(inode)->root = root; |
| 496 | BTRFS_I(inode)->location.objectid = objectid; |
| 497 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 498 | BTRFS_I(inode)->location.offset = 0; |
| 499 | btrfs_read_locked_inode(inode); |
| 500 | unlock_new_inode(inode); |
| 501 | |
| 502 | } |
| 503 | if (is_bad_inode(inode)) { |
| 504 | iput(inode); |
| 505 | inode = NULL; |
| 506 | } |
| 507 | return inode; |
| 508 | } |
| 509 | |
| 510 | /* replays a single extent in 'eb' at 'slot' with 'key' into the |
| 511 | * subvolume 'root'. path is released on entry and should be released |
| 512 | * on exit. |
| 513 | * |
| 514 | * extents in the log tree have not been allocated out of the extent |
| 515 | * tree yet. So, this completes the allocation, taking a reference |
| 516 | * as required if the extent already exists or creating a new extent |
| 517 | * if it isn't in the extent allocation tree yet. |
| 518 | * |
| 519 | * The extent is inserted into the file, dropping any existing extents |
| 520 | * from the file that overlap the new one. |
| 521 | */ |
| 522 | static noinline int replay_one_extent(struct btrfs_trans_handle *trans, |
| 523 | struct btrfs_root *root, |
| 524 | struct btrfs_path *path, |
| 525 | struct extent_buffer *eb, int slot, |
| 526 | struct btrfs_key *key) |
| 527 | { |
| 528 | int found_type; |
| 529 | u64 mask = root->sectorsize - 1; |
| 530 | u64 extent_end; |
| 531 | u64 alloc_hint; |
| 532 | u64 start = key->offset; |
| 533 | struct btrfs_file_extent_item *item; |
| 534 | struct inode *inode = NULL; |
| 535 | unsigned long size; |
| 536 | int ret = 0; |
| 537 | |
| 538 | item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 539 | found_type = btrfs_file_extent_type(eb, item); |
| 540 | |
| 541 | if (found_type == BTRFS_FILE_EXTENT_REG) |
| 542 | extent_end = start + btrfs_file_extent_num_bytes(eb, item); |
| 543 | else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 544 | size = btrfs_file_extent_inline_len(eb, item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 545 | extent_end = (start + size + mask) & ~mask; |
| 546 | } else { |
| 547 | ret = 0; |
| 548 | goto out; |
| 549 | } |
| 550 | |
| 551 | inode = read_one_inode(root, key->objectid); |
| 552 | if (!inode) { |
| 553 | ret = -EIO; |
| 554 | goto out; |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * first check to see if we already have this extent in the |
| 559 | * file. This must be done before the btrfs_drop_extents run |
| 560 | * so we don't try to drop this extent. |
| 561 | */ |
| 562 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 563 | start, 0); |
| 564 | |
| 565 | if (ret == 0 && found_type == BTRFS_FILE_EXTENT_REG) { |
| 566 | struct btrfs_file_extent_item cmp1; |
| 567 | struct btrfs_file_extent_item cmp2; |
| 568 | struct btrfs_file_extent_item *existing; |
| 569 | struct extent_buffer *leaf; |
| 570 | |
| 571 | leaf = path->nodes[0]; |
| 572 | existing = btrfs_item_ptr(leaf, path->slots[0], |
| 573 | struct btrfs_file_extent_item); |
| 574 | |
| 575 | read_extent_buffer(eb, &cmp1, (unsigned long)item, |
| 576 | sizeof(cmp1)); |
| 577 | read_extent_buffer(leaf, &cmp2, (unsigned long)existing, |
| 578 | sizeof(cmp2)); |
| 579 | |
| 580 | /* |
| 581 | * we already have a pointer to this exact extent, |
| 582 | * we don't have to do anything |
| 583 | */ |
| 584 | if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { |
| 585 | btrfs_release_path(root, path); |
| 586 | goto out; |
| 587 | } |
| 588 | } |
| 589 | btrfs_release_path(root, path); |
| 590 | |
| 591 | /* drop any overlapping extents */ |
| 592 | ret = btrfs_drop_extents(trans, root, inode, |
| 593 | start, extent_end, start, &alloc_hint); |
| 594 | BUG_ON(ret); |
| 595 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 596 | /* insert the extent */ |
| 597 | ret = overwrite_item(trans, root, path, eb, slot, key); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 598 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 599 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 600 | /* btrfs_drop_extents changes i_bytes & i_blocks, update it here */ |
| 601 | inode_add_bytes(inode, extent_end - start); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 602 | btrfs_update_inode(trans, root, inode); |
| 603 | out: |
| 604 | if (inode) |
| 605 | iput(inode); |
| 606 | return ret; |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * when cleaning up conflicts between the directory names in the |
| 611 | * subvolume, directory names in the log and directory names in the |
| 612 | * inode back references, we may have to unlink inodes from directories. |
| 613 | * |
| 614 | * This is a helper function to do the unlink of a specific directory |
| 615 | * item |
| 616 | */ |
| 617 | static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, |
| 618 | struct btrfs_root *root, |
| 619 | struct btrfs_path *path, |
| 620 | struct inode *dir, |
| 621 | struct btrfs_dir_item *di) |
| 622 | { |
| 623 | struct inode *inode; |
| 624 | char *name; |
| 625 | int name_len; |
| 626 | struct extent_buffer *leaf; |
| 627 | struct btrfs_key location; |
| 628 | int ret; |
| 629 | |
| 630 | leaf = path->nodes[0]; |
| 631 | |
| 632 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
| 633 | name_len = btrfs_dir_name_len(leaf, di); |
| 634 | name = kmalloc(name_len, GFP_NOFS); |
| 635 | read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len); |
| 636 | btrfs_release_path(root, path); |
| 637 | |
| 638 | inode = read_one_inode(root, location.objectid); |
| 639 | BUG_ON(!inode); |
| 640 | |
| 641 | btrfs_inc_nlink(inode); |
| 642 | ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 643 | kfree(name); |
| 644 | |
| 645 | iput(inode); |
| 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * helper function to see if a given name and sequence number found |
| 651 | * in an inode back reference are already in a directory and correctly |
| 652 | * point to this inode |
| 653 | */ |
| 654 | static noinline int inode_in_dir(struct btrfs_root *root, |
| 655 | struct btrfs_path *path, |
| 656 | u64 dirid, u64 objectid, u64 index, |
| 657 | const char *name, int name_len) |
| 658 | { |
| 659 | struct btrfs_dir_item *di; |
| 660 | struct btrfs_key location; |
| 661 | int match = 0; |
| 662 | |
| 663 | di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, |
| 664 | index, name, name_len, 0); |
| 665 | if (di && !IS_ERR(di)) { |
| 666 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
| 667 | if (location.objectid != objectid) |
| 668 | goto out; |
| 669 | } else |
| 670 | goto out; |
| 671 | btrfs_release_path(root, path); |
| 672 | |
| 673 | di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0); |
| 674 | if (di && !IS_ERR(di)) { |
| 675 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
| 676 | if (location.objectid != objectid) |
| 677 | goto out; |
| 678 | } else |
| 679 | goto out; |
| 680 | match = 1; |
| 681 | out: |
| 682 | btrfs_release_path(root, path); |
| 683 | return match; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * helper function to check a log tree for a named back reference in |
| 688 | * an inode. This is used to decide if a back reference that is |
| 689 | * found in the subvolume conflicts with what we find in the log. |
| 690 | * |
| 691 | * inode backreferences may have multiple refs in a single item, |
| 692 | * during replay we process one reference at a time, and we don't |
| 693 | * want to delete valid links to a file from the subvolume if that |
| 694 | * link is also in the log. |
| 695 | */ |
| 696 | static noinline int backref_in_log(struct btrfs_root *log, |
| 697 | struct btrfs_key *key, |
| 698 | char *name, int namelen) |
| 699 | { |
| 700 | struct btrfs_path *path; |
| 701 | struct btrfs_inode_ref *ref; |
| 702 | unsigned long ptr; |
| 703 | unsigned long ptr_end; |
| 704 | unsigned long name_ptr; |
| 705 | int found_name_len; |
| 706 | int item_size; |
| 707 | int ret; |
| 708 | int match = 0; |
| 709 | |
| 710 | path = btrfs_alloc_path(); |
| 711 | ret = btrfs_search_slot(NULL, log, key, path, 0, 0); |
| 712 | if (ret != 0) |
| 713 | goto out; |
| 714 | |
| 715 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); |
| 716 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
| 717 | ptr_end = ptr + item_size; |
| 718 | while (ptr < ptr_end) { |
| 719 | ref = (struct btrfs_inode_ref *)ptr; |
| 720 | found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref); |
| 721 | if (found_name_len == namelen) { |
| 722 | name_ptr = (unsigned long)(ref + 1); |
| 723 | ret = memcmp_extent_buffer(path->nodes[0], name, |
| 724 | name_ptr, namelen); |
| 725 | if (ret == 0) { |
| 726 | match = 1; |
| 727 | goto out; |
| 728 | } |
| 729 | } |
| 730 | ptr = (unsigned long)(ref + 1) + found_name_len; |
| 731 | } |
| 732 | out: |
| 733 | btrfs_free_path(path); |
| 734 | return match; |
| 735 | } |
| 736 | |
| 737 | |
| 738 | /* |
| 739 | * replay one inode back reference item found in the log tree. |
| 740 | * eb, slot and key refer to the buffer and key found in the log tree. |
| 741 | * root is the destination we are replaying into, and path is for temp |
| 742 | * use by this function. (it should be released on return). |
| 743 | */ |
| 744 | static noinline int add_inode_ref(struct btrfs_trans_handle *trans, |
| 745 | struct btrfs_root *root, |
| 746 | struct btrfs_root *log, |
| 747 | struct btrfs_path *path, |
| 748 | struct extent_buffer *eb, int slot, |
| 749 | struct btrfs_key *key) |
| 750 | { |
| 751 | struct inode *dir; |
| 752 | int ret; |
| 753 | struct btrfs_key location; |
| 754 | struct btrfs_inode_ref *ref; |
| 755 | struct btrfs_dir_item *di; |
| 756 | struct inode *inode; |
| 757 | char *name; |
| 758 | int namelen; |
| 759 | unsigned long ref_ptr; |
| 760 | unsigned long ref_end; |
| 761 | |
| 762 | location.objectid = key->objectid; |
| 763 | location.type = BTRFS_INODE_ITEM_KEY; |
| 764 | location.offset = 0; |
| 765 | |
| 766 | /* |
| 767 | * it is possible that we didn't log all the parent directories |
| 768 | * for a given inode. If we don't find the dir, just don't |
| 769 | * copy the back ref in. The link count fixup code will take |
| 770 | * care of the rest |
| 771 | */ |
| 772 | dir = read_one_inode(root, key->offset); |
| 773 | if (!dir) |
| 774 | return -ENOENT; |
| 775 | |
| 776 | inode = read_one_inode(root, key->objectid); |
| 777 | BUG_ON(!dir); |
| 778 | |
| 779 | ref_ptr = btrfs_item_ptr_offset(eb, slot); |
| 780 | ref_end = ref_ptr + btrfs_item_size_nr(eb, slot); |
| 781 | |
| 782 | again: |
| 783 | ref = (struct btrfs_inode_ref *)ref_ptr; |
| 784 | |
| 785 | namelen = btrfs_inode_ref_name_len(eb, ref); |
| 786 | name = kmalloc(namelen, GFP_NOFS); |
| 787 | BUG_ON(!name); |
| 788 | |
| 789 | read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen); |
| 790 | |
| 791 | /* if we already have a perfect match, we're done */ |
| 792 | if (inode_in_dir(root, path, dir->i_ino, inode->i_ino, |
| 793 | btrfs_inode_ref_index(eb, ref), |
| 794 | name, namelen)) { |
| 795 | goto out; |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * look for a conflicting back reference in the metadata. |
| 800 | * if we find one we have to unlink that name of the file |
| 801 | * before we add our new link. Later on, we overwrite any |
| 802 | * existing back reference, and we don't want to create |
| 803 | * dangling pointers in the directory. |
| 804 | */ |
| 805 | conflict_again: |
| 806 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 807 | if (ret == 0) { |
| 808 | char *victim_name; |
| 809 | int victim_name_len; |
| 810 | struct btrfs_inode_ref *victim_ref; |
| 811 | unsigned long ptr; |
| 812 | unsigned long ptr_end; |
| 813 | struct extent_buffer *leaf = path->nodes[0]; |
| 814 | |
| 815 | /* are we trying to overwrite a back ref for the root directory |
| 816 | * if so, just jump out, we're done |
| 817 | */ |
| 818 | if (key->objectid == key->offset) |
| 819 | goto out_nowrite; |
| 820 | |
| 821 | /* check all the names in this back reference to see |
| 822 | * if they are in the log. if so, we allow them to stay |
| 823 | * otherwise they must be unlinked as a conflict |
| 824 | */ |
| 825 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 826 | ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]); |
| 827 | while(ptr < ptr_end) { |
| 828 | victim_ref = (struct btrfs_inode_ref *)ptr; |
| 829 | victim_name_len = btrfs_inode_ref_name_len(leaf, |
| 830 | victim_ref); |
| 831 | victim_name = kmalloc(victim_name_len, GFP_NOFS); |
| 832 | BUG_ON(!victim_name); |
| 833 | |
| 834 | read_extent_buffer(leaf, victim_name, |
| 835 | (unsigned long)(victim_ref + 1), |
| 836 | victim_name_len); |
| 837 | |
| 838 | if (!backref_in_log(log, key, victim_name, |
| 839 | victim_name_len)) { |
| 840 | btrfs_inc_nlink(inode); |
| 841 | btrfs_release_path(root, path); |
| 842 | ret = btrfs_unlink_inode(trans, root, dir, |
| 843 | inode, victim_name, |
| 844 | victim_name_len); |
| 845 | kfree(victim_name); |
| 846 | btrfs_release_path(root, path); |
| 847 | goto conflict_again; |
| 848 | } |
| 849 | kfree(victim_name); |
| 850 | ptr = (unsigned long)(victim_ref + 1) + victim_name_len; |
| 851 | } |
| 852 | BUG_ON(ret); |
| 853 | } |
| 854 | btrfs_release_path(root, path); |
| 855 | |
| 856 | /* look for a conflicting sequence number */ |
| 857 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 858 | btrfs_inode_ref_index(eb, ref), |
| 859 | name, namelen, 0); |
| 860 | if (di && !IS_ERR(di)) { |
| 861 | ret = drop_one_dir_item(trans, root, path, dir, di); |
| 862 | BUG_ON(ret); |
| 863 | } |
| 864 | btrfs_release_path(root, path); |
| 865 | |
| 866 | |
| 867 | /* look for a conflicting name */ |
| 868 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 869 | name, namelen, 0); |
| 870 | if (di && !IS_ERR(di)) { |
| 871 | ret = drop_one_dir_item(trans, root, path, dir, di); |
| 872 | BUG_ON(ret); |
| 873 | } |
| 874 | btrfs_release_path(root, path); |
| 875 | |
| 876 | /* insert our name */ |
| 877 | ret = btrfs_add_link(trans, dir, inode, name, namelen, 0, |
| 878 | btrfs_inode_ref_index(eb, ref)); |
| 879 | BUG_ON(ret); |
| 880 | |
| 881 | btrfs_update_inode(trans, root, inode); |
| 882 | |
| 883 | out: |
| 884 | ref_ptr = (unsigned long)(ref + 1) + namelen; |
| 885 | kfree(name); |
| 886 | if (ref_ptr < ref_end) |
| 887 | goto again; |
| 888 | |
| 889 | /* finally write the back reference in the inode */ |
| 890 | ret = overwrite_item(trans, root, path, eb, slot, key); |
| 891 | BUG_ON(ret); |
| 892 | |
| 893 | out_nowrite: |
| 894 | btrfs_release_path(root, path); |
| 895 | iput(dir); |
| 896 | iput(inode); |
| 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * replay one csum item from the log tree into the subvolume 'root' |
| 902 | * eb, slot and key all refer to the log tree |
| 903 | * path is for temp use by this function and should be released on return |
| 904 | * |
| 905 | * This copies the checksums out of the log tree and inserts them into |
| 906 | * the subvolume. Any existing checksums for this range in the file |
| 907 | * are overwritten, and new items are added where required. |
| 908 | * |
| 909 | * We keep this simple by reusing the btrfs_ordered_sum code from |
| 910 | * the data=ordered mode. This basically means making a copy |
| 911 | * of all the checksums in ram, which we have to do anyway for kmap |
| 912 | * rules. |
| 913 | * |
| 914 | * The copy is then sent down to btrfs_csum_file_blocks, which |
| 915 | * does all the hard work of finding existing items in the file |
| 916 | * or adding new ones. |
| 917 | */ |
| 918 | static noinline int replay_one_csum(struct btrfs_trans_handle *trans, |
| 919 | struct btrfs_root *root, |
| 920 | struct btrfs_path *path, |
| 921 | struct extent_buffer *eb, int slot, |
| 922 | struct btrfs_key *key) |
| 923 | { |
| 924 | int ret; |
| 925 | u32 item_size = btrfs_item_size_nr(eb, slot); |
| 926 | u64 cur_offset; |
| 927 | unsigned long file_bytes; |
| 928 | struct btrfs_ordered_sum *sums; |
| 929 | struct btrfs_sector_sum *sector_sum; |
| 930 | struct inode *inode; |
| 931 | unsigned long ptr; |
| 932 | |
| 933 | file_bytes = (item_size / BTRFS_CRC32_SIZE) * root->sectorsize; |
| 934 | inode = read_one_inode(root, key->objectid); |
| 935 | if (!inode) { |
| 936 | return -EIO; |
| 937 | } |
| 938 | |
| 939 | sums = kzalloc(btrfs_ordered_sum_size(root, file_bytes), GFP_NOFS); |
| 940 | if (!sums) { |
| 941 | iput(inode); |
| 942 | return -ENOMEM; |
| 943 | } |
| 944 | |
| 945 | INIT_LIST_HEAD(&sums->list); |
| 946 | sums->len = file_bytes; |
| 947 | sums->file_offset = key->offset; |
| 948 | |
| 949 | /* |
| 950 | * copy all the sums into the ordered sum struct |
| 951 | */ |
| 952 | sector_sum = sums->sums; |
| 953 | cur_offset = key->offset; |
| 954 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 955 | while(item_size > 0) { |
| 956 | sector_sum->offset = cur_offset; |
| 957 | read_extent_buffer(eb, §or_sum->sum, ptr, BTRFS_CRC32_SIZE); |
| 958 | sector_sum++; |
| 959 | item_size -= BTRFS_CRC32_SIZE; |
| 960 | ptr += BTRFS_CRC32_SIZE; |
| 961 | cur_offset += root->sectorsize; |
| 962 | } |
| 963 | |
| 964 | /* let btrfs_csum_file_blocks add them into the file */ |
| 965 | ret = btrfs_csum_file_blocks(trans, root, inode, sums); |
| 966 | BUG_ON(ret); |
| 967 | kfree(sums); |
| 968 | iput(inode); |
| 969 | |
| 970 | return 0; |
| 971 | } |
| 972 | /* |
| 973 | * There are a few corners where the link count of the file can't |
| 974 | * be properly maintained during replay. So, instead of adding |
| 975 | * lots of complexity to the log code, we just scan the backrefs |
| 976 | * for any file that has been through replay. |
| 977 | * |
| 978 | * The scan will update the link count on the inode to reflect the |
| 979 | * number of back refs found. If it goes down to zero, the iput |
| 980 | * will free the inode. |
| 981 | */ |
| 982 | static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, |
| 983 | struct btrfs_root *root, |
| 984 | struct inode *inode) |
| 985 | { |
| 986 | struct btrfs_path *path; |
| 987 | int ret; |
| 988 | struct btrfs_key key; |
| 989 | u64 nlink = 0; |
| 990 | unsigned long ptr; |
| 991 | unsigned long ptr_end; |
| 992 | int name_len; |
| 993 | |
| 994 | key.objectid = inode->i_ino; |
| 995 | key.type = BTRFS_INODE_REF_KEY; |
| 996 | key.offset = (u64)-1; |
| 997 | |
| 998 | path = btrfs_alloc_path(); |
| 999 | |
| 1000 | while(1) { |
| 1001 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1002 | if (ret < 0) |
| 1003 | break; |
| 1004 | if (ret > 0) { |
| 1005 | if (path->slots[0] == 0) |
| 1006 | break; |
| 1007 | path->slots[0]--; |
| 1008 | } |
| 1009 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1010 | path->slots[0]); |
| 1011 | if (key.objectid != inode->i_ino || |
| 1012 | key.type != BTRFS_INODE_REF_KEY) |
| 1013 | break; |
| 1014 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
| 1015 | ptr_end = ptr + btrfs_item_size_nr(path->nodes[0], |
| 1016 | path->slots[0]); |
| 1017 | while(ptr < ptr_end) { |
| 1018 | struct btrfs_inode_ref *ref; |
| 1019 | |
| 1020 | ref = (struct btrfs_inode_ref *)ptr; |
| 1021 | name_len = btrfs_inode_ref_name_len(path->nodes[0], |
| 1022 | ref); |
| 1023 | ptr = (unsigned long)(ref + 1) + name_len; |
| 1024 | nlink++; |
| 1025 | } |
| 1026 | |
| 1027 | if (key.offset == 0) |
| 1028 | break; |
| 1029 | key.offset--; |
| 1030 | btrfs_release_path(root, path); |
| 1031 | } |
| 1032 | btrfs_free_path(path); |
| 1033 | if (nlink != inode->i_nlink) { |
| 1034 | inode->i_nlink = nlink; |
| 1035 | btrfs_update_inode(trans, root, inode); |
| 1036 | } |
Chris Mason | 8d5bf1c | 2008-09-11 15:51:21 -0400 | [diff] [blame] | 1037 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1038 | |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, |
| 1043 | struct btrfs_root *root, |
| 1044 | struct btrfs_path *path) |
| 1045 | { |
| 1046 | int ret; |
| 1047 | struct btrfs_key key; |
| 1048 | struct inode *inode; |
| 1049 | |
| 1050 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; |
| 1051 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
| 1052 | key.offset = (u64)-1; |
| 1053 | while(1) { |
| 1054 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1055 | if (ret < 0) |
| 1056 | break; |
| 1057 | |
| 1058 | if (ret == 1) { |
| 1059 | if (path->slots[0] == 0) |
| 1060 | break; |
| 1061 | path->slots[0]--; |
| 1062 | } |
| 1063 | |
| 1064 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 1065 | if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || |
| 1066 | key.type != BTRFS_ORPHAN_ITEM_KEY) |
| 1067 | break; |
| 1068 | |
| 1069 | ret = btrfs_del_item(trans, root, path); |
| 1070 | BUG_ON(ret); |
| 1071 | |
| 1072 | btrfs_release_path(root, path); |
| 1073 | inode = read_one_inode(root, key.offset); |
| 1074 | BUG_ON(!inode); |
| 1075 | |
| 1076 | ret = fixup_inode_link_count(trans, root, inode); |
| 1077 | BUG_ON(ret); |
| 1078 | |
| 1079 | iput(inode); |
| 1080 | |
| 1081 | if (key.offset == 0) |
| 1082 | break; |
| 1083 | key.offset--; |
| 1084 | } |
| 1085 | btrfs_release_path(root, path); |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | |
| 1090 | /* |
| 1091 | * record a given inode in the fixup dir so we can check its link |
| 1092 | * count when replay is done. The link count is incremented here |
| 1093 | * so the inode won't go away until we check it |
| 1094 | */ |
| 1095 | static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, |
| 1096 | struct btrfs_root *root, |
| 1097 | struct btrfs_path *path, |
| 1098 | u64 objectid) |
| 1099 | { |
| 1100 | struct btrfs_key key; |
| 1101 | int ret = 0; |
| 1102 | struct inode *inode; |
| 1103 | |
| 1104 | inode = read_one_inode(root, objectid); |
| 1105 | BUG_ON(!inode); |
| 1106 | |
| 1107 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; |
| 1108 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 1109 | key.offset = objectid; |
| 1110 | |
| 1111 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
| 1112 | |
| 1113 | btrfs_release_path(root, path); |
| 1114 | if (ret == 0) { |
| 1115 | btrfs_inc_nlink(inode); |
| 1116 | btrfs_update_inode(trans, root, inode); |
| 1117 | } else if (ret == -EEXIST) { |
| 1118 | ret = 0; |
| 1119 | } else { |
| 1120 | BUG(); |
| 1121 | } |
| 1122 | iput(inode); |
| 1123 | |
| 1124 | return ret; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * when replaying the log for a directory, we only insert names |
| 1129 | * for inodes that actually exist. This means an fsync on a directory |
| 1130 | * does not implicitly fsync all the new files in it |
| 1131 | */ |
| 1132 | static noinline int insert_one_name(struct btrfs_trans_handle *trans, |
| 1133 | struct btrfs_root *root, |
| 1134 | struct btrfs_path *path, |
| 1135 | u64 dirid, u64 index, |
| 1136 | char *name, int name_len, u8 type, |
| 1137 | struct btrfs_key *location) |
| 1138 | { |
| 1139 | struct inode *inode; |
| 1140 | struct inode *dir; |
| 1141 | int ret; |
| 1142 | |
| 1143 | inode = read_one_inode(root, location->objectid); |
| 1144 | if (!inode) |
| 1145 | return -ENOENT; |
| 1146 | |
| 1147 | dir = read_one_inode(root, dirid); |
| 1148 | if (!dir) { |
| 1149 | iput(inode); |
| 1150 | return -EIO; |
| 1151 | } |
| 1152 | ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index); |
| 1153 | |
| 1154 | /* FIXME, put inode into FIXUP list */ |
| 1155 | |
| 1156 | iput(inode); |
| 1157 | iput(dir); |
| 1158 | return ret; |
| 1159 | } |
| 1160 | |
| 1161 | /* |
| 1162 | * take a single entry in a log directory item and replay it into |
| 1163 | * the subvolume. |
| 1164 | * |
| 1165 | * if a conflicting item exists in the subdirectory already, |
| 1166 | * the inode it points to is unlinked and put into the link count |
| 1167 | * fix up tree. |
| 1168 | * |
| 1169 | * If a name from the log points to a file or directory that does |
| 1170 | * not exist in the FS, it is skipped. fsyncs on directories |
| 1171 | * do not force down inodes inside that directory, just changes to the |
| 1172 | * names or unlinks in a directory. |
| 1173 | */ |
| 1174 | static noinline int replay_one_name(struct btrfs_trans_handle *trans, |
| 1175 | struct btrfs_root *root, |
| 1176 | struct btrfs_path *path, |
| 1177 | struct extent_buffer *eb, |
| 1178 | struct btrfs_dir_item *di, |
| 1179 | struct btrfs_key *key) |
| 1180 | { |
| 1181 | char *name; |
| 1182 | int name_len; |
| 1183 | struct btrfs_dir_item *dst_di; |
| 1184 | struct btrfs_key found_key; |
| 1185 | struct btrfs_key log_key; |
| 1186 | struct inode *dir; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1187 | u8 log_type; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1188 | int exists; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1189 | int ret; |
| 1190 | |
| 1191 | dir = read_one_inode(root, key->objectid); |
| 1192 | BUG_ON(!dir); |
| 1193 | |
| 1194 | name_len = btrfs_dir_name_len(eb, di); |
| 1195 | name = kmalloc(name_len, GFP_NOFS); |
| 1196 | log_type = btrfs_dir_type(eb, di); |
| 1197 | read_extent_buffer(eb, name, (unsigned long)(di + 1), |
| 1198 | name_len); |
| 1199 | |
| 1200 | btrfs_dir_item_key_to_cpu(eb, di, &log_key); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1201 | exists = btrfs_lookup_inode(trans, root, path, &log_key, 0); |
| 1202 | if (exists == 0) |
| 1203 | exists = 1; |
| 1204 | else |
| 1205 | exists = 0; |
| 1206 | btrfs_release_path(root, path); |
| 1207 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1208 | if (key->type == BTRFS_DIR_ITEM_KEY) { |
| 1209 | dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, |
| 1210 | name, name_len, 1); |
| 1211 | } |
| 1212 | else if (key->type == BTRFS_DIR_INDEX_KEY) { |
| 1213 | dst_di = btrfs_lookup_dir_index_item(trans, root, path, |
| 1214 | key->objectid, |
| 1215 | key->offset, name, |
| 1216 | name_len, 1); |
| 1217 | } else { |
| 1218 | BUG(); |
| 1219 | } |
| 1220 | if (!dst_di || IS_ERR(dst_di)) { |
| 1221 | /* we need a sequence number to insert, so we only |
| 1222 | * do inserts for the BTRFS_DIR_INDEX_KEY types |
| 1223 | */ |
| 1224 | if (key->type != BTRFS_DIR_INDEX_KEY) |
| 1225 | goto out; |
| 1226 | goto insert; |
| 1227 | } |
| 1228 | |
| 1229 | btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); |
| 1230 | /* the existing item matches the logged item */ |
| 1231 | if (found_key.objectid == log_key.objectid && |
| 1232 | found_key.type == log_key.type && |
| 1233 | found_key.offset == log_key.offset && |
| 1234 | btrfs_dir_type(path->nodes[0], dst_di) == log_type) { |
| 1235 | goto out; |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * don't drop the conflicting directory entry if the inode |
| 1240 | * for the new entry doesn't exist |
| 1241 | */ |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1242 | if (!exists) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1243 | goto out; |
| 1244 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1245 | ret = drop_one_dir_item(trans, root, path, dir, dst_di); |
| 1246 | BUG_ON(ret); |
| 1247 | |
| 1248 | if (key->type == BTRFS_DIR_INDEX_KEY) |
| 1249 | goto insert; |
| 1250 | out: |
| 1251 | btrfs_release_path(root, path); |
| 1252 | kfree(name); |
| 1253 | iput(dir); |
| 1254 | return 0; |
| 1255 | |
| 1256 | insert: |
| 1257 | btrfs_release_path(root, path); |
| 1258 | ret = insert_one_name(trans, root, path, key->objectid, key->offset, |
| 1259 | name, name_len, log_type, &log_key); |
| 1260 | |
| 1261 | if (ret && ret != -ENOENT) |
| 1262 | BUG(); |
| 1263 | goto out; |
| 1264 | } |
| 1265 | |
| 1266 | /* |
| 1267 | * find all the names in a directory item and reconcile them into |
| 1268 | * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than |
| 1269 | * one name in a directory item, but the same code gets used for |
| 1270 | * both directory index types |
| 1271 | */ |
| 1272 | static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, |
| 1273 | struct btrfs_root *root, |
| 1274 | struct btrfs_path *path, |
| 1275 | struct extent_buffer *eb, int slot, |
| 1276 | struct btrfs_key *key) |
| 1277 | { |
| 1278 | int ret; |
| 1279 | u32 item_size = btrfs_item_size_nr(eb, slot); |
| 1280 | struct btrfs_dir_item *di; |
| 1281 | int name_len; |
| 1282 | unsigned long ptr; |
| 1283 | unsigned long ptr_end; |
| 1284 | |
| 1285 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 1286 | ptr_end = ptr + item_size; |
| 1287 | while(ptr < ptr_end) { |
| 1288 | di = (struct btrfs_dir_item *)ptr; |
| 1289 | name_len = btrfs_dir_name_len(eb, di); |
| 1290 | ret = replay_one_name(trans, root, path, eb, di, key); |
| 1291 | BUG_ON(ret); |
| 1292 | ptr = (unsigned long)(di + 1); |
| 1293 | ptr += name_len; |
| 1294 | } |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
| 1299 | * directory replay has two parts. There are the standard directory |
| 1300 | * items in the log copied from the subvolume, and range items |
| 1301 | * created in the log while the subvolume was logged. |
| 1302 | * |
| 1303 | * The range items tell us which parts of the key space the log |
| 1304 | * is authoritative for. During replay, if a key in the subvolume |
| 1305 | * directory is in a logged range item, but not actually in the log |
| 1306 | * that means it was deleted from the directory before the fsync |
| 1307 | * and should be removed. |
| 1308 | */ |
| 1309 | static noinline int find_dir_range(struct btrfs_root *root, |
| 1310 | struct btrfs_path *path, |
| 1311 | u64 dirid, int key_type, |
| 1312 | u64 *start_ret, u64 *end_ret) |
| 1313 | { |
| 1314 | struct btrfs_key key; |
| 1315 | u64 found_end; |
| 1316 | struct btrfs_dir_log_item *item; |
| 1317 | int ret; |
| 1318 | int nritems; |
| 1319 | |
| 1320 | if (*start_ret == (u64)-1) |
| 1321 | return 1; |
| 1322 | |
| 1323 | key.objectid = dirid; |
| 1324 | key.type = key_type; |
| 1325 | key.offset = *start_ret; |
| 1326 | |
| 1327 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1328 | if (ret < 0) |
| 1329 | goto out; |
| 1330 | if (ret > 0) { |
| 1331 | if (path->slots[0] == 0) |
| 1332 | goto out; |
| 1333 | path->slots[0]--; |
| 1334 | } |
| 1335 | if (ret != 0) |
| 1336 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 1337 | |
| 1338 | if (key.type != key_type || key.objectid != dirid) { |
| 1339 | ret = 1; |
| 1340 | goto next; |
| 1341 | } |
| 1342 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1343 | struct btrfs_dir_log_item); |
| 1344 | found_end = btrfs_dir_log_end(path->nodes[0], item); |
| 1345 | |
| 1346 | if (*start_ret >= key.offset && *start_ret <= found_end) { |
| 1347 | ret = 0; |
| 1348 | *start_ret = key.offset; |
| 1349 | *end_ret = found_end; |
| 1350 | goto out; |
| 1351 | } |
| 1352 | ret = 1; |
| 1353 | next: |
| 1354 | /* check the next slot in the tree to see if it is a valid item */ |
| 1355 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 1356 | if (path->slots[0] >= nritems) { |
| 1357 | ret = btrfs_next_leaf(root, path); |
| 1358 | if (ret) |
| 1359 | goto out; |
| 1360 | } else { |
| 1361 | path->slots[0]++; |
| 1362 | } |
| 1363 | |
| 1364 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 1365 | |
| 1366 | if (key.type != key_type || key.objectid != dirid) { |
| 1367 | ret = 1; |
| 1368 | goto out; |
| 1369 | } |
| 1370 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1371 | struct btrfs_dir_log_item); |
| 1372 | found_end = btrfs_dir_log_end(path->nodes[0], item); |
| 1373 | *start_ret = key.offset; |
| 1374 | *end_ret = found_end; |
| 1375 | ret = 0; |
| 1376 | out: |
| 1377 | btrfs_release_path(root, path); |
| 1378 | return ret; |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * this looks for a given directory item in the log. If the directory |
| 1383 | * item is not in the log, the item is removed and the inode it points |
| 1384 | * to is unlinked |
| 1385 | */ |
| 1386 | static noinline int check_item_in_log(struct btrfs_trans_handle *trans, |
| 1387 | struct btrfs_root *root, |
| 1388 | struct btrfs_root *log, |
| 1389 | struct btrfs_path *path, |
| 1390 | struct btrfs_path *log_path, |
| 1391 | struct inode *dir, |
| 1392 | struct btrfs_key *dir_key) |
| 1393 | { |
| 1394 | int ret; |
| 1395 | struct extent_buffer *eb; |
| 1396 | int slot; |
| 1397 | u32 item_size; |
| 1398 | struct btrfs_dir_item *di; |
| 1399 | struct btrfs_dir_item *log_di; |
| 1400 | int name_len; |
| 1401 | unsigned long ptr; |
| 1402 | unsigned long ptr_end; |
| 1403 | char *name; |
| 1404 | struct inode *inode; |
| 1405 | struct btrfs_key location; |
| 1406 | |
| 1407 | again: |
| 1408 | eb = path->nodes[0]; |
| 1409 | slot = path->slots[0]; |
| 1410 | item_size = btrfs_item_size_nr(eb, slot); |
| 1411 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 1412 | ptr_end = ptr + item_size; |
| 1413 | while(ptr < ptr_end) { |
| 1414 | di = (struct btrfs_dir_item *)ptr; |
| 1415 | name_len = btrfs_dir_name_len(eb, di); |
| 1416 | name = kmalloc(name_len, GFP_NOFS); |
| 1417 | if (!name) { |
| 1418 | ret = -ENOMEM; |
| 1419 | goto out; |
| 1420 | } |
| 1421 | read_extent_buffer(eb, name, (unsigned long)(di + 1), |
| 1422 | name_len); |
| 1423 | log_di = NULL; |
| 1424 | if (dir_key->type == BTRFS_DIR_ITEM_KEY) { |
| 1425 | log_di = btrfs_lookup_dir_item(trans, log, log_path, |
| 1426 | dir_key->objectid, |
| 1427 | name, name_len, 0); |
| 1428 | } else if (dir_key->type == BTRFS_DIR_INDEX_KEY) { |
| 1429 | log_di = btrfs_lookup_dir_index_item(trans, log, |
| 1430 | log_path, |
| 1431 | dir_key->objectid, |
| 1432 | dir_key->offset, |
| 1433 | name, name_len, 0); |
| 1434 | } |
| 1435 | if (!log_di || IS_ERR(log_di)) { |
| 1436 | btrfs_dir_item_key_to_cpu(eb, di, &location); |
| 1437 | btrfs_release_path(root, path); |
| 1438 | btrfs_release_path(log, log_path); |
| 1439 | inode = read_one_inode(root, location.objectid); |
| 1440 | BUG_ON(!inode); |
| 1441 | |
| 1442 | ret = link_to_fixup_dir(trans, root, |
| 1443 | path, location.objectid); |
| 1444 | BUG_ON(ret); |
| 1445 | btrfs_inc_nlink(inode); |
| 1446 | ret = btrfs_unlink_inode(trans, root, dir, inode, |
| 1447 | name, name_len); |
| 1448 | BUG_ON(ret); |
| 1449 | kfree(name); |
| 1450 | iput(inode); |
| 1451 | |
| 1452 | /* there might still be more names under this key |
| 1453 | * check and repeat if required |
| 1454 | */ |
| 1455 | ret = btrfs_search_slot(NULL, root, dir_key, path, |
| 1456 | 0, 0); |
| 1457 | if (ret == 0) |
| 1458 | goto again; |
| 1459 | ret = 0; |
| 1460 | goto out; |
| 1461 | } |
| 1462 | btrfs_release_path(log, log_path); |
| 1463 | kfree(name); |
| 1464 | |
| 1465 | ptr = (unsigned long)(di + 1); |
| 1466 | ptr += name_len; |
| 1467 | } |
| 1468 | ret = 0; |
| 1469 | out: |
| 1470 | btrfs_release_path(root, path); |
| 1471 | btrfs_release_path(log, log_path); |
| 1472 | return ret; |
| 1473 | } |
| 1474 | |
| 1475 | /* |
| 1476 | * deletion replay happens before we copy any new directory items |
| 1477 | * out of the log or out of backreferences from inodes. It |
| 1478 | * scans the log to find ranges of keys that log is authoritative for, |
| 1479 | * and then scans the directory to find items in those ranges that are |
| 1480 | * not present in the log. |
| 1481 | * |
| 1482 | * Anything we don't find in the log is unlinked and removed from the |
| 1483 | * directory. |
| 1484 | */ |
| 1485 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, |
| 1486 | struct btrfs_root *root, |
| 1487 | struct btrfs_root *log, |
| 1488 | struct btrfs_path *path, |
| 1489 | u64 dirid) |
| 1490 | { |
| 1491 | u64 range_start; |
| 1492 | u64 range_end; |
| 1493 | int key_type = BTRFS_DIR_LOG_ITEM_KEY; |
| 1494 | int ret = 0; |
| 1495 | struct btrfs_key dir_key; |
| 1496 | struct btrfs_key found_key; |
| 1497 | struct btrfs_path *log_path; |
| 1498 | struct inode *dir; |
| 1499 | |
| 1500 | dir_key.objectid = dirid; |
| 1501 | dir_key.type = BTRFS_DIR_ITEM_KEY; |
| 1502 | log_path = btrfs_alloc_path(); |
| 1503 | if (!log_path) |
| 1504 | return -ENOMEM; |
| 1505 | |
| 1506 | dir = read_one_inode(root, dirid); |
| 1507 | /* it isn't an error if the inode isn't there, that can happen |
| 1508 | * because we replay the deletes before we copy in the inode item |
| 1509 | * from the log |
| 1510 | */ |
| 1511 | if (!dir) { |
| 1512 | btrfs_free_path(log_path); |
| 1513 | return 0; |
| 1514 | } |
| 1515 | again: |
| 1516 | range_start = 0; |
| 1517 | range_end = 0; |
| 1518 | while(1) { |
| 1519 | ret = find_dir_range(log, path, dirid, key_type, |
| 1520 | &range_start, &range_end); |
| 1521 | if (ret != 0) |
| 1522 | break; |
| 1523 | |
| 1524 | dir_key.offset = range_start; |
| 1525 | while(1) { |
| 1526 | int nritems; |
| 1527 | ret = btrfs_search_slot(NULL, root, &dir_key, path, |
| 1528 | 0, 0); |
| 1529 | if (ret < 0) |
| 1530 | goto out; |
| 1531 | |
| 1532 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 1533 | if (path->slots[0] >= nritems) { |
| 1534 | ret = btrfs_next_leaf(root, path); |
| 1535 | if (ret) |
| 1536 | break; |
| 1537 | } |
| 1538 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1539 | path->slots[0]); |
| 1540 | if (found_key.objectid != dirid || |
| 1541 | found_key.type != dir_key.type) |
| 1542 | goto next_type; |
| 1543 | |
| 1544 | if (found_key.offset > range_end) |
| 1545 | break; |
| 1546 | |
| 1547 | ret = check_item_in_log(trans, root, log, path, |
| 1548 | log_path, dir, &found_key); |
| 1549 | BUG_ON(ret); |
| 1550 | if (found_key.offset == (u64)-1) |
| 1551 | break; |
| 1552 | dir_key.offset = found_key.offset + 1; |
| 1553 | } |
| 1554 | btrfs_release_path(root, path); |
| 1555 | if (range_end == (u64)-1) |
| 1556 | break; |
| 1557 | range_start = range_end + 1; |
| 1558 | } |
| 1559 | |
| 1560 | next_type: |
| 1561 | ret = 0; |
| 1562 | if (key_type == BTRFS_DIR_LOG_ITEM_KEY) { |
| 1563 | key_type = BTRFS_DIR_LOG_INDEX_KEY; |
| 1564 | dir_key.type = BTRFS_DIR_INDEX_KEY; |
| 1565 | btrfs_release_path(root, path); |
| 1566 | goto again; |
| 1567 | } |
| 1568 | out: |
| 1569 | btrfs_release_path(root, path); |
| 1570 | btrfs_free_path(log_path); |
| 1571 | iput(dir); |
| 1572 | return ret; |
| 1573 | } |
| 1574 | |
| 1575 | /* |
| 1576 | * the process_func used to replay items from the log tree. This |
| 1577 | * gets called in two different stages. The first stage just looks |
| 1578 | * for inodes and makes sure they are all copied into the subvolume. |
| 1579 | * |
| 1580 | * The second stage copies all the other item types from the log into |
| 1581 | * the subvolume. The two stage approach is slower, but gets rid of |
| 1582 | * lots of complexity around inodes referencing other inodes that exist |
| 1583 | * only in the log (references come from either directory items or inode |
| 1584 | * back refs). |
| 1585 | */ |
| 1586 | static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, |
| 1587 | struct walk_control *wc, u64 gen) |
| 1588 | { |
| 1589 | int nritems; |
| 1590 | struct btrfs_path *path; |
| 1591 | struct btrfs_root *root = wc->replay_dest; |
| 1592 | struct btrfs_key key; |
| 1593 | u32 item_size; |
| 1594 | int level; |
| 1595 | int i; |
| 1596 | int ret; |
| 1597 | |
| 1598 | btrfs_read_buffer(eb, gen); |
| 1599 | |
| 1600 | level = btrfs_header_level(eb); |
| 1601 | |
| 1602 | if (level != 0) |
| 1603 | return 0; |
| 1604 | |
| 1605 | path = btrfs_alloc_path(); |
| 1606 | BUG_ON(!path); |
| 1607 | |
| 1608 | nritems = btrfs_header_nritems(eb); |
| 1609 | for (i = 0; i < nritems; i++) { |
| 1610 | btrfs_item_key_to_cpu(eb, &key, i); |
| 1611 | item_size = btrfs_item_size_nr(eb, i); |
| 1612 | |
| 1613 | /* inode keys are done during the first stage */ |
| 1614 | if (key.type == BTRFS_INODE_ITEM_KEY && |
| 1615 | wc->stage == LOG_WALK_REPLAY_INODES) { |
| 1616 | struct inode *inode; |
| 1617 | struct btrfs_inode_item *inode_item; |
| 1618 | u32 mode; |
| 1619 | |
| 1620 | inode_item = btrfs_item_ptr(eb, i, |
| 1621 | struct btrfs_inode_item); |
| 1622 | mode = btrfs_inode_mode(eb, inode_item); |
| 1623 | if (S_ISDIR(mode)) { |
| 1624 | ret = replay_dir_deletes(wc->trans, |
| 1625 | root, log, path, key.objectid); |
| 1626 | BUG_ON(ret); |
| 1627 | } |
| 1628 | ret = overwrite_item(wc->trans, root, path, |
| 1629 | eb, i, &key); |
| 1630 | BUG_ON(ret); |
| 1631 | |
| 1632 | /* for regular files, truncate away |
| 1633 | * extents past the new EOF |
| 1634 | */ |
| 1635 | if (S_ISREG(mode)) { |
| 1636 | inode = read_one_inode(root, |
| 1637 | key.objectid); |
| 1638 | BUG_ON(!inode); |
| 1639 | |
| 1640 | ret = btrfs_truncate_inode_items(wc->trans, |
| 1641 | root, inode, inode->i_size, |
| 1642 | BTRFS_EXTENT_DATA_KEY); |
| 1643 | BUG_ON(ret); |
| 1644 | iput(inode); |
| 1645 | } |
| 1646 | ret = link_to_fixup_dir(wc->trans, root, |
| 1647 | path, key.objectid); |
| 1648 | BUG_ON(ret); |
| 1649 | } |
| 1650 | if (wc->stage < LOG_WALK_REPLAY_ALL) |
| 1651 | continue; |
| 1652 | |
| 1653 | /* these keys are simply copied */ |
| 1654 | if (key.type == BTRFS_XATTR_ITEM_KEY) { |
| 1655 | ret = overwrite_item(wc->trans, root, path, |
| 1656 | eb, i, &key); |
| 1657 | BUG_ON(ret); |
| 1658 | } else if (key.type == BTRFS_INODE_REF_KEY) { |
| 1659 | ret = add_inode_ref(wc->trans, root, log, path, |
| 1660 | eb, i, &key); |
| 1661 | BUG_ON(ret && ret != -ENOENT); |
| 1662 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 1663 | ret = replay_one_extent(wc->trans, root, path, |
| 1664 | eb, i, &key); |
| 1665 | BUG_ON(ret); |
| 1666 | } else if (key.type == BTRFS_CSUM_ITEM_KEY) { |
| 1667 | ret = replay_one_csum(wc->trans, root, path, |
| 1668 | eb, i, &key); |
| 1669 | BUG_ON(ret); |
| 1670 | } else if (key.type == BTRFS_DIR_ITEM_KEY || |
| 1671 | key.type == BTRFS_DIR_INDEX_KEY) { |
| 1672 | ret = replay_one_dir_item(wc->trans, root, path, |
| 1673 | eb, i, &key); |
| 1674 | BUG_ON(ret); |
| 1675 | } |
| 1676 | } |
| 1677 | btrfs_free_path(path); |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
| 1681 | static int noinline walk_down_log_tree(struct btrfs_trans_handle *trans, |
| 1682 | struct btrfs_root *root, |
| 1683 | struct btrfs_path *path, int *level, |
| 1684 | struct walk_control *wc) |
| 1685 | { |
| 1686 | u64 root_owner; |
| 1687 | u64 root_gen; |
| 1688 | u64 bytenr; |
| 1689 | u64 ptr_gen; |
| 1690 | struct extent_buffer *next; |
| 1691 | struct extent_buffer *cur; |
| 1692 | struct extent_buffer *parent; |
| 1693 | u32 blocksize; |
| 1694 | int ret = 0; |
| 1695 | |
| 1696 | WARN_ON(*level < 0); |
| 1697 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 1698 | |
| 1699 | while(*level > 0) { |
| 1700 | WARN_ON(*level < 0); |
| 1701 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 1702 | cur = path->nodes[*level]; |
| 1703 | |
| 1704 | if (btrfs_header_level(cur) != *level) |
| 1705 | WARN_ON(1); |
| 1706 | |
| 1707 | if (path->slots[*level] >= |
| 1708 | btrfs_header_nritems(cur)) |
| 1709 | break; |
| 1710 | |
| 1711 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 1712 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
| 1713 | blocksize = btrfs_level_size(root, *level - 1); |
| 1714 | |
| 1715 | parent = path->nodes[*level]; |
| 1716 | root_owner = btrfs_header_owner(parent); |
| 1717 | root_gen = btrfs_header_generation(parent); |
| 1718 | |
| 1719 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 1720 | |
| 1721 | wc->process_func(root, next, wc, ptr_gen); |
| 1722 | |
| 1723 | if (*level == 1) { |
| 1724 | path->slots[*level]++; |
| 1725 | if (wc->free) { |
| 1726 | btrfs_read_buffer(next, ptr_gen); |
| 1727 | |
| 1728 | btrfs_tree_lock(next); |
| 1729 | clean_tree_block(trans, root, next); |
| 1730 | btrfs_wait_tree_block_writeback(next); |
| 1731 | btrfs_tree_unlock(next); |
| 1732 | |
| 1733 | ret = btrfs_drop_leaf_ref(trans, root, next); |
| 1734 | BUG_ON(ret); |
| 1735 | |
| 1736 | WARN_ON(root_owner != |
| 1737 | BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 1738 | ret = btrfs_free_reserved_extent(root, |
| 1739 | bytenr, blocksize); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1740 | BUG_ON(ret); |
| 1741 | } |
| 1742 | free_extent_buffer(next); |
| 1743 | continue; |
| 1744 | } |
| 1745 | btrfs_read_buffer(next, ptr_gen); |
| 1746 | |
| 1747 | WARN_ON(*level <= 0); |
| 1748 | if (path->nodes[*level-1]) |
| 1749 | free_extent_buffer(path->nodes[*level-1]); |
| 1750 | path->nodes[*level-1] = next; |
| 1751 | *level = btrfs_header_level(next); |
| 1752 | path->slots[*level] = 0; |
| 1753 | cond_resched(); |
| 1754 | } |
| 1755 | WARN_ON(*level < 0); |
| 1756 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 1757 | |
| 1758 | if (path->nodes[*level] == root->node) { |
| 1759 | parent = path->nodes[*level]; |
| 1760 | } else { |
| 1761 | parent = path->nodes[*level + 1]; |
| 1762 | } |
| 1763 | bytenr = path->nodes[*level]->start; |
| 1764 | |
| 1765 | blocksize = btrfs_level_size(root, *level); |
| 1766 | root_owner = btrfs_header_owner(parent); |
| 1767 | root_gen = btrfs_header_generation(parent); |
| 1768 | |
| 1769 | wc->process_func(root, path->nodes[*level], wc, |
| 1770 | btrfs_header_generation(path->nodes[*level])); |
| 1771 | |
| 1772 | if (wc->free) { |
| 1773 | next = path->nodes[*level]; |
| 1774 | btrfs_tree_lock(next); |
| 1775 | clean_tree_block(trans, root, next); |
| 1776 | btrfs_wait_tree_block_writeback(next); |
| 1777 | btrfs_tree_unlock(next); |
| 1778 | |
| 1779 | if (*level == 0) { |
| 1780 | ret = btrfs_drop_leaf_ref(trans, root, next); |
| 1781 | BUG_ON(ret); |
| 1782 | } |
| 1783 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 1784 | ret = btrfs_free_reserved_extent(root, bytenr, blocksize); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1785 | BUG_ON(ret); |
| 1786 | } |
| 1787 | free_extent_buffer(path->nodes[*level]); |
| 1788 | path->nodes[*level] = NULL; |
| 1789 | *level += 1; |
| 1790 | |
| 1791 | cond_resched(); |
| 1792 | return 0; |
| 1793 | } |
| 1794 | |
| 1795 | static int noinline walk_up_log_tree(struct btrfs_trans_handle *trans, |
| 1796 | struct btrfs_root *root, |
| 1797 | struct btrfs_path *path, int *level, |
| 1798 | struct walk_control *wc) |
| 1799 | { |
| 1800 | u64 root_owner; |
| 1801 | u64 root_gen; |
| 1802 | int i; |
| 1803 | int slot; |
| 1804 | int ret; |
| 1805 | |
| 1806 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
| 1807 | slot = path->slots[i]; |
| 1808 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 1809 | struct extent_buffer *node; |
| 1810 | node = path->nodes[i]; |
| 1811 | path->slots[i]++; |
| 1812 | *level = i; |
| 1813 | WARN_ON(*level == 0); |
| 1814 | return 0; |
| 1815 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1816 | struct extent_buffer *parent; |
| 1817 | if (path->nodes[*level] == root->node) |
| 1818 | parent = path->nodes[*level]; |
| 1819 | else |
| 1820 | parent = path->nodes[*level + 1]; |
| 1821 | |
| 1822 | root_owner = btrfs_header_owner(parent); |
| 1823 | root_gen = btrfs_header_generation(parent); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1824 | wc->process_func(root, path->nodes[*level], wc, |
| 1825 | btrfs_header_generation(path->nodes[*level])); |
| 1826 | if (wc->free) { |
| 1827 | struct extent_buffer *next; |
| 1828 | |
| 1829 | next = path->nodes[*level]; |
| 1830 | |
| 1831 | btrfs_tree_lock(next); |
| 1832 | clean_tree_block(trans, root, next); |
| 1833 | btrfs_wait_tree_block_writeback(next); |
| 1834 | btrfs_tree_unlock(next); |
| 1835 | |
| 1836 | if (*level == 0) { |
| 1837 | ret = btrfs_drop_leaf_ref(trans, root, |
| 1838 | next); |
| 1839 | BUG_ON(ret); |
| 1840 | } |
| 1841 | |
| 1842 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 1843 | ret = btrfs_free_reserved_extent(root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1844 | path->nodes[*level]->start, |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 1845 | path->nodes[*level]->len); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1846 | BUG_ON(ret); |
| 1847 | } |
| 1848 | free_extent_buffer(path->nodes[*level]); |
| 1849 | path->nodes[*level] = NULL; |
| 1850 | *level = i + 1; |
| 1851 | } |
| 1852 | } |
| 1853 | return 1; |
| 1854 | } |
| 1855 | |
| 1856 | /* |
| 1857 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 1858 | * the tree freeing any blocks that have a ref count of zero after being |
| 1859 | * decremented. |
| 1860 | */ |
| 1861 | static int walk_log_tree(struct btrfs_trans_handle *trans, |
| 1862 | struct btrfs_root *log, struct walk_control *wc) |
| 1863 | { |
| 1864 | int ret = 0; |
| 1865 | int wret; |
| 1866 | int level; |
| 1867 | struct btrfs_path *path; |
| 1868 | int i; |
| 1869 | int orig_level; |
| 1870 | |
| 1871 | path = btrfs_alloc_path(); |
| 1872 | BUG_ON(!path); |
| 1873 | |
| 1874 | level = btrfs_header_level(log->node); |
| 1875 | orig_level = level; |
| 1876 | path->nodes[level] = log->node; |
| 1877 | extent_buffer_get(log->node); |
| 1878 | path->slots[level] = 0; |
| 1879 | |
| 1880 | while(1) { |
| 1881 | wret = walk_down_log_tree(trans, log, path, &level, wc); |
| 1882 | if (wret > 0) |
| 1883 | break; |
| 1884 | if (wret < 0) |
| 1885 | ret = wret; |
| 1886 | |
| 1887 | wret = walk_up_log_tree(trans, log, path, &level, wc); |
| 1888 | if (wret > 0) |
| 1889 | break; |
| 1890 | if (wret < 0) |
| 1891 | ret = wret; |
| 1892 | } |
| 1893 | |
| 1894 | /* was the root node processed? if not, catch it here */ |
| 1895 | if (path->nodes[orig_level]) { |
| 1896 | wc->process_func(log, path->nodes[orig_level], wc, |
| 1897 | btrfs_header_generation(path->nodes[orig_level])); |
| 1898 | if (wc->free) { |
| 1899 | struct extent_buffer *next; |
| 1900 | |
| 1901 | next = path->nodes[orig_level]; |
| 1902 | |
| 1903 | btrfs_tree_lock(next); |
| 1904 | clean_tree_block(trans, log, next); |
| 1905 | btrfs_wait_tree_block_writeback(next); |
| 1906 | btrfs_tree_unlock(next); |
| 1907 | |
| 1908 | if (orig_level == 0) { |
| 1909 | ret = btrfs_drop_leaf_ref(trans, log, |
| 1910 | next); |
| 1911 | BUG_ON(ret); |
| 1912 | } |
| 1913 | WARN_ON(log->root_key.objectid != |
| 1914 | BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 1915 | ret = btrfs_free_reserved_extent(log, next->start, |
| 1916 | next->len); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1917 | BUG_ON(ret); |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | for (i = 0; i <= orig_level; i++) { |
| 1922 | if (path->nodes[i]) { |
| 1923 | free_extent_buffer(path->nodes[i]); |
| 1924 | path->nodes[i] = NULL; |
| 1925 | } |
| 1926 | } |
| 1927 | btrfs_free_path(path); |
| 1928 | if (wc->free) |
| 1929 | free_extent_buffer(log->node); |
| 1930 | return ret; |
| 1931 | } |
| 1932 | |
| 1933 | int wait_log_commit(struct btrfs_root *log) |
| 1934 | { |
| 1935 | DEFINE_WAIT(wait); |
| 1936 | u64 transid = log->fs_info->tree_log_transid; |
| 1937 | |
| 1938 | do { |
| 1939 | prepare_to_wait(&log->fs_info->tree_log_wait, &wait, |
| 1940 | TASK_UNINTERRUPTIBLE); |
| 1941 | mutex_unlock(&log->fs_info->tree_log_mutex); |
| 1942 | if (atomic_read(&log->fs_info->tree_log_commit)) |
| 1943 | schedule(); |
| 1944 | finish_wait(&log->fs_info->tree_log_wait, &wait); |
| 1945 | mutex_lock(&log->fs_info->tree_log_mutex); |
| 1946 | } while(transid == log->fs_info->tree_log_transid && |
| 1947 | atomic_read(&log->fs_info->tree_log_commit)); |
| 1948 | return 0; |
| 1949 | } |
| 1950 | |
| 1951 | /* |
| 1952 | * btrfs_sync_log does sends a given tree log down to the disk and |
| 1953 | * updates the super blocks to record it. When this call is done, |
| 1954 | * you know that any inodes previously logged are safely on disk |
| 1955 | */ |
| 1956 | int btrfs_sync_log(struct btrfs_trans_handle *trans, |
| 1957 | struct btrfs_root *root) |
| 1958 | { |
| 1959 | int ret; |
| 1960 | unsigned long batch; |
| 1961 | struct btrfs_root *log = root->log_root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1962 | |
| 1963 | mutex_lock(&log->fs_info->tree_log_mutex); |
| 1964 | if (atomic_read(&log->fs_info->tree_log_commit)) { |
| 1965 | wait_log_commit(log); |
| 1966 | goto out; |
| 1967 | } |
| 1968 | atomic_set(&log->fs_info->tree_log_commit, 1); |
| 1969 | |
| 1970 | while(1) { |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1971 | batch = log->fs_info->tree_log_batch; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1972 | mutex_unlock(&log->fs_info->tree_log_mutex); |
| 1973 | schedule_timeout_uninterruptible(1); |
| 1974 | mutex_lock(&log->fs_info->tree_log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1975 | |
| 1976 | while(atomic_read(&log->fs_info->tree_log_writers)) { |
| 1977 | DEFINE_WAIT(wait); |
| 1978 | prepare_to_wait(&log->fs_info->tree_log_wait, &wait, |
| 1979 | TASK_UNINTERRUPTIBLE); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1980 | mutex_unlock(&log->fs_info->tree_log_mutex); |
| 1981 | if (atomic_read(&log->fs_info->tree_log_writers)) |
| 1982 | schedule(); |
| 1983 | mutex_lock(&log->fs_info->tree_log_mutex); |
| 1984 | finish_wait(&log->fs_info->tree_log_wait, &wait); |
| 1985 | } |
| 1986 | if (batch == log->fs_info->tree_log_batch) |
| 1987 | break; |
| 1988 | } |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 1989 | |
| 1990 | ret = btrfs_write_and_wait_marked_extents(log, &log->dirty_log_pages); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1991 | BUG_ON(ret); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 1992 | ret = btrfs_write_and_wait_marked_extents(root->fs_info->log_root_tree, |
| 1993 | &root->fs_info->log_root_tree->dirty_log_pages); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1994 | BUG_ON(ret); |
| 1995 | |
| 1996 | btrfs_set_super_log_root(&root->fs_info->super_for_commit, |
| 1997 | log->fs_info->log_root_tree->node->start); |
| 1998 | btrfs_set_super_log_root_level(&root->fs_info->super_for_commit, |
| 1999 | btrfs_header_level(log->fs_info->log_root_tree->node)); |
| 2000 | |
| 2001 | write_ctree_super(trans, log->fs_info->tree_root); |
| 2002 | log->fs_info->tree_log_transid++; |
| 2003 | log->fs_info->tree_log_batch = 0; |
| 2004 | atomic_set(&log->fs_info->tree_log_commit, 0); |
| 2005 | smp_mb(); |
| 2006 | if (waitqueue_active(&log->fs_info->tree_log_wait)) |
| 2007 | wake_up(&log->fs_info->tree_log_wait); |
| 2008 | out: |
| 2009 | mutex_unlock(&log->fs_info->tree_log_mutex); |
| 2010 | return 0; |
| 2011 | |
| 2012 | } |
| 2013 | |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 2014 | /* * free all the extents used by the tree log. This should be called |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2015 | * at commit time of the full transaction |
| 2016 | */ |
| 2017 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) |
| 2018 | { |
| 2019 | int ret; |
| 2020 | struct btrfs_root *log; |
| 2021 | struct key; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2022 | u64 start; |
| 2023 | u64 end; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2024 | struct walk_control wc = { |
| 2025 | .free = 1, |
| 2026 | .process_func = process_one_buffer |
| 2027 | }; |
| 2028 | |
| 2029 | if (!root->log_root) |
| 2030 | return 0; |
| 2031 | |
| 2032 | log = root->log_root; |
| 2033 | ret = walk_log_tree(trans, log, &wc); |
| 2034 | BUG_ON(ret); |
| 2035 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2036 | while(1) { |
| 2037 | ret = find_first_extent_bit(&log->dirty_log_pages, |
| 2038 | 0, &start, &end, EXTENT_DIRTY); |
| 2039 | if (ret) |
| 2040 | break; |
| 2041 | |
| 2042 | clear_extent_dirty(&log->dirty_log_pages, |
| 2043 | start, end, GFP_NOFS); |
| 2044 | } |
| 2045 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2046 | log = root->log_root; |
| 2047 | ret = btrfs_del_root(trans, root->fs_info->log_root_tree, |
| 2048 | &log->root_key); |
| 2049 | BUG_ON(ret); |
| 2050 | root->log_root = NULL; |
| 2051 | kfree(root->log_root); |
| 2052 | return 0; |
| 2053 | } |
| 2054 | |
| 2055 | /* |
| 2056 | * helper function to update the item for a given subvolumes log root |
| 2057 | * in the tree of log roots |
| 2058 | */ |
| 2059 | static int update_log_root(struct btrfs_trans_handle *trans, |
| 2060 | struct btrfs_root *log) |
| 2061 | { |
| 2062 | u64 bytenr = btrfs_root_bytenr(&log->root_item); |
| 2063 | int ret; |
| 2064 | |
| 2065 | if (log->node->start == bytenr) |
| 2066 | return 0; |
| 2067 | |
| 2068 | btrfs_set_root_bytenr(&log->root_item, log->node->start); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame^] | 2069 | btrfs_set_root_generation(&log->root_item, trans->transid); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2070 | btrfs_set_root_level(&log->root_item, btrfs_header_level(log->node)); |
| 2071 | ret = btrfs_update_root(trans, log->fs_info->log_root_tree, |
| 2072 | &log->root_key, &log->root_item); |
| 2073 | BUG_ON(ret); |
| 2074 | return ret; |
| 2075 | } |
| 2076 | |
| 2077 | /* |
| 2078 | * If both a file and directory are logged, and unlinks or renames are |
| 2079 | * mixed in, we have a few interesting corners: |
| 2080 | * |
| 2081 | * create file X in dir Y |
| 2082 | * link file X to X.link in dir Y |
| 2083 | * fsync file X |
| 2084 | * unlink file X but leave X.link |
| 2085 | * fsync dir Y |
| 2086 | * |
| 2087 | * After a crash we would expect only X.link to exist. But file X |
| 2088 | * didn't get fsync'd again so the log has back refs for X and X.link. |
| 2089 | * |
| 2090 | * We solve this by removing directory entries and inode backrefs from the |
| 2091 | * log when a file that was logged in the current transaction is |
| 2092 | * unlinked. Any later fsync will include the updated log entries, and |
| 2093 | * we'll be able to reconstruct the proper directory items from backrefs. |
| 2094 | * |
| 2095 | * This optimizations allows us to avoid relogging the entire inode |
| 2096 | * or the entire directory. |
| 2097 | */ |
| 2098 | int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, |
| 2099 | struct btrfs_root *root, |
| 2100 | const char *name, int name_len, |
| 2101 | struct inode *dir, u64 index) |
| 2102 | { |
| 2103 | struct btrfs_root *log; |
| 2104 | struct btrfs_dir_item *di; |
| 2105 | struct btrfs_path *path; |
| 2106 | int ret; |
| 2107 | int bytes_del = 0; |
| 2108 | |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 2109 | if (BTRFS_I(dir)->logged_trans < trans->transid) |
| 2110 | return 0; |
| 2111 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2112 | ret = join_running_log_trans(root); |
| 2113 | if (ret) |
| 2114 | return 0; |
| 2115 | |
| 2116 | mutex_lock(&BTRFS_I(dir)->log_mutex); |
| 2117 | |
| 2118 | log = root->log_root; |
| 2119 | path = btrfs_alloc_path(); |
| 2120 | di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino, |
| 2121 | name, name_len, -1); |
| 2122 | if (di && !IS_ERR(di)) { |
| 2123 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
| 2124 | bytes_del += name_len; |
| 2125 | BUG_ON(ret); |
| 2126 | } |
| 2127 | btrfs_release_path(log, path); |
| 2128 | di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino, |
| 2129 | index, name, name_len, -1); |
| 2130 | if (di && !IS_ERR(di)) { |
| 2131 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
| 2132 | bytes_del += name_len; |
| 2133 | BUG_ON(ret); |
| 2134 | } |
| 2135 | |
| 2136 | /* update the directory size in the log to reflect the names |
| 2137 | * we have removed |
| 2138 | */ |
| 2139 | if (bytes_del) { |
| 2140 | struct btrfs_key key; |
| 2141 | |
| 2142 | key.objectid = dir->i_ino; |
| 2143 | key.offset = 0; |
| 2144 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2145 | btrfs_release_path(log, path); |
| 2146 | |
| 2147 | ret = btrfs_search_slot(trans, log, &key, path, 0, 1); |
| 2148 | if (ret == 0) { |
| 2149 | struct btrfs_inode_item *item; |
| 2150 | u64 i_size; |
| 2151 | |
| 2152 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2153 | struct btrfs_inode_item); |
| 2154 | i_size = btrfs_inode_size(path->nodes[0], item); |
| 2155 | if (i_size > bytes_del) |
| 2156 | i_size -= bytes_del; |
| 2157 | else |
| 2158 | i_size = 0; |
| 2159 | btrfs_set_inode_size(path->nodes[0], item, i_size); |
| 2160 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 2161 | } else |
| 2162 | ret = 0; |
| 2163 | btrfs_release_path(log, path); |
| 2164 | } |
| 2165 | |
| 2166 | btrfs_free_path(path); |
| 2167 | mutex_unlock(&BTRFS_I(dir)->log_mutex); |
| 2168 | end_log_trans(root); |
| 2169 | |
| 2170 | return 0; |
| 2171 | } |
| 2172 | |
| 2173 | /* see comments for btrfs_del_dir_entries_in_log */ |
| 2174 | int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, |
| 2175 | struct btrfs_root *root, |
| 2176 | const char *name, int name_len, |
| 2177 | struct inode *inode, u64 dirid) |
| 2178 | { |
| 2179 | struct btrfs_root *log; |
| 2180 | u64 index; |
| 2181 | int ret; |
| 2182 | |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 2183 | if (BTRFS_I(inode)->logged_trans < trans->transid) |
| 2184 | return 0; |
| 2185 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2186 | ret = join_running_log_trans(root); |
| 2187 | if (ret) |
| 2188 | return 0; |
| 2189 | log = root->log_root; |
| 2190 | mutex_lock(&BTRFS_I(inode)->log_mutex); |
| 2191 | |
| 2192 | ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino, |
| 2193 | dirid, &index); |
| 2194 | mutex_unlock(&BTRFS_I(inode)->log_mutex); |
| 2195 | end_log_trans(root); |
| 2196 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2197 | return ret; |
| 2198 | } |
| 2199 | |
| 2200 | /* |
| 2201 | * creates a range item in the log for 'dirid'. first_offset and |
| 2202 | * last_offset tell us which parts of the key space the log should |
| 2203 | * be considered authoritative for. |
| 2204 | */ |
| 2205 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, |
| 2206 | struct btrfs_root *log, |
| 2207 | struct btrfs_path *path, |
| 2208 | int key_type, u64 dirid, |
| 2209 | u64 first_offset, u64 last_offset) |
| 2210 | { |
| 2211 | int ret; |
| 2212 | struct btrfs_key key; |
| 2213 | struct btrfs_dir_log_item *item; |
| 2214 | |
| 2215 | key.objectid = dirid; |
| 2216 | key.offset = first_offset; |
| 2217 | if (key_type == BTRFS_DIR_ITEM_KEY) |
| 2218 | key.type = BTRFS_DIR_LOG_ITEM_KEY; |
| 2219 | else |
| 2220 | key.type = BTRFS_DIR_LOG_INDEX_KEY; |
| 2221 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); |
| 2222 | BUG_ON(ret); |
| 2223 | |
| 2224 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2225 | struct btrfs_dir_log_item); |
| 2226 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); |
| 2227 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 2228 | btrfs_release_path(log, path); |
| 2229 | return 0; |
| 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | * log all the items included in the current transaction for a given |
| 2234 | * directory. This also creates the range items in the log tree required |
| 2235 | * to replay anything deleted before the fsync |
| 2236 | */ |
| 2237 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, |
| 2238 | struct btrfs_root *root, struct inode *inode, |
| 2239 | struct btrfs_path *path, |
| 2240 | struct btrfs_path *dst_path, int key_type, |
| 2241 | u64 min_offset, u64 *last_offset_ret) |
| 2242 | { |
| 2243 | struct btrfs_key min_key; |
| 2244 | struct btrfs_key max_key; |
| 2245 | struct btrfs_root *log = root->log_root; |
| 2246 | struct extent_buffer *src; |
| 2247 | int ret; |
| 2248 | int i; |
| 2249 | int nritems; |
| 2250 | u64 first_offset = min_offset; |
| 2251 | u64 last_offset = (u64)-1; |
| 2252 | |
| 2253 | log = root->log_root; |
| 2254 | max_key.objectid = inode->i_ino; |
| 2255 | max_key.offset = (u64)-1; |
| 2256 | max_key.type = key_type; |
| 2257 | |
| 2258 | min_key.objectid = inode->i_ino; |
| 2259 | min_key.type = key_type; |
| 2260 | min_key.offset = min_offset; |
| 2261 | |
| 2262 | path->keep_locks = 1; |
| 2263 | |
| 2264 | ret = btrfs_search_forward(root, &min_key, &max_key, |
| 2265 | path, 0, trans->transid); |
| 2266 | |
| 2267 | /* |
| 2268 | * we didn't find anything from this transaction, see if there |
| 2269 | * is anything at all |
| 2270 | */ |
| 2271 | if (ret != 0 || min_key.objectid != inode->i_ino || |
| 2272 | min_key.type != key_type) { |
| 2273 | min_key.objectid = inode->i_ino; |
| 2274 | min_key.type = key_type; |
| 2275 | min_key.offset = (u64)-1; |
| 2276 | btrfs_release_path(root, path); |
| 2277 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
| 2278 | if (ret < 0) { |
| 2279 | btrfs_release_path(root, path); |
| 2280 | return ret; |
| 2281 | } |
| 2282 | ret = btrfs_previous_item(root, path, inode->i_ino, key_type); |
| 2283 | |
| 2284 | /* if ret == 0 there are items for this type, |
| 2285 | * create a range to tell us the last key of this type. |
| 2286 | * otherwise, there are no items in this directory after |
| 2287 | * *min_offset, and we create a range to indicate that. |
| 2288 | */ |
| 2289 | if (ret == 0) { |
| 2290 | struct btrfs_key tmp; |
| 2291 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, |
| 2292 | path->slots[0]); |
| 2293 | if (key_type == tmp.type) { |
| 2294 | first_offset = max(min_offset, tmp.offset) + 1; |
| 2295 | } |
| 2296 | } |
| 2297 | goto done; |
| 2298 | } |
| 2299 | |
| 2300 | /* go backward to find any previous key */ |
| 2301 | ret = btrfs_previous_item(root, path, inode->i_ino, key_type); |
| 2302 | if (ret == 0) { |
| 2303 | struct btrfs_key tmp; |
| 2304 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
| 2305 | if (key_type == tmp.type) { |
| 2306 | first_offset = tmp.offset; |
| 2307 | ret = overwrite_item(trans, log, dst_path, |
| 2308 | path->nodes[0], path->slots[0], |
| 2309 | &tmp); |
| 2310 | } |
| 2311 | } |
| 2312 | btrfs_release_path(root, path); |
| 2313 | |
| 2314 | /* find the first key from this transaction again */ |
| 2315 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
| 2316 | if (ret != 0) { |
| 2317 | WARN_ON(1); |
| 2318 | goto done; |
| 2319 | } |
| 2320 | |
| 2321 | /* |
| 2322 | * we have a block from this transaction, log every item in it |
| 2323 | * from our directory |
| 2324 | */ |
| 2325 | while(1) { |
| 2326 | struct btrfs_key tmp; |
| 2327 | src = path->nodes[0]; |
| 2328 | nritems = btrfs_header_nritems(src); |
| 2329 | for (i = path->slots[0]; i < nritems; i++) { |
| 2330 | btrfs_item_key_to_cpu(src, &min_key, i); |
| 2331 | |
| 2332 | if (min_key.objectid != inode->i_ino || |
| 2333 | min_key.type != key_type) |
| 2334 | goto done; |
| 2335 | ret = overwrite_item(trans, log, dst_path, src, i, |
| 2336 | &min_key); |
| 2337 | BUG_ON(ret); |
| 2338 | } |
| 2339 | path->slots[0] = nritems; |
| 2340 | |
| 2341 | /* |
| 2342 | * look ahead to the next item and see if it is also |
| 2343 | * from this directory and from this transaction |
| 2344 | */ |
| 2345 | ret = btrfs_next_leaf(root, path); |
| 2346 | if (ret == 1) { |
| 2347 | last_offset = (u64)-1; |
| 2348 | goto done; |
| 2349 | } |
| 2350 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
| 2351 | if (tmp.objectid != inode->i_ino || tmp.type != key_type) { |
| 2352 | last_offset = (u64)-1; |
| 2353 | goto done; |
| 2354 | } |
| 2355 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { |
| 2356 | ret = overwrite_item(trans, log, dst_path, |
| 2357 | path->nodes[0], path->slots[0], |
| 2358 | &tmp); |
| 2359 | |
| 2360 | BUG_ON(ret); |
| 2361 | last_offset = tmp.offset; |
| 2362 | goto done; |
| 2363 | } |
| 2364 | } |
| 2365 | done: |
| 2366 | *last_offset_ret = last_offset; |
| 2367 | btrfs_release_path(root, path); |
| 2368 | btrfs_release_path(log, dst_path); |
| 2369 | |
| 2370 | /* insert the log range keys to indicate where the log is valid */ |
| 2371 | ret = insert_dir_log_key(trans, log, path, key_type, inode->i_ino, |
| 2372 | first_offset, last_offset); |
| 2373 | BUG_ON(ret); |
| 2374 | return 0; |
| 2375 | } |
| 2376 | |
| 2377 | /* |
| 2378 | * logging directories is very similar to logging inodes, We find all the items |
| 2379 | * from the current transaction and write them to the log. |
| 2380 | * |
| 2381 | * The recovery code scans the directory in the subvolume, and if it finds a |
| 2382 | * key in the range logged that is not present in the log tree, then it means |
| 2383 | * that dir entry was unlinked during the transaction. |
| 2384 | * |
| 2385 | * In order for that scan to work, we must include one key smaller than |
| 2386 | * the smallest logged by this transaction and one key larger than the largest |
| 2387 | * key logged by this transaction. |
| 2388 | */ |
| 2389 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, |
| 2390 | struct btrfs_root *root, struct inode *inode, |
| 2391 | struct btrfs_path *path, |
| 2392 | struct btrfs_path *dst_path) |
| 2393 | { |
| 2394 | u64 min_key; |
| 2395 | u64 max_key; |
| 2396 | int ret; |
| 2397 | int key_type = BTRFS_DIR_ITEM_KEY; |
| 2398 | |
| 2399 | again: |
| 2400 | min_key = 0; |
| 2401 | max_key = 0; |
| 2402 | while(1) { |
| 2403 | ret = log_dir_items(trans, root, inode, path, |
| 2404 | dst_path, key_type, min_key, |
| 2405 | &max_key); |
| 2406 | BUG_ON(ret); |
| 2407 | if (max_key == (u64)-1) |
| 2408 | break; |
| 2409 | min_key = max_key + 1; |
| 2410 | } |
| 2411 | |
| 2412 | if (key_type == BTRFS_DIR_ITEM_KEY) { |
| 2413 | key_type = BTRFS_DIR_INDEX_KEY; |
| 2414 | goto again; |
| 2415 | } |
| 2416 | return 0; |
| 2417 | } |
| 2418 | |
| 2419 | /* |
| 2420 | * a helper function to drop items from the log before we relog an |
| 2421 | * inode. max_key_type indicates the highest item type to remove. |
| 2422 | * This cannot be run for file data extents because it does not |
| 2423 | * free the extents they point to. |
| 2424 | */ |
| 2425 | static int drop_objectid_items(struct btrfs_trans_handle *trans, |
| 2426 | struct btrfs_root *log, |
| 2427 | struct btrfs_path *path, |
| 2428 | u64 objectid, int max_key_type) |
| 2429 | { |
| 2430 | int ret; |
| 2431 | struct btrfs_key key; |
| 2432 | struct btrfs_key found_key; |
| 2433 | |
| 2434 | key.objectid = objectid; |
| 2435 | key.type = max_key_type; |
| 2436 | key.offset = (u64)-1; |
| 2437 | |
| 2438 | while(1) { |
| 2439 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); |
| 2440 | |
| 2441 | if (ret != 1) |
| 2442 | break; |
| 2443 | |
| 2444 | if (path->slots[0] == 0) |
| 2445 | break; |
| 2446 | |
| 2447 | path->slots[0]--; |
| 2448 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2449 | path->slots[0]); |
| 2450 | |
| 2451 | if (found_key.objectid != objectid) |
| 2452 | break; |
| 2453 | |
| 2454 | ret = btrfs_del_item(trans, log, path); |
| 2455 | BUG_ON(ret); |
| 2456 | btrfs_release_path(log, path); |
| 2457 | } |
| 2458 | btrfs_release_path(log, path); |
| 2459 | return 0; |
| 2460 | } |
| 2461 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2462 | static noinline int copy_items(struct btrfs_trans_handle *trans, |
| 2463 | struct btrfs_root *log, |
| 2464 | struct btrfs_path *dst_path, |
| 2465 | struct extent_buffer *src, |
| 2466 | int start_slot, int nr, int inode_only) |
| 2467 | { |
| 2468 | unsigned long src_offset; |
| 2469 | unsigned long dst_offset; |
| 2470 | struct btrfs_file_extent_item *extent; |
| 2471 | struct btrfs_inode_item *inode_item; |
| 2472 | int ret; |
| 2473 | struct btrfs_key *ins_keys; |
| 2474 | u32 *ins_sizes; |
| 2475 | char *ins_data; |
| 2476 | int i; |
| 2477 | |
| 2478 | ins_data = kmalloc(nr * sizeof(struct btrfs_key) + |
| 2479 | nr * sizeof(u32), GFP_NOFS); |
| 2480 | ins_sizes = (u32 *)ins_data; |
| 2481 | ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); |
| 2482 | |
| 2483 | for (i = 0; i < nr; i++) { |
| 2484 | ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot); |
| 2485 | btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot); |
| 2486 | } |
| 2487 | ret = btrfs_insert_empty_items(trans, log, dst_path, |
| 2488 | ins_keys, ins_sizes, nr); |
| 2489 | BUG_ON(ret); |
| 2490 | |
| 2491 | for (i = 0; i < nr; i++) { |
| 2492 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], |
| 2493 | dst_path->slots[0]); |
| 2494 | |
| 2495 | src_offset = btrfs_item_ptr_offset(src, start_slot + i); |
| 2496 | |
| 2497 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, |
| 2498 | src_offset, ins_sizes[i]); |
| 2499 | |
| 2500 | if (inode_only == LOG_INODE_EXISTS && |
| 2501 | ins_keys[i].type == BTRFS_INODE_ITEM_KEY) { |
| 2502 | inode_item = btrfs_item_ptr(dst_path->nodes[0], |
| 2503 | dst_path->slots[0], |
| 2504 | struct btrfs_inode_item); |
| 2505 | btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0); |
| 2506 | |
| 2507 | /* set the generation to zero so the recover code |
| 2508 | * can tell the difference between an logging |
| 2509 | * just to say 'this inode exists' and a logging |
| 2510 | * to say 'update this inode with these values' |
| 2511 | */ |
| 2512 | btrfs_set_inode_generation(dst_path->nodes[0], |
| 2513 | inode_item, 0); |
| 2514 | } |
| 2515 | /* take a reference on file data extents so that truncates |
| 2516 | * or deletes of this inode don't have to relog the inode |
| 2517 | * again |
| 2518 | */ |
| 2519 | if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) { |
| 2520 | int found_type; |
| 2521 | extent = btrfs_item_ptr(src, start_slot + i, |
| 2522 | struct btrfs_file_extent_item); |
| 2523 | |
| 2524 | found_type = btrfs_file_extent_type(src, extent); |
| 2525 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
| 2526 | u64 ds = btrfs_file_extent_disk_bytenr(src, |
| 2527 | extent); |
| 2528 | u64 dl = btrfs_file_extent_disk_num_bytes(src, |
| 2529 | extent); |
| 2530 | /* ds == 0 is a hole */ |
| 2531 | if (ds != 0) { |
| 2532 | ret = btrfs_inc_extent_ref(trans, log, |
| 2533 | ds, dl, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2534 | dst_path->nodes[0]->start, |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2535 | BTRFS_TREE_LOG_OBJECTID, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2536 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2537 | ins_keys[i].objectid); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2538 | BUG_ON(ret); |
| 2539 | } |
| 2540 | } |
| 2541 | } |
| 2542 | dst_path->slots[0]++; |
| 2543 | } |
| 2544 | |
| 2545 | btrfs_mark_buffer_dirty(dst_path->nodes[0]); |
| 2546 | btrfs_release_path(log, dst_path); |
| 2547 | kfree(ins_data); |
| 2548 | return 0; |
| 2549 | } |
| 2550 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2551 | /* log a single inode in the tree log. |
| 2552 | * At least one parent directory for this inode must exist in the tree |
| 2553 | * or be logged already. |
| 2554 | * |
| 2555 | * Any items from this inode changed by the current transaction are copied |
| 2556 | * to the log tree. An extra reference is taken on any extents in this |
| 2557 | * file, allowing us to avoid a whole pile of corner cases around logging |
| 2558 | * blocks that have been removed from the tree. |
| 2559 | * |
| 2560 | * See LOG_INODE_ALL and related defines for a description of what inode_only |
| 2561 | * does. |
| 2562 | * |
| 2563 | * This handles both files and directories. |
| 2564 | */ |
| 2565 | static int __btrfs_log_inode(struct btrfs_trans_handle *trans, |
| 2566 | struct btrfs_root *root, struct inode *inode, |
| 2567 | int inode_only) |
| 2568 | { |
| 2569 | struct btrfs_path *path; |
| 2570 | struct btrfs_path *dst_path; |
| 2571 | struct btrfs_key min_key; |
| 2572 | struct btrfs_key max_key; |
| 2573 | struct btrfs_root *log = root->log_root; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2574 | struct extent_buffer *src = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2575 | u32 size; |
| 2576 | int ret; |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 2577 | int nritems; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2578 | int ins_start_slot = 0; |
| 2579 | int ins_nr; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2580 | |
| 2581 | log = root->log_root; |
| 2582 | |
| 2583 | path = btrfs_alloc_path(); |
| 2584 | dst_path = btrfs_alloc_path(); |
| 2585 | |
| 2586 | min_key.objectid = inode->i_ino; |
| 2587 | min_key.type = BTRFS_INODE_ITEM_KEY; |
| 2588 | min_key.offset = 0; |
| 2589 | |
| 2590 | max_key.objectid = inode->i_ino; |
| 2591 | if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode)) |
| 2592 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
| 2593 | else |
| 2594 | max_key.type = (u8)-1; |
| 2595 | max_key.offset = (u64)-1; |
| 2596 | |
| 2597 | /* |
| 2598 | * if this inode has already been logged and we're in inode_only |
| 2599 | * mode, we don't want to delete the things that have already |
| 2600 | * been written to the log. |
| 2601 | * |
| 2602 | * But, if the inode has been through an inode_only log, |
| 2603 | * the logged_trans field is not set. This allows us to catch |
| 2604 | * any new names for this inode in the backrefs by logging it |
| 2605 | * again |
| 2606 | */ |
| 2607 | if (inode_only == LOG_INODE_EXISTS && |
| 2608 | BTRFS_I(inode)->logged_trans == trans->transid) { |
| 2609 | btrfs_free_path(path); |
| 2610 | btrfs_free_path(dst_path); |
| 2611 | goto out; |
| 2612 | } |
| 2613 | mutex_lock(&BTRFS_I(inode)->log_mutex); |
| 2614 | |
| 2615 | /* |
| 2616 | * a brute force approach to making sure we get the most uptodate |
| 2617 | * copies of everything. |
| 2618 | */ |
| 2619 | if (S_ISDIR(inode->i_mode)) { |
| 2620 | int max_key_type = BTRFS_DIR_LOG_INDEX_KEY; |
| 2621 | |
| 2622 | if (inode_only == LOG_INODE_EXISTS) |
| 2623 | max_key_type = BTRFS_XATTR_ITEM_KEY; |
| 2624 | ret = drop_objectid_items(trans, log, path, |
| 2625 | inode->i_ino, max_key_type); |
| 2626 | } else { |
| 2627 | ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0); |
| 2628 | } |
| 2629 | BUG_ON(ret); |
| 2630 | path->keep_locks = 1; |
| 2631 | |
| 2632 | while(1) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2633 | ins_nr = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2634 | ret = btrfs_search_forward(root, &min_key, &max_key, |
| 2635 | path, 0, trans->transid); |
| 2636 | if (ret != 0) |
| 2637 | break; |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 2638 | again: |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2639 | /* note, ins_nr might be > 0 here, cleanup outside the loop */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2640 | if (min_key.objectid != inode->i_ino) |
| 2641 | break; |
| 2642 | if (min_key.type > max_key.type) |
| 2643 | break; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2644 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2645 | src = path->nodes[0]; |
| 2646 | size = btrfs_item_size_nr(src, path->slots[0]); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2647 | if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { |
| 2648 | ins_nr++; |
| 2649 | goto next_slot; |
| 2650 | } else if (!ins_nr) { |
| 2651 | ins_start_slot = path->slots[0]; |
| 2652 | ins_nr = 1; |
| 2653 | goto next_slot; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2654 | } |
| 2655 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2656 | ret = copy_items(trans, log, dst_path, src, ins_start_slot, |
| 2657 | ins_nr, inode_only); |
| 2658 | BUG_ON(ret); |
| 2659 | ins_nr = 1; |
| 2660 | ins_start_slot = path->slots[0]; |
| 2661 | next_slot: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2662 | |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 2663 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2664 | path->slots[0]++; |
| 2665 | if (path->slots[0] < nritems) { |
| 2666 | btrfs_item_key_to_cpu(path->nodes[0], &min_key, |
| 2667 | path->slots[0]); |
| 2668 | goto again; |
| 2669 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2670 | if (ins_nr) { |
| 2671 | ret = copy_items(trans, log, dst_path, src, |
| 2672 | ins_start_slot, |
| 2673 | ins_nr, inode_only); |
| 2674 | BUG_ON(ret); |
| 2675 | ins_nr = 0; |
| 2676 | } |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 2677 | btrfs_release_path(root, path); |
| 2678 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2679 | if (min_key.offset < (u64)-1) |
| 2680 | min_key.offset++; |
| 2681 | else if (min_key.type < (u8)-1) |
| 2682 | min_key.type++; |
| 2683 | else if (min_key.objectid < (u64)-1) |
| 2684 | min_key.objectid++; |
| 2685 | else |
| 2686 | break; |
| 2687 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2688 | if (ins_nr) { |
| 2689 | ret = copy_items(trans, log, dst_path, src, |
| 2690 | ins_start_slot, |
| 2691 | ins_nr, inode_only); |
| 2692 | BUG_ON(ret); |
| 2693 | ins_nr = 0; |
| 2694 | } |
| 2695 | WARN_ON(ins_nr); |
Chris Mason | 9623f9a | 2008-09-11 17:42:42 -0400 | [diff] [blame] | 2696 | if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2697 | btrfs_release_path(root, path); |
| 2698 | btrfs_release_path(log, dst_path); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2699 | BTRFS_I(inode)->log_dirty_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2700 | ret = log_directory_changes(trans, root, inode, path, dst_path); |
| 2701 | BUG_ON(ret); |
| 2702 | } |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 2703 | BTRFS_I(inode)->logged_trans = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2704 | mutex_unlock(&BTRFS_I(inode)->log_mutex); |
| 2705 | |
| 2706 | btrfs_free_path(path); |
| 2707 | btrfs_free_path(dst_path); |
| 2708 | |
| 2709 | mutex_lock(&root->fs_info->tree_log_mutex); |
| 2710 | ret = update_log_root(trans, log); |
| 2711 | BUG_ON(ret); |
| 2712 | mutex_unlock(&root->fs_info->tree_log_mutex); |
| 2713 | out: |
| 2714 | return 0; |
| 2715 | } |
| 2716 | |
| 2717 | int btrfs_log_inode(struct btrfs_trans_handle *trans, |
| 2718 | struct btrfs_root *root, struct inode *inode, |
| 2719 | int inode_only) |
| 2720 | { |
| 2721 | int ret; |
| 2722 | |
| 2723 | start_log_trans(trans, root); |
| 2724 | ret = __btrfs_log_inode(trans, root, inode, inode_only); |
| 2725 | end_log_trans(root); |
| 2726 | return ret; |
| 2727 | } |
| 2728 | |
| 2729 | /* |
| 2730 | * helper function around btrfs_log_inode to make sure newly created |
| 2731 | * parent directories also end up in the log. A minimal inode and backref |
| 2732 | * only logging is done of any parent directories that are older than |
| 2733 | * the last committed transaction |
| 2734 | */ |
| 2735 | int btrfs_log_dentry(struct btrfs_trans_handle *trans, |
| 2736 | struct btrfs_root *root, struct dentry *dentry) |
| 2737 | { |
| 2738 | int inode_only = LOG_INODE_ALL; |
| 2739 | struct super_block *sb; |
| 2740 | int ret; |
| 2741 | |
| 2742 | start_log_trans(trans, root); |
| 2743 | sb = dentry->d_inode->i_sb; |
| 2744 | while(1) { |
| 2745 | ret = __btrfs_log_inode(trans, root, dentry->d_inode, |
| 2746 | inode_only); |
| 2747 | BUG_ON(ret); |
| 2748 | inode_only = LOG_INODE_EXISTS; |
| 2749 | |
| 2750 | dentry = dentry->d_parent; |
| 2751 | if (!dentry || !dentry->d_inode || sb != dentry->d_inode->i_sb) |
| 2752 | break; |
| 2753 | |
| 2754 | if (BTRFS_I(dentry->d_inode)->generation <= |
| 2755 | root->fs_info->last_trans_committed) |
| 2756 | break; |
| 2757 | } |
| 2758 | end_log_trans(root); |
| 2759 | return 0; |
| 2760 | } |
| 2761 | |
| 2762 | /* |
| 2763 | * it is not safe to log dentry if the chunk root has added new |
| 2764 | * chunks. This returns 0 if the dentry was logged, and 1 otherwise. |
| 2765 | * If this returns 1, you must commit the transaction to safely get your |
| 2766 | * data on disk. |
| 2767 | */ |
| 2768 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, |
| 2769 | struct btrfs_root *root, struct dentry *dentry) |
| 2770 | { |
| 2771 | u64 gen; |
| 2772 | gen = root->fs_info->last_trans_new_blockgroup; |
| 2773 | if (gen > root->fs_info->last_trans_committed) |
| 2774 | return 1; |
| 2775 | else |
| 2776 | return btrfs_log_dentry(trans, root, dentry); |
| 2777 | } |
| 2778 | |
| 2779 | /* |
| 2780 | * should be called during mount to recover any replay any log trees |
| 2781 | * from the FS |
| 2782 | */ |
| 2783 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) |
| 2784 | { |
| 2785 | int ret; |
| 2786 | struct btrfs_path *path; |
| 2787 | struct btrfs_trans_handle *trans; |
| 2788 | struct btrfs_key key; |
| 2789 | struct btrfs_key found_key; |
| 2790 | struct btrfs_key tmp_key; |
| 2791 | struct btrfs_root *log; |
| 2792 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; |
Chris Mason | 8d5bf1c | 2008-09-11 15:51:21 -0400 | [diff] [blame] | 2793 | u64 highest_inode; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2794 | struct walk_control wc = { |
| 2795 | .process_func = process_one_buffer, |
| 2796 | .stage = 0, |
| 2797 | }; |
| 2798 | |
| 2799 | fs_info->log_root_recovering = 1; |
| 2800 | path = btrfs_alloc_path(); |
| 2801 | BUG_ON(!path); |
| 2802 | |
| 2803 | trans = btrfs_start_transaction(fs_info->tree_root, 1); |
| 2804 | |
| 2805 | wc.trans = trans; |
| 2806 | wc.pin = 1; |
| 2807 | |
| 2808 | walk_log_tree(trans, log_root_tree, &wc); |
| 2809 | |
| 2810 | again: |
| 2811 | key.objectid = BTRFS_TREE_LOG_OBJECTID; |
| 2812 | key.offset = (u64)-1; |
| 2813 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 2814 | |
| 2815 | while(1) { |
| 2816 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); |
| 2817 | if (ret < 0) |
| 2818 | break; |
| 2819 | if (ret > 0) { |
| 2820 | if (path->slots[0] == 0) |
| 2821 | break; |
| 2822 | path->slots[0]--; |
| 2823 | } |
| 2824 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2825 | path->slots[0]); |
| 2826 | btrfs_release_path(log_root_tree, path); |
| 2827 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 2828 | break; |
| 2829 | |
| 2830 | log = btrfs_read_fs_root_no_radix(log_root_tree, |
| 2831 | &found_key); |
| 2832 | BUG_ON(!log); |
| 2833 | |
| 2834 | |
| 2835 | tmp_key.objectid = found_key.offset; |
| 2836 | tmp_key.type = BTRFS_ROOT_ITEM_KEY; |
| 2837 | tmp_key.offset = (u64)-1; |
| 2838 | |
| 2839 | wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key); |
| 2840 | |
| 2841 | BUG_ON(!wc.replay_dest); |
| 2842 | |
| 2843 | btrfs_record_root_in_trans(wc.replay_dest); |
| 2844 | ret = walk_log_tree(trans, log, &wc); |
| 2845 | BUG_ON(ret); |
| 2846 | |
| 2847 | if (wc.stage == LOG_WALK_REPLAY_ALL) { |
| 2848 | ret = fixup_inode_link_counts(trans, wc.replay_dest, |
| 2849 | path); |
| 2850 | BUG_ON(ret); |
| 2851 | } |
Chris Mason | 8d5bf1c | 2008-09-11 15:51:21 -0400 | [diff] [blame] | 2852 | ret = btrfs_find_highest_inode(wc.replay_dest, &highest_inode); |
| 2853 | if (ret == 0) { |
| 2854 | wc.replay_dest->highest_inode = highest_inode; |
| 2855 | wc.replay_dest->last_inode_alloc = highest_inode; |
| 2856 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2857 | |
| 2858 | key.offset = found_key.offset - 1; |
| 2859 | free_extent_buffer(log->node); |
| 2860 | kfree(log); |
| 2861 | |
| 2862 | if (found_key.offset == 0) |
| 2863 | break; |
| 2864 | } |
| 2865 | btrfs_release_path(log_root_tree, path); |
| 2866 | |
| 2867 | /* step one is to pin it all, step two is to replay just inodes */ |
| 2868 | if (wc.pin) { |
| 2869 | wc.pin = 0; |
| 2870 | wc.process_func = replay_one_buffer; |
| 2871 | wc.stage = LOG_WALK_REPLAY_INODES; |
| 2872 | goto again; |
| 2873 | } |
| 2874 | /* step three is to replay everything */ |
| 2875 | if (wc.stage < LOG_WALK_REPLAY_ALL) { |
| 2876 | wc.stage++; |
| 2877 | goto again; |
| 2878 | } |
| 2879 | |
| 2880 | btrfs_free_path(path); |
| 2881 | |
| 2882 | free_extent_buffer(log_root_tree->node); |
| 2883 | log_root_tree->log_root = NULL; |
| 2884 | fs_info->log_root_recovering = 0; |
| 2885 | |
| 2886 | /* step 4: commit the transaction, which also unpins the blocks */ |
| 2887 | btrfs_commit_transaction(trans, fs_info->tree_root); |
| 2888 | |
| 2889 | kfree(log_root_tree); |
| 2890 | return 0; |
| 2891 | } |