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