1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2008 Oracle. All rights reserved. 4 */ 5 6 #include <linux/sched.h> 7 #include <linux/slab.h> 8 #include <linux/blkdev.h> 9 #include <linux/list_sort.h> 10 #include <linux/iversion.h> 11 #include "misc.h" 12 #include "ctree.h" 13 #include "tree-log.h" 14 #include "disk-io.h" 15 #include "locking.h" 16 #include "backref.h" 17 #include "compression.h" 18 #include "qgroup.h" 19 #include "block-group.h" 20 #include "space-info.h" 21 #include "inode-item.h" 22 #include "fs.h" 23 #include "accessors.h" 24 #include "extent-tree.h" 25 #include "root-tree.h" 26 #include "dir-item.h" 27 #include "file-item.h" 28 #include "file.h" 29 #include "orphan.h" 30 #include "tree-checker.h" 31 32 #define MAX_CONFLICT_INODES 10 33 34 /* magic values for the inode_only field in btrfs_log_inode: 35 * 36 * LOG_INODE_ALL means to log everything 37 * LOG_INODE_EXISTS means to log just enough to recreate the inode 38 * during log replay 39 */ 40 enum { 41 LOG_INODE_ALL, 42 LOG_INODE_EXISTS, 43 }; 44 45 /* 46 * directory trouble cases 47 * 48 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync 49 * log, we must force a full commit before doing an fsync of the directory 50 * where the unlink was done. 51 * ---> record transid of last unlink/rename per directory 52 * 53 * mkdir foo/some_dir 54 * normal commit 55 * rename foo/some_dir foo2/some_dir 56 * mkdir foo/some_dir 57 * fsync foo/some_dir/some_file 58 * 59 * The fsync above will unlink the original some_dir without recording 60 * it in its new location (foo2). After a crash, some_dir will be gone 61 * unless the fsync of some_file forces a full commit 62 * 63 * 2) we must log any new names for any file or dir that is in the fsync 64 * log. ---> check inode while renaming/linking. 65 * 66 * 2a) we must log any new names for any file or dir during rename 67 * when the directory they are being removed from was logged. 68 * ---> check inode and old parent dir during rename 69 * 70 * 2a is actually the more important variant. With the extra logging 71 * a crash might unlink the old name without recreating the new one 72 * 73 * 3) after a crash, we must go through any directories with a link count 74 * of zero and redo the rm -rf 75 * 76 * mkdir f1/foo 77 * normal commit 78 * rm -rf f1/foo 79 * fsync(f1) 80 * 81 * The directory f1 was fully removed from the FS, but fsync was never 82 * called on f1, only its parent dir. After a crash the rm -rf must 83 * be replayed. This must be able to recurse down the entire 84 * directory tree. The inode link count fixup code takes care of the 85 * ugly details. 86 */ 87 88 /* 89 * stages for the tree walking. The first 90 * stage (0) is to only pin down the blocks we find 91 * the second stage (1) is to make sure that all the inodes 92 * we find in the log are created in the subvolume. 93 * 94 * The last stage is to deal with directories and links and extents 95 * and all the other fun semantics 96 */ 97 enum { 98 LOG_WALK_PIN_ONLY, 99 LOG_WALK_REPLAY_INODES, 100 LOG_WALK_REPLAY_DIR_INDEX, 101 LOG_WALK_REPLAY_ALL, 102 }; 103 104 static int btrfs_log_inode(struct btrfs_trans_handle *trans, 105 struct btrfs_inode *inode, 106 int inode_only, 107 struct btrfs_log_ctx *ctx); 108 static int link_to_fixup_dir(struct btrfs_trans_handle *trans, 109 struct btrfs_root *root, 110 struct btrfs_path *path, u64 objectid); 111 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, 112 struct btrfs_root *root, 113 struct btrfs_root *log, 114 struct btrfs_path *path, 115 u64 dirid, int del_all); 116 static void wait_log_commit(struct btrfs_root *root, int transid); 117 118 /* 119 * tree logging is a special write ahead log used to make sure that 120 * fsyncs and O_SYNCs can happen without doing full tree commits. 121 * 122 * Full tree commits are expensive because they require commonly 123 * modified blocks to be recowed, creating many dirty pages in the 124 * extent tree an 4x-6x higher write load than ext3. 125 * 126 * Instead of doing a tree commit on every fsync, we use the 127 * key ranges and transaction ids to find items for a given file or directory 128 * that have changed in this transaction. Those items are copied into 129 * a special tree (one per subvolume root), that tree is written to disk 130 * and then the fsync is considered complete. 131 * 132 * After a crash, items are copied out of the log-tree back into the 133 * subvolume tree. Any file data extents found are recorded in the extent 134 * allocation tree, and the log-tree freed. 135 * 136 * The log tree is read three times, once to pin down all the extents it is 137 * using in ram and once, once to create all the inodes logged in the tree 138 * and once to do all the other items. 139 */ 140 141 static struct inode *btrfs_iget_logging(u64 objectid, struct btrfs_root *root) 142 { 143 unsigned int nofs_flag; 144 struct inode *inode; 145 146 /* 147 * We're holding a transaction handle whether we are logging or 148 * replaying a log tree, so we must make sure NOFS semantics apply 149 * because btrfs_alloc_inode() may be triggered and it uses GFP_KERNEL 150 * to allocate an inode, which can recurse back into the filesystem and 151 * attempt a transaction commit, resulting in a deadlock. 152 */ 153 nofs_flag = memalloc_nofs_save(); 154 inode = btrfs_iget(objectid, root); 155 memalloc_nofs_restore(nofs_flag); 156 157 return inode; 158 } 159 160 /* 161 * start a sub transaction and setup the log tree 162 * this increments the log tree writer count to make the people 163 * syncing the tree wait for us to finish 164 */ 165 static int start_log_trans(struct btrfs_trans_handle *trans, 166 struct btrfs_root *root, 167 struct btrfs_log_ctx *ctx) 168 { 169 struct btrfs_fs_info *fs_info = root->fs_info; 170 struct btrfs_root *tree_root = fs_info->tree_root; 171 const bool zoned = btrfs_is_zoned(fs_info); 172 int ret = 0; 173 bool created = false; 174 175 /* 176 * First check if the log root tree was already created. If not, create 177 * it before locking the root's log_mutex, just to keep lockdep happy. 178 */ 179 if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state)) { 180 mutex_lock(&tree_root->log_mutex); 181 if (!fs_info->log_root_tree) { 182 ret = btrfs_init_log_root_tree(trans, fs_info); 183 if (!ret) { 184 set_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state); 185 created = true; 186 } 187 } 188 mutex_unlock(&tree_root->log_mutex); 189 if (ret) 190 return ret; 191 } 192 193 mutex_lock(&root->log_mutex); 194 195 again: 196 if (root->log_root) { 197 int index = (root->log_transid + 1) % 2; 198 199 if (btrfs_need_log_full_commit(trans)) { 200 ret = BTRFS_LOG_FORCE_COMMIT; 201 goto out; 202 } 203 204 if (zoned && atomic_read(&root->log_commit[index])) { 205 wait_log_commit(root, root->log_transid - 1); 206 goto again; 207 } 208 209 if (!root->log_start_pid) { 210 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); 211 root->log_start_pid = current->pid; 212 } else if (root->log_start_pid != current->pid) { 213 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); 214 } 215 } else { 216 /* 217 * This means fs_info->log_root_tree was already created 218 * for some other FS trees. Do the full commit not to mix 219 * nodes from multiple log transactions to do sequential 220 * writing. 221 */ 222 if (zoned && !created) { 223 ret = BTRFS_LOG_FORCE_COMMIT; 224 goto out; 225 } 226 227 ret = btrfs_add_log_tree(trans, root); 228 if (ret) 229 goto out; 230 231 set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state); 232 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); 233 root->log_start_pid = current->pid; 234 } 235 236 atomic_inc(&root->log_writers); 237 if (!ctx->logging_new_name) { 238 int index = root->log_transid % 2; 239 list_add_tail(&ctx->list, &root->log_ctxs[index]); 240 ctx->log_transid = root->log_transid; 241 } 242 243 out: 244 mutex_unlock(&root->log_mutex); 245 return ret; 246 } 247 248 /* 249 * returns 0 if there was a log transaction running and we were able 250 * to join, or returns -ENOENT if there were not transactions 251 * in progress 252 */ 253 static int join_running_log_trans(struct btrfs_root *root) 254 { 255 const bool zoned = btrfs_is_zoned(root->fs_info); 256 int ret = -ENOENT; 257 258 if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state)) 259 return ret; 260 261 mutex_lock(&root->log_mutex); 262 again: 263 if (root->log_root) { 264 int index = (root->log_transid + 1) % 2; 265 266 ret = 0; 267 if (zoned && atomic_read(&root->log_commit[index])) { 268 wait_log_commit(root, root->log_transid - 1); 269 goto again; 270 } 271 atomic_inc(&root->log_writers); 272 } 273 mutex_unlock(&root->log_mutex); 274 return ret; 275 } 276 277 /* 278 * This either makes the current running log transaction wait 279 * until you call btrfs_end_log_trans() or it makes any future 280 * log transactions wait until you call btrfs_end_log_trans() 281 */ 282 void btrfs_pin_log_trans(struct btrfs_root *root) 283 { 284 atomic_inc(&root->log_writers); 285 } 286 287 /* 288 * indicate we're done making changes to the log tree 289 * and wake up anyone waiting to do a sync 290 */ 291 void btrfs_end_log_trans(struct btrfs_root *root) 292 { 293 if (atomic_dec_and_test(&root->log_writers)) { 294 /* atomic_dec_and_test implies a barrier */ 295 cond_wake_up_nomb(&root->log_writer_wait); 296 } 297 } 298 299 /* 300 * the walk control struct is used to pass state down the chain when 301 * processing the log tree. The stage field tells us which part 302 * of the log tree processing we are currently doing. The others 303 * are state fields used for that specific part 304 */ 305 struct walk_control { 306 /* should we free the extent on disk when done? This is used 307 * at transaction commit time while freeing a log tree 308 */ 309 int free; 310 311 /* pin only walk, we record which extents on disk belong to the 312 * log trees 313 */ 314 int pin; 315 316 /* what stage of the replay code we're currently in */ 317 int stage; 318 319 /* 320 * Ignore any items from the inode currently being processed. Needs 321 * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in 322 * the LOG_WALK_REPLAY_INODES stage. 323 */ 324 bool ignore_cur_inode; 325 326 /* the root we are currently replaying */ 327 struct btrfs_root *replay_dest; 328 329 /* the trans handle for the current replay */ 330 struct btrfs_trans_handle *trans; 331 332 /* the function that gets used to process blocks we find in the 333 * tree. Note the extent_buffer might not be up to date when it is 334 * passed in, and it must be checked or read if you need the data 335 * inside it 336 */ 337 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb, 338 struct walk_control *wc, u64 gen, int level); 339 }; 340 341 /* 342 * process_func used to pin down extents, write them or wait on them 343 */ 344 static int process_one_buffer(struct btrfs_root *log, 345 struct extent_buffer *eb, 346 struct walk_control *wc, u64 gen, int level) 347 { 348 struct btrfs_fs_info *fs_info = log->fs_info; 349 int ret = 0; 350 351 /* 352 * If this fs is mixed then we need to be able to process the leaves to 353 * pin down any logged extents, so we have to read the block. 354 */ 355 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { 356 struct btrfs_tree_parent_check check = { 357 .level = level, 358 .transid = gen 359 }; 360 361 ret = btrfs_read_extent_buffer(eb, &check); 362 if (ret) 363 return ret; 364 } 365 366 if (wc->pin) { 367 ret = btrfs_pin_extent_for_log_replay(wc->trans, eb); 368 if (ret) 369 return ret; 370 371 if (btrfs_buffer_uptodate(eb, gen, 0) && 372 btrfs_header_level(eb) == 0) 373 ret = btrfs_exclude_logged_extents(eb); 374 } 375 return ret; 376 } 377 378 /* 379 * Item overwrite used by replay and tree logging. eb, slot and key all refer 380 * to the src data we are copying out. 381 * 382 * root is the tree we are copying into, and path is a scratch 383 * path for use in this function (it should be released on entry and 384 * will be released on exit). 385 * 386 * If the key is already in the destination tree the existing item is 387 * overwritten. If the existing item isn't big enough, it is extended. 388 * If it is too large, it is truncated. 389 * 390 * If the key isn't in the destination yet, a new item is inserted. 391 */ 392 static int overwrite_item(struct btrfs_trans_handle *trans, 393 struct btrfs_root *root, 394 struct btrfs_path *path, 395 struct extent_buffer *eb, int slot, 396 struct btrfs_key *key) 397 { 398 int ret; 399 u32 item_size; 400 u64 saved_i_size = 0; 401 int save_old_i_size = 0; 402 unsigned long src_ptr; 403 unsigned long dst_ptr; 404 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY; 405 406 /* 407 * This is only used during log replay, so the root is always from a 408 * fs/subvolume tree. In case we ever need to support a log root, then 409 * we'll have to clone the leaf in the path, release the path and use 410 * the leaf before writing into the log tree. See the comments at 411 * copy_items() for more details. 412 */ 413 ASSERT(btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID); 414 415 item_size = btrfs_item_size(eb, slot); 416 src_ptr = btrfs_item_ptr_offset(eb, slot); 417 418 /* Look for the key in the destination tree. */ 419 ret = btrfs_search_slot(NULL, root, key, path, 0, 0); 420 if (ret < 0) 421 return ret; 422 423 if (ret == 0) { 424 char *src_copy; 425 char *dst_copy; 426 u32 dst_size = btrfs_item_size(path->nodes[0], 427 path->slots[0]); 428 if (dst_size != item_size) 429 goto insert; 430 431 if (item_size == 0) { 432 btrfs_release_path(path); 433 return 0; 434 } 435 dst_copy = kmalloc(item_size, GFP_NOFS); 436 src_copy = kmalloc(item_size, GFP_NOFS); 437 if (!dst_copy || !src_copy) { 438 btrfs_release_path(path); 439 kfree(dst_copy); 440 kfree(src_copy); 441 return -ENOMEM; 442 } 443 444 read_extent_buffer(eb, src_copy, src_ptr, item_size); 445 446 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); 447 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, 448 item_size); 449 ret = memcmp(dst_copy, src_copy, item_size); 450 451 kfree(dst_copy); 452 kfree(src_copy); 453 /* 454 * they have the same contents, just return, this saves 455 * us from cowing blocks in the destination tree and doing 456 * extra writes that may not have been done by a previous 457 * sync 458 */ 459 if (ret == 0) { 460 btrfs_release_path(path); 461 return 0; 462 } 463 464 /* 465 * We need to load the old nbytes into the inode so when we 466 * replay the extents we've logged we get the right nbytes. 467 */ 468 if (inode_item) { 469 struct btrfs_inode_item *item; 470 u64 nbytes; 471 u32 mode; 472 473 item = btrfs_item_ptr(path->nodes[0], path->slots[0], 474 struct btrfs_inode_item); 475 nbytes = btrfs_inode_nbytes(path->nodes[0], item); 476 item = btrfs_item_ptr(eb, slot, 477 struct btrfs_inode_item); 478 btrfs_set_inode_nbytes(eb, item, nbytes); 479 480 /* 481 * If this is a directory we need to reset the i_size to 482 * 0 so that we can set it up properly when replaying 483 * the rest of the items in this log. 484 */ 485 mode = btrfs_inode_mode(eb, item); 486 if (S_ISDIR(mode)) 487 btrfs_set_inode_size(eb, item, 0); 488 } 489 } else if (inode_item) { 490 struct btrfs_inode_item *item; 491 u32 mode; 492 493 /* 494 * New inode, set nbytes to 0 so that the nbytes comes out 495 * properly when we replay the extents. 496 */ 497 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); 498 btrfs_set_inode_nbytes(eb, item, 0); 499 500 /* 501 * If this is a directory we need to reset the i_size to 0 so 502 * that we can set it up properly when replaying the rest of 503 * the items in this log. 504 */ 505 mode = btrfs_inode_mode(eb, item); 506 if (S_ISDIR(mode)) 507 btrfs_set_inode_size(eb, item, 0); 508 } 509 insert: 510 btrfs_release_path(path); 511 /* try to insert the key into the destination tree */ 512 path->skip_release_on_error = 1; 513 ret = btrfs_insert_empty_item(trans, root, path, 514 key, item_size); 515 path->skip_release_on_error = 0; 516 517 /* make sure any existing item is the correct size */ 518 if (ret == -EEXIST || ret == -EOVERFLOW) { 519 u32 found_size; 520 found_size = btrfs_item_size(path->nodes[0], 521 path->slots[0]); 522 if (found_size > item_size) 523 btrfs_truncate_item(trans, path, item_size, 1); 524 else if (found_size < item_size) 525 btrfs_extend_item(trans, path, item_size - found_size); 526 } else if (ret) { 527 return ret; 528 } 529 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], 530 path->slots[0]); 531 532 /* don't overwrite an existing inode if the generation number 533 * was logged as zero. This is done when the tree logging code 534 * is just logging an inode to make sure it exists after recovery. 535 * 536 * Also, don't overwrite i_size on directories during replay. 537 * log replay inserts and removes directory items based on the 538 * state of the tree found in the subvolume, and i_size is modified 539 * as it goes 540 */ 541 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) { 542 struct btrfs_inode_item *src_item; 543 struct btrfs_inode_item *dst_item; 544 545 src_item = (struct btrfs_inode_item *)src_ptr; 546 dst_item = (struct btrfs_inode_item *)dst_ptr; 547 548 if (btrfs_inode_generation(eb, src_item) == 0) { 549 struct extent_buffer *dst_eb = path->nodes[0]; 550 const u64 ino_size = btrfs_inode_size(eb, src_item); 551 552 /* 553 * For regular files an ino_size == 0 is used only when 554 * logging that an inode exists, as part of a directory 555 * fsync, and the inode wasn't fsynced before. In this 556 * case don't set the size of the inode in the fs/subvol 557 * tree, otherwise we would be throwing valid data away. 558 */ 559 if (S_ISREG(btrfs_inode_mode(eb, src_item)) && 560 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) && 561 ino_size != 0) 562 btrfs_set_inode_size(dst_eb, dst_item, ino_size); 563 goto no_copy; 564 } 565 566 if (S_ISDIR(btrfs_inode_mode(eb, src_item)) && 567 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { 568 save_old_i_size = 1; 569 saved_i_size = btrfs_inode_size(path->nodes[0], 570 dst_item); 571 } 572 } 573 574 copy_extent_buffer(path->nodes[0], eb, dst_ptr, 575 src_ptr, item_size); 576 577 if (save_old_i_size) { 578 struct btrfs_inode_item *dst_item; 579 dst_item = (struct btrfs_inode_item *)dst_ptr; 580 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); 581 } 582 583 /* make sure the generation is filled in */ 584 if (key->type == BTRFS_INODE_ITEM_KEY) { 585 struct btrfs_inode_item *dst_item; 586 dst_item = (struct btrfs_inode_item *)dst_ptr; 587 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { 588 btrfs_set_inode_generation(path->nodes[0], dst_item, 589 trans->transid); 590 } 591 } 592 no_copy: 593 btrfs_release_path(path); 594 return 0; 595 } 596 597 static int read_alloc_one_name(struct extent_buffer *eb, void *start, int len, 598 struct fscrypt_str *name) 599 { 600 char *buf; 601 602 buf = kmalloc(len, GFP_NOFS); 603 if (!buf) 604 return -ENOMEM; 605 606 read_extent_buffer(eb, buf, (unsigned long)start, len); 607 name->name = buf; 608 name->len = len; 609 return 0; 610 } 611 612 /* 613 * simple helper to read an inode off the disk from a given root 614 * This can only be called for subvolume roots and not for the log 615 */ 616 static noinline struct inode *read_one_inode(struct btrfs_root *root, 617 u64 objectid) 618 { 619 struct inode *inode; 620 621 inode = btrfs_iget_logging(objectid, root); 622 if (IS_ERR(inode)) 623 inode = NULL; 624 return inode; 625 } 626 627 /* replays a single extent in 'eb' at 'slot' with 'key' into the 628 * subvolume 'root'. path is released on entry and should be released 629 * on exit. 630 * 631 * extents in the log tree have not been allocated out of the extent 632 * tree yet. So, this completes the allocation, taking a reference 633 * as required if the extent already exists or creating a new extent 634 * if it isn't in the extent allocation tree yet. 635 * 636 * The extent is inserted into the file, dropping any existing extents 637 * from the file that overlap the new one. 638 */ 639 static noinline int replay_one_extent(struct btrfs_trans_handle *trans, 640 struct btrfs_root *root, 641 struct btrfs_path *path, 642 struct extent_buffer *eb, int slot, 643 struct btrfs_key *key) 644 { 645 struct btrfs_drop_extents_args drop_args = { 0 }; 646 struct btrfs_fs_info *fs_info = root->fs_info; 647 int found_type; 648 u64 extent_end; 649 u64 start = key->offset; 650 u64 nbytes = 0; 651 struct btrfs_file_extent_item *item; 652 struct inode *inode = NULL; 653 unsigned long size; 654 int ret = 0; 655 656 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 657 found_type = btrfs_file_extent_type(eb, item); 658 659 if (found_type == BTRFS_FILE_EXTENT_REG || 660 found_type == BTRFS_FILE_EXTENT_PREALLOC) { 661 nbytes = btrfs_file_extent_num_bytes(eb, item); 662 extent_end = start + nbytes; 663 664 /* 665 * We don't add to the inodes nbytes if we are prealloc or a 666 * hole. 667 */ 668 if (btrfs_file_extent_disk_bytenr(eb, item) == 0) 669 nbytes = 0; 670 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { 671 size = btrfs_file_extent_ram_bytes(eb, item); 672 nbytes = btrfs_file_extent_ram_bytes(eb, item); 673 extent_end = ALIGN(start + size, 674 fs_info->sectorsize); 675 } else { 676 ret = 0; 677 goto out; 678 } 679 680 inode = read_one_inode(root, key->objectid); 681 if (!inode) { 682 ret = -EIO; 683 goto out; 684 } 685 686 /* 687 * first check to see if we already have this extent in the 688 * file. This must be done before the btrfs_drop_extents run 689 * so we don't try to drop this extent. 690 */ 691 ret = btrfs_lookup_file_extent(trans, root, path, 692 btrfs_ino(BTRFS_I(inode)), start, 0); 693 694 if (ret == 0 && 695 (found_type == BTRFS_FILE_EXTENT_REG || 696 found_type == BTRFS_FILE_EXTENT_PREALLOC)) { 697 struct btrfs_file_extent_item cmp1; 698 struct btrfs_file_extent_item cmp2; 699 struct btrfs_file_extent_item *existing; 700 struct extent_buffer *leaf; 701 702 leaf = path->nodes[0]; 703 existing = btrfs_item_ptr(leaf, path->slots[0], 704 struct btrfs_file_extent_item); 705 706 read_extent_buffer(eb, &cmp1, (unsigned long)item, 707 sizeof(cmp1)); 708 read_extent_buffer(leaf, &cmp2, (unsigned long)existing, 709 sizeof(cmp2)); 710 711 /* 712 * we already have a pointer to this exact extent, 713 * we don't have to do anything 714 */ 715 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { 716 btrfs_release_path(path); 717 goto out; 718 } 719 } 720 btrfs_release_path(path); 721 722 /* drop any overlapping extents */ 723 drop_args.start = start; 724 drop_args.end = extent_end; 725 drop_args.drop_cache = true; 726 ret = btrfs_drop_extents(trans, root, BTRFS_I(inode), &drop_args); 727 if (ret) 728 goto out; 729 730 if (found_type == BTRFS_FILE_EXTENT_REG || 731 found_type == BTRFS_FILE_EXTENT_PREALLOC) { 732 u64 offset; 733 unsigned long dest_offset; 734 struct btrfs_key ins; 735 736 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 && 737 btrfs_fs_incompat(fs_info, NO_HOLES)) 738 goto update_inode; 739 740 ret = btrfs_insert_empty_item(trans, root, path, key, 741 sizeof(*item)); 742 if (ret) 743 goto out; 744 dest_offset = btrfs_item_ptr_offset(path->nodes[0], 745 path->slots[0]); 746 copy_extent_buffer(path->nodes[0], eb, dest_offset, 747 (unsigned long)item, sizeof(*item)); 748 749 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item); 750 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item); 751 ins.type = BTRFS_EXTENT_ITEM_KEY; 752 offset = key->offset - btrfs_file_extent_offset(eb, item); 753 754 /* 755 * Manually record dirty extent, as here we did a shallow 756 * file extent item copy and skip normal backref update, 757 * but modifying extent tree all by ourselves. 758 * So need to manually record dirty extent for qgroup, 759 * as the owner of the file extent changed from log tree 760 * (doesn't affect qgroup) to fs/file tree(affects qgroup) 761 */ 762 ret = btrfs_qgroup_trace_extent(trans, 763 btrfs_file_extent_disk_bytenr(eb, item), 764 btrfs_file_extent_disk_num_bytes(eb, item)); 765 if (ret < 0) 766 goto out; 767 768 if (ins.objectid > 0) { 769 u64 csum_start; 770 u64 csum_end; 771 LIST_HEAD(ordered_sums); 772 773 /* 774 * is this extent already allocated in the extent 775 * allocation tree? If so, just add a reference 776 */ 777 ret = btrfs_lookup_data_extent(fs_info, ins.objectid, 778 ins.offset); 779 if (ret < 0) { 780 goto out; 781 } else if (ret == 0) { 782 struct btrfs_ref ref = { 783 .action = BTRFS_ADD_DELAYED_REF, 784 .bytenr = ins.objectid, 785 .num_bytes = ins.offset, 786 .owning_root = btrfs_root_id(root), 787 .ref_root = btrfs_root_id(root), 788 }; 789 btrfs_init_data_ref(&ref, key->objectid, offset, 790 0, false); 791 ret = btrfs_inc_extent_ref(trans, &ref); 792 if (ret) 793 goto out; 794 } else { 795 /* 796 * insert the extent pointer in the extent 797 * allocation tree 798 */ 799 ret = btrfs_alloc_logged_file_extent(trans, 800 btrfs_root_id(root), 801 key->objectid, offset, &ins); 802 if (ret) 803 goto out; 804 } 805 btrfs_release_path(path); 806 807 if (btrfs_file_extent_compression(eb, item)) { 808 csum_start = ins.objectid; 809 csum_end = csum_start + ins.offset; 810 } else { 811 csum_start = ins.objectid + 812 btrfs_file_extent_offset(eb, item); 813 csum_end = csum_start + 814 btrfs_file_extent_num_bytes(eb, item); 815 } 816 817 ret = btrfs_lookup_csums_list(root->log_root, 818 csum_start, csum_end - 1, 819 &ordered_sums, false); 820 if (ret < 0) 821 goto out; 822 ret = 0; 823 /* 824 * Now delete all existing cums in the csum root that 825 * cover our range. We do this because we can have an 826 * extent that is completely referenced by one file 827 * extent item and partially referenced by another 828 * file extent item (like after using the clone or 829 * extent_same ioctls). In this case if we end up doing 830 * the replay of the one that partially references the 831 * extent first, and we do not do the csum deletion 832 * below, we can get 2 csum items in the csum tree that 833 * overlap each other. For example, imagine our log has 834 * the two following file extent items: 835 * 836 * key (257 EXTENT_DATA 409600) 837 * extent data disk byte 12845056 nr 102400 838 * extent data offset 20480 nr 20480 ram 102400 839 * 840 * key (257 EXTENT_DATA 819200) 841 * extent data disk byte 12845056 nr 102400 842 * extent data offset 0 nr 102400 ram 102400 843 * 844 * Where the second one fully references the 100K extent 845 * that starts at disk byte 12845056, and the log tree 846 * has a single csum item that covers the entire range 847 * of the extent: 848 * 849 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 850 * 851 * After the first file extent item is replayed, the 852 * csum tree gets the following csum item: 853 * 854 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 855 * 856 * Which covers the 20K sub-range starting at offset 20K 857 * of our extent. Now when we replay the second file 858 * extent item, if we do not delete existing csum items 859 * that cover any of its blocks, we end up getting two 860 * csum items in our csum tree that overlap each other: 861 * 862 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 863 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 864 * 865 * Which is a problem, because after this anyone trying 866 * to lookup up for the checksum of any block of our 867 * extent starting at an offset of 40K or higher, will 868 * end up looking at the second csum item only, which 869 * does not contain the checksum for any block starting 870 * at offset 40K or higher of our extent. 871 */ 872 while (!list_empty(&ordered_sums)) { 873 struct btrfs_ordered_sum *sums; 874 struct btrfs_root *csum_root; 875 876 sums = list_entry(ordered_sums.next, 877 struct btrfs_ordered_sum, 878 list); 879 csum_root = btrfs_csum_root(fs_info, 880 sums->logical); 881 if (!ret) 882 ret = btrfs_del_csums(trans, csum_root, 883 sums->logical, 884 sums->len); 885 if (!ret) 886 ret = btrfs_csum_file_blocks(trans, 887 csum_root, 888 sums); 889 list_del(&sums->list); 890 kfree(sums); 891 } 892 if (ret) 893 goto out; 894 } else { 895 btrfs_release_path(path); 896 } 897 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { 898 /* inline extents are easy, we just overwrite them */ 899 ret = overwrite_item(trans, root, path, eb, slot, key); 900 if (ret) 901 goto out; 902 } 903 904 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, 905 extent_end - start); 906 if (ret) 907 goto out; 908 909 update_inode: 910 btrfs_update_inode_bytes(BTRFS_I(inode), nbytes, drop_args.bytes_found); 911 ret = btrfs_update_inode(trans, BTRFS_I(inode)); 912 out: 913 iput(inode); 914 return ret; 915 } 916 917 static int unlink_inode_for_log_replay(struct btrfs_trans_handle *trans, 918 struct btrfs_inode *dir, 919 struct btrfs_inode *inode, 920 const struct fscrypt_str *name) 921 { 922 int ret; 923 924 ret = btrfs_unlink_inode(trans, dir, inode, name); 925 if (ret) 926 return ret; 927 /* 928 * Whenever we need to check if a name exists or not, we check the 929 * fs/subvolume tree. So after an unlink we must run delayed items, so 930 * that future checks for a name during log replay see that the name 931 * does not exists anymore. 932 */ 933 return btrfs_run_delayed_items(trans); 934 } 935 936 /* 937 * when cleaning up conflicts between the directory names in the 938 * subvolume, directory names in the log and directory names in the 939 * inode back references, we may have to unlink inodes from directories. 940 * 941 * This is a helper function to do the unlink of a specific directory 942 * item 943 */ 944 static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, 945 struct btrfs_path *path, 946 struct btrfs_inode *dir, 947 struct btrfs_dir_item *di) 948 { 949 struct btrfs_root *root = dir->root; 950 struct inode *inode; 951 struct fscrypt_str name; 952 struct extent_buffer *leaf; 953 struct btrfs_key location; 954 int ret; 955 956 leaf = path->nodes[0]; 957 958 btrfs_dir_item_key_to_cpu(leaf, di, &location); 959 ret = read_alloc_one_name(leaf, di + 1, btrfs_dir_name_len(leaf, di), &name); 960 if (ret) 961 return -ENOMEM; 962 963 btrfs_release_path(path); 964 965 inode = read_one_inode(root, location.objectid); 966 if (!inode) { 967 ret = -EIO; 968 goto out; 969 } 970 971 ret = link_to_fixup_dir(trans, root, path, location.objectid); 972 if (ret) 973 goto out; 974 975 ret = unlink_inode_for_log_replay(trans, dir, BTRFS_I(inode), &name); 976 out: 977 kfree(name.name); 978 iput(inode); 979 return ret; 980 } 981 982 /* 983 * See if a given name and sequence number found in an inode back reference are 984 * already in a directory and correctly point to this inode. 985 * 986 * Returns: < 0 on error, 0 if the directory entry does not exists and 1 if it 987 * exists. 988 */ 989 static noinline int inode_in_dir(struct btrfs_root *root, 990 struct btrfs_path *path, 991 u64 dirid, u64 objectid, u64 index, 992 struct fscrypt_str *name) 993 { 994 struct btrfs_dir_item *di; 995 struct btrfs_key location; 996 int ret = 0; 997 998 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, 999 index, name, 0); 1000 if (IS_ERR(di)) { 1001 ret = PTR_ERR(di); 1002 goto out; 1003 } else if (di) { 1004 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 1005 if (location.objectid != objectid) 1006 goto out; 1007 } else { 1008 goto out; 1009 } 1010 1011 btrfs_release_path(path); 1012 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, 0); 1013 if (IS_ERR(di)) { 1014 ret = PTR_ERR(di); 1015 goto out; 1016 } else if (di) { 1017 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 1018 if (location.objectid == objectid) 1019 ret = 1; 1020 } 1021 out: 1022 btrfs_release_path(path); 1023 return ret; 1024 } 1025 1026 /* 1027 * helper function to check a log tree for a named back reference in 1028 * an inode. This is used to decide if a back reference that is 1029 * found in the subvolume conflicts with what we find in the log. 1030 * 1031 * inode backreferences may have multiple refs in a single item, 1032 * during replay we process one reference at a time, and we don't 1033 * want to delete valid links to a file from the subvolume if that 1034 * link is also in the log. 1035 */ 1036 static noinline int backref_in_log(struct btrfs_root *log, 1037 struct btrfs_key *key, 1038 u64 ref_objectid, 1039 const struct fscrypt_str *name) 1040 { 1041 struct btrfs_path *path; 1042 int ret; 1043 1044 path = btrfs_alloc_path(); 1045 if (!path) 1046 return -ENOMEM; 1047 1048 ret = btrfs_search_slot(NULL, log, key, path, 0, 0); 1049 if (ret < 0) { 1050 goto out; 1051 } else if (ret == 1) { 1052 ret = 0; 1053 goto out; 1054 } 1055 1056 if (key->type == BTRFS_INODE_EXTREF_KEY) 1057 ret = !!btrfs_find_name_in_ext_backref(path->nodes[0], 1058 path->slots[0], 1059 ref_objectid, name); 1060 else 1061 ret = !!btrfs_find_name_in_backref(path->nodes[0], 1062 path->slots[0], name); 1063 out: 1064 btrfs_free_path(path); 1065 return ret; 1066 } 1067 1068 static inline int __add_inode_ref(struct btrfs_trans_handle *trans, 1069 struct btrfs_root *root, 1070 struct btrfs_path *path, 1071 struct btrfs_root *log_root, 1072 struct btrfs_inode *dir, 1073 struct btrfs_inode *inode, 1074 u64 inode_objectid, u64 parent_objectid, 1075 u64 ref_index, struct fscrypt_str *name) 1076 { 1077 int ret; 1078 struct extent_buffer *leaf; 1079 struct btrfs_dir_item *di; 1080 struct btrfs_key search_key; 1081 struct btrfs_inode_extref *extref; 1082 1083 again: 1084 /* Search old style refs */ 1085 search_key.objectid = inode_objectid; 1086 search_key.type = BTRFS_INODE_REF_KEY; 1087 search_key.offset = parent_objectid; 1088 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 1089 if (ret == 0) { 1090 struct btrfs_inode_ref *victim_ref; 1091 unsigned long ptr; 1092 unsigned long ptr_end; 1093 1094 leaf = path->nodes[0]; 1095 1096 /* are we trying to overwrite a back ref for the root directory 1097 * if so, just jump out, we're done 1098 */ 1099 if (search_key.objectid == search_key.offset) 1100 return 1; 1101 1102 /* check all the names in this back reference to see 1103 * if they are in the log. if so, we allow them to stay 1104 * otherwise they must be unlinked as a conflict 1105 */ 1106 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1107 ptr_end = ptr + btrfs_item_size(leaf, path->slots[0]); 1108 while (ptr < ptr_end) { 1109 struct fscrypt_str victim_name; 1110 1111 victim_ref = (struct btrfs_inode_ref *)ptr; 1112 ret = read_alloc_one_name(leaf, (victim_ref + 1), 1113 btrfs_inode_ref_name_len(leaf, victim_ref), 1114 &victim_name); 1115 if (ret) 1116 return ret; 1117 1118 ret = backref_in_log(log_root, &search_key, 1119 parent_objectid, &victim_name); 1120 if (ret < 0) { 1121 kfree(victim_name.name); 1122 return ret; 1123 } else if (!ret) { 1124 inc_nlink(&inode->vfs_inode); 1125 btrfs_release_path(path); 1126 1127 ret = unlink_inode_for_log_replay(trans, dir, inode, 1128 &victim_name); 1129 kfree(victim_name.name); 1130 if (ret) 1131 return ret; 1132 goto again; 1133 } 1134 kfree(victim_name.name); 1135 1136 ptr = (unsigned long)(victim_ref + 1) + victim_name.len; 1137 } 1138 } 1139 btrfs_release_path(path); 1140 1141 /* Same search but for extended refs */ 1142 extref = btrfs_lookup_inode_extref(NULL, root, path, name, 1143 inode_objectid, parent_objectid, 0, 1144 0); 1145 if (IS_ERR(extref)) { 1146 return PTR_ERR(extref); 1147 } else if (extref) { 1148 u32 item_size; 1149 u32 cur_offset = 0; 1150 unsigned long base; 1151 struct inode *victim_parent; 1152 1153 leaf = path->nodes[0]; 1154 1155 item_size = btrfs_item_size(leaf, path->slots[0]); 1156 base = btrfs_item_ptr_offset(leaf, path->slots[0]); 1157 1158 while (cur_offset < item_size) { 1159 struct fscrypt_str victim_name; 1160 1161 extref = (struct btrfs_inode_extref *)(base + cur_offset); 1162 1163 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid) 1164 goto next; 1165 1166 ret = read_alloc_one_name(leaf, &extref->name, 1167 btrfs_inode_extref_name_len(leaf, extref), 1168 &victim_name); 1169 if (ret) 1170 return ret; 1171 1172 search_key.objectid = inode_objectid; 1173 search_key.type = BTRFS_INODE_EXTREF_KEY; 1174 search_key.offset = btrfs_extref_hash(parent_objectid, 1175 victim_name.name, 1176 victim_name.len); 1177 ret = backref_in_log(log_root, &search_key, 1178 parent_objectid, &victim_name); 1179 if (ret < 0) { 1180 kfree(victim_name.name); 1181 return ret; 1182 } else if (!ret) { 1183 ret = -ENOENT; 1184 victim_parent = read_one_inode(root, 1185 parent_objectid); 1186 if (victim_parent) { 1187 inc_nlink(&inode->vfs_inode); 1188 btrfs_release_path(path); 1189 1190 ret = unlink_inode_for_log_replay(trans, 1191 BTRFS_I(victim_parent), 1192 inode, &victim_name); 1193 } 1194 iput(victim_parent); 1195 kfree(victim_name.name); 1196 if (ret) 1197 return ret; 1198 goto again; 1199 } 1200 kfree(victim_name.name); 1201 next: 1202 cur_offset += victim_name.len + sizeof(*extref); 1203 } 1204 } 1205 btrfs_release_path(path); 1206 1207 /* look for a conflicting sequence number */ 1208 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir), 1209 ref_index, name, 0); 1210 if (IS_ERR(di)) { 1211 return PTR_ERR(di); 1212 } else if (di) { 1213 ret = drop_one_dir_item(trans, path, dir, di); 1214 if (ret) 1215 return ret; 1216 } 1217 btrfs_release_path(path); 1218 1219 /* look for a conflicting name */ 1220 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), name, 0); 1221 if (IS_ERR(di)) { 1222 return PTR_ERR(di); 1223 } else if (di) { 1224 ret = drop_one_dir_item(trans, path, dir, di); 1225 if (ret) 1226 return ret; 1227 } 1228 btrfs_release_path(path); 1229 1230 return 0; 1231 } 1232 1233 static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, 1234 struct fscrypt_str *name, u64 *index, 1235 u64 *parent_objectid) 1236 { 1237 struct btrfs_inode_extref *extref; 1238 int ret; 1239 1240 extref = (struct btrfs_inode_extref *)ref_ptr; 1241 1242 ret = read_alloc_one_name(eb, &extref->name, 1243 btrfs_inode_extref_name_len(eb, extref), name); 1244 if (ret) 1245 return ret; 1246 1247 if (index) 1248 *index = btrfs_inode_extref_index(eb, extref); 1249 if (parent_objectid) 1250 *parent_objectid = btrfs_inode_extref_parent(eb, extref); 1251 1252 return 0; 1253 } 1254 1255 static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, 1256 struct fscrypt_str *name, u64 *index) 1257 { 1258 struct btrfs_inode_ref *ref; 1259 int ret; 1260 1261 ref = (struct btrfs_inode_ref *)ref_ptr; 1262 1263 ret = read_alloc_one_name(eb, ref + 1, btrfs_inode_ref_name_len(eb, ref), 1264 name); 1265 if (ret) 1266 return ret; 1267 1268 if (index) 1269 *index = btrfs_inode_ref_index(eb, ref); 1270 1271 return 0; 1272 } 1273 1274 /* 1275 * Take an inode reference item from the log tree and iterate all names from the 1276 * inode reference item in the subvolume tree with the same key (if it exists). 1277 * For any name that is not in the inode reference item from the log tree, do a 1278 * proper unlink of that name (that is, remove its entry from the inode 1279 * reference item and both dir index keys). 1280 */ 1281 static int unlink_old_inode_refs(struct btrfs_trans_handle *trans, 1282 struct btrfs_root *root, 1283 struct btrfs_path *path, 1284 struct btrfs_inode *inode, 1285 struct extent_buffer *log_eb, 1286 int log_slot, 1287 struct btrfs_key *key) 1288 { 1289 int ret; 1290 unsigned long ref_ptr; 1291 unsigned long ref_end; 1292 struct extent_buffer *eb; 1293 1294 again: 1295 btrfs_release_path(path); 1296 ret = btrfs_search_slot(NULL, root, key, path, 0, 0); 1297 if (ret > 0) { 1298 ret = 0; 1299 goto out; 1300 } 1301 if (ret < 0) 1302 goto out; 1303 1304 eb = path->nodes[0]; 1305 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]); 1306 ref_end = ref_ptr + btrfs_item_size(eb, path->slots[0]); 1307 while (ref_ptr < ref_end) { 1308 struct fscrypt_str name; 1309 u64 parent_id; 1310 1311 if (key->type == BTRFS_INODE_EXTREF_KEY) { 1312 ret = extref_get_fields(eb, ref_ptr, &name, 1313 NULL, &parent_id); 1314 } else { 1315 parent_id = key->offset; 1316 ret = ref_get_fields(eb, ref_ptr, &name, NULL); 1317 } 1318 if (ret) 1319 goto out; 1320 1321 if (key->type == BTRFS_INODE_EXTREF_KEY) 1322 ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot, 1323 parent_id, &name); 1324 else 1325 ret = !!btrfs_find_name_in_backref(log_eb, log_slot, &name); 1326 1327 if (!ret) { 1328 struct inode *dir; 1329 1330 btrfs_release_path(path); 1331 dir = read_one_inode(root, parent_id); 1332 if (!dir) { 1333 ret = -ENOENT; 1334 kfree(name.name); 1335 goto out; 1336 } 1337 ret = unlink_inode_for_log_replay(trans, BTRFS_I(dir), 1338 inode, &name); 1339 kfree(name.name); 1340 iput(dir); 1341 if (ret) 1342 goto out; 1343 goto again; 1344 } 1345 1346 kfree(name.name); 1347 ref_ptr += name.len; 1348 if (key->type == BTRFS_INODE_EXTREF_KEY) 1349 ref_ptr += sizeof(struct btrfs_inode_extref); 1350 else 1351 ref_ptr += sizeof(struct btrfs_inode_ref); 1352 } 1353 ret = 0; 1354 out: 1355 btrfs_release_path(path); 1356 return ret; 1357 } 1358 1359 /* 1360 * replay one inode back reference item found in the log tree. 1361 * eb, slot and key refer to the buffer and key found in the log tree. 1362 * root is the destination we are replaying into, and path is for temp 1363 * use by this function. (it should be released on return). 1364 */ 1365 static noinline int add_inode_ref(struct btrfs_trans_handle *trans, 1366 struct btrfs_root *root, 1367 struct btrfs_root *log, 1368 struct btrfs_path *path, 1369 struct extent_buffer *eb, int slot, 1370 struct btrfs_key *key) 1371 { 1372 struct inode *dir = NULL; 1373 struct inode *inode = NULL; 1374 unsigned long ref_ptr; 1375 unsigned long ref_end; 1376 struct fscrypt_str name = { 0 }; 1377 int ret; 1378 int log_ref_ver = 0; 1379 u64 parent_objectid; 1380 u64 inode_objectid; 1381 u64 ref_index = 0; 1382 int ref_struct_size; 1383 1384 ref_ptr = btrfs_item_ptr_offset(eb, slot); 1385 ref_end = ref_ptr + btrfs_item_size(eb, slot); 1386 1387 if (key->type == BTRFS_INODE_EXTREF_KEY) { 1388 struct btrfs_inode_extref *r; 1389 1390 ref_struct_size = sizeof(struct btrfs_inode_extref); 1391 log_ref_ver = 1; 1392 r = (struct btrfs_inode_extref *)ref_ptr; 1393 parent_objectid = btrfs_inode_extref_parent(eb, r); 1394 } else { 1395 ref_struct_size = sizeof(struct btrfs_inode_ref); 1396 parent_objectid = key->offset; 1397 } 1398 inode_objectid = key->objectid; 1399 1400 /* 1401 * it is possible that we didn't log all the parent directories 1402 * for a given inode. If we don't find the dir, just don't 1403 * copy the back ref in. The link count fixup code will take 1404 * care of the rest 1405 */ 1406 dir = read_one_inode(root, parent_objectid); 1407 if (!dir) { 1408 ret = -ENOENT; 1409 goto out; 1410 } 1411 1412 inode = read_one_inode(root, inode_objectid); 1413 if (!inode) { 1414 ret = -EIO; 1415 goto out; 1416 } 1417 1418 while (ref_ptr < ref_end) { 1419 if (log_ref_ver) { 1420 ret = extref_get_fields(eb, ref_ptr, &name, 1421 &ref_index, &parent_objectid); 1422 /* 1423 * parent object can change from one array 1424 * item to another. 1425 */ 1426 if (!dir) 1427 dir = read_one_inode(root, parent_objectid); 1428 if (!dir) { 1429 ret = -ENOENT; 1430 goto out; 1431 } 1432 } else { 1433 ret = ref_get_fields(eb, ref_ptr, &name, &ref_index); 1434 } 1435 if (ret) 1436 goto out; 1437 1438 ret = inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)), 1439 btrfs_ino(BTRFS_I(inode)), ref_index, &name); 1440 if (ret < 0) { 1441 goto out; 1442 } else if (ret == 0) { 1443 /* 1444 * look for a conflicting back reference in the 1445 * metadata. if we find one we have to unlink that name 1446 * of the file before we add our new link. Later on, we 1447 * overwrite any existing back reference, and we don't 1448 * want to create dangling pointers in the directory. 1449 */ 1450 ret = __add_inode_ref(trans, root, path, log, 1451 BTRFS_I(dir), BTRFS_I(inode), 1452 inode_objectid, parent_objectid, 1453 ref_index, &name); 1454 if (ret) { 1455 if (ret == 1) 1456 ret = 0; 1457 goto out; 1458 } 1459 1460 /* insert our name */ 1461 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), 1462 &name, 0, ref_index); 1463 if (ret) 1464 goto out; 1465 1466 ret = btrfs_update_inode(trans, BTRFS_I(inode)); 1467 if (ret) 1468 goto out; 1469 } 1470 /* Else, ret == 1, we already have a perfect match, we're done. */ 1471 1472 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + name.len; 1473 kfree(name.name); 1474 name.name = NULL; 1475 if (log_ref_ver) { 1476 iput(dir); 1477 dir = NULL; 1478 } 1479 } 1480 1481 /* 1482 * Before we overwrite the inode reference item in the subvolume tree 1483 * with the item from the log tree, we must unlink all names from the 1484 * parent directory that are in the subvolume's tree inode reference 1485 * item, otherwise we end up with an inconsistent subvolume tree where 1486 * dir index entries exist for a name but there is no inode reference 1487 * item with the same name. 1488 */ 1489 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot, 1490 key); 1491 if (ret) 1492 goto out; 1493 1494 /* finally write the back reference in the inode */ 1495 ret = overwrite_item(trans, root, path, eb, slot, key); 1496 out: 1497 btrfs_release_path(path); 1498 kfree(name.name); 1499 iput(dir); 1500 iput(inode); 1501 return ret; 1502 } 1503 1504 static int count_inode_extrefs(struct btrfs_inode *inode, struct btrfs_path *path) 1505 { 1506 int ret = 0; 1507 int name_len; 1508 unsigned int nlink = 0; 1509 u32 item_size; 1510 u32 cur_offset = 0; 1511 u64 inode_objectid = btrfs_ino(inode); 1512 u64 offset = 0; 1513 unsigned long ptr; 1514 struct btrfs_inode_extref *extref; 1515 struct extent_buffer *leaf; 1516 1517 while (1) { 1518 ret = btrfs_find_one_extref(inode->root, inode_objectid, offset, 1519 path, &extref, &offset); 1520 if (ret) 1521 break; 1522 1523 leaf = path->nodes[0]; 1524 item_size = btrfs_item_size(leaf, path->slots[0]); 1525 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1526 cur_offset = 0; 1527 1528 while (cur_offset < item_size) { 1529 extref = (struct btrfs_inode_extref *) (ptr + cur_offset); 1530 name_len = btrfs_inode_extref_name_len(leaf, extref); 1531 1532 nlink++; 1533 1534 cur_offset += name_len + sizeof(*extref); 1535 } 1536 1537 offset++; 1538 btrfs_release_path(path); 1539 } 1540 btrfs_release_path(path); 1541 1542 if (ret < 0 && ret != -ENOENT) 1543 return ret; 1544 return nlink; 1545 } 1546 1547 static int count_inode_refs(struct btrfs_inode *inode, struct btrfs_path *path) 1548 { 1549 int ret; 1550 struct btrfs_key key; 1551 unsigned int nlink = 0; 1552 unsigned long ptr; 1553 unsigned long ptr_end; 1554 int name_len; 1555 u64 ino = btrfs_ino(inode); 1556 1557 key.objectid = ino; 1558 key.type = BTRFS_INODE_REF_KEY; 1559 key.offset = (u64)-1; 1560 1561 while (1) { 1562 ret = btrfs_search_slot(NULL, inode->root, &key, path, 0, 0); 1563 if (ret < 0) 1564 break; 1565 if (ret > 0) { 1566 if (path->slots[0] == 0) 1567 break; 1568 path->slots[0]--; 1569 } 1570 process_slot: 1571 btrfs_item_key_to_cpu(path->nodes[0], &key, 1572 path->slots[0]); 1573 if (key.objectid != ino || 1574 key.type != BTRFS_INODE_REF_KEY) 1575 break; 1576 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); 1577 ptr_end = ptr + btrfs_item_size(path->nodes[0], 1578 path->slots[0]); 1579 while (ptr < ptr_end) { 1580 struct btrfs_inode_ref *ref; 1581 1582 ref = (struct btrfs_inode_ref *)ptr; 1583 name_len = btrfs_inode_ref_name_len(path->nodes[0], 1584 ref); 1585 ptr = (unsigned long)(ref + 1) + name_len; 1586 nlink++; 1587 } 1588 1589 if (key.offset == 0) 1590 break; 1591 if (path->slots[0] > 0) { 1592 path->slots[0]--; 1593 goto process_slot; 1594 } 1595 key.offset--; 1596 btrfs_release_path(path); 1597 } 1598 btrfs_release_path(path); 1599 1600 return nlink; 1601 } 1602 1603 /* 1604 * There are a few corners where the link count of the file can't 1605 * be properly maintained during replay. So, instead of adding 1606 * lots of complexity to the log code, we just scan the backrefs 1607 * for any file that has been through replay. 1608 * 1609 * The scan will update the link count on the inode to reflect the 1610 * number of back refs found. If it goes down to zero, the iput 1611 * will free the inode. 1612 */ 1613 static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, 1614 struct inode *inode) 1615 { 1616 struct btrfs_root *root = BTRFS_I(inode)->root; 1617 struct btrfs_path *path; 1618 int ret; 1619 u64 nlink = 0; 1620 u64 ino = btrfs_ino(BTRFS_I(inode)); 1621 1622 path = btrfs_alloc_path(); 1623 if (!path) 1624 return -ENOMEM; 1625 1626 ret = count_inode_refs(BTRFS_I(inode), path); 1627 if (ret < 0) 1628 goto out; 1629 1630 nlink = ret; 1631 1632 ret = count_inode_extrefs(BTRFS_I(inode), path); 1633 if (ret < 0) 1634 goto out; 1635 1636 nlink += ret; 1637 1638 ret = 0; 1639 1640 if (nlink != inode->i_nlink) { 1641 set_nlink(inode, nlink); 1642 ret = btrfs_update_inode(trans, BTRFS_I(inode)); 1643 if (ret) 1644 goto out; 1645 } 1646 if (S_ISDIR(inode->i_mode)) 1647 BTRFS_I(inode)->index_cnt = (u64)-1; 1648 1649 if (inode->i_nlink == 0) { 1650 if (S_ISDIR(inode->i_mode)) { 1651 ret = replay_dir_deletes(trans, root, NULL, path, 1652 ino, 1); 1653 if (ret) 1654 goto out; 1655 } 1656 ret = btrfs_insert_orphan_item(trans, root, ino); 1657 if (ret == -EEXIST) 1658 ret = 0; 1659 } 1660 1661 out: 1662 btrfs_free_path(path); 1663 return ret; 1664 } 1665 1666 static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, 1667 struct btrfs_root *root, 1668 struct btrfs_path *path) 1669 { 1670 int ret; 1671 struct btrfs_key key; 1672 struct inode *inode; 1673 1674 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; 1675 key.type = BTRFS_ORPHAN_ITEM_KEY; 1676 key.offset = (u64)-1; 1677 while (1) { 1678 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 1679 if (ret < 0) 1680 break; 1681 1682 if (ret == 1) { 1683 ret = 0; 1684 if (path->slots[0] == 0) 1685 break; 1686 path->slots[0]--; 1687 } 1688 1689 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 1690 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || 1691 key.type != BTRFS_ORPHAN_ITEM_KEY) 1692 break; 1693 1694 ret = btrfs_del_item(trans, root, path); 1695 if (ret) 1696 break; 1697 1698 btrfs_release_path(path); 1699 inode = read_one_inode(root, key.offset); 1700 if (!inode) { 1701 ret = -EIO; 1702 break; 1703 } 1704 1705 ret = fixup_inode_link_count(trans, inode); 1706 iput(inode); 1707 if (ret) 1708 break; 1709 1710 /* 1711 * fixup on a directory may create new entries, 1712 * make sure we always look for the highset possible 1713 * offset 1714 */ 1715 key.offset = (u64)-1; 1716 } 1717 btrfs_release_path(path); 1718 return ret; 1719 } 1720 1721 1722 /* 1723 * record a given inode in the fixup dir so we can check its link 1724 * count when replay is done. The link count is incremented here 1725 * so the inode won't go away until we check it 1726 */ 1727 static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, 1728 struct btrfs_root *root, 1729 struct btrfs_path *path, 1730 u64 objectid) 1731 { 1732 struct btrfs_key key; 1733 int ret = 0; 1734 struct inode *inode; 1735 1736 inode = read_one_inode(root, objectid); 1737 if (!inode) 1738 return -EIO; 1739 1740 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; 1741 key.type = BTRFS_ORPHAN_ITEM_KEY; 1742 key.offset = objectid; 1743 1744 ret = btrfs_insert_empty_item(trans, root, path, &key, 0); 1745 1746 btrfs_release_path(path); 1747 if (ret == 0) { 1748 if (!inode->i_nlink) 1749 set_nlink(inode, 1); 1750 else 1751 inc_nlink(inode); 1752 ret = btrfs_update_inode(trans, BTRFS_I(inode)); 1753 } else if (ret == -EEXIST) { 1754 ret = 0; 1755 } 1756 iput(inode); 1757 1758 return ret; 1759 } 1760 1761 /* 1762 * when replaying the log for a directory, we only insert names 1763 * for inodes that actually exist. This means an fsync on a directory 1764 * does not implicitly fsync all the new files in it 1765 */ 1766 static noinline int insert_one_name(struct btrfs_trans_handle *trans, 1767 struct btrfs_root *root, 1768 u64 dirid, u64 index, 1769 const struct fscrypt_str *name, 1770 struct btrfs_key *location) 1771 { 1772 struct inode *inode; 1773 struct inode *dir; 1774 int ret; 1775 1776 inode = read_one_inode(root, location->objectid); 1777 if (!inode) 1778 return -ENOENT; 1779 1780 dir = read_one_inode(root, dirid); 1781 if (!dir) { 1782 iput(inode); 1783 return -EIO; 1784 } 1785 1786 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name, 1787 1, index); 1788 1789 /* FIXME, put inode into FIXUP list */ 1790 1791 iput(inode); 1792 iput(dir); 1793 return ret; 1794 } 1795 1796 static int delete_conflicting_dir_entry(struct btrfs_trans_handle *trans, 1797 struct btrfs_inode *dir, 1798 struct btrfs_path *path, 1799 struct btrfs_dir_item *dst_di, 1800 const struct btrfs_key *log_key, 1801 u8 log_flags, 1802 bool exists) 1803 { 1804 struct btrfs_key found_key; 1805 1806 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); 1807 /* The existing dentry points to the same inode, don't delete it. */ 1808 if (found_key.objectid == log_key->objectid && 1809 found_key.type == log_key->type && 1810 found_key.offset == log_key->offset && 1811 btrfs_dir_flags(path->nodes[0], dst_di) == log_flags) 1812 return 1; 1813 1814 /* 1815 * Don't drop the conflicting directory entry if the inode for the new 1816 * entry doesn't exist. 1817 */ 1818 if (!exists) 1819 return 0; 1820 1821 return drop_one_dir_item(trans, path, dir, dst_di); 1822 } 1823 1824 /* 1825 * take a single entry in a log directory item and replay it into 1826 * the subvolume. 1827 * 1828 * if a conflicting item exists in the subdirectory already, 1829 * the inode it points to is unlinked and put into the link count 1830 * fix up tree. 1831 * 1832 * If a name from the log points to a file or directory that does 1833 * not exist in the FS, it is skipped. fsyncs on directories 1834 * do not force down inodes inside that directory, just changes to the 1835 * names or unlinks in a directory. 1836 * 1837 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a 1838 * non-existing inode) and 1 if the name was replayed. 1839 */ 1840 static noinline int replay_one_name(struct btrfs_trans_handle *trans, 1841 struct btrfs_root *root, 1842 struct btrfs_path *path, 1843 struct extent_buffer *eb, 1844 struct btrfs_dir_item *di, 1845 struct btrfs_key *key) 1846 { 1847 struct fscrypt_str name = { 0 }; 1848 struct btrfs_dir_item *dir_dst_di; 1849 struct btrfs_dir_item *index_dst_di; 1850 bool dir_dst_matches = false; 1851 bool index_dst_matches = false; 1852 struct btrfs_key log_key; 1853 struct btrfs_key search_key; 1854 struct inode *dir; 1855 u8 log_flags; 1856 bool exists; 1857 int ret; 1858 bool update_size = true; 1859 bool name_added = false; 1860 1861 dir = read_one_inode(root, key->objectid); 1862 if (!dir) 1863 return -EIO; 1864 1865 ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name); 1866 if (ret) 1867 goto out; 1868 1869 log_flags = btrfs_dir_flags(eb, di); 1870 btrfs_dir_item_key_to_cpu(eb, di, &log_key); 1871 ret = btrfs_lookup_inode(trans, root, path, &log_key, 0); 1872 btrfs_release_path(path); 1873 if (ret < 0) 1874 goto out; 1875 exists = (ret == 0); 1876 ret = 0; 1877 1878 dir_dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, 1879 &name, 1); 1880 if (IS_ERR(dir_dst_di)) { 1881 ret = PTR_ERR(dir_dst_di); 1882 goto out; 1883 } else if (dir_dst_di) { 1884 ret = delete_conflicting_dir_entry(trans, BTRFS_I(dir), path, 1885 dir_dst_di, &log_key, 1886 log_flags, exists); 1887 if (ret < 0) 1888 goto out; 1889 dir_dst_matches = (ret == 1); 1890 } 1891 1892 btrfs_release_path(path); 1893 1894 index_dst_di = btrfs_lookup_dir_index_item(trans, root, path, 1895 key->objectid, key->offset, 1896 &name, 1); 1897 if (IS_ERR(index_dst_di)) { 1898 ret = PTR_ERR(index_dst_di); 1899 goto out; 1900 } else if (index_dst_di) { 1901 ret = delete_conflicting_dir_entry(trans, BTRFS_I(dir), path, 1902 index_dst_di, &log_key, 1903 log_flags, exists); 1904 if (ret < 0) 1905 goto out; 1906 index_dst_matches = (ret == 1); 1907 } 1908 1909 btrfs_release_path(path); 1910 1911 if (dir_dst_matches && index_dst_matches) { 1912 ret = 0; 1913 update_size = false; 1914 goto out; 1915 } 1916 1917 /* 1918 * Check if the inode reference exists in the log for the given name, 1919 * inode and parent inode 1920 */ 1921 search_key.objectid = log_key.objectid; 1922 search_key.type = BTRFS_INODE_REF_KEY; 1923 search_key.offset = key->objectid; 1924 ret = backref_in_log(root->log_root, &search_key, 0, &name); 1925 if (ret < 0) { 1926 goto out; 1927 } else if (ret) { 1928 /* The dentry will be added later. */ 1929 ret = 0; 1930 update_size = false; 1931 goto out; 1932 } 1933 1934 search_key.objectid = log_key.objectid; 1935 search_key.type = BTRFS_INODE_EXTREF_KEY; 1936 search_key.offset = key->objectid; 1937 ret = backref_in_log(root->log_root, &search_key, key->objectid, &name); 1938 if (ret < 0) { 1939 goto out; 1940 } else if (ret) { 1941 /* The dentry will be added later. */ 1942 ret = 0; 1943 update_size = false; 1944 goto out; 1945 } 1946 btrfs_release_path(path); 1947 ret = insert_one_name(trans, root, key->objectid, key->offset, 1948 &name, &log_key); 1949 if (ret && ret != -ENOENT && ret != -EEXIST) 1950 goto out; 1951 if (!ret) 1952 name_added = true; 1953 update_size = false; 1954 ret = 0; 1955 1956 out: 1957 if (!ret && update_size) { 1958 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name.len * 2); 1959 ret = btrfs_update_inode(trans, BTRFS_I(dir)); 1960 } 1961 kfree(name.name); 1962 iput(dir); 1963 if (!ret && name_added) 1964 ret = 1; 1965 return ret; 1966 } 1967 1968 /* Replay one dir item from a BTRFS_DIR_INDEX_KEY key. */ 1969 static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, 1970 struct btrfs_root *root, 1971 struct btrfs_path *path, 1972 struct extent_buffer *eb, int slot, 1973 struct btrfs_key *key) 1974 { 1975 int ret; 1976 struct btrfs_dir_item *di; 1977 1978 /* We only log dir index keys, which only contain a single dir item. */ 1979 ASSERT(key->type == BTRFS_DIR_INDEX_KEY); 1980 1981 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); 1982 ret = replay_one_name(trans, root, path, eb, di, key); 1983 if (ret < 0) 1984 return ret; 1985 1986 /* 1987 * If this entry refers to a non-directory (directories can not have a 1988 * link count > 1) and it was added in the transaction that was not 1989 * committed, make sure we fixup the link count of the inode the entry 1990 * points to. Otherwise something like the following would result in a 1991 * directory pointing to an inode with a wrong link that does not account 1992 * for this dir entry: 1993 * 1994 * mkdir testdir 1995 * touch testdir/foo 1996 * touch testdir/bar 1997 * sync 1998 * 1999 * ln testdir/bar testdir/bar_link 2000 * ln testdir/foo testdir/foo_link 2001 * xfs_io -c "fsync" testdir/bar 2002 * 2003 * <power failure> 2004 * 2005 * mount fs, log replay happens 2006 * 2007 * File foo would remain with a link count of 1 when it has two entries 2008 * pointing to it in the directory testdir. This would make it impossible 2009 * to ever delete the parent directory has it would result in stale 2010 * dentries that can never be deleted. 2011 */ 2012 if (ret == 1 && btrfs_dir_ftype(eb, di) != BTRFS_FT_DIR) { 2013 struct btrfs_path *fixup_path; 2014 struct btrfs_key di_key; 2015 2016 fixup_path = btrfs_alloc_path(); 2017 if (!fixup_path) 2018 return -ENOMEM; 2019 2020 btrfs_dir_item_key_to_cpu(eb, di, &di_key); 2021 ret = link_to_fixup_dir(trans, root, fixup_path, di_key.objectid); 2022 btrfs_free_path(fixup_path); 2023 } 2024 2025 return ret; 2026 } 2027 2028 /* 2029 * directory replay has two parts. There are the standard directory 2030 * items in the log copied from the subvolume, and range items 2031 * created in the log while the subvolume was logged. 2032 * 2033 * The range items tell us which parts of the key space the log 2034 * is authoritative for. During replay, if a key in the subvolume 2035 * directory is in a logged range item, but not actually in the log 2036 * that means it was deleted from the directory before the fsync 2037 * and should be removed. 2038 */ 2039 static noinline int find_dir_range(struct btrfs_root *root, 2040 struct btrfs_path *path, 2041 u64 dirid, 2042 u64 *start_ret, u64 *end_ret) 2043 { 2044 struct btrfs_key key; 2045 u64 found_end; 2046 struct btrfs_dir_log_item *item; 2047 int ret; 2048 int nritems; 2049 2050 if (*start_ret == (u64)-1) 2051 return 1; 2052 2053 key.objectid = dirid; 2054 key.type = BTRFS_DIR_LOG_INDEX_KEY; 2055 key.offset = *start_ret; 2056 2057 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2058 if (ret < 0) 2059 goto out; 2060 if (ret > 0) { 2061 if (path->slots[0] == 0) 2062 goto out; 2063 path->slots[0]--; 2064 } 2065 if (ret != 0) 2066 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2067 2068 if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) { 2069 ret = 1; 2070 goto next; 2071 } 2072 item = btrfs_item_ptr(path->nodes[0], path->slots[0], 2073 struct btrfs_dir_log_item); 2074 found_end = btrfs_dir_log_end(path->nodes[0], item); 2075 2076 if (*start_ret >= key.offset && *start_ret <= found_end) { 2077 ret = 0; 2078 *start_ret = key.offset; 2079 *end_ret = found_end; 2080 goto out; 2081 } 2082 ret = 1; 2083 next: 2084 /* check the next slot in the tree to see if it is a valid item */ 2085 nritems = btrfs_header_nritems(path->nodes[0]); 2086 path->slots[0]++; 2087 if (path->slots[0] >= nritems) { 2088 ret = btrfs_next_leaf(root, path); 2089 if (ret) 2090 goto out; 2091 } 2092 2093 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2094 2095 if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) { 2096 ret = 1; 2097 goto out; 2098 } 2099 item = btrfs_item_ptr(path->nodes[0], path->slots[0], 2100 struct btrfs_dir_log_item); 2101 found_end = btrfs_dir_log_end(path->nodes[0], item); 2102 *start_ret = key.offset; 2103 *end_ret = found_end; 2104 ret = 0; 2105 out: 2106 btrfs_release_path(path); 2107 return ret; 2108 } 2109 2110 /* 2111 * this looks for a given directory item in the log. If the directory 2112 * item is not in the log, the item is removed and the inode it points 2113 * to is unlinked 2114 */ 2115 static noinline int check_item_in_log(struct btrfs_trans_handle *trans, 2116 struct btrfs_root *log, 2117 struct btrfs_path *path, 2118 struct btrfs_path *log_path, 2119 struct inode *dir, 2120 struct btrfs_key *dir_key) 2121 { 2122 struct btrfs_root *root = BTRFS_I(dir)->root; 2123 int ret; 2124 struct extent_buffer *eb; 2125 int slot; 2126 struct btrfs_dir_item *di; 2127 struct fscrypt_str name = { 0 }; 2128 struct inode *inode = NULL; 2129 struct btrfs_key location; 2130 2131 /* 2132 * Currently we only log dir index keys. Even if we replay a log created 2133 * by an older kernel that logged both dir index and dir item keys, all 2134 * we need to do is process the dir index keys, we (and our caller) can 2135 * safely ignore dir item keys (key type BTRFS_DIR_ITEM_KEY). 2136 */ 2137 ASSERT(dir_key->type == BTRFS_DIR_INDEX_KEY); 2138 2139 eb = path->nodes[0]; 2140 slot = path->slots[0]; 2141 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); 2142 ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name); 2143 if (ret) 2144 goto out; 2145 2146 if (log) { 2147 struct btrfs_dir_item *log_di; 2148 2149 log_di = btrfs_lookup_dir_index_item(trans, log, log_path, 2150 dir_key->objectid, 2151 dir_key->offset, &name, 0); 2152 if (IS_ERR(log_di)) { 2153 ret = PTR_ERR(log_di); 2154 goto out; 2155 } else if (log_di) { 2156 /* The dentry exists in the log, we have nothing to do. */ 2157 ret = 0; 2158 goto out; 2159 } 2160 } 2161 2162 btrfs_dir_item_key_to_cpu(eb, di, &location); 2163 btrfs_release_path(path); 2164 btrfs_release_path(log_path); 2165 inode = read_one_inode(root, location.objectid); 2166 if (!inode) { 2167 ret = -EIO; 2168 goto out; 2169 } 2170 2171 ret = link_to_fixup_dir(trans, root, path, location.objectid); 2172 if (ret) 2173 goto out; 2174 2175 inc_nlink(inode); 2176 ret = unlink_inode_for_log_replay(trans, BTRFS_I(dir), BTRFS_I(inode), 2177 &name); 2178 /* 2179 * Unlike dir item keys, dir index keys can only have one name (entry) in 2180 * them, as there are no key collisions since each key has a unique offset 2181 * (an index number), so we're done. 2182 */ 2183 out: 2184 btrfs_release_path(path); 2185 btrfs_release_path(log_path); 2186 kfree(name.name); 2187 iput(inode); 2188 return ret; 2189 } 2190 2191 static int replay_xattr_deletes(struct btrfs_trans_handle *trans, 2192 struct btrfs_root *root, 2193 struct btrfs_root *log, 2194 struct btrfs_path *path, 2195 const u64 ino) 2196 { 2197 struct btrfs_key search_key; 2198 struct btrfs_path *log_path; 2199 int i; 2200 int nritems; 2201 int ret; 2202 2203 log_path = btrfs_alloc_path(); 2204 if (!log_path) 2205 return -ENOMEM; 2206 2207 search_key.objectid = ino; 2208 search_key.type = BTRFS_XATTR_ITEM_KEY; 2209 search_key.offset = 0; 2210 again: 2211 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 2212 if (ret < 0) 2213 goto out; 2214 process_leaf: 2215 nritems = btrfs_header_nritems(path->nodes[0]); 2216 for (i = path->slots[0]; i < nritems; i++) { 2217 struct btrfs_key key; 2218 struct btrfs_dir_item *di; 2219 struct btrfs_dir_item *log_di; 2220 u32 total_size; 2221 u32 cur; 2222 2223 btrfs_item_key_to_cpu(path->nodes[0], &key, i); 2224 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) { 2225 ret = 0; 2226 goto out; 2227 } 2228 2229 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item); 2230 total_size = btrfs_item_size(path->nodes[0], i); 2231 cur = 0; 2232 while (cur < total_size) { 2233 u16 name_len = btrfs_dir_name_len(path->nodes[0], di); 2234 u16 data_len = btrfs_dir_data_len(path->nodes[0], di); 2235 u32 this_len = sizeof(*di) + name_len + data_len; 2236 char *name; 2237 2238 name = kmalloc(name_len, GFP_NOFS); 2239 if (!name) { 2240 ret = -ENOMEM; 2241 goto out; 2242 } 2243 read_extent_buffer(path->nodes[0], name, 2244 (unsigned long)(di + 1), name_len); 2245 2246 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino, 2247 name, name_len, 0); 2248 btrfs_release_path(log_path); 2249 if (!log_di) { 2250 /* Doesn't exist in log tree, so delete it. */ 2251 btrfs_release_path(path); 2252 di = btrfs_lookup_xattr(trans, root, path, ino, 2253 name, name_len, -1); 2254 kfree(name); 2255 if (IS_ERR(di)) { 2256 ret = PTR_ERR(di); 2257 goto out; 2258 } 2259 ASSERT(di); 2260 ret = btrfs_delete_one_dir_name(trans, root, 2261 path, di); 2262 if (ret) 2263 goto out; 2264 btrfs_release_path(path); 2265 search_key = key; 2266 goto again; 2267 } 2268 kfree(name); 2269 if (IS_ERR(log_di)) { 2270 ret = PTR_ERR(log_di); 2271 goto out; 2272 } 2273 cur += this_len; 2274 di = (struct btrfs_dir_item *)((char *)di + this_len); 2275 } 2276 } 2277 ret = btrfs_next_leaf(root, path); 2278 if (ret > 0) 2279 ret = 0; 2280 else if (ret == 0) 2281 goto process_leaf; 2282 out: 2283 btrfs_free_path(log_path); 2284 btrfs_release_path(path); 2285 return ret; 2286 } 2287 2288 2289 /* 2290 * deletion replay happens before we copy any new directory items 2291 * out of the log or out of backreferences from inodes. It 2292 * scans the log to find ranges of keys that log is authoritative for, 2293 * and then scans the directory to find items in those ranges that are 2294 * not present in the log. 2295 * 2296 * Anything we don't find in the log is unlinked and removed from the 2297 * directory. 2298 */ 2299 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, 2300 struct btrfs_root *root, 2301 struct btrfs_root *log, 2302 struct btrfs_path *path, 2303 u64 dirid, int del_all) 2304 { 2305 u64 range_start; 2306 u64 range_end; 2307 int ret = 0; 2308 struct btrfs_key dir_key; 2309 struct btrfs_key found_key; 2310 struct btrfs_path *log_path; 2311 struct inode *dir; 2312 2313 dir_key.objectid = dirid; 2314 dir_key.type = BTRFS_DIR_INDEX_KEY; 2315 log_path = btrfs_alloc_path(); 2316 if (!log_path) 2317 return -ENOMEM; 2318 2319 dir = read_one_inode(root, dirid); 2320 /* it isn't an error if the inode isn't there, that can happen 2321 * because we replay the deletes before we copy in the inode item 2322 * from the log 2323 */ 2324 if (!dir) { 2325 btrfs_free_path(log_path); 2326 return 0; 2327 } 2328 2329 range_start = 0; 2330 range_end = 0; 2331 while (1) { 2332 if (del_all) 2333 range_end = (u64)-1; 2334 else { 2335 ret = find_dir_range(log, path, dirid, 2336 &range_start, &range_end); 2337 if (ret < 0) 2338 goto out; 2339 else if (ret > 0) 2340 break; 2341 } 2342 2343 dir_key.offset = range_start; 2344 while (1) { 2345 int nritems; 2346 ret = btrfs_search_slot(NULL, root, &dir_key, path, 2347 0, 0); 2348 if (ret < 0) 2349 goto out; 2350 2351 nritems = btrfs_header_nritems(path->nodes[0]); 2352 if (path->slots[0] >= nritems) { 2353 ret = btrfs_next_leaf(root, path); 2354 if (ret == 1) 2355 break; 2356 else if (ret < 0) 2357 goto out; 2358 } 2359 btrfs_item_key_to_cpu(path->nodes[0], &found_key, 2360 path->slots[0]); 2361 if (found_key.objectid != dirid || 2362 found_key.type != dir_key.type) { 2363 ret = 0; 2364 goto out; 2365 } 2366 2367 if (found_key.offset > range_end) 2368 break; 2369 2370 ret = check_item_in_log(trans, log, path, 2371 log_path, dir, 2372 &found_key); 2373 if (ret) 2374 goto out; 2375 if (found_key.offset == (u64)-1) 2376 break; 2377 dir_key.offset = found_key.offset + 1; 2378 } 2379 btrfs_release_path(path); 2380 if (range_end == (u64)-1) 2381 break; 2382 range_start = range_end + 1; 2383 } 2384 ret = 0; 2385 out: 2386 btrfs_release_path(path); 2387 btrfs_free_path(log_path); 2388 iput(dir); 2389 return ret; 2390 } 2391 2392 /* 2393 * the process_func used to replay items from the log tree. This 2394 * gets called in two different stages. The first stage just looks 2395 * for inodes and makes sure they are all copied into the subvolume. 2396 * 2397 * The second stage copies all the other item types from the log into 2398 * the subvolume. The two stage approach is slower, but gets rid of 2399 * lots of complexity around inodes referencing other inodes that exist 2400 * only in the log (references come from either directory items or inode 2401 * back refs). 2402 */ 2403 static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, 2404 struct walk_control *wc, u64 gen, int level) 2405 { 2406 int nritems; 2407 struct btrfs_tree_parent_check check = { 2408 .transid = gen, 2409 .level = level 2410 }; 2411 struct btrfs_path *path; 2412 struct btrfs_root *root = wc->replay_dest; 2413 struct btrfs_key key; 2414 int i; 2415 int ret; 2416 2417 ret = btrfs_read_extent_buffer(eb, &check); 2418 if (ret) 2419 return ret; 2420 2421 level = btrfs_header_level(eb); 2422 2423 if (level != 0) 2424 return 0; 2425 2426 path = btrfs_alloc_path(); 2427 if (!path) 2428 return -ENOMEM; 2429 2430 nritems = btrfs_header_nritems(eb); 2431 for (i = 0; i < nritems; i++) { 2432 btrfs_item_key_to_cpu(eb, &key, i); 2433 2434 /* inode keys are done during the first stage */ 2435 if (key.type == BTRFS_INODE_ITEM_KEY && 2436 wc->stage == LOG_WALK_REPLAY_INODES) { 2437 struct btrfs_inode_item *inode_item; 2438 u32 mode; 2439 2440 inode_item = btrfs_item_ptr(eb, i, 2441 struct btrfs_inode_item); 2442 /* 2443 * If we have a tmpfile (O_TMPFILE) that got fsync'ed 2444 * and never got linked before the fsync, skip it, as 2445 * replaying it is pointless since it would be deleted 2446 * later. We skip logging tmpfiles, but it's always 2447 * possible we are replaying a log created with a kernel 2448 * that used to log tmpfiles. 2449 */ 2450 if (btrfs_inode_nlink(eb, inode_item) == 0) { 2451 wc->ignore_cur_inode = true; 2452 continue; 2453 } else { 2454 wc->ignore_cur_inode = false; 2455 } 2456 ret = replay_xattr_deletes(wc->trans, root, log, 2457 path, key.objectid); 2458 if (ret) 2459 break; 2460 mode = btrfs_inode_mode(eb, inode_item); 2461 if (S_ISDIR(mode)) { 2462 ret = replay_dir_deletes(wc->trans, 2463 root, log, path, key.objectid, 0); 2464 if (ret) 2465 break; 2466 } 2467 ret = overwrite_item(wc->trans, root, path, 2468 eb, i, &key); 2469 if (ret) 2470 break; 2471 2472 /* 2473 * Before replaying extents, truncate the inode to its 2474 * size. We need to do it now and not after log replay 2475 * because before an fsync we can have prealloc extents 2476 * added beyond the inode's i_size. If we did it after, 2477 * through orphan cleanup for example, we would drop 2478 * those prealloc extents just after replaying them. 2479 */ 2480 if (S_ISREG(mode)) { 2481 struct btrfs_drop_extents_args drop_args = { 0 }; 2482 struct inode *inode; 2483 u64 from; 2484 2485 inode = read_one_inode(root, key.objectid); 2486 if (!inode) { 2487 ret = -EIO; 2488 break; 2489 } 2490 from = ALIGN(i_size_read(inode), 2491 root->fs_info->sectorsize); 2492 drop_args.start = from; 2493 drop_args.end = (u64)-1; 2494 drop_args.drop_cache = true; 2495 ret = btrfs_drop_extents(wc->trans, root, 2496 BTRFS_I(inode), 2497 &drop_args); 2498 if (!ret) { 2499 inode_sub_bytes(inode, 2500 drop_args.bytes_found); 2501 /* Update the inode's nbytes. */ 2502 ret = btrfs_update_inode(wc->trans, 2503 BTRFS_I(inode)); 2504 } 2505 iput(inode); 2506 if (ret) 2507 break; 2508 } 2509 2510 ret = link_to_fixup_dir(wc->trans, root, 2511 path, key.objectid); 2512 if (ret) 2513 break; 2514 } 2515 2516 if (wc->ignore_cur_inode) 2517 continue; 2518 2519 if (key.type == BTRFS_DIR_INDEX_KEY && 2520 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) { 2521 ret = replay_one_dir_item(wc->trans, root, path, 2522 eb, i, &key); 2523 if (ret) 2524 break; 2525 } 2526 2527 if (wc->stage < LOG_WALK_REPLAY_ALL) 2528 continue; 2529 2530 /* these keys are simply copied */ 2531 if (key.type == BTRFS_XATTR_ITEM_KEY) { 2532 ret = overwrite_item(wc->trans, root, path, 2533 eb, i, &key); 2534 if (ret) 2535 break; 2536 } else if (key.type == BTRFS_INODE_REF_KEY || 2537 key.type == BTRFS_INODE_EXTREF_KEY) { 2538 ret = add_inode_ref(wc->trans, root, log, path, 2539 eb, i, &key); 2540 if (ret && ret != -ENOENT) 2541 break; 2542 ret = 0; 2543 } else if (key.type == BTRFS_EXTENT_DATA_KEY) { 2544 ret = replay_one_extent(wc->trans, root, path, 2545 eb, i, &key); 2546 if (ret) 2547 break; 2548 } 2549 /* 2550 * We don't log BTRFS_DIR_ITEM_KEY keys anymore, only the 2551 * BTRFS_DIR_INDEX_KEY items which we use to derive the 2552 * BTRFS_DIR_ITEM_KEY items. If we are replaying a log from an 2553 * older kernel with such keys, ignore them. 2554 */ 2555 } 2556 btrfs_free_path(path); 2557 return ret; 2558 } 2559 2560 /* 2561 * Correctly adjust the reserved bytes occupied by a log tree extent buffer 2562 */ 2563 static void unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start) 2564 { 2565 struct btrfs_block_group *cache; 2566 2567 cache = btrfs_lookup_block_group(fs_info, start); 2568 if (!cache) { 2569 btrfs_err(fs_info, "unable to find block group for %llu", start); 2570 return; 2571 } 2572 2573 spin_lock(&cache->space_info->lock); 2574 spin_lock(&cache->lock); 2575 cache->reserved -= fs_info->nodesize; 2576 cache->space_info->bytes_reserved -= fs_info->nodesize; 2577 spin_unlock(&cache->lock); 2578 spin_unlock(&cache->space_info->lock); 2579 2580 btrfs_put_block_group(cache); 2581 } 2582 2583 static int clean_log_buffer(struct btrfs_trans_handle *trans, 2584 struct extent_buffer *eb) 2585 { 2586 int ret; 2587 2588 btrfs_tree_lock(eb); 2589 btrfs_clear_buffer_dirty(trans, eb); 2590 wait_on_extent_buffer_writeback(eb); 2591 btrfs_tree_unlock(eb); 2592 2593 if (trans) { 2594 ret = btrfs_pin_reserved_extent(trans, eb); 2595 if (ret) 2596 return ret; 2597 } else { 2598 unaccount_log_buffer(eb->fs_info, eb->start); 2599 } 2600 2601 return 0; 2602 } 2603 2604 static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, 2605 struct btrfs_root *root, 2606 struct btrfs_path *path, int *level, 2607 struct walk_control *wc) 2608 { 2609 struct btrfs_fs_info *fs_info = root->fs_info; 2610 u64 bytenr; 2611 u64 ptr_gen; 2612 struct extent_buffer *next; 2613 struct extent_buffer *cur; 2614 int ret = 0; 2615 2616 while (*level > 0) { 2617 struct btrfs_tree_parent_check check = { 0 }; 2618 2619 cur = path->nodes[*level]; 2620 2621 WARN_ON(btrfs_header_level(cur) != *level); 2622 2623 if (path->slots[*level] >= 2624 btrfs_header_nritems(cur)) 2625 break; 2626 2627 bytenr = btrfs_node_blockptr(cur, path->slots[*level]); 2628 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); 2629 check.transid = ptr_gen; 2630 check.level = *level - 1; 2631 check.has_first_key = true; 2632 btrfs_node_key_to_cpu(cur, &check.first_key, path->slots[*level]); 2633 2634 next = btrfs_find_create_tree_block(fs_info, bytenr, 2635 btrfs_header_owner(cur), 2636 *level - 1); 2637 if (IS_ERR(next)) 2638 return PTR_ERR(next); 2639 2640 if (*level == 1) { 2641 ret = wc->process_func(root, next, wc, ptr_gen, 2642 *level - 1); 2643 if (ret) { 2644 free_extent_buffer(next); 2645 return ret; 2646 } 2647 2648 path->slots[*level]++; 2649 if (wc->free) { 2650 ret = btrfs_read_extent_buffer(next, &check); 2651 if (ret) { 2652 free_extent_buffer(next); 2653 return ret; 2654 } 2655 2656 ret = clean_log_buffer(trans, next); 2657 if (ret) { 2658 free_extent_buffer(next); 2659 return ret; 2660 } 2661 } 2662 free_extent_buffer(next); 2663 continue; 2664 } 2665 ret = btrfs_read_extent_buffer(next, &check); 2666 if (ret) { 2667 free_extent_buffer(next); 2668 return ret; 2669 } 2670 2671 if (path->nodes[*level-1]) 2672 free_extent_buffer(path->nodes[*level-1]); 2673 path->nodes[*level-1] = next; 2674 *level = btrfs_header_level(next); 2675 path->slots[*level] = 0; 2676 cond_resched(); 2677 } 2678 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]); 2679 2680 cond_resched(); 2681 return 0; 2682 } 2683 2684 static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans, 2685 struct btrfs_root *root, 2686 struct btrfs_path *path, int *level, 2687 struct walk_control *wc) 2688 { 2689 int i; 2690 int slot; 2691 int ret; 2692 2693 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { 2694 slot = path->slots[i]; 2695 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) { 2696 path->slots[i]++; 2697 *level = i; 2698 WARN_ON(*level == 0); 2699 return 0; 2700 } else { 2701 ret = wc->process_func(root, path->nodes[*level], wc, 2702 btrfs_header_generation(path->nodes[*level]), 2703 *level); 2704 if (ret) 2705 return ret; 2706 2707 if (wc->free) { 2708 ret = clean_log_buffer(trans, path->nodes[*level]); 2709 if (ret) 2710 return ret; 2711 } 2712 free_extent_buffer(path->nodes[*level]); 2713 path->nodes[*level] = NULL; 2714 *level = i + 1; 2715 } 2716 } 2717 return 1; 2718 } 2719 2720 /* 2721 * drop the reference count on the tree rooted at 'snap'. This traverses 2722 * the tree freeing any blocks that have a ref count of zero after being 2723 * decremented. 2724 */ 2725 static int walk_log_tree(struct btrfs_trans_handle *trans, 2726 struct btrfs_root *log, struct walk_control *wc) 2727 { 2728 int ret = 0; 2729 int wret; 2730 int level; 2731 struct btrfs_path *path; 2732 int orig_level; 2733 2734 path = btrfs_alloc_path(); 2735 if (!path) 2736 return -ENOMEM; 2737 2738 level = btrfs_header_level(log->node); 2739 orig_level = level; 2740 path->nodes[level] = log->node; 2741 atomic_inc(&log->node->refs); 2742 path->slots[level] = 0; 2743 2744 while (1) { 2745 wret = walk_down_log_tree(trans, log, path, &level, wc); 2746 if (wret > 0) 2747 break; 2748 if (wret < 0) { 2749 ret = wret; 2750 goto out; 2751 } 2752 2753 wret = walk_up_log_tree(trans, log, path, &level, wc); 2754 if (wret > 0) 2755 break; 2756 if (wret < 0) { 2757 ret = wret; 2758 goto out; 2759 } 2760 } 2761 2762 /* was the root node processed? if not, catch it here */ 2763 if (path->nodes[orig_level]) { 2764 ret = wc->process_func(log, path->nodes[orig_level], wc, 2765 btrfs_header_generation(path->nodes[orig_level]), 2766 orig_level); 2767 if (ret) 2768 goto out; 2769 if (wc->free) 2770 ret = clean_log_buffer(trans, path->nodes[orig_level]); 2771 } 2772 2773 out: 2774 btrfs_free_path(path); 2775 return ret; 2776 } 2777 2778 /* 2779 * helper function to update the item for a given subvolumes log root 2780 * in the tree of log roots 2781 */ 2782 static int update_log_root(struct btrfs_trans_handle *trans, 2783 struct btrfs_root *log, 2784 struct btrfs_root_item *root_item) 2785 { 2786 struct btrfs_fs_info *fs_info = log->fs_info; 2787 int ret; 2788 2789 if (log->log_transid == 1) { 2790 /* insert root item on the first sync */ 2791 ret = btrfs_insert_root(trans, fs_info->log_root_tree, 2792 &log->root_key, root_item); 2793 } else { 2794 ret = btrfs_update_root(trans, fs_info->log_root_tree, 2795 &log->root_key, root_item); 2796 } 2797 return ret; 2798 } 2799 2800 static void wait_log_commit(struct btrfs_root *root, int transid) 2801 { 2802 DEFINE_WAIT(wait); 2803 int index = transid % 2; 2804 2805 /* 2806 * we only allow two pending log transactions at a time, 2807 * so we know that if ours is more than 2 older than the 2808 * current transaction, we're done 2809 */ 2810 for (;;) { 2811 prepare_to_wait(&root->log_commit_wait[index], 2812 &wait, TASK_UNINTERRUPTIBLE); 2813 2814 if (!(root->log_transid_committed < transid && 2815 atomic_read(&root->log_commit[index]))) 2816 break; 2817 2818 mutex_unlock(&root->log_mutex); 2819 schedule(); 2820 mutex_lock(&root->log_mutex); 2821 } 2822 finish_wait(&root->log_commit_wait[index], &wait); 2823 } 2824 2825 static void wait_for_writer(struct btrfs_root *root) 2826 { 2827 DEFINE_WAIT(wait); 2828 2829 for (;;) { 2830 prepare_to_wait(&root->log_writer_wait, &wait, 2831 TASK_UNINTERRUPTIBLE); 2832 if (!atomic_read(&root->log_writers)) 2833 break; 2834 2835 mutex_unlock(&root->log_mutex); 2836 schedule(); 2837 mutex_lock(&root->log_mutex); 2838 } 2839 finish_wait(&root->log_writer_wait, &wait); 2840 } 2841 2842 void btrfs_init_log_ctx(struct btrfs_log_ctx *ctx, struct btrfs_inode *inode) 2843 { 2844 ctx->log_ret = 0; 2845 ctx->log_transid = 0; 2846 ctx->log_new_dentries = false; 2847 ctx->logging_new_name = false; 2848 ctx->logging_new_delayed_dentries = false; 2849 ctx->logged_before = false; 2850 ctx->inode = inode; 2851 INIT_LIST_HEAD(&ctx->list); 2852 INIT_LIST_HEAD(&ctx->ordered_extents); 2853 INIT_LIST_HEAD(&ctx->conflict_inodes); 2854 ctx->num_conflict_inodes = 0; 2855 ctx->logging_conflict_inodes = false; 2856 ctx->scratch_eb = NULL; 2857 } 2858 2859 void btrfs_init_log_ctx_scratch_eb(struct btrfs_log_ctx *ctx) 2860 { 2861 struct btrfs_inode *inode = ctx->inode; 2862 2863 if (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) && 2864 !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags)) 2865 return; 2866 2867 /* 2868 * Don't care about allocation failure. This is just for optimization, 2869 * if we fail to allocate here, we will try again later if needed. 2870 */ 2871 ctx->scratch_eb = alloc_dummy_extent_buffer(inode->root->fs_info, 0); 2872 } 2873 2874 void btrfs_release_log_ctx_extents(struct btrfs_log_ctx *ctx) 2875 { 2876 struct btrfs_ordered_extent *ordered; 2877 struct btrfs_ordered_extent *tmp; 2878 2879 btrfs_assert_inode_locked(ctx->inode); 2880 2881 list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) { 2882 list_del_init(&ordered->log_list); 2883 btrfs_put_ordered_extent(ordered); 2884 } 2885 } 2886 2887 2888 static inline void btrfs_remove_log_ctx(struct btrfs_root *root, 2889 struct btrfs_log_ctx *ctx) 2890 { 2891 mutex_lock(&root->log_mutex); 2892 list_del_init(&ctx->list); 2893 mutex_unlock(&root->log_mutex); 2894 } 2895 2896 /* 2897 * Invoked in log mutex context, or be sure there is no other task which 2898 * can access the list. 2899 */ 2900 static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root, 2901 int index, int error) 2902 { 2903 struct btrfs_log_ctx *ctx; 2904 struct btrfs_log_ctx *safe; 2905 2906 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) { 2907 list_del_init(&ctx->list); 2908 ctx->log_ret = error; 2909 } 2910 } 2911 2912 /* 2913 * Sends a given tree log down to the disk and updates the super blocks to 2914 * record it. When this call is done, you know that any inodes previously 2915 * logged are safely on disk only if it returns 0. 2916 * 2917 * Any other return value means you need to call btrfs_commit_transaction. 2918 * Some of the edge cases for fsyncing directories that have had unlinks 2919 * or renames done in the past mean that sometimes the only safe 2920 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN, 2921 * that has happened. 2922 */ 2923 int btrfs_sync_log(struct btrfs_trans_handle *trans, 2924 struct btrfs_root *root, struct btrfs_log_ctx *ctx) 2925 { 2926 int index1; 2927 int index2; 2928 int mark; 2929 int ret; 2930 struct btrfs_fs_info *fs_info = root->fs_info; 2931 struct btrfs_root *log = root->log_root; 2932 struct btrfs_root *log_root_tree = fs_info->log_root_tree; 2933 struct btrfs_root_item new_root_item; 2934 int log_transid = 0; 2935 struct btrfs_log_ctx root_log_ctx; 2936 struct blk_plug plug; 2937 u64 log_root_start; 2938 u64 log_root_level; 2939 2940 mutex_lock(&root->log_mutex); 2941 log_transid = ctx->log_transid; 2942 if (root->log_transid_committed >= log_transid) { 2943 mutex_unlock(&root->log_mutex); 2944 return ctx->log_ret; 2945 } 2946 2947 index1 = log_transid % 2; 2948 if (atomic_read(&root->log_commit[index1])) { 2949 wait_log_commit(root, log_transid); 2950 mutex_unlock(&root->log_mutex); 2951 return ctx->log_ret; 2952 } 2953 ASSERT(log_transid == root->log_transid); 2954 atomic_set(&root->log_commit[index1], 1); 2955 2956 /* wait for previous tree log sync to complete */ 2957 if (atomic_read(&root->log_commit[(index1 + 1) % 2])) 2958 wait_log_commit(root, log_transid - 1); 2959 2960 while (1) { 2961 int batch = atomic_read(&root->log_batch); 2962 /* when we're on an ssd, just kick the log commit out */ 2963 if (!btrfs_test_opt(fs_info, SSD) && 2964 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) { 2965 mutex_unlock(&root->log_mutex); 2966 schedule_timeout_uninterruptible(1); 2967 mutex_lock(&root->log_mutex); 2968 } 2969 wait_for_writer(root); 2970 if (batch == atomic_read(&root->log_batch)) 2971 break; 2972 } 2973 2974 /* bail out if we need to do a full commit */ 2975 if (btrfs_need_log_full_commit(trans)) { 2976 ret = BTRFS_LOG_FORCE_COMMIT; 2977 mutex_unlock(&root->log_mutex); 2978 goto out; 2979 } 2980 2981 if (log_transid % 2 == 0) 2982 mark = EXTENT_DIRTY; 2983 else 2984 mark = EXTENT_NEW; 2985 2986 /* we start IO on all the marked extents here, but we don't actually 2987 * wait for them until later. 2988 */ 2989 blk_start_plug(&plug); 2990 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark); 2991 /* 2992 * -EAGAIN happens when someone, e.g., a concurrent transaction 2993 * commit, writes a dirty extent in this tree-log commit. This 2994 * concurrent write will create a hole writing out the extents, 2995 * and we cannot proceed on a zoned filesystem, requiring 2996 * sequential writing. While we can bail out to a full commit 2997 * here, but we can continue hoping the concurrent writing fills 2998 * the hole. 2999 */ 3000 if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) 3001 ret = 0; 3002 if (ret) { 3003 blk_finish_plug(&plug); 3004 btrfs_set_log_full_commit(trans); 3005 mutex_unlock(&root->log_mutex); 3006 goto out; 3007 } 3008 3009 /* 3010 * We _must_ update under the root->log_mutex in order to make sure we 3011 * have a consistent view of the log root we are trying to commit at 3012 * this moment. 3013 * 3014 * We _must_ copy this into a local copy, because we are not holding the 3015 * log_root_tree->log_mutex yet. This is important because when we 3016 * commit the log_root_tree we must have a consistent view of the 3017 * log_root_tree when we update the super block to point at the 3018 * log_root_tree bytenr. If we update the log_root_tree here we'll race 3019 * with the commit and possibly point at the new block which we may not 3020 * have written out. 3021 */ 3022 btrfs_set_root_node(&log->root_item, log->node); 3023 memcpy(&new_root_item, &log->root_item, sizeof(new_root_item)); 3024 3025 btrfs_set_root_log_transid(root, root->log_transid + 1); 3026 log->log_transid = root->log_transid; 3027 root->log_start_pid = 0; 3028 /* 3029 * IO has been started, blocks of the log tree have WRITTEN flag set 3030 * in their headers. new modifications of the log will be written to 3031 * new positions. so it's safe to allow log writers to go in. 3032 */ 3033 mutex_unlock(&root->log_mutex); 3034 3035 if (btrfs_is_zoned(fs_info)) { 3036 mutex_lock(&fs_info->tree_root->log_mutex); 3037 if (!log_root_tree->node) { 3038 ret = btrfs_alloc_log_tree_node(trans, log_root_tree); 3039 if (ret) { 3040 mutex_unlock(&fs_info->tree_root->log_mutex); 3041 blk_finish_plug(&plug); 3042 goto out; 3043 } 3044 } 3045 mutex_unlock(&fs_info->tree_root->log_mutex); 3046 } 3047 3048 btrfs_init_log_ctx(&root_log_ctx, NULL); 3049 3050 mutex_lock(&log_root_tree->log_mutex); 3051 3052 index2 = log_root_tree->log_transid % 2; 3053 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]); 3054 root_log_ctx.log_transid = log_root_tree->log_transid; 3055 3056 /* 3057 * Now we are safe to update the log_root_tree because we're under the 3058 * log_mutex, and we're a current writer so we're holding the commit 3059 * open until we drop the log_mutex. 3060 */ 3061 ret = update_log_root(trans, log, &new_root_item); 3062 if (ret) { 3063 list_del_init(&root_log_ctx.list); 3064 blk_finish_plug(&plug); 3065 btrfs_set_log_full_commit(trans); 3066 if (ret != -ENOSPC) 3067 btrfs_err(fs_info, 3068 "failed to update log for root %llu ret %d", 3069 btrfs_root_id(root), ret); 3070 btrfs_wait_tree_log_extents(log, mark); 3071 mutex_unlock(&log_root_tree->log_mutex); 3072 goto out; 3073 } 3074 3075 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) { 3076 blk_finish_plug(&plug); 3077 list_del_init(&root_log_ctx.list); 3078 mutex_unlock(&log_root_tree->log_mutex); 3079 ret = root_log_ctx.log_ret; 3080 goto out; 3081 } 3082 3083 if (atomic_read(&log_root_tree->log_commit[index2])) { 3084 blk_finish_plug(&plug); 3085 ret = btrfs_wait_tree_log_extents(log, mark); 3086 wait_log_commit(log_root_tree, 3087 root_log_ctx.log_transid); 3088 mutex_unlock(&log_root_tree->log_mutex); 3089 if (!ret) 3090 ret = root_log_ctx.log_ret; 3091 goto out; 3092 } 3093 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid); 3094 atomic_set(&log_root_tree->log_commit[index2], 1); 3095 3096 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) { 3097 wait_log_commit(log_root_tree, 3098 root_log_ctx.log_transid - 1); 3099 } 3100 3101 /* 3102 * now that we've moved on to the tree of log tree roots, 3103 * check the full commit flag again 3104 */ 3105 if (btrfs_need_log_full_commit(trans)) { 3106 blk_finish_plug(&plug); 3107 btrfs_wait_tree_log_extents(log, mark); 3108 mutex_unlock(&log_root_tree->log_mutex); 3109 ret = BTRFS_LOG_FORCE_COMMIT; 3110 goto out_wake_log_root; 3111 } 3112 3113 ret = btrfs_write_marked_extents(fs_info, 3114 &log_root_tree->dirty_log_pages, 3115 EXTENT_DIRTY | EXTENT_NEW); 3116 blk_finish_plug(&plug); 3117 /* 3118 * As described above, -EAGAIN indicates a hole in the extents. We 3119 * cannot wait for these write outs since the waiting cause a 3120 * deadlock. Bail out to the full commit instead. 3121 */ 3122 if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) { 3123 btrfs_set_log_full_commit(trans); 3124 btrfs_wait_tree_log_extents(log, mark); 3125 mutex_unlock(&log_root_tree->log_mutex); 3126 goto out_wake_log_root; 3127 } else if (ret) { 3128 btrfs_set_log_full_commit(trans); 3129 mutex_unlock(&log_root_tree->log_mutex); 3130 goto out_wake_log_root; 3131 } 3132 ret = btrfs_wait_tree_log_extents(log, mark); 3133 if (!ret) 3134 ret = btrfs_wait_tree_log_extents(log_root_tree, 3135 EXTENT_NEW | EXTENT_DIRTY); 3136 if (ret) { 3137 btrfs_set_log_full_commit(trans); 3138 mutex_unlock(&log_root_tree->log_mutex); 3139 goto out_wake_log_root; 3140 } 3141 3142 log_root_start = log_root_tree->node->start; 3143 log_root_level = btrfs_header_level(log_root_tree->node); 3144 log_root_tree->log_transid++; 3145 mutex_unlock(&log_root_tree->log_mutex); 3146 3147 /* 3148 * Here we are guaranteed that nobody is going to write the superblock 3149 * for the current transaction before us and that neither we do write 3150 * our superblock before the previous transaction finishes its commit 3151 * and writes its superblock, because: 3152 * 3153 * 1) We are holding a handle on the current transaction, so no body 3154 * can commit it until we release the handle; 3155 * 3156 * 2) Before writing our superblock we acquire the tree_log_mutex, so 3157 * if the previous transaction is still committing, and hasn't yet 3158 * written its superblock, we wait for it to do it, because a 3159 * transaction commit acquires the tree_log_mutex when the commit 3160 * begins and releases it only after writing its superblock. 3161 */ 3162 mutex_lock(&fs_info->tree_log_mutex); 3163 3164 /* 3165 * The previous transaction writeout phase could have failed, and thus 3166 * marked the fs in an error state. We must not commit here, as we 3167 * could have updated our generation in the super_for_commit and 3168 * writing the super here would result in transid mismatches. If there 3169 * is an error here just bail. 3170 */ 3171 if (BTRFS_FS_ERROR(fs_info)) { 3172 ret = -EIO; 3173 btrfs_set_log_full_commit(trans); 3174 btrfs_abort_transaction(trans, ret); 3175 mutex_unlock(&fs_info->tree_log_mutex); 3176 goto out_wake_log_root; 3177 } 3178 3179 btrfs_set_super_log_root(fs_info->super_for_commit, log_root_start); 3180 btrfs_set_super_log_root_level(fs_info->super_for_commit, log_root_level); 3181 ret = write_all_supers(fs_info, 1); 3182 mutex_unlock(&fs_info->tree_log_mutex); 3183 if (ret) { 3184 btrfs_set_log_full_commit(trans); 3185 btrfs_abort_transaction(trans, ret); 3186 goto out_wake_log_root; 3187 } 3188 3189 /* 3190 * We know there can only be one task here, since we have not yet set 3191 * root->log_commit[index1] to 0 and any task attempting to sync the 3192 * log must wait for the previous log transaction to commit if it's 3193 * still in progress or wait for the current log transaction commit if 3194 * someone else already started it. We use <= and not < because the 3195 * first log transaction has an ID of 0. 3196 */ 3197 ASSERT(btrfs_get_root_last_log_commit(root) <= log_transid); 3198 btrfs_set_root_last_log_commit(root, log_transid); 3199 3200 out_wake_log_root: 3201 mutex_lock(&log_root_tree->log_mutex); 3202 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret); 3203 3204 log_root_tree->log_transid_committed++; 3205 atomic_set(&log_root_tree->log_commit[index2], 0); 3206 mutex_unlock(&log_root_tree->log_mutex); 3207 3208 /* 3209 * The barrier before waitqueue_active (in cond_wake_up) is needed so 3210 * all the updates above are seen by the woken threads. It might not be 3211 * necessary, but proving that seems to be hard. 3212 */ 3213 cond_wake_up(&log_root_tree->log_commit_wait[index2]); 3214 out: 3215 mutex_lock(&root->log_mutex); 3216 btrfs_remove_all_log_ctxs(root, index1, ret); 3217 root->log_transid_committed++; 3218 atomic_set(&root->log_commit[index1], 0); 3219 mutex_unlock(&root->log_mutex); 3220 3221 /* 3222 * The barrier before waitqueue_active (in cond_wake_up) is needed so 3223 * all the updates above are seen by the woken threads. It might not be 3224 * necessary, but proving that seems to be hard. 3225 */ 3226 cond_wake_up(&root->log_commit_wait[index1]); 3227 return ret; 3228 } 3229 3230 static void free_log_tree(struct btrfs_trans_handle *trans, 3231 struct btrfs_root *log) 3232 { 3233 int ret; 3234 struct walk_control wc = { 3235 .free = 1, 3236 .process_func = process_one_buffer 3237 }; 3238 3239 if (log->node) { 3240 ret = walk_log_tree(trans, log, &wc); 3241 if (ret) { 3242 /* 3243 * We weren't able to traverse the entire log tree, the 3244 * typical scenario is getting an -EIO when reading an 3245 * extent buffer of the tree, due to a previous writeback 3246 * failure of it. 3247 */ 3248 set_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR, 3249 &log->fs_info->fs_state); 3250 3251 /* 3252 * Some extent buffers of the log tree may still be dirty 3253 * and not yet written back to storage, because we may 3254 * have updates to a log tree without syncing a log tree, 3255 * such as during rename and link operations. So flush 3256 * them out and wait for their writeback to complete, so 3257 * that we properly cleanup their state and pages. 3258 */ 3259 btrfs_write_marked_extents(log->fs_info, 3260 &log->dirty_log_pages, 3261 EXTENT_DIRTY | EXTENT_NEW); 3262 btrfs_wait_tree_log_extents(log, 3263 EXTENT_DIRTY | EXTENT_NEW); 3264 3265 if (trans) 3266 btrfs_abort_transaction(trans, ret); 3267 else 3268 btrfs_handle_fs_error(log->fs_info, ret, NULL); 3269 } 3270 } 3271 3272 extent_io_tree_release(&log->dirty_log_pages); 3273 extent_io_tree_release(&log->log_csum_range); 3274 3275 btrfs_put_root(log); 3276 } 3277 3278 /* 3279 * free all the extents used by the tree log. This should be called 3280 * at commit time of the full transaction 3281 */ 3282 int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) 3283 { 3284 if (root->log_root) { 3285 free_log_tree(trans, root->log_root); 3286 root->log_root = NULL; 3287 clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state); 3288 } 3289 return 0; 3290 } 3291 3292 int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, 3293 struct btrfs_fs_info *fs_info) 3294 { 3295 if (fs_info->log_root_tree) { 3296 free_log_tree(trans, fs_info->log_root_tree); 3297 fs_info->log_root_tree = NULL; 3298 clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &fs_info->tree_root->state); 3299 } 3300 return 0; 3301 } 3302 3303 /* 3304 * Check if an inode was logged in the current transaction. This correctly deals 3305 * with the case where the inode was logged but has a logged_trans of 0, which 3306 * happens if the inode is evicted and loaded again, as logged_trans is an in 3307 * memory only field (not persisted). 3308 * 3309 * Returns 1 if the inode was logged before in the transaction, 0 if it was not, 3310 * and < 0 on error. 3311 */ 3312 static int inode_logged(const struct btrfs_trans_handle *trans, 3313 struct btrfs_inode *inode, 3314 struct btrfs_path *path_in) 3315 { 3316 struct btrfs_path *path = path_in; 3317 struct btrfs_key key; 3318 int ret; 3319 3320 if (inode->logged_trans == trans->transid) 3321 return 1; 3322 3323 /* 3324 * If logged_trans is not 0, then we know the inode logged was not logged 3325 * in this transaction, so we can return false right away. 3326 */ 3327 if (inode->logged_trans > 0) 3328 return 0; 3329 3330 /* 3331 * If no log tree was created for this root in this transaction, then 3332 * the inode can not have been logged in this transaction. In that case 3333 * set logged_trans to anything greater than 0 and less than the current 3334 * transaction's ID, to avoid the search below in a future call in case 3335 * a log tree gets created after this. 3336 */ 3337 if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state)) { 3338 inode->logged_trans = trans->transid - 1; 3339 return 0; 3340 } 3341 3342 /* 3343 * We have a log tree and the inode's logged_trans is 0. We can't tell 3344 * for sure if the inode was logged before in this transaction by looking 3345 * only at logged_trans. We could be pessimistic and assume it was, but 3346 * that can lead to unnecessarily logging an inode during rename and link 3347 * operations, and then further updating the log in followup rename and 3348 * link operations, specially if it's a directory, which adds latency 3349 * visible to applications doing a series of rename or link operations. 3350 * 3351 * A logged_trans of 0 here can mean several things: 3352 * 3353 * 1) The inode was never logged since the filesystem was mounted, and may 3354 * or may have not been evicted and loaded again; 3355 * 3356 * 2) The inode was logged in a previous transaction, then evicted and 3357 * then loaded again; 3358 * 3359 * 3) The inode was logged in the current transaction, then evicted and 3360 * then loaded again. 3361 * 3362 * For cases 1) and 2) we don't want to return true, but we need to detect 3363 * case 3) and return true. So we do a search in the log root for the inode 3364 * item. 3365 */ 3366 key.objectid = btrfs_ino(inode); 3367 key.type = BTRFS_INODE_ITEM_KEY; 3368 key.offset = 0; 3369 3370 if (!path) { 3371 path = btrfs_alloc_path(); 3372 if (!path) 3373 return -ENOMEM; 3374 } 3375 3376 ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0); 3377 3378 if (path_in) 3379 btrfs_release_path(path); 3380 else 3381 btrfs_free_path(path); 3382 3383 /* 3384 * Logging an inode always results in logging its inode item. So if we 3385 * did not find the item we know the inode was not logged for sure. 3386 */ 3387 if (ret < 0) { 3388 return ret; 3389 } else if (ret > 0) { 3390 /* 3391 * Set logged_trans to a value greater than 0 and less then the 3392 * current transaction to avoid doing the search in future calls. 3393 */ 3394 inode->logged_trans = trans->transid - 1; 3395 return 0; 3396 } 3397 3398 /* 3399 * The inode was previously logged and then evicted, set logged_trans to 3400 * the current transacion's ID, to avoid future tree searches as long as 3401 * the inode is not evicted again. 3402 */ 3403 inode->logged_trans = trans->transid; 3404 3405 /* 3406 * If it's a directory, then we must set last_dir_index_offset to the 3407 * maximum possible value, so that the next attempt to log the inode does 3408 * not skip checking if dir index keys found in modified subvolume tree 3409 * leaves have been logged before, otherwise it would result in attempts 3410 * to insert duplicate dir index keys in the log tree. This must be done 3411 * because last_dir_index_offset is an in-memory only field, not persisted 3412 * in the inode item or any other on-disk structure, so its value is lost 3413 * once the inode is evicted. 3414 */ 3415 if (S_ISDIR(inode->vfs_inode.i_mode)) 3416 inode->last_dir_index_offset = (u64)-1; 3417 3418 return 1; 3419 } 3420 3421 /* 3422 * Delete a directory entry from the log if it exists. 3423 * 3424 * Returns < 0 on error 3425 * 1 if the entry does not exists 3426 * 0 if the entry existed and was successfully deleted 3427 */ 3428 static int del_logged_dentry(struct btrfs_trans_handle *trans, 3429 struct btrfs_root *log, 3430 struct btrfs_path *path, 3431 u64 dir_ino, 3432 const struct fscrypt_str *name, 3433 u64 index) 3434 { 3435 struct btrfs_dir_item *di; 3436 3437 /* 3438 * We only log dir index items of a directory, so we don't need to look 3439 * for dir item keys. 3440 */ 3441 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, 3442 index, name, -1); 3443 if (IS_ERR(di)) 3444 return PTR_ERR(di); 3445 else if (!di) 3446 return 1; 3447 3448 /* 3449 * We do not need to update the size field of the directory's 3450 * inode item because on log replay we update the field to reflect 3451 * all existing entries in the directory (see overwrite_item()). 3452 */ 3453 return btrfs_delete_one_dir_name(trans, log, path, di); 3454 } 3455 3456 /* 3457 * If both a file and directory are logged, and unlinks or renames are 3458 * mixed in, we have a few interesting corners: 3459 * 3460 * create file X in dir Y 3461 * link file X to X.link in dir Y 3462 * fsync file X 3463 * unlink file X but leave X.link 3464 * fsync dir Y 3465 * 3466 * After a crash we would expect only X.link to exist. But file X 3467 * didn't get fsync'd again so the log has back refs for X and X.link. 3468 * 3469 * We solve this by removing directory entries and inode backrefs from the 3470 * log when a file that was logged in the current transaction is 3471 * unlinked. Any later fsync will include the updated log entries, and 3472 * we'll be able to reconstruct the proper directory items from backrefs. 3473 * 3474 * This optimizations allows us to avoid relogging the entire inode 3475 * or the entire directory. 3476 */ 3477 void btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, 3478 struct btrfs_root *root, 3479 const struct fscrypt_str *name, 3480 struct btrfs_inode *dir, u64 index) 3481 { 3482 struct btrfs_path *path; 3483 int ret; 3484 3485 ret = inode_logged(trans, dir, NULL); 3486 if (ret == 0) 3487 return; 3488 else if (ret < 0) { 3489 btrfs_set_log_full_commit(trans); 3490 return; 3491 } 3492 3493 ret = join_running_log_trans(root); 3494 if (ret) 3495 return; 3496 3497 mutex_lock(&dir->log_mutex); 3498 3499 path = btrfs_alloc_path(); 3500 if (!path) { 3501 ret = -ENOMEM; 3502 goto out_unlock; 3503 } 3504 3505 ret = del_logged_dentry(trans, root->log_root, path, btrfs_ino(dir), 3506 name, index); 3507 btrfs_free_path(path); 3508 out_unlock: 3509 mutex_unlock(&dir->log_mutex); 3510 if (ret < 0) 3511 btrfs_set_log_full_commit(trans); 3512 btrfs_end_log_trans(root); 3513 } 3514 3515 /* see comments for btrfs_del_dir_entries_in_log */ 3516 void btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, 3517 struct btrfs_root *root, 3518 const struct fscrypt_str *name, 3519 struct btrfs_inode *inode, u64 dirid) 3520 { 3521 struct btrfs_root *log; 3522 u64 index; 3523 int ret; 3524 3525 ret = inode_logged(trans, inode, NULL); 3526 if (ret == 0) 3527 return; 3528 else if (ret < 0) { 3529 btrfs_set_log_full_commit(trans); 3530 return; 3531 } 3532 3533 ret = join_running_log_trans(root); 3534 if (ret) 3535 return; 3536 log = root->log_root; 3537 mutex_lock(&inode->log_mutex); 3538 3539 ret = btrfs_del_inode_ref(trans, log, name, btrfs_ino(inode), 3540 dirid, &index); 3541 mutex_unlock(&inode->log_mutex); 3542 if (ret < 0 && ret != -ENOENT) 3543 btrfs_set_log_full_commit(trans); 3544 btrfs_end_log_trans(root); 3545 } 3546 3547 /* 3548 * creates a range item in the log for 'dirid'. first_offset and 3549 * last_offset tell us which parts of the key space the log should 3550 * be considered authoritative for. 3551 */ 3552 static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, 3553 struct btrfs_root *log, 3554 struct btrfs_path *path, 3555 u64 dirid, 3556 u64 first_offset, u64 last_offset) 3557 { 3558 int ret; 3559 struct btrfs_key key; 3560 struct btrfs_dir_log_item *item; 3561 3562 key.objectid = dirid; 3563 key.offset = first_offset; 3564 key.type = BTRFS_DIR_LOG_INDEX_KEY; 3565 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); 3566 /* 3567 * -EEXIST is fine and can happen sporadically when we are logging a 3568 * directory and have concurrent insertions in the subvolume's tree for 3569 * items from other inodes and that result in pushing off some dir items 3570 * from one leaf to another in order to accommodate for the new items. 3571 * This results in logging the same dir index range key. 3572 */ 3573 if (ret && ret != -EEXIST) 3574 return ret; 3575 3576 item = btrfs_item_ptr(path->nodes[0], path->slots[0], 3577 struct btrfs_dir_log_item); 3578 if (ret == -EEXIST) { 3579 const u64 curr_end = btrfs_dir_log_end(path->nodes[0], item); 3580 3581 /* 3582 * btrfs_del_dir_entries_in_log() might have been called during 3583 * an unlink between the initial insertion of this key and the 3584 * current update, or we might be logging a single entry deletion 3585 * during a rename, so set the new last_offset to the max value. 3586 */ 3587 last_offset = max(last_offset, curr_end); 3588 } 3589 btrfs_set_dir_log_end(path->nodes[0], item, last_offset); 3590 btrfs_release_path(path); 3591 return 0; 3592 } 3593 3594 static int flush_dir_items_batch(struct btrfs_trans_handle *trans, 3595 struct btrfs_inode *inode, 3596 struct extent_buffer *src, 3597 struct btrfs_path *dst_path, 3598 int start_slot, 3599 int count) 3600 { 3601 struct btrfs_root *log = inode->root->log_root; 3602 char *ins_data = NULL; 3603 struct btrfs_item_batch batch; 3604 struct extent_buffer *dst; 3605 unsigned long src_offset; 3606 unsigned long dst_offset; 3607 u64 last_index; 3608 struct btrfs_key key; 3609 u32 item_size; 3610 int ret; 3611 int i; 3612 3613 ASSERT(count > 0); 3614 batch.nr = count; 3615 3616 if (count == 1) { 3617 btrfs_item_key_to_cpu(src, &key, start_slot); 3618 item_size = btrfs_item_size(src, start_slot); 3619 batch.keys = &key; 3620 batch.data_sizes = &item_size; 3621 batch.total_data_size = item_size; 3622 } else { 3623 struct btrfs_key *ins_keys; 3624 u32 *ins_sizes; 3625 3626 ins_data = kmalloc(count * sizeof(u32) + 3627 count * sizeof(struct btrfs_key), GFP_NOFS); 3628 if (!ins_data) 3629 return -ENOMEM; 3630 3631 ins_sizes = (u32 *)ins_data; 3632 ins_keys = (struct btrfs_key *)(ins_data + count * sizeof(u32)); 3633 batch.keys = ins_keys; 3634 batch.data_sizes = ins_sizes; 3635 batch.total_data_size = 0; 3636 3637 for (i = 0; i < count; i++) { 3638 const int slot = start_slot + i; 3639 3640 btrfs_item_key_to_cpu(src, &ins_keys[i], slot); 3641 ins_sizes[i] = btrfs_item_size(src, slot); 3642 batch.total_data_size += ins_sizes[i]; 3643 } 3644 } 3645 3646 ret = btrfs_insert_empty_items(trans, log, dst_path, &batch); 3647 if (ret) 3648 goto out; 3649 3650 dst = dst_path->nodes[0]; 3651 /* 3652 * Copy all the items in bulk, in a single copy operation. Item data is 3653 * organized such that it's placed at the end of a leaf and from right 3654 * to left. For example, the data for the second item ends at an offset 3655 * that matches the offset where the data for the first item starts, the 3656 * data for the third item ends at an offset that matches the offset 3657 * where the data of the second items starts, and so on. 3658 * Therefore our source and destination start offsets for copy match the 3659 * offsets of the last items (highest slots). 3660 */ 3661 dst_offset = btrfs_item_ptr_offset(dst, dst_path->slots[0] + count - 1); 3662 src_offset = btrfs_item_ptr_offset(src, start_slot + count - 1); 3663 copy_extent_buffer(dst, src, dst_offset, src_offset, batch.total_data_size); 3664 btrfs_release_path(dst_path); 3665 3666 last_index = batch.keys[count - 1].offset; 3667 ASSERT(last_index > inode->last_dir_index_offset); 3668 3669 /* 3670 * If for some unexpected reason the last item's index is not greater 3671 * than the last index we logged, warn and force a transaction commit. 3672 */ 3673 if (WARN_ON(last_index <= inode->last_dir_index_offset)) 3674 ret = BTRFS_LOG_FORCE_COMMIT; 3675 else 3676 inode->last_dir_index_offset = last_index; 3677 3678 if (btrfs_get_first_dir_index_to_log(inode) == 0) 3679 btrfs_set_first_dir_index_to_log(inode, batch.keys[0].offset); 3680 out: 3681 kfree(ins_data); 3682 3683 return ret; 3684 } 3685 3686 static int clone_leaf(struct btrfs_path *path, struct btrfs_log_ctx *ctx) 3687 { 3688 const int slot = path->slots[0]; 3689 3690 if (ctx->scratch_eb) { 3691 copy_extent_buffer_full(ctx->scratch_eb, path->nodes[0]); 3692 } else { 3693 ctx->scratch_eb = btrfs_clone_extent_buffer(path->nodes[0]); 3694 if (!ctx->scratch_eb) 3695 return -ENOMEM; 3696 } 3697 3698 btrfs_release_path(path); 3699 path->nodes[0] = ctx->scratch_eb; 3700 path->slots[0] = slot; 3701 /* 3702 * Add extra ref to scratch eb so that it is not freed when callers 3703 * release the path, so we can reuse it later if needed. 3704 */ 3705 atomic_inc(&ctx->scratch_eb->refs); 3706 3707 return 0; 3708 } 3709 3710 static int process_dir_items_leaf(struct btrfs_trans_handle *trans, 3711 struct btrfs_inode *inode, 3712 struct btrfs_path *path, 3713 struct btrfs_path *dst_path, 3714 struct btrfs_log_ctx *ctx, 3715 u64 *last_old_dentry_offset) 3716 { 3717 struct btrfs_root *log = inode->root->log_root; 3718 struct extent_buffer *src; 3719 const int nritems = btrfs_header_nritems(path->nodes[0]); 3720 const u64 ino = btrfs_ino(inode); 3721 bool last_found = false; 3722 int batch_start = 0; 3723 int batch_size = 0; 3724 int ret; 3725 3726 /* 3727 * We need to clone the leaf, release the read lock on it, and use the 3728 * clone before modifying the log tree. See the comment at copy_items() 3729 * about why we need to do this. 3730 */ 3731 ret = clone_leaf(path, ctx); 3732 if (ret < 0) 3733 return ret; 3734 3735 src = path->nodes[0]; 3736 3737 for (int i = path->slots[0]; i < nritems; i++) { 3738 struct btrfs_dir_item *di; 3739 struct btrfs_key key; 3740 int ret; 3741 3742 btrfs_item_key_to_cpu(src, &key, i); 3743 3744 if (key.objectid != ino || key.type != BTRFS_DIR_INDEX_KEY) { 3745 last_found = true; 3746 break; 3747 } 3748 3749 di = btrfs_item_ptr(src, i, struct btrfs_dir_item); 3750 3751 /* 3752 * Skip ranges of items that consist only of dir item keys created 3753 * in past transactions. However if we find a gap, we must log a 3754 * dir index range item for that gap, so that index keys in that 3755 * gap are deleted during log replay. 3756 */ 3757 if (btrfs_dir_transid(src, di) < trans->transid) { 3758 if (key.offset > *last_old_dentry_offset + 1) { 3759 ret = insert_dir_log_key(trans, log, dst_path, 3760 ino, *last_old_dentry_offset + 1, 3761 key.offset - 1); 3762 if (ret < 0) 3763 return ret; 3764 } 3765 3766 *last_old_dentry_offset = key.offset; 3767 continue; 3768 } 3769 3770 /* If we logged this dir index item before, we can skip it. */ 3771 if (key.offset <= inode->last_dir_index_offset) 3772 continue; 3773 3774 /* 3775 * We must make sure that when we log a directory entry, the 3776 * corresponding inode, after log replay, has a matching link 3777 * count. For example: 3778 * 3779 * touch foo 3780 * mkdir mydir 3781 * sync 3782 * ln foo mydir/bar 3783 * xfs_io -c "fsync" mydir 3784 * <crash> 3785 * <mount fs and log replay> 3786 * 3787 * Would result in a fsync log that when replayed, our file inode 3788 * would have a link count of 1, but we get two directory entries 3789 * pointing to the same inode. After removing one of the names, 3790 * it would not be possible to remove the other name, which 3791 * resulted always in stale file handle errors, and would not be 3792 * possible to rmdir the parent directory, since its i_size could 3793 * never be decremented to the value BTRFS_EMPTY_DIR_SIZE, 3794 * resulting in -ENOTEMPTY errors. 3795 */ 3796 if (!ctx->log_new_dentries) { 3797 struct btrfs_key di_key; 3798 3799 btrfs_dir_item_key_to_cpu(src, di, &di_key); 3800 if (di_key.type != BTRFS_ROOT_ITEM_KEY) 3801 ctx->log_new_dentries = true; 3802 } 3803 3804 if (batch_size == 0) 3805 batch_start = i; 3806 batch_size++; 3807 } 3808 3809 if (batch_size > 0) { 3810 int ret; 3811 3812 ret = flush_dir_items_batch(trans, inode, src, dst_path, 3813 batch_start, batch_size); 3814 if (ret < 0) 3815 return ret; 3816 } 3817 3818 return last_found ? 1 : 0; 3819 } 3820 3821 /* 3822 * log all the items included in the current transaction for a given 3823 * directory. This also creates the range items in the log tree required 3824 * to replay anything deleted before the fsync 3825 */ 3826 static noinline int log_dir_items(struct btrfs_trans_handle *trans, 3827 struct btrfs_inode *inode, 3828 struct btrfs_path *path, 3829 struct btrfs_path *dst_path, 3830 struct btrfs_log_ctx *ctx, 3831 u64 min_offset, u64 *last_offset_ret) 3832 { 3833 struct btrfs_key min_key; 3834 struct btrfs_root *root = inode->root; 3835 struct btrfs_root *log = root->log_root; 3836 int ret; 3837 u64 last_old_dentry_offset = min_offset - 1; 3838 u64 last_offset = (u64)-1; 3839 u64 ino = btrfs_ino(inode); 3840 3841 min_key.objectid = ino; 3842 min_key.type = BTRFS_DIR_INDEX_KEY; 3843 min_key.offset = min_offset; 3844 3845 ret = btrfs_search_forward(root, &min_key, path, trans->transid); 3846 3847 /* 3848 * we didn't find anything from this transaction, see if there 3849 * is anything at all 3850 */ 3851 if (ret != 0 || min_key.objectid != ino || 3852 min_key.type != BTRFS_DIR_INDEX_KEY) { 3853 min_key.objectid = ino; 3854 min_key.type = BTRFS_DIR_INDEX_KEY; 3855 min_key.offset = (u64)-1; 3856 btrfs_release_path(path); 3857 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); 3858 if (ret < 0) { 3859 btrfs_release_path(path); 3860 return ret; 3861 } 3862 ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY); 3863 3864 /* if ret == 0 there are items for this type, 3865 * create a range to tell us the last key of this type. 3866 * otherwise, there are no items in this directory after 3867 * *min_offset, and we create a range to indicate that. 3868 */ 3869 if (ret == 0) { 3870 struct btrfs_key tmp; 3871 3872 btrfs_item_key_to_cpu(path->nodes[0], &tmp, 3873 path->slots[0]); 3874 if (tmp.type == BTRFS_DIR_INDEX_KEY) 3875 last_old_dentry_offset = tmp.offset; 3876 } else if (ret > 0) { 3877 ret = 0; 3878 } 3879 3880 goto done; 3881 } 3882 3883 /* go backward to find any previous key */ 3884 ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY); 3885 if (ret == 0) { 3886 struct btrfs_key tmp; 3887 3888 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); 3889 /* 3890 * The dir index key before the first one we found that needs to 3891 * be logged might be in a previous leaf, and there might be a 3892 * gap between these keys, meaning that we had deletions that 3893 * happened. So the key range item we log (key type 3894 * BTRFS_DIR_LOG_INDEX_KEY) must cover a range that starts at the 3895 * previous key's offset plus 1, so that those deletes are replayed. 3896 */ 3897 if (tmp.type == BTRFS_DIR_INDEX_KEY) 3898 last_old_dentry_offset = tmp.offset; 3899 } else if (ret < 0) { 3900 goto done; 3901 } 3902 3903 btrfs_release_path(path); 3904 3905 /* 3906 * Find the first key from this transaction again or the one we were at 3907 * in the loop below in case we had to reschedule. We may be logging the 3908 * directory without holding its VFS lock, which happen when logging new 3909 * dentries (through log_new_dir_dentries()) or in some cases when we 3910 * need to log the parent directory of an inode. This means a dir index 3911 * key might be deleted from the inode's root, and therefore we may not 3912 * find it anymore. If we can't find it, just move to the next key. We 3913 * can not bail out and ignore, because if we do that we will simply 3914 * not log dir index keys that come after the one that was just deleted 3915 * and we can end up logging a dir index range that ends at (u64)-1 3916 * (@last_offset is initialized to that), resulting in removing dir 3917 * entries we should not remove at log replay time. 3918 */ 3919 search: 3920 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); 3921 if (ret > 0) { 3922 ret = btrfs_next_item(root, path); 3923 if (ret > 0) { 3924 /* There are no more keys in the inode's root. */ 3925 ret = 0; 3926 goto done; 3927 } 3928 } 3929 if (ret < 0) 3930 goto done; 3931 3932 /* 3933 * we have a block from this transaction, log every item in it 3934 * from our directory 3935 */ 3936 while (1) { 3937 ret = process_dir_items_leaf(trans, inode, path, dst_path, ctx, 3938 &last_old_dentry_offset); 3939 if (ret != 0) { 3940 if (ret > 0) 3941 ret = 0; 3942 goto done; 3943 } 3944 path->slots[0] = btrfs_header_nritems(path->nodes[0]); 3945 3946 /* 3947 * look ahead to the next item and see if it is also 3948 * from this directory and from this transaction 3949 */ 3950 ret = btrfs_next_leaf(root, path); 3951 if (ret) { 3952 if (ret == 1) { 3953 last_offset = (u64)-1; 3954 ret = 0; 3955 } 3956 goto done; 3957 } 3958 btrfs_item_key_to_cpu(path->nodes[0], &min_key, path->slots[0]); 3959 if (min_key.objectid != ino || min_key.type != BTRFS_DIR_INDEX_KEY) { 3960 last_offset = (u64)-1; 3961 goto done; 3962 } 3963 if (btrfs_header_generation(path->nodes[0]) != trans->transid) { 3964 /* 3965 * The next leaf was not changed in the current transaction 3966 * and has at least one dir index key. 3967 * We check for the next key because there might have been 3968 * one or more deletions between the last key we logged and 3969 * that next key. So the key range item we log (key type 3970 * BTRFS_DIR_LOG_INDEX_KEY) must end at the next key's 3971 * offset minus 1, so that those deletes are replayed. 3972 */ 3973 last_offset = min_key.offset - 1; 3974 goto done; 3975 } 3976 if (need_resched()) { 3977 btrfs_release_path(path); 3978 cond_resched(); 3979 goto search; 3980 } 3981 } 3982 done: 3983 btrfs_release_path(path); 3984 btrfs_release_path(dst_path); 3985 3986 if (ret == 0) { 3987 *last_offset_ret = last_offset; 3988 /* 3989 * In case the leaf was changed in the current transaction but 3990 * all its dir items are from a past transaction, the last item 3991 * in the leaf is a dir item and there's no gap between that last 3992 * dir item and the first one on the next leaf (which did not 3993 * change in the current transaction), then we don't need to log 3994 * a range, last_old_dentry_offset is == to last_offset. 3995 */ 3996 ASSERT(last_old_dentry_offset <= last_offset); 3997 if (last_old_dentry_offset < last_offset) 3998 ret = insert_dir_log_key(trans, log, path, ino, 3999 last_old_dentry_offset + 1, 4000 last_offset); 4001 } 4002 4003 return ret; 4004 } 4005 4006 /* 4007 * If the inode was logged before and it was evicted, then its 4008 * last_dir_index_offset is (u64)-1, so we don't the value of the last index 4009 * key offset. If that's the case, search for it and update the inode. This 4010 * is to avoid lookups in the log tree every time we try to insert a dir index 4011 * key from a leaf changed in the current transaction, and to allow us to always 4012 * do batch insertions of dir index keys. 4013 */ 4014 static int update_last_dir_index_offset(struct btrfs_inode *inode, 4015 struct btrfs_path *path, 4016 const struct btrfs_log_ctx *ctx) 4017 { 4018 const u64 ino = btrfs_ino(inode); 4019 struct btrfs_key key; 4020 int ret; 4021 4022 lockdep_assert_held(&inode->log_mutex); 4023 4024 if (inode->last_dir_index_offset != (u64)-1) 4025 return 0; 4026 4027 if (!ctx->logged_before) { 4028 inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1; 4029 return 0; 4030 } 4031 4032 key.objectid = ino; 4033 key.type = BTRFS_DIR_INDEX_KEY; 4034 key.offset = (u64)-1; 4035 4036 ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0); 4037 /* 4038 * An error happened or we actually have an index key with an offset 4039 * value of (u64)-1. Bail out, we're done. 4040 */ 4041 if (ret <= 0) 4042 goto out; 4043 4044 ret = 0; 4045 inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1; 4046 4047 /* 4048 * No dir index items, bail out and leave last_dir_index_offset with 4049 * the value right before the first valid index value. 4050 */ 4051 if (path->slots[0] == 0) 4052 goto out; 4053 4054 /* 4055 * btrfs_search_slot() left us at one slot beyond the slot with the last 4056 * index key, or beyond the last key of the directory that is not an 4057 * index key. If we have an index key before, set last_dir_index_offset 4058 * to its offset value, otherwise leave it with a value right before the 4059 * first valid index value, as it means we have an empty directory. 4060 */ 4061 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1); 4062 if (key.objectid == ino && key.type == BTRFS_DIR_INDEX_KEY) 4063 inode->last_dir_index_offset = key.offset; 4064 4065 out: 4066 btrfs_release_path(path); 4067 4068 return ret; 4069 } 4070 4071 /* 4072 * logging directories is very similar to logging inodes, We find all the items 4073 * from the current transaction and write them to the log. 4074 * 4075 * The recovery code scans the directory in the subvolume, and if it finds a 4076 * key in the range logged that is not present in the log tree, then it means 4077 * that dir entry was unlinked during the transaction. 4078 * 4079 * In order for that scan to work, we must include one key smaller than 4080 * the smallest logged by this transaction and one key larger than the largest 4081 * key logged by this transaction. 4082 */ 4083 static noinline int log_directory_changes(struct btrfs_trans_handle *trans, 4084 struct btrfs_inode *inode, 4085 struct btrfs_path *path, 4086 struct btrfs_path *dst_path, 4087 struct btrfs_log_ctx *ctx) 4088 { 4089 u64 min_key; 4090 u64 max_key; 4091 int ret; 4092 4093 ret = update_last_dir_index_offset(inode, path, ctx); 4094 if (ret) 4095 return ret; 4096 4097 min_key = BTRFS_DIR_START_INDEX; 4098 max_key = 0; 4099 4100 while (1) { 4101 ret = log_dir_items(trans, inode, path, dst_path, 4102 ctx, min_key, &max_key); 4103 if (ret) 4104 return ret; 4105 if (max_key == (u64)-1) 4106 break; 4107 min_key = max_key + 1; 4108 } 4109 4110 return 0; 4111 } 4112 4113 /* 4114 * a helper function to drop items from the log before we relog an 4115 * inode. max_key_type indicates the highest item type to remove. 4116 * This cannot be run for file data extents because it does not 4117 * free the extents they point to. 4118 */ 4119 static int drop_inode_items(struct btrfs_trans_handle *trans, 4120 struct btrfs_root *log, 4121 struct btrfs_path *path, 4122 struct btrfs_inode *inode, 4123 int max_key_type) 4124 { 4125 int ret; 4126 struct btrfs_key key; 4127 struct btrfs_key found_key; 4128 int start_slot; 4129 4130 key.objectid = btrfs_ino(inode); 4131 key.type = max_key_type; 4132 key.offset = (u64)-1; 4133 4134 while (1) { 4135 ret = btrfs_search_slot(trans, log, &key, path, -1, 1); 4136 if (ret < 0) { 4137 break; 4138 } else if (ret > 0) { 4139 if (path->slots[0] == 0) 4140 break; 4141 path->slots[0]--; 4142 } 4143 4144 btrfs_item_key_to_cpu(path->nodes[0], &found_key, 4145 path->slots[0]); 4146 4147 if (found_key.objectid != key.objectid) 4148 break; 4149 4150 found_key.offset = 0; 4151 found_key.type = 0; 4152 ret = btrfs_bin_search(path->nodes[0], 0, &found_key, &start_slot); 4153 if (ret < 0) 4154 break; 4155 4156 ret = btrfs_del_items(trans, log, path, start_slot, 4157 path->slots[0] - start_slot + 1); 4158 /* 4159 * If start slot isn't 0 then we don't need to re-search, we've 4160 * found the last guy with the objectid in this tree. 4161 */ 4162 if (ret || start_slot != 0) 4163 break; 4164 btrfs_release_path(path); 4165 } 4166 btrfs_release_path(path); 4167 if (ret > 0) 4168 ret = 0; 4169 return ret; 4170 } 4171 4172 static int truncate_inode_items(struct btrfs_trans_handle *trans, 4173 struct btrfs_root *log_root, 4174 struct btrfs_inode *inode, 4175 u64 new_size, u32 min_type) 4176 { 4177 struct btrfs_truncate_control control = { 4178 .new_size = new_size, 4179 .ino = btrfs_ino(inode), 4180 .min_type = min_type, 4181 .skip_ref_updates = true, 4182 }; 4183 4184 return btrfs_truncate_inode_items(trans, log_root, &control); 4185 } 4186 4187 static void fill_inode_item(struct btrfs_trans_handle *trans, 4188 struct extent_buffer *leaf, 4189 struct btrfs_inode_item *item, 4190 struct inode *inode, int log_inode_only, 4191 u64 logged_isize) 4192 { 4193 struct btrfs_map_token token; 4194 u64 flags; 4195 4196 btrfs_init_map_token(&token, leaf); 4197 4198 if (log_inode_only) { 4199 /* set the generation to zero so the recover code 4200 * can tell the difference between an logging 4201 * just to say 'this inode exists' and a logging 4202 * to say 'update this inode with these values' 4203 */ 4204 btrfs_set_token_inode_generation(&token, item, 0); 4205 btrfs_set_token_inode_size(&token, item, logged_isize); 4206 } else { 4207 btrfs_set_token_inode_generation(&token, item, 4208 BTRFS_I(inode)->generation); 4209 btrfs_set_token_inode_size(&token, item, inode->i_size); 4210 } 4211 4212 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); 4213 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); 4214 btrfs_set_token_inode_mode(&token, item, inode->i_mode); 4215 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); 4216 4217 btrfs_set_token_timespec_sec(&token, &item->atime, 4218 inode_get_atime_sec(inode)); 4219 btrfs_set_token_timespec_nsec(&token, &item->atime, 4220 inode_get_atime_nsec(inode)); 4221 4222 btrfs_set_token_timespec_sec(&token, &item->mtime, 4223 inode_get_mtime_sec(inode)); 4224 btrfs_set_token_timespec_nsec(&token, &item->mtime, 4225 inode_get_mtime_nsec(inode)); 4226 4227 btrfs_set_token_timespec_sec(&token, &item->ctime, 4228 inode_get_ctime_sec(inode)); 4229 btrfs_set_token_timespec_nsec(&token, &item->ctime, 4230 inode_get_ctime_nsec(inode)); 4231 4232 /* 4233 * We do not need to set the nbytes field, in fact during a fast fsync 4234 * its value may not even be correct, since a fast fsync does not wait 4235 * for ordered extent completion, which is where we update nbytes, it 4236 * only waits for writeback to complete. During log replay as we find 4237 * file extent items and replay them, we adjust the nbytes field of the 4238 * inode item in subvolume tree as needed (see overwrite_item()). 4239 */ 4240 4241 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); 4242 btrfs_set_token_inode_transid(&token, item, trans->transid); 4243 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); 4244 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags, 4245 BTRFS_I(inode)->ro_flags); 4246 btrfs_set_token_inode_flags(&token, item, flags); 4247 btrfs_set_token_inode_block_group(&token, item, 0); 4248 } 4249 4250 static int log_inode_item(struct btrfs_trans_handle *trans, 4251 struct btrfs_root *log, struct btrfs_path *path, 4252 struct btrfs_inode *inode, bool inode_item_dropped) 4253 { 4254 struct btrfs_inode_item *inode_item; 4255 struct btrfs_key key; 4256 int ret; 4257 4258 btrfs_get_inode_key(inode, &key); 4259 /* 4260 * If we are doing a fast fsync and the inode was logged before in the 4261 * current transaction, then we know the inode was previously logged and 4262 * it exists in the log tree. For performance reasons, in this case use 4263 * btrfs_search_slot() directly with ins_len set to 0 so that we never 4264 * attempt a write lock on the leaf's parent, which adds unnecessary lock 4265 * contention in case there are concurrent fsyncs for other inodes of the 4266 * same subvolume. Using btrfs_insert_empty_item() when the inode item 4267 * already exists can also result in unnecessarily splitting a leaf. 4268 */ 4269 if (!inode_item_dropped && inode->logged_trans == trans->transid) { 4270 ret = btrfs_search_slot(trans, log, &key, path, 0, 1); 4271 ASSERT(ret <= 0); 4272 if (ret > 0) 4273 ret = -ENOENT; 4274 } else { 4275 /* 4276 * This means it is the first fsync in the current transaction, 4277 * so the inode item is not in the log and we need to insert it. 4278 * We can never get -EEXIST because we are only called for a fast 4279 * fsync and in case an inode eviction happens after the inode was 4280 * logged before in the current transaction, when we load again 4281 * the inode, we set BTRFS_INODE_NEEDS_FULL_SYNC on its runtime 4282 * flags and set ->logged_trans to 0. 4283 */ 4284 ret = btrfs_insert_empty_item(trans, log, path, &key, 4285 sizeof(*inode_item)); 4286 ASSERT(ret != -EEXIST); 4287 } 4288 if (ret) 4289 return ret; 4290 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], 4291 struct btrfs_inode_item); 4292 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode, 4293 0, 0); 4294 btrfs_release_path(path); 4295 return 0; 4296 } 4297 4298 static int log_csums(struct btrfs_trans_handle *trans, 4299 struct btrfs_inode *inode, 4300 struct btrfs_root *log_root, 4301 struct btrfs_ordered_sum *sums) 4302 { 4303 const u64 lock_end = sums->logical + sums->len - 1; 4304 struct extent_state *cached_state = NULL; 4305 int ret; 4306 4307 /* 4308 * If this inode was not used for reflink operations in the current 4309 * transaction with new extents, then do the fast path, no need to 4310 * worry about logging checksum items with overlapping ranges. 4311 */ 4312 if (inode->last_reflink_trans < trans->transid) 4313 return btrfs_csum_file_blocks(trans, log_root, sums); 4314 4315 /* 4316 * Serialize logging for checksums. This is to avoid racing with the 4317 * same checksum being logged by another task that is logging another 4318 * file which happens to refer to the same extent as well. Such races 4319 * can leave checksum items in the log with overlapping ranges. 4320 */ 4321 ret = lock_extent(&log_root->log_csum_range, sums->logical, lock_end, 4322 &cached_state); 4323 if (ret) 4324 return ret; 4325 /* 4326 * Due to extent cloning, we might have logged a csum item that covers a 4327 * subrange of a cloned extent, and later we can end up logging a csum 4328 * item for a larger subrange of the same extent or the entire range. 4329 * This would leave csum items in the log tree that cover the same range 4330 * and break the searches for checksums in the log tree, resulting in 4331 * some checksums missing in the fs/subvolume tree. So just delete (or 4332 * trim and adjust) any existing csum items in the log for this range. 4333 */ 4334 ret = btrfs_del_csums(trans, log_root, sums->logical, sums->len); 4335 if (!ret) 4336 ret = btrfs_csum_file_blocks(trans, log_root, sums); 4337 4338 unlock_extent(&log_root->log_csum_range, sums->logical, lock_end, 4339 &cached_state); 4340 4341 return ret; 4342 } 4343 4344 static noinline int copy_items(struct btrfs_trans_handle *trans, 4345 struct btrfs_inode *inode, 4346 struct btrfs_path *dst_path, 4347 struct btrfs_path *src_path, 4348 int start_slot, int nr, int inode_only, 4349 u64 logged_isize, struct btrfs_log_ctx *ctx) 4350 { 4351 struct btrfs_root *log = inode->root->log_root; 4352 struct btrfs_file_extent_item *extent; 4353 struct extent_buffer *src; 4354 int ret; 4355 struct btrfs_key *ins_keys; 4356 u32 *ins_sizes; 4357 struct btrfs_item_batch batch; 4358 char *ins_data; 4359 int dst_index; 4360 const bool skip_csum = (inode->flags & BTRFS_INODE_NODATASUM); 4361 const u64 i_size = i_size_read(&inode->vfs_inode); 4362 4363 /* 4364 * To keep lockdep happy and avoid deadlocks, clone the source leaf and 4365 * use the clone. This is because otherwise we would be changing the log 4366 * tree, to insert items from the subvolume tree or insert csum items, 4367 * while holding a read lock on a leaf from the subvolume tree, which 4368 * creates a nasty lock dependency when COWing log tree nodes/leaves: 4369 * 4370 * 1) Modifying the log tree triggers an extent buffer allocation while 4371 * holding a write lock on a parent extent buffer from the log tree. 4372 * Allocating the pages for an extent buffer, or the extent buffer 4373 * struct, can trigger inode eviction and finally the inode eviction 4374 * will trigger a release/remove of a delayed node, which requires 4375 * taking the delayed node's mutex; 4376 * 4377 * 2) Allocating a metadata extent for a log tree can trigger the async 4378 * reclaim thread and make us wait for it to release enough space and 4379 * unblock our reservation ticket. The reclaim thread can start 4380 * flushing delayed items, and that in turn results in the need to 4381 * lock delayed node mutexes and in the need to write lock extent 4382 * buffers of a subvolume tree - all this while holding a write lock 4383 * on the parent extent buffer in the log tree. 4384 * 4385 * So one task in scenario 1) running in parallel with another task in 4386 * scenario 2) could lead to a deadlock, one wanting to lock a delayed 4387 * node mutex while having a read lock on a leaf from the subvolume, 4388 * while the other is holding the delayed node's mutex and wants to 4389 * write lock the same subvolume leaf for flushing delayed items. 4390 */ 4391 ret = clone_leaf(src_path, ctx); 4392 if (ret < 0) 4393 return ret; 4394 4395 src = src_path->nodes[0]; 4396 4397 ins_data = kmalloc(nr * sizeof(struct btrfs_key) + 4398 nr * sizeof(u32), GFP_NOFS); 4399 if (!ins_data) 4400 return -ENOMEM; 4401 4402 ins_sizes = (u32 *)ins_data; 4403 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); 4404 batch.keys = ins_keys; 4405 batch.data_sizes = ins_sizes; 4406 batch.total_data_size = 0; 4407 batch.nr = 0; 4408 4409 dst_index = 0; 4410 for (int i = 0; i < nr; i++) { 4411 const int src_slot = start_slot + i; 4412 struct btrfs_root *csum_root; 4413 struct btrfs_ordered_sum *sums; 4414 struct btrfs_ordered_sum *sums_next; 4415 LIST_HEAD(ordered_sums); 4416 u64 disk_bytenr; 4417 u64 disk_num_bytes; 4418 u64 extent_offset; 4419 u64 extent_num_bytes; 4420 bool is_old_extent; 4421 4422 btrfs_item_key_to_cpu(src, &ins_keys[dst_index], src_slot); 4423 4424 if (ins_keys[dst_index].type != BTRFS_EXTENT_DATA_KEY) 4425 goto add_to_batch; 4426 4427 extent = btrfs_item_ptr(src, src_slot, 4428 struct btrfs_file_extent_item); 4429 4430 is_old_extent = (btrfs_file_extent_generation(src, extent) < 4431 trans->transid); 4432 4433 /* 4434 * Don't copy extents from past generations. That would make us 4435 * log a lot more metadata for common cases like doing only a 4436 * few random writes into a file and then fsync it for the first 4437 * time or after the full sync flag is set on the inode. We can 4438 * get leaves full of extent items, most of which are from past 4439 * generations, so we can skip them - as long as the inode has 4440 * not been the target of a reflink operation in this transaction, 4441 * as in that case it might have had file extent items with old 4442 * generations copied into it. We also must always log prealloc 4443 * extents that start at or beyond eof, otherwise we would lose 4444 * them on log replay. 4445 */ 4446 if (is_old_extent && 4447 ins_keys[dst_index].offset < i_size && 4448 inode->last_reflink_trans < trans->transid) 4449 continue; 4450 4451 if (skip_csum) 4452 goto add_to_batch; 4453 4454 /* Only regular extents have checksums. */ 4455 if (btrfs_file_extent_type(src, extent) != BTRFS_FILE_EXTENT_REG) 4456 goto add_to_batch; 4457 4458 /* 4459 * If it's an extent created in a past transaction, then its 4460 * checksums are already accessible from the committed csum tree, 4461 * no need to log them. 4462 */ 4463 if (is_old_extent) 4464 goto add_to_batch; 4465 4466 disk_bytenr = btrfs_file_extent_disk_bytenr(src, extent); 4467 /* If it's an explicit hole, there are no checksums. */ 4468 if (disk_bytenr == 0) 4469 goto add_to_batch; 4470 4471 disk_num_bytes = btrfs_file_extent_disk_num_bytes(src, extent); 4472 4473 if (btrfs_file_extent_compression(src, extent)) { 4474 extent_offset = 0; 4475 extent_num_bytes = disk_num_bytes; 4476 } else { 4477 extent_offset = btrfs_file_extent_offset(src, extent); 4478 extent_num_bytes = btrfs_file_extent_num_bytes(src, extent); 4479 } 4480 4481 csum_root = btrfs_csum_root(trans->fs_info, disk_bytenr); 4482 disk_bytenr += extent_offset; 4483 ret = btrfs_lookup_csums_list(csum_root, disk_bytenr, 4484 disk_bytenr + extent_num_bytes - 1, 4485 &ordered_sums, false); 4486 if (ret < 0) 4487 goto out; 4488 ret = 0; 4489 4490 list_for_each_entry_safe(sums, sums_next, &ordered_sums, list) { 4491 if (!ret) 4492 ret = log_csums(trans, inode, log, sums); 4493 list_del(&sums->list); 4494 kfree(sums); 4495 } 4496 if (ret) 4497 goto out; 4498 4499 add_to_batch: 4500 ins_sizes[dst_index] = btrfs_item_size(src, src_slot); 4501 batch.total_data_size += ins_sizes[dst_index]; 4502 batch.nr++; 4503 dst_index++; 4504 } 4505 4506 /* 4507 * We have a leaf full of old extent items that don't need to be logged, 4508 * so we don't need to do anything. 4509 */ 4510 if (batch.nr == 0) 4511 goto out; 4512 4513 ret = btrfs_insert_empty_items(trans, log, dst_path, &batch); 4514 if (ret) 4515 goto out; 4516 4517 dst_index = 0; 4518 for (int i = 0; i < nr; i++) { 4519 const int src_slot = start_slot + i; 4520 const int dst_slot = dst_path->slots[0] + dst_index; 4521 struct btrfs_key key; 4522 unsigned long src_offset; 4523 unsigned long dst_offset; 4524 4525 /* 4526 * We're done, all the remaining items in the source leaf 4527 * correspond to old file extent items. 4528 */ 4529 if (dst_index >= batch.nr) 4530 break; 4531 4532 btrfs_item_key_to_cpu(src, &key, src_slot); 4533 4534 if (key.type != BTRFS_EXTENT_DATA_KEY) 4535 goto copy_item; 4536 4537 extent = btrfs_item_ptr(src, src_slot, 4538 struct btrfs_file_extent_item); 4539 4540 /* See the comment in the previous loop, same logic. */ 4541 if (btrfs_file_extent_generation(src, extent) < trans->transid && 4542 key.offset < i_size && 4543 inode->last_reflink_trans < trans->transid) 4544 continue; 4545 4546 copy_item: 4547 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], dst_slot); 4548 src_offset = btrfs_item_ptr_offset(src, src_slot); 4549 4550 if (key.type == BTRFS_INODE_ITEM_KEY) { 4551 struct btrfs_inode_item *inode_item; 4552 4553 inode_item = btrfs_item_ptr(dst_path->nodes[0], dst_slot, 4554 struct btrfs_inode_item); 4555 fill_inode_item(trans, dst_path->nodes[0], inode_item, 4556 &inode->vfs_inode, 4557 inode_only == LOG_INODE_EXISTS, 4558 logged_isize); 4559 } else { 4560 copy_extent_buffer(dst_path->nodes[0], src, dst_offset, 4561 src_offset, ins_sizes[dst_index]); 4562 } 4563 4564 dst_index++; 4565 } 4566 4567 btrfs_release_path(dst_path); 4568 out: 4569 kfree(ins_data); 4570 4571 return ret; 4572 } 4573 4574 static int extent_cmp(void *priv, const struct list_head *a, 4575 const struct list_head *b) 4576 { 4577 const struct extent_map *em1, *em2; 4578 4579 em1 = list_entry(a, struct extent_map, list); 4580 em2 = list_entry(b, struct extent_map, list); 4581 4582 if (em1->start < em2->start) 4583 return -1; 4584 else if (em1->start > em2->start) 4585 return 1; 4586 return 0; 4587 } 4588 4589 static int log_extent_csums(struct btrfs_trans_handle *trans, 4590 struct btrfs_inode *inode, 4591 struct btrfs_root *log_root, 4592 const struct extent_map *em, 4593 struct btrfs_log_ctx *ctx) 4594 { 4595 struct btrfs_ordered_extent *ordered; 4596 struct btrfs_root *csum_root; 4597 u64 block_start; 4598 u64 csum_offset; 4599 u64 csum_len; 4600 u64 mod_start = em->start; 4601 u64 mod_len = em->len; 4602 LIST_HEAD(ordered_sums); 4603 int ret = 0; 4604 4605 if (inode->flags & BTRFS_INODE_NODATASUM || 4606 (em->flags & EXTENT_FLAG_PREALLOC) || 4607 em->disk_bytenr == EXTENT_MAP_HOLE) 4608 return 0; 4609 4610 list_for_each_entry(ordered, &ctx->ordered_extents, log_list) { 4611 const u64 ordered_end = ordered->file_offset + ordered->num_bytes; 4612 const u64 mod_end = mod_start + mod_len; 4613 struct btrfs_ordered_sum *sums; 4614 4615 if (mod_len == 0) 4616 break; 4617 4618 if (ordered_end <= mod_start) 4619 continue; 4620 if (mod_end <= ordered->file_offset) 4621 break; 4622 4623 /* 4624 * We are going to copy all the csums on this ordered extent, so 4625 * go ahead and adjust mod_start and mod_len in case this ordered 4626 * extent has already been logged. 4627 */ 4628 if (ordered->file_offset > mod_start) { 4629 if (ordered_end >= mod_end) 4630 mod_len = ordered->file_offset - mod_start; 4631 /* 4632 * If we have this case 4633 * 4634 * |--------- logged extent ---------| 4635 * |----- ordered extent ----| 4636 * 4637 * Just don't mess with mod_start and mod_len, we'll 4638 * just end up logging more csums than we need and it 4639 * will be ok. 4640 */ 4641 } else { 4642 if (ordered_end < mod_end) { 4643 mod_len = mod_end - ordered_end; 4644 mod_start = ordered_end; 4645 } else { 4646 mod_len = 0; 4647 } 4648 } 4649 4650 /* 4651 * To keep us from looping for the above case of an ordered 4652 * extent that falls inside of the logged extent. 4653 */ 4654 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags)) 4655 continue; 4656 4657 list_for_each_entry(sums, &ordered->list, list) { 4658 ret = log_csums(trans, inode, log_root, sums); 4659 if (ret) 4660 return ret; 4661 } 4662 } 4663 4664 /* We're done, found all csums in the ordered extents. */ 4665 if (mod_len == 0) 4666 return 0; 4667 4668 /* If we're compressed we have to save the entire range of csums. */ 4669 if (extent_map_is_compressed(em)) { 4670 csum_offset = 0; 4671 csum_len = em->disk_num_bytes; 4672 } else { 4673 csum_offset = mod_start - em->start; 4674 csum_len = mod_len; 4675 } 4676 4677 /* block start is already adjusted for the file extent offset. */ 4678 block_start = extent_map_block_start(em); 4679 csum_root = btrfs_csum_root(trans->fs_info, block_start); 4680 ret = btrfs_lookup_csums_list(csum_root, block_start + csum_offset, 4681 block_start + csum_offset + csum_len - 1, 4682 &ordered_sums, false); 4683 if (ret < 0) 4684 return ret; 4685 ret = 0; 4686 4687 while (!list_empty(&ordered_sums)) { 4688 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, 4689 struct btrfs_ordered_sum, 4690 list); 4691 if (!ret) 4692 ret = log_csums(trans, inode, log_root, sums); 4693 list_del(&sums->list); 4694 kfree(sums); 4695 } 4696 4697 return ret; 4698 } 4699 4700 static int log_one_extent(struct btrfs_trans_handle *trans, 4701 struct btrfs_inode *inode, 4702 const struct extent_map *em, 4703 struct btrfs_path *path, 4704 struct btrfs_log_ctx *ctx) 4705 { 4706 struct btrfs_drop_extents_args drop_args = { 0 }; 4707 struct btrfs_root *log = inode->root->log_root; 4708 struct btrfs_file_extent_item fi = { 0 }; 4709 struct extent_buffer *leaf; 4710 struct btrfs_key key; 4711 enum btrfs_compression_type compress_type; 4712 u64 extent_offset = em->offset; 4713 u64 block_start = extent_map_block_start(em); 4714 u64 block_len; 4715 int ret; 4716 4717 btrfs_set_stack_file_extent_generation(&fi, trans->transid); 4718 if (em->flags & EXTENT_FLAG_PREALLOC) 4719 btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_PREALLOC); 4720 else 4721 btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_REG); 4722 4723 block_len = em->disk_num_bytes; 4724 compress_type = extent_map_compression(em); 4725 if (compress_type != BTRFS_COMPRESS_NONE) { 4726 btrfs_set_stack_file_extent_disk_bytenr(&fi, block_start); 4727 btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len); 4728 } else if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE) { 4729 btrfs_set_stack_file_extent_disk_bytenr(&fi, block_start - extent_offset); 4730 btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len); 4731 } 4732 4733 btrfs_set_stack_file_extent_offset(&fi, extent_offset); 4734 btrfs_set_stack_file_extent_num_bytes(&fi, em->len); 4735 btrfs_set_stack_file_extent_ram_bytes(&fi, em->ram_bytes); 4736 btrfs_set_stack_file_extent_compression(&fi, compress_type); 4737 4738 ret = log_extent_csums(trans, inode, log, em, ctx); 4739 if (ret) 4740 return ret; 4741 4742 /* 4743 * If this is the first time we are logging the inode in the current 4744 * transaction, we can avoid btrfs_drop_extents(), which is expensive 4745 * because it does a deletion search, which always acquires write locks 4746 * for extent buffers at levels 2, 1 and 0. This not only wastes time 4747 * but also adds significant contention in a log tree, since log trees 4748 * are small, with a root at level 2 or 3 at most, due to their short 4749 * life span. 4750 */ 4751 if (ctx->logged_before) { 4752 drop_args.path = path; 4753 drop_args.start = em->start; 4754 drop_args.end = em->start + em->len; 4755 drop_args.replace_extent = true; 4756 drop_args.extent_item_size = sizeof(fi); 4757 ret = btrfs_drop_extents(trans, log, inode, &drop_args); 4758 if (ret) 4759 return ret; 4760 } 4761 4762 if (!drop_args.extent_inserted) { 4763 key.objectid = btrfs_ino(inode); 4764 key.type = BTRFS_EXTENT_DATA_KEY; 4765 key.offset = em->start; 4766 4767 ret = btrfs_insert_empty_item(trans, log, path, &key, 4768 sizeof(fi)); 4769 if (ret) 4770 return ret; 4771 } 4772 leaf = path->nodes[0]; 4773 write_extent_buffer(leaf, &fi, 4774 btrfs_item_ptr_offset(leaf, path->slots[0]), 4775 sizeof(fi)); 4776 4777 btrfs_release_path(path); 4778 4779 return ret; 4780 } 4781 4782 /* 4783 * Log all prealloc extents beyond the inode's i_size to make sure we do not 4784 * lose them after doing a full/fast fsync and replaying the log. We scan the 4785 * subvolume's root instead of iterating the inode's extent map tree because 4786 * otherwise we can log incorrect extent items based on extent map conversion. 4787 * That can happen due to the fact that extent maps are merged when they 4788 * are not in the extent map tree's list of modified extents. 4789 */ 4790 static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans, 4791 struct btrfs_inode *inode, 4792 struct btrfs_path *path, 4793 struct btrfs_log_ctx *ctx) 4794 { 4795 struct btrfs_root *root = inode->root; 4796 struct btrfs_key key; 4797 const u64 i_size = i_size_read(&inode->vfs_inode); 4798 const u64 ino = btrfs_ino(inode); 4799 struct btrfs_path *dst_path = NULL; 4800 bool dropped_extents = false; 4801 u64 truncate_offset = i_size; 4802 struct extent_buffer *leaf; 4803 int slot; 4804 int ins_nr = 0; 4805 int start_slot = 0; 4806 int ret; 4807 4808 if (!(inode->flags & BTRFS_INODE_PREALLOC)) 4809 return 0; 4810 4811 key.objectid = ino; 4812 key.type = BTRFS_EXTENT_DATA_KEY; 4813 key.offset = i_size; 4814 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 4815 if (ret < 0) 4816 goto out; 4817 4818 /* 4819 * We must check if there is a prealloc extent that starts before the 4820 * i_size and crosses the i_size boundary. This is to ensure later we 4821 * truncate down to the end of that extent and not to the i_size, as 4822 * otherwise we end up losing part of the prealloc extent after a log 4823 * replay and with an implicit hole if there is another prealloc extent 4824 * that starts at an offset beyond i_size. 4825 */ 4826 ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY); 4827 if (ret < 0) 4828 goto out; 4829 4830 if (ret == 0) { 4831 struct btrfs_file_extent_item *ei; 4832 4833 leaf = path->nodes[0]; 4834 slot = path->slots[0]; 4835 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 4836 4837 if (btrfs_file_extent_type(leaf, ei) == 4838 BTRFS_FILE_EXTENT_PREALLOC) { 4839 u64 extent_end; 4840 4841 btrfs_item_key_to_cpu(leaf, &key, slot); 4842 extent_end = key.offset + 4843 btrfs_file_extent_num_bytes(leaf, ei); 4844 4845 if (extent_end > i_size) 4846 truncate_offset = extent_end; 4847 } 4848 } else { 4849 ret = 0; 4850 } 4851 4852 while (true) { 4853 leaf = path->nodes[0]; 4854 slot = path->slots[0]; 4855 4856 if (slot >= btrfs_header_nritems(leaf)) { 4857 if (ins_nr > 0) { 4858 ret = copy_items(trans, inode, dst_path, path, 4859 start_slot, ins_nr, 1, 0, ctx); 4860 if (ret < 0) 4861 goto out; 4862 ins_nr = 0; 4863 } 4864 ret = btrfs_next_leaf(root, path); 4865 if (ret < 0) 4866 goto out; 4867 if (ret > 0) { 4868 ret = 0; 4869 break; 4870 } 4871 continue; 4872 } 4873 4874 btrfs_item_key_to_cpu(leaf, &key, slot); 4875 if (key.objectid > ino) 4876 break; 4877 if (WARN_ON_ONCE(key.objectid < ino) || 4878 key.type < BTRFS_EXTENT_DATA_KEY || 4879 key.offset < i_size) { 4880 path->slots[0]++; 4881 continue; 4882 } 4883 /* 4884 * Avoid overlapping items in the log tree. The first time we 4885 * get here, get rid of everything from a past fsync. After 4886 * that, if the current extent starts before the end of the last 4887 * extent we copied, truncate the last one. This can happen if 4888 * an ordered extent completion modifies the subvolume tree 4889 * while btrfs_next_leaf() has the tree unlocked. 4890 */ 4891 if (!dropped_extents || key.offset < truncate_offset) { 4892 ret = truncate_inode_items(trans, root->log_root, inode, 4893 min(key.offset, truncate_offset), 4894 BTRFS_EXTENT_DATA_KEY); 4895 if (ret) 4896 goto out; 4897 dropped_extents = true; 4898 } 4899 truncate_offset = btrfs_file_extent_end(path); 4900 if (ins_nr == 0) 4901 start_slot = slot; 4902 ins_nr++; 4903 path->slots[0]++; 4904 if (!dst_path) { 4905 dst_path = btrfs_alloc_path(); 4906 if (!dst_path) { 4907 ret = -ENOMEM; 4908 goto out; 4909 } 4910 } 4911 } 4912 if (ins_nr > 0) 4913 ret = copy_items(trans, inode, dst_path, path, 4914 start_slot, ins_nr, 1, 0, ctx); 4915 out: 4916 btrfs_release_path(path); 4917 btrfs_free_path(dst_path); 4918 return ret; 4919 } 4920 4921 static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, 4922 struct btrfs_inode *inode, 4923 struct btrfs_path *path, 4924 struct btrfs_log_ctx *ctx) 4925 { 4926 struct btrfs_ordered_extent *ordered; 4927 struct btrfs_ordered_extent *tmp; 4928 struct extent_map *em, *n; 4929 LIST_HEAD(extents); 4930 struct extent_map_tree *tree = &inode->extent_tree; 4931 int ret = 0; 4932 int num = 0; 4933 4934 write_lock(&tree->lock); 4935 4936 list_for_each_entry_safe(em, n, &tree->modified_extents, list) { 4937 list_del_init(&em->list); 4938 /* 4939 * Just an arbitrary number, this can be really CPU intensive 4940 * once we start getting a lot of extents, and really once we 4941 * have a bunch of extents we just want to commit since it will 4942 * be faster. 4943 */ 4944 if (++num > 32768) { 4945 list_del_init(&tree->modified_extents); 4946 ret = -EFBIG; 4947 goto process; 4948 } 4949 4950 if (em->generation < trans->transid) 4951 continue; 4952 4953 /* We log prealloc extents beyond eof later. */ 4954 if ((em->flags & EXTENT_FLAG_PREALLOC) && 4955 em->start >= i_size_read(&inode->vfs_inode)) 4956 continue; 4957 4958 /* Need a ref to keep it from getting evicted from cache */ 4959 refcount_inc(&em->refs); 4960 em->flags |= EXTENT_FLAG_LOGGING; 4961 list_add_tail(&em->list, &extents); 4962 num++; 4963 } 4964 4965 list_sort(NULL, &extents, extent_cmp); 4966 process: 4967 while (!list_empty(&extents)) { 4968 em = list_entry(extents.next, struct extent_map, list); 4969 4970 list_del_init(&em->list); 4971 4972 /* 4973 * If we had an error we just need to delete everybody from our 4974 * private list. 4975 */ 4976 if (ret) { 4977 clear_em_logging(inode, em); 4978 free_extent_map(em); 4979 continue; 4980 } 4981 4982 write_unlock(&tree->lock); 4983 4984 ret = log_one_extent(trans, inode, em, path, ctx); 4985 write_lock(&tree->lock); 4986 clear_em_logging(inode, em); 4987 free_extent_map(em); 4988 } 4989 WARN_ON(!list_empty(&extents)); 4990 write_unlock(&tree->lock); 4991 4992 if (!ret) 4993 ret = btrfs_log_prealloc_extents(trans, inode, path, ctx); 4994 if (ret) 4995 return ret; 4996 4997 /* 4998 * We have logged all extents successfully, now make sure the commit of 4999 * the current transaction waits for the ordered extents to complete 5000 * before it commits and wipes out the log trees, otherwise we would 5001 * lose data if an ordered extents completes after the transaction 5002 * commits and a power failure happens after the transaction commit. 5003 */ 5004 list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) { 5005 list_del_init(&ordered->log_list); 5006 set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags); 5007 5008 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) { 5009 spin_lock_irq(&inode->ordered_tree_lock); 5010 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) { 5011 set_bit(BTRFS_ORDERED_PENDING, &ordered->flags); 5012 atomic_inc(&trans->transaction->pending_ordered); 5013 } 5014 spin_unlock_irq(&inode->ordered_tree_lock); 5015 } 5016 btrfs_put_ordered_extent(ordered); 5017 } 5018 5019 return 0; 5020 } 5021 5022 static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode, 5023 struct btrfs_path *path, u64 *size_ret) 5024 { 5025 struct btrfs_key key; 5026 int ret; 5027 5028 key.objectid = btrfs_ino(inode); 5029 key.type = BTRFS_INODE_ITEM_KEY; 5030 key.offset = 0; 5031 5032 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0); 5033 if (ret < 0) { 5034 return ret; 5035 } else if (ret > 0) { 5036 *size_ret = 0; 5037 } else { 5038 struct btrfs_inode_item *item; 5039 5040 item = btrfs_item_ptr(path->nodes[0], path->slots[0], 5041 struct btrfs_inode_item); 5042 *size_ret = btrfs_inode_size(path->nodes[0], item); 5043 /* 5044 * If the in-memory inode's i_size is smaller then the inode 5045 * size stored in the btree, return the inode's i_size, so 5046 * that we get a correct inode size after replaying the log 5047 * when before a power failure we had a shrinking truncate 5048 * followed by addition of a new name (rename / new hard link). 5049 * Otherwise return the inode size from the btree, to avoid 5050 * data loss when replaying a log due to previously doing a 5051 * write that expands the inode's size and logging a new name 5052 * immediately after. 5053 */ 5054 if (*size_ret > inode->vfs_inode.i_size) 5055 *size_ret = inode->vfs_inode.i_size; 5056 } 5057 5058 btrfs_release_path(path); 5059 return 0; 5060 } 5061 5062 /* 5063 * At the moment we always log all xattrs. This is to figure out at log replay 5064 * time which xattrs must have their deletion replayed. If a xattr is missing 5065 * in the log tree and exists in the fs/subvol tree, we delete it. This is 5066 * because if a xattr is deleted, the inode is fsynced and a power failure 5067 * happens, causing the log to be replayed the next time the fs is mounted, 5068 * we want the xattr to not exist anymore (same behaviour as other filesystems 5069 * with a journal, ext3/4, xfs, f2fs, etc). 5070 */ 5071 static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans, 5072 struct btrfs_inode *inode, 5073 struct btrfs_path *path, 5074 struct btrfs_path *dst_path, 5075 struct btrfs_log_ctx *ctx) 5076 { 5077 struct btrfs_root *root = inode->root; 5078 int ret; 5079 struct btrfs_key key; 5080 const u64 ino = btrfs_ino(inode); 5081 int ins_nr = 0; 5082 int start_slot = 0; 5083 bool found_xattrs = false; 5084 5085 if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags)) 5086 return 0; 5087 5088 key.objectid = ino; 5089 key.type = BTRFS_XATTR_ITEM_KEY; 5090 key.offset = 0; 5091 5092 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 5093 if (ret < 0) 5094 return ret; 5095 5096 while (true) { 5097 int slot = path->slots[0]; 5098 struct extent_buffer *leaf = path->nodes[0]; 5099 int nritems = btrfs_header_nritems(leaf); 5100 5101 if (slot >= nritems) { 5102 if (ins_nr > 0) { 5103 ret = copy_items(trans, inode, dst_path, path, 5104 start_slot, ins_nr, 1, 0, ctx); 5105 if (ret < 0) 5106 return ret; 5107 ins_nr = 0; 5108 } 5109 ret = btrfs_next_leaf(root, path); 5110 if (ret < 0) 5111 return ret; 5112 else if (ret > 0) 5113 break; 5114 continue; 5115 } 5116 5117 btrfs_item_key_to_cpu(leaf, &key, slot); 5118 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) 5119 break; 5120 5121 if (ins_nr == 0) 5122 start_slot = slot; 5123 ins_nr++; 5124 path->slots[0]++; 5125 found_xattrs = true; 5126 cond_resched(); 5127 } 5128 if (ins_nr > 0) { 5129 ret = copy_items(trans, inode, dst_path, path, 5130 start_slot, ins_nr, 1, 0, ctx); 5131 if (ret < 0) 5132 return ret; 5133 } 5134 5135 if (!found_xattrs) 5136 set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags); 5137 5138 return 0; 5139 } 5140 5141 /* 5142 * When using the NO_HOLES feature if we punched a hole that causes the 5143 * deletion of entire leafs or all the extent items of the first leaf (the one 5144 * that contains the inode item and references) we may end up not processing 5145 * any extents, because there are no leafs with a generation matching the 5146 * current transaction that have extent items for our inode. So we need to find 5147 * if any holes exist and then log them. We also need to log holes after any 5148 * truncate operation that changes the inode's size. 5149 */ 5150 static int btrfs_log_holes(struct btrfs_trans_handle *trans, 5151 struct btrfs_inode *inode, 5152 struct btrfs_path *path) 5153 { 5154 struct btrfs_root *root = inode->root; 5155 struct btrfs_fs_info *fs_info = root->fs_info; 5156 struct btrfs_key key; 5157 const u64 ino = btrfs_ino(inode); 5158 const u64 i_size = i_size_read(&inode->vfs_inode); 5159 u64 prev_extent_end = 0; 5160 int ret; 5161 5162 if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0) 5163 return 0; 5164 5165 key.objectid = ino; 5166 key.type = BTRFS_EXTENT_DATA_KEY; 5167 key.offset = 0; 5168 5169 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 5170 if (ret < 0) 5171 return ret; 5172 5173 while (true) { 5174 struct extent_buffer *leaf = path->nodes[0]; 5175 5176 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 5177 ret = btrfs_next_leaf(root, path); 5178 if (ret < 0) 5179 return ret; 5180 if (ret > 0) { 5181 ret = 0; 5182 break; 5183 } 5184 leaf = path->nodes[0]; 5185 } 5186 5187 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 5188 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) 5189 break; 5190 5191 /* We have a hole, log it. */ 5192 if (prev_extent_end < key.offset) { 5193 const u64 hole_len = key.offset - prev_extent_end; 5194 5195 /* 5196 * Release the path to avoid deadlocks with other code 5197 * paths that search the root while holding locks on 5198 * leafs from the log root. 5199 */ 5200 btrfs_release_path(path); 5201 ret = btrfs_insert_hole_extent(trans, root->log_root, 5202 ino, prev_extent_end, 5203 hole_len); 5204 if (ret < 0) 5205 return ret; 5206 5207 /* 5208 * Search for the same key again in the root. Since it's 5209 * an extent item and we are holding the inode lock, the 5210 * key must still exist. If it doesn't just emit warning 5211 * and return an error to fall back to a transaction 5212 * commit. 5213 */ 5214 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 5215 if (ret < 0) 5216 return ret; 5217 if (WARN_ON(ret > 0)) 5218 return -ENOENT; 5219 leaf = path->nodes[0]; 5220 } 5221 5222 prev_extent_end = btrfs_file_extent_end(path); 5223 path->slots[0]++; 5224 cond_resched(); 5225 } 5226 5227 if (prev_extent_end < i_size) { 5228 u64 hole_len; 5229 5230 btrfs_release_path(path); 5231 hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize); 5232 ret = btrfs_insert_hole_extent(trans, root->log_root, ino, 5233 prev_extent_end, hole_len); 5234 if (ret < 0) 5235 return ret; 5236 } 5237 5238 return 0; 5239 } 5240 5241 /* 5242 * When we are logging a new inode X, check if it doesn't have a reference that 5243 * matches the reference from some other inode Y created in a past transaction 5244 * and that was renamed in the current transaction. If we don't do this, then at 5245 * log replay time we can lose inode Y (and all its files if it's a directory): 5246 * 5247 * mkdir /mnt/x 5248 * echo "hello world" > /mnt/x/foobar 5249 * sync 5250 * mv /mnt/x /mnt/y 5251 * mkdir /mnt/x # or touch /mnt/x 5252 * xfs_io -c fsync /mnt/x 5253 * <power fail> 5254 * mount fs, trigger log replay 5255 * 5256 * After the log replay procedure, we would lose the first directory and all its 5257 * files (file foobar). 5258 * For the case where inode Y is not a directory we simply end up losing it: 5259 * 5260 * echo "123" > /mnt/foo 5261 * sync 5262 * mv /mnt/foo /mnt/bar 5263 * echo "abc" > /mnt/foo 5264 * xfs_io -c fsync /mnt/foo 5265 * <power fail> 5266 * 5267 * We also need this for cases where a snapshot entry is replaced by some other 5268 * entry (file or directory) otherwise we end up with an unreplayable log due to 5269 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as 5270 * if it were a regular entry: 5271 * 5272 * mkdir /mnt/x 5273 * btrfs subvolume snapshot /mnt /mnt/x/snap 5274 * btrfs subvolume delete /mnt/x/snap 5275 * rmdir /mnt/x 5276 * mkdir /mnt/x 5277 * fsync /mnt/x or fsync some new file inside it 5278 * <power fail> 5279 * 5280 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in 5281 * the same transaction. 5282 */ 5283 static int btrfs_check_ref_name_override(struct extent_buffer *eb, 5284 const int slot, 5285 const struct btrfs_key *key, 5286 struct btrfs_inode *inode, 5287 u64 *other_ino, u64 *other_parent) 5288 { 5289 int ret; 5290 struct btrfs_path *search_path; 5291 char *name = NULL; 5292 u32 name_len = 0; 5293 u32 item_size = btrfs_item_size(eb, slot); 5294 u32 cur_offset = 0; 5295 unsigned long ptr = btrfs_item_ptr_offset(eb, slot); 5296 5297 search_path = btrfs_alloc_path(); 5298 if (!search_path) 5299 return -ENOMEM; 5300 search_path->search_commit_root = 1; 5301 search_path->skip_locking = 1; 5302 5303 while (cur_offset < item_size) { 5304 u64 parent; 5305 u32 this_name_len; 5306 u32 this_len; 5307 unsigned long name_ptr; 5308 struct btrfs_dir_item *di; 5309 struct fscrypt_str name_str; 5310 5311 if (key->type == BTRFS_INODE_REF_KEY) { 5312 struct btrfs_inode_ref *iref; 5313 5314 iref = (struct btrfs_inode_ref *)(ptr + cur_offset); 5315 parent = key->offset; 5316 this_name_len = btrfs_inode_ref_name_len(eb, iref); 5317 name_ptr = (unsigned long)(iref + 1); 5318 this_len = sizeof(*iref) + this_name_len; 5319 } else { 5320 struct btrfs_inode_extref *extref; 5321 5322 extref = (struct btrfs_inode_extref *)(ptr + 5323 cur_offset); 5324 parent = btrfs_inode_extref_parent(eb, extref); 5325 this_name_len = btrfs_inode_extref_name_len(eb, extref); 5326 name_ptr = (unsigned long)&extref->name; 5327 this_len = sizeof(*extref) + this_name_len; 5328 } 5329 5330 if (this_name_len > name_len) { 5331 char *new_name; 5332 5333 new_name = krealloc(name, this_name_len, GFP_NOFS); 5334 if (!new_name) { 5335 ret = -ENOMEM; 5336 goto out; 5337 } 5338 name_len = this_name_len; 5339 name = new_name; 5340 } 5341 5342 read_extent_buffer(eb, name, name_ptr, this_name_len); 5343 5344 name_str.name = name; 5345 name_str.len = this_name_len; 5346 di = btrfs_lookup_dir_item(NULL, inode->root, search_path, 5347 parent, &name_str, 0); 5348 if (di && !IS_ERR(di)) { 5349 struct btrfs_key di_key; 5350 5351 btrfs_dir_item_key_to_cpu(search_path->nodes[0], 5352 di, &di_key); 5353 if (di_key.type == BTRFS_INODE_ITEM_KEY) { 5354 if (di_key.objectid != key->objectid) { 5355 ret = 1; 5356 *other_ino = di_key.objectid; 5357 *other_parent = parent; 5358 } else { 5359 ret = 0; 5360 } 5361 } else { 5362 ret = -EAGAIN; 5363 } 5364 goto out; 5365 } else if (IS_ERR(di)) { 5366 ret = PTR_ERR(di); 5367 goto out; 5368 } 5369 btrfs_release_path(search_path); 5370 5371 cur_offset += this_len; 5372 } 5373 ret = 0; 5374 out: 5375 btrfs_free_path(search_path); 5376 kfree(name); 5377 return ret; 5378 } 5379 5380 /* 5381 * Check if we need to log an inode. This is used in contexts where while 5382 * logging an inode we need to log another inode (either that it exists or in 5383 * full mode). This is used instead of btrfs_inode_in_log() because the later 5384 * requires the inode to be in the log and have the log transaction committed, 5385 * while here we do not care if the log transaction was already committed - our 5386 * caller will commit the log later - and we want to avoid logging an inode 5387 * multiple times when multiple tasks have joined the same log transaction. 5388 */ 5389 static bool need_log_inode(const struct btrfs_trans_handle *trans, 5390 struct btrfs_inode *inode) 5391 { 5392 /* 5393 * If a directory was not modified, no dentries added or removed, we can 5394 * and should avoid logging it. 5395 */ 5396 if (S_ISDIR(inode->vfs_inode.i_mode) && inode->last_trans < trans->transid) 5397 return false; 5398 5399 /* 5400 * If this inode does not have new/updated/deleted xattrs since the last 5401 * time it was logged and is flagged as logged in the current transaction, 5402 * we can skip logging it. As for new/deleted names, those are updated in 5403 * the log by link/unlink/rename operations. 5404 * In case the inode was logged and then evicted and reloaded, its 5405 * logged_trans will be 0, in which case we have to fully log it since 5406 * logged_trans is a transient field, not persisted. 5407 */ 5408 if (inode_logged(trans, inode, NULL) == 1 && 5409 !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags)) 5410 return false; 5411 5412 return true; 5413 } 5414 5415 struct btrfs_dir_list { 5416 u64 ino; 5417 struct list_head list; 5418 }; 5419 5420 /* 5421 * Log the inodes of the new dentries of a directory. 5422 * See process_dir_items_leaf() for details about why it is needed. 5423 * This is a recursive operation - if an existing dentry corresponds to a 5424 * directory, that directory's new entries are logged too (same behaviour as 5425 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes 5426 * the dentries point to we do not acquire their VFS lock, otherwise lockdep 5427 * complains about the following circular lock dependency / possible deadlock: 5428 * 5429 * CPU0 CPU1 5430 * ---- ---- 5431 * lock(&type->i_mutex_dir_key#3/2); 5432 * lock(sb_internal#2); 5433 * lock(&type->i_mutex_dir_key#3/2); 5434 * lock(&sb->s_type->i_mutex_key#14); 5435 * 5436 * Where sb_internal is the lock (a counter that works as a lock) acquired by 5437 * sb_start_intwrite() in btrfs_start_transaction(). 5438 * Not acquiring the VFS lock of the inodes is still safe because: 5439 * 5440 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible 5441 * that while logging the inode new references (names) are added or removed 5442 * from the inode, leaving the logged inode item with a link count that does 5443 * not match the number of logged inode reference items. This is fine because 5444 * at log replay time we compute the real number of links and correct the 5445 * link count in the inode item (see replay_one_buffer() and 5446 * link_to_fixup_dir()); 5447 * 5448 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that 5449 * while logging the inode's items new index items (key type 5450 * BTRFS_DIR_INDEX_KEY) are added to fs/subvol tree and the logged inode item 5451 * has a size that doesn't match the sum of the lengths of all the logged 5452 * names - this is ok, not a problem, because at log replay time we set the 5453 * directory's i_size to the correct value (see replay_one_name() and 5454 * overwrite_item()). 5455 */ 5456 static int log_new_dir_dentries(struct btrfs_trans_handle *trans, 5457 struct btrfs_inode *start_inode, 5458 struct btrfs_log_ctx *ctx) 5459 { 5460 struct btrfs_root *root = start_inode->root; 5461 struct btrfs_path *path; 5462 LIST_HEAD(dir_list); 5463 struct btrfs_dir_list *dir_elem; 5464 u64 ino = btrfs_ino(start_inode); 5465 struct btrfs_inode *curr_inode = start_inode; 5466 int ret = 0; 5467 5468 /* 5469 * If we are logging a new name, as part of a link or rename operation, 5470 * don't bother logging new dentries, as we just want to log the names 5471 * of an inode and that any new parents exist. 5472 */ 5473 if (ctx->logging_new_name) 5474 return 0; 5475 5476 path = btrfs_alloc_path(); 5477 if (!path) 5478 return -ENOMEM; 5479 5480 /* Pairs with btrfs_add_delayed_iput below. */ 5481 ihold(&curr_inode->vfs_inode); 5482 5483 while (true) { 5484 struct inode *vfs_inode; 5485 struct btrfs_key key; 5486 struct btrfs_key found_key; 5487 u64 next_index; 5488 bool continue_curr_inode = true; 5489 int iter_ret; 5490 5491 key.objectid = ino; 5492 key.type = BTRFS_DIR_INDEX_KEY; 5493 key.offset = btrfs_get_first_dir_index_to_log(curr_inode); 5494 next_index = key.offset; 5495 again: 5496 btrfs_for_each_slot(root->log_root, &key, &found_key, path, iter_ret) { 5497 struct extent_buffer *leaf = path->nodes[0]; 5498 struct btrfs_dir_item *di; 5499 struct btrfs_key di_key; 5500 struct inode *di_inode; 5501 int log_mode = LOG_INODE_EXISTS; 5502 int type; 5503 5504 if (found_key.objectid != ino || 5505 found_key.type != BTRFS_DIR_INDEX_KEY) { 5506 continue_curr_inode = false; 5507 break; 5508 } 5509 5510 next_index = found_key.offset + 1; 5511 5512 di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); 5513 type = btrfs_dir_ftype(leaf, di); 5514 if (btrfs_dir_transid(leaf, di) < trans->transid) 5515 continue; 5516 btrfs_dir_item_key_to_cpu(leaf, di, &di_key); 5517 if (di_key.type == BTRFS_ROOT_ITEM_KEY) 5518 continue; 5519 5520 btrfs_release_path(path); 5521 di_inode = btrfs_iget_logging(di_key.objectid, root); 5522 if (IS_ERR(di_inode)) { 5523 ret = PTR_ERR(di_inode); 5524 goto out; 5525 } 5526 5527 if (!need_log_inode(trans, BTRFS_I(di_inode))) { 5528 btrfs_add_delayed_iput(BTRFS_I(di_inode)); 5529 break; 5530 } 5531 5532 ctx->log_new_dentries = false; 5533 if (type == BTRFS_FT_DIR) 5534 log_mode = LOG_INODE_ALL; 5535 ret = btrfs_log_inode(trans, BTRFS_I(di_inode), 5536 log_mode, ctx); 5537 btrfs_add_delayed_iput(BTRFS_I(di_inode)); 5538 if (ret) 5539 goto out; 5540 if (ctx->log_new_dentries) { 5541 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS); 5542 if (!dir_elem) { 5543 ret = -ENOMEM; 5544 goto out; 5545 } 5546 dir_elem->ino = di_key.objectid; 5547 list_add_tail(&dir_elem->list, &dir_list); 5548 } 5549 break; 5550 } 5551 5552 btrfs_release_path(path); 5553 5554 if (iter_ret < 0) { 5555 ret = iter_ret; 5556 goto out; 5557 } else if (iter_ret > 0) { 5558 continue_curr_inode = false; 5559 } else { 5560 key = found_key; 5561 } 5562 5563 if (continue_curr_inode && key.offset < (u64)-1) { 5564 key.offset++; 5565 goto again; 5566 } 5567 5568 btrfs_set_first_dir_index_to_log(curr_inode, next_index); 5569 5570 if (list_empty(&dir_list)) 5571 break; 5572 5573 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, list); 5574 ino = dir_elem->ino; 5575 list_del(&dir_elem->list); 5576 kfree(dir_elem); 5577 5578 btrfs_add_delayed_iput(curr_inode); 5579 curr_inode = NULL; 5580 5581 vfs_inode = btrfs_iget_logging(ino, root); 5582 if (IS_ERR(vfs_inode)) { 5583 ret = PTR_ERR(vfs_inode); 5584 break; 5585 } 5586 curr_inode = BTRFS_I(vfs_inode); 5587 } 5588 out: 5589 btrfs_free_path(path); 5590 if (curr_inode) 5591 btrfs_add_delayed_iput(curr_inode); 5592 5593 if (ret) { 5594 struct btrfs_dir_list *next; 5595 5596 list_for_each_entry_safe(dir_elem, next, &dir_list, list) 5597 kfree(dir_elem); 5598 } 5599 5600 return ret; 5601 } 5602 5603 struct btrfs_ino_list { 5604 u64 ino; 5605 u64 parent; 5606 struct list_head list; 5607 }; 5608 5609 static void free_conflicting_inodes(struct btrfs_log_ctx *ctx) 5610 { 5611 struct btrfs_ino_list *curr; 5612 struct btrfs_ino_list *next; 5613 5614 list_for_each_entry_safe(curr, next, &ctx->conflict_inodes, list) { 5615 list_del(&curr->list); 5616 kfree(curr); 5617 } 5618 } 5619 5620 static int conflicting_inode_is_dir(struct btrfs_root *root, u64 ino, 5621 struct btrfs_path *path) 5622 { 5623 struct btrfs_key key; 5624 int ret; 5625 5626 key.objectid = ino; 5627 key.type = BTRFS_INODE_ITEM_KEY; 5628 key.offset = 0; 5629 5630 path->search_commit_root = 1; 5631 path->skip_locking = 1; 5632 5633 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 5634 if (WARN_ON_ONCE(ret > 0)) { 5635 /* 5636 * We have previously found the inode through the commit root 5637 * so this should not happen. If it does, just error out and 5638 * fallback to a transaction commit. 5639 */ 5640 ret = -ENOENT; 5641 } else if (ret == 0) { 5642 struct btrfs_inode_item *item; 5643 5644 item = btrfs_item_ptr(path->nodes[0], path->slots[0], 5645 struct btrfs_inode_item); 5646 if (S_ISDIR(btrfs_inode_mode(path->nodes[0], item))) 5647 ret = 1; 5648 } 5649 5650 btrfs_release_path(path); 5651 path->search_commit_root = 0; 5652 path->skip_locking = 0; 5653 5654 return ret; 5655 } 5656 5657 static int add_conflicting_inode(struct btrfs_trans_handle *trans, 5658 struct btrfs_root *root, 5659 struct btrfs_path *path, 5660 u64 ino, u64 parent, 5661 struct btrfs_log_ctx *ctx) 5662 { 5663 struct btrfs_ino_list *ino_elem; 5664 struct inode *inode; 5665 5666 /* 5667 * It's rare to have a lot of conflicting inodes, in practice it is not 5668 * common to have more than 1 or 2. We don't want to collect too many, 5669 * as we could end up logging too many inodes (even if only in 5670 * LOG_INODE_EXISTS mode) and slow down other fsyncs or transaction 5671 * commits. 5672 */ 5673 if (ctx->num_conflict_inodes >= MAX_CONFLICT_INODES) 5674 return BTRFS_LOG_FORCE_COMMIT; 5675 5676 inode = btrfs_iget_logging(ino, root); 5677 /* 5678 * If the other inode that had a conflicting dir entry was deleted in 5679 * the current transaction then we either: 5680 * 5681 * 1) Log the parent directory (later after adding it to the list) if 5682 * the inode is a directory. This is because it may be a deleted 5683 * subvolume/snapshot or it may be a regular directory that had 5684 * deleted subvolumes/snapshots (or subdirectories that had them), 5685 * and at the moment we can't deal with dropping subvolumes/snapshots 5686 * during log replay. So we just log the parent, which will result in 5687 * a fallback to a transaction commit if we are dealing with those 5688 * cases (last_unlink_trans will match the current transaction); 5689 * 5690 * 2) Do nothing if it's not a directory. During log replay we simply 5691 * unlink the conflicting dentry from the parent directory and then 5692 * add the dentry for our inode. Like this we can avoid logging the 5693 * parent directory (and maybe fallback to a transaction commit in 5694 * case it has a last_unlink_trans == trans->transid, due to moving 5695 * some inode from it to some other directory). 5696 */ 5697 if (IS_ERR(inode)) { 5698 int ret = PTR_ERR(inode); 5699 5700 if (ret != -ENOENT) 5701 return ret; 5702 5703 ret = conflicting_inode_is_dir(root, ino, path); 5704 /* Not a directory or we got an error. */ 5705 if (ret <= 0) 5706 return ret; 5707 5708 /* Conflicting inode is a directory, so we'll log its parent. */ 5709 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS); 5710 if (!ino_elem) 5711 return -ENOMEM; 5712 ino_elem->ino = ino; 5713 ino_elem->parent = parent; 5714 list_add_tail(&ino_elem->list, &ctx->conflict_inodes); 5715 ctx->num_conflict_inodes++; 5716 5717 return 0; 5718 } 5719 5720 /* 5721 * If the inode was already logged skip it - otherwise we can hit an 5722 * infinite loop. Example: 5723 * 5724 * From the commit root (previous transaction) we have the following 5725 * inodes: 5726 * 5727 * inode 257 a directory 5728 * inode 258 with references "zz" and "zz_link" on inode 257 5729 * inode 259 with reference "a" on inode 257 5730 * 5731 * And in the current (uncommitted) transaction we have: 5732 * 5733 * inode 257 a directory, unchanged 5734 * inode 258 with references "a" and "a2" on inode 257 5735 * inode 259 with reference "zz_link" on inode 257 5736 * inode 261 with reference "zz" on inode 257 5737 * 5738 * When logging inode 261 the following infinite loop could 5739 * happen if we don't skip already logged inodes: 5740 * 5741 * - we detect inode 258 as a conflicting inode, with inode 261 5742 * on reference "zz", and log it; 5743 * 5744 * - we detect inode 259 as a conflicting inode, with inode 258 5745 * on reference "a", and log it; 5746 * 5747 * - we detect inode 258 as a conflicting inode, with inode 259 5748 * on reference "zz_link", and log it - again! After this we 5749 * repeat the above steps forever. 5750 * 5751 * Here we can use need_log_inode() because we only need to log the 5752 * inode in LOG_INODE_EXISTS mode and rename operations update the log, 5753 * so that the log ends up with the new name and without the old name. 5754 */ 5755 if (!need_log_inode(trans, BTRFS_I(inode))) { 5756 btrfs_add_delayed_iput(BTRFS_I(inode)); 5757 return 0; 5758 } 5759 5760 btrfs_add_delayed_iput(BTRFS_I(inode)); 5761 5762 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS); 5763 if (!ino_elem) 5764 return -ENOMEM; 5765 ino_elem->ino = ino; 5766 ino_elem->parent = parent; 5767 list_add_tail(&ino_elem->list, &ctx->conflict_inodes); 5768 ctx->num_conflict_inodes++; 5769 5770 return 0; 5771 } 5772 5773 static int log_conflicting_inodes(struct btrfs_trans_handle *trans, 5774 struct btrfs_root *root, 5775 struct btrfs_log_ctx *ctx) 5776 { 5777 int ret = 0; 5778 5779 /* 5780 * Conflicting inodes are logged by the first call to btrfs_log_inode(), 5781 * otherwise we could have unbounded recursion of btrfs_log_inode() 5782 * calls. This check guarantees we can have only 1 level of recursion. 5783 */ 5784 if (ctx->logging_conflict_inodes) 5785 return 0; 5786 5787 ctx->logging_conflict_inodes = true; 5788 5789 /* 5790 * New conflicting inodes may be found and added to the list while we 5791 * are logging a conflicting inode, so keep iterating while the list is 5792 * not empty. 5793 */ 5794 while (!list_empty(&ctx->conflict_inodes)) { 5795 struct btrfs_ino_list *curr; 5796 struct inode *inode; 5797 u64 ino; 5798 u64 parent; 5799 5800 curr = list_first_entry(&ctx->conflict_inodes, 5801 struct btrfs_ino_list, list); 5802 ino = curr->ino; 5803 parent = curr->parent; 5804 list_del(&curr->list); 5805 kfree(curr); 5806 5807 inode = btrfs_iget_logging(ino, root); 5808 /* 5809 * If the other inode that had a conflicting dir entry was 5810 * deleted in the current transaction, we need to log its parent 5811 * directory. See the comment at add_conflicting_inode(). 5812 */ 5813 if (IS_ERR(inode)) { 5814 ret = PTR_ERR(inode); 5815 if (ret != -ENOENT) 5816 break; 5817 5818 inode = btrfs_iget_logging(parent, root); 5819 if (IS_ERR(inode)) { 5820 ret = PTR_ERR(inode); 5821 break; 5822 } 5823 5824 /* 5825 * Always log the directory, we cannot make this 5826 * conditional on need_log_inode() because the directory 5827 * might have been logged in LOG_INODE_EXISTS mode or 5828 * the dir index of the conflicting inode is not in a 5829 * dir index key range logged for the directory. So we 5830 * must make sure the deletion is recorded. 5831 */ 5832 ret = btrfs_log_inode(trans, BTRFS_I(inode), 5833 LOG_INODE_ALL, ctx); 5834 btrfs_add_delayed_iput(BTRFS_I(inode)); 5835 if (ret) 5836 break; 5837 continue; 5838 } 5839 5840 /* 5841 * Here we can use need_log_inode() because we only need to log 5842 * the inode in LOG_INODE_EXISTS mode and rename operations 5843 * update the log, so that the log ends up with the new name and 5844 * without the old name. 5845 * 5846 * We did this check at add_conflicting_inode(), but here we do 5847 * it again because if some other task logged the inode after 5848 * that, we can avoid doing it again. 5849 */ 5850 if (!need_log_inode(trans, BTRFS_I(inode))) { 5851 btrfs_add_delayed_iput(BTRFS_I(inode)); 5852 continue; 5853 } 5854 5855 /* 5856 * We are safe logging the other inode without acquiring its 5857 * lock as long as we log with the LOG_INODE_EXISTS mode. We 5858 * are safe against concurrent renames of the other inode as 5859 * well because during a rename we pin the log and update the 5860 * log with the new name before we unpin it. 5861 */ 5862 ret = btrfs_log_inode(trans, BTRFS_I(inode), LOG_INODE_EXISTS, ctx); 5863 btrfs_add_delayed_iput(BTRFS_I(inode)); 5864 if (ret) 5865 break; 5866 } 5867 5868 ctx->logging_conflict_inodes = false; 5869 if (ret) 5870 free_conflicting_inodes(ctx); 5871 5872 return ret; 5873 } 5874 5875 static int copy_inode_items_to_log(struct btrfs_trans_handle *trans, 5876 struct btrfs_inode *inode, 5877 struct btrfs_key *min_key, 5878 const struct btrfs_key *max_key, 5879 struct btrfs_path *path, 5880 struct btrfs_path *dst_path, 5881 const u64 logged_isize, 5882 const int inode_only, 5883 struct btrfs_log_ctx *ctx, 5884 bool *need_log_inode_item) 5885 { 5886 const u64 i_size = i_size_read(&inode->vfs_inode); 5887 struct btrfs_root *root = inode->root; 5888 int ins_start_slot = 0; 5889 int ins_nr = 0; 5890 int ret; 5891 5892 while (1) { 5893 ret = btrfs_search_forward(root, min_key, path, trans->transid); 5894 if (ret < 0) 5895 return ret; 5896 if (ret > 0) { 5897 ret = 0; 5898 break; 5899 } 5900 again: 5901 /* Note, ins_nr might be > 0 here, cleanup outside the loop */ 5902 if (min_key->objectid != max_key->objectid) 5903 break; 5904 if (min_key->type > max_key->type) 5905 break; 5906 5907 if (min_key->type == BTRFS_INODE_ITEM_KEY) { 5908 *need_log_inode_item = false; 5909 } else if (min_key->type == BTRFS_EXTENT_DATA_KEY && 5910 min_key->offset >= i_size) { 5911 /* 5912 * Extents at and beyond eof are logged with 5913 * btrfs_log_prealloc_extents(). 5914 * Only regular files have BTRFS_EXTENT_DATA_KEY keys, 5915 * and no keys greater than that, so bail out. 5916 */ 5917 break; 5918 } else if ((min_key->type == BTRFS_INODE_REF_KEY || 5919 min_key->type == BTRFS_INODE_EXTREF_KEY) && 5920 (inode->generation == trans->transid || 5921 ctx->logging_conflict_inodes)) { 5922 u64 other_ino = 0; 5923 u64 other_parent = 0; 5924 5925 ret = btrfs_check_ref_name_override(path->nodes[0], 5926 path->slots[0], min_key, inode, 5927 &other_ino, &other_parent); 5928 if (ret < 0) { 5929 return ret; 5930 } else if (ret > 0 && 5931 other_ino != btrfs_ino(ctx->inode)) { 5932 if (ins_nr > 0) { 5933 ins_nr++; 5934 } else { 5935 ins_nr = 1; 5936 ins_start_slot = path->slots[0]; 5937 } 5938 ret = copy_items(trans, inode, dst_path, path, 5939 ins_start_slot, ins_nr, 5940 inode_only, logged_isize, ctx); 5941 if (ret < 0) 5942 return ret; 5943 ins_nr = 0; 5944 5945 btrfs_release_path(path); 5946 ret = add_conflicting_inode(trans, root, path, 5947 other_ino, 5948 other_parent, ctx); 5949 if (ret) 5950 return ret; 5951 goto next_key; 5952 } 5953 } else if (min_key->type == BTRFS_XATTR_ITEM_KEY) { 5954 /* Skip xattrs, logged later with btrfs_log_all_xattrs() */ 5955 if (ins_nr == 0) 5956 goto next_slot; 5957 ret = copy_items(trans, inode, dst_path, path, 5958 ins_start_slot, 5959 ins_nr, inode_only, logged_isize, ctx); 5960 if (ret < 0) 5961 return ret; 5962 ins_nr = 0; 5963 goto next_slot; 5964 } 5965 5966 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { 5967 ins_nr++; 5968 goto next_slot; 5969 } else if (!ins_nr) { 5970 ins_start_slot = path->slots[0]; 5971 ins_nr = 1; 5972 goto next_slot; 5973 } 5974 5975 ret = copy_items(trans, inode, dst_path, path, ins_start_slot, 5976 ins_nr, inode_only, logged_isize, ctx); 5977 if (ret < 0) 5978 return ret; 5979 ins_nr = 1; 5980 ins_start_slot = path->slots[0]; 5981 next_slot: 5982 path->slots[0]++; 5983 if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) { 5984 btrfs_item_key_to_cpu(path->nodes[0], min_key, 5985 path->slots[0]); 5986 goto again; 5987 } 5988 if (ins_nr) { 5989 ret = copy_items(trans, inode, dst_path, path, 5990 ins_start_slot, ins_nr, inode_only, 5991 logged_isize, ctx); 5992 if (ret < 0) 5993 return ret; 5994 ins_nr = 0; 5995 } 5996 btrfs_release_path(path); 5997 next_key: 5998 if (min_key->offset < (u64)-1) { 5999 min_key->offset++; 6000 } else if (min_key->type < max_key->type) { 6001 min_key->type++; 6002 min_key->offset = 0; 6003 } else { 6004 break; 6005 } 6006 6007 /* 6008 * We may process many leaves full of items for our inode, so 6009 * avoid monopolizing a cpu for too long by rescheduling while 6010 * not holding locks on any tree. 6011 */ 6012 cond_resched(); 6013 } 6014 if (ins_nr) { 6015 ret = copy_items(trans, inode, dst_path, path, ins_start_slot, 6016 ins_nr, inode_only, logged_isize, ctx); 6017 if (ret) 6018 return ret; 6019 } 6020 6021 if (inode_only == LOG_INODE_ALL && S_ISREG(inode->vfs_inode.i_mode)) { 6022 /* 6023 * Release the path because otherwise we might attempt to double 6024 * lock the same leaf with btrfs_log_prealloc_extents() below. 6025 */ 6026 btrfs_release_path(path); 6027 ret = btrfs_log_prealloc_extents(trans, inode, dst_path, ctx); 6028 } 6029 6030 return ret; 6031 } 6032 6033 static int insert_delayed_items_batch(struct btrfs_trans_handle *trans, 6034 struct btrfs_root *log, 6035 struct btrfs_path *path, 6036 const struct btrfs_item_batch *batch, 6037 const struct btrfs_delayed_item *first_item) 6038 { 6039 const struct btrfs_delayed_item *curr = first_item; 6040 int ret; 6041 6042 ret = btrfs_insert_empty_items(trans, log, path, batch); 6043 if (ret) 6044 return ret; 6045 6046 for (int i = 0; i < batch->nr; i++) { 6047 char *data_ptr; 6048 6049 data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char); 6050 write_extent_buffer(path->nodes[0], &curr->data, 6051 (unsigned long)data_ptr, curr->data_len); 6052 curr = list_next_entry(curr, log_list); 6053 path->slots[0]++; 6054 } 6055 6056 btrfs_release_path(path); 6057 6058 return 0; 6059 } 6060 6061 static int log_delayed_insertion_items(struct btrfs_trans_handle *trans, 6062 struct btrfs_inode *inode, 6063 struct btrfs_path *path, 6064 const struct list_head *delayed_ins_list, 6065 struct btrfs_log_ctx *ctx) 6066 { 6067 /* 195 (4095 bytes of keys and sizes) fits in a single 4K page. */ 6068 const int max_batch_size = 195; 6069 const int leaf_data_size = BTRFS_LEAF_DATA_SIZE(trans->fs_info); 6070 const u64 ino = btrfs_ino(inode); 6071 struct btrfs_root *log = inode->root->log_root; 6072 struct btrfs_item_batch batch = { 6073 .nr = 0, 6074 .total_data_size = 0, 6075 }; 6076 const struct btrfs_delayed_item *first = NULL; 6077 const struct btrfs_delayed_item *curr; 6078 char *ins_data; 6079 struct btrfs_key *ins_keys; 6080 u32 *ins_sizes; 6081 u64 curr_batch_size = 0; 6082 int batch_idx = 0; 6083 int ret; 6084 6085 /* We are adding dir index items to the log tree. */ 6086 lockdep_assert_held(&inode->log_mutex); 6087 6088 /* 6089 * We collect delayed items before copying index keys from the subvolume 6090 * to the log tree. However just after we collected them, they may have 6091 * been flushed (all of them or just some of them), and therefore we 6092 * could have copied them from the subvolume tree to the log tree. 6093 * So find the first delayed item that was not yet logged (they are 6094 * sorted by index number). 6095 */ 6096 list_for_each_entry(curr, delayed_ins_list, log_list) { 6097 if (curr->index > inode->last_dir_index_offset) { 6098 first = curr; 6099 break; 6100 } 6101 } 6102 6103 /* Empty list or all delayed items were already logged. */ 6104 if (!first) 6105 return 0; 6106 6107 ins_data = kmalloc(max_batch_size * sizeof(u32) + 6108 max_batch_size * sizeof(struct btrfs_key), GFP_NOFS); 6109 if (!ins_data) 6110 return -ENOMEM; 6111 ins_sizes = (u32 *)ins_data; 6112 batch.data_sizes = ins_sizes; 6113 ins_keys = (struct btrfs_key *)(ins_data + max_batch_size * sizeof(u32)); 6114 batch.keys = ins_keys; 6115 6116 curr = first; 6117 while (!list_entry_is_head(curr, delayed_ins_list, log_list)) { 6118 const u32 curr_size = curr->data_len + sizeof(struct btrfs_item); 6119 6120 if (curr_batch_size + curr_size > leaf_data_size || 6121 batch.nr == max_batch_size) { 6122 ret = insert_delayed_items_batch(trans, log, path, 6123 &batch, first); 6124 if (ret) 6125 goto out; 6126 batch_idx = 0; 6127 batch.nr = 0; 6128 batch.total_data_size = 0; 6129 curr_batch_size = 0; 6130 first = curr; 6131 } 6132 6133 ins_sizes[batch_idx] = curr->data_len; 6134 ins_keys[batch_idx].objectid = ino; 6135 ins_keys[batch_idx].type = BTRFS_DIR_INDEX_KEY; 6136 ins_keys[batch_idx].offset = curr->index; 6137 curr_batch_size += curr_size; 6138 batch.total_data_size += curr->data_len; 6139 batch.nr++; 6140 batch_idx++; 6141 curr = list_next_entry(curr, log_list); 6142 } 6143 6144 ASSERT(batch.nr >= 1); 6145 ret = insert_delayed_items_batch(trans, log, path, &batch, first); 6146 6147 curr = list_last_entry(delayed_ins_list, struct btrfs_delayed_item, 6148 log_list); 6149 inode->last_dir_index_offset = curr->index; 6150 out: 6151 kfree(ins_data); 6152 6153 return ret; 6154 } 6155 6156 static int log_delayed_deletions_full(struct btrfs_trans_handle *trans, 6157 struct btrfs_inode *inode, 6158 struct btrfs_path *path, 6159 const struct list_head *delayed_del_list, 6160 struct btrfs_log_ctx *ctx) 6161 { 6162 const u64 ino = btrfs_ino(inode); 6163 const struct btrfs_delayed_item *curr; 6164 6165 curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item, 6166 log_list); 6167 6168 while (!list_entry_is_head(curr, delayed_del_list, log_list)) { 6169 u64 first_dir_index = curr->index; 6170 u64 last_dir_index; 6171 const struct btrfs_delayed_item *next; 6172 int ret; 6173 6174 /* 6175 * Find a range of consecutive dir index items to delete. Like 6176 * this we log a single dir range item spanning several contiguous 6177 * dir items instead of logging one range item per dir index item. 6178 */ 6179 next = list_next_entry(curr, log_list); 6180 while (!list_entry_is_head(next, delayed_del_list, log_list)) { 6181 if (next->index != curr->index + 1) 6182 break; 6183 curr = next; 6184 next = list_next_entry(next, log_list); 6185 } 6186 6187 last_dir_index = curr->index; 6188 ASSERT(last_dir_index >= first_dir_index); 6189 6190 ret = insert_dir_log_key(trans, inode->root->log_root, path, 6191 ino, first_dir_index, last_dir_index); 6192 if (ret) 6193 return ret; 6194 curr = list_next_entry(curr, log_list); 6195 } 6196 6197 return 0; 6198 } 6199 6200 static int batch_delete_dir_index_items(struct btrfs_trans_handle *trans, 6201 struct btrfs_inode *inode, 6202 struct btrfs_path *path, 6203 const struct list_head *delayed_del_list, 6204 const struct btrfs_delayed_item *first, 6205 const struct btrfs_delayed_item **last_ret) 6206 { 6207 const struct btrfs_delayed_item *next; 6208 struct extent_buffer *leaf = path->nodes[0]; 6209 const int last_slot = btrfs_header_nritems(leaf) - 1; 6210 int slot = path->slots[0] + 1; 6211 const u64 ino = btrfs_ino(inode); 6212 6213 next = list_next_entry(first, log_list); 6214 6215 while (slot < last_slot && 6216 !list_entry_is_head(next, delayed_del_list, log_list)) { 6217 struct btrfs_key key; 6218 6219 btrfs_item_key_to_cpu(leaf, &key, slot); 6220 if (key.objectid != ino || 6221 key.type != BTRFS_DIR_INDEX_KEY || 6222 key.offset != next->index) 6223 break; 6224 6225 slot++; 6226 *last_ret = next; 6227 next = list_next_entry(next, log_list); 6228 } 6229 6230 return btrfs_del_items(trans, inode->root->log_root, path, 6231 path->slots[0], slot - path->slots[0]); 6232 } 6233 6234 static int log_delayed_deletions_incremental(struct btrfs_trans_handle *trans, 6235 struct btrfs_inode *inode, 6236 struct btrfs_path *path, 6237 const struct list_head *delayed_del_list, 6238 struct btrfs_log_ctx *ctx) 6239 { 6240 struct btrfs_root *log = inode->root->log_root; 6241 const struct btrfs_delayed_item *curr; 6242 u64 last_range_start = 0; 6243 u64 last_range_end = 0; 6244 struct btrfs_key key; 6245 6246 key.objectid = btrfs_ino(inode); 6247 key.type = BTRFS_DIR_INDEX_KEY; 6248 curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item, 6249 log_list); 6250 6251 while (!list_entry_is_head(curr, delayed_del_list, log_list)) { 6252 const struct btrfs_delayed_item *last = curr; 6253 u64 first_dir_index = curr->index; 6254 u64 last_dir_index; 6255 bool deleted_items = false; 6256 int ret; 6257 6258 key.offset = curr->index; 6259 ret = btrfs_search_slot(trans, log, &key, path, -1, 1); 6260 if (ret < 0) { 6261 return ret; 6262 } else if (ret == 0) { 6263 ret = batch_delete_dir_index_items(trans, inode, path, 6264 delayed_del_list, curr, 6265 &last); 6266 if (ret) 6267 return ret; 6268 deleted_items = true; 6269 } 6270 6271 btrfs_release_path(path); 6272 6273 /* 6274 * If we deleted items from the leaf, it means we have a range 6275 * item logging their range, so no need to add one or update an 6276 * existing one. Otherwise we have to log a dir range item. 6277 */ 6278 if (deleted_items) 6279 goto next_batch; 6280 6281 last_dir_index = last->index; 6282 ASSERT(last_dir_index >= first_dir_index); 6283 /* 6284 * If this range starts right after where the previous one ends, 6285 * then we want to reuse the previous range item and change its 6286 * end offset to the end of this range. This is just to minimize 6287 * leaf space usage, by avoiding adding a new range item. 6288 */ 6289 if (last_range_end != 0 && first_dir_index == last_range_end + 1) 6290 first_dir_index = last_range_start; 6291 6292 ret = insert_dir_log_key(trans, log, path, key.objectid, 6293 first_dir_index, last_dir_index); 6294 if (ret) 6295 return ret; 6296 6297 last_range_start = first_dir_index; 6298 last_range_end = last_dir_index; 6299 next_batch: 6300 curr = list_next_entry(last, log_list); 6301 } 6302 6303 return 0; 6304 } 6305 6306 static int log_delayed_deletion_items(struct btrfs_trans_handle *trans, 6307 struct btrfs_inode *inode, 6308 struct btrfs_path *path, 6309 const struct list_head *delayed_del_list, 6310 struct btrfs_log_ctx *ctx) 6311 { 6312 /* 6313 * We are deleting dir index items from the log tree or adding range 6314 * items to it. 6315 */ 6316 lockdep_assert_held(&inode->log_mutex); 6317 6318 if (list_empty(delayed_del_list)) 6319 return 0; 6320 6321 if (ctx->logged_before) 6322 return log_delayed_deletions_incremental(trans, inode, path, 6323 delayed_del_list, ctx); 6324 6325 return log_delayed_deletions_full(trans, inode, path, delayed_del_list, 6326 ctx); 6327 } 6328 6329 /* 6330 * Similar logic as for log_new_dir_dentries(), but it iterates over the delayed 6331 * items instead of the subvolume tree. 6332 */ 6333 static int log_new_delayed_dentries(struct btrfs_trans_handle *trans, 6334 struct btrfs_inode *inode, 6335 const struct list_head *delayed_ins_list, 6336 struct btrfs_log_ctx *ctx) 6337 { 6338 const bool orig_log_new_dentries = ctx->log_new_dentries; 6339 struct btrfs_delayed_item *item; 6340 int ret = 0; 6341 6342 /* 6343 * No need for the log mutex, plus to avoid potential deadlocks or 6344 * lockdep annotations due to nesting of delayed inode mutexes and log 6345 * mutexes. 6346 */ 6347 lockdep_assert_not_held(&inode->log_mutex); 6348 6349 ASSERT(!ctx->logging_new_delayed_dentries); 6350 ctx->logging_new_delayed_dentries = true; 6351 6352 list_for_each_entry(item, delayed_ins_list, log_list) { 6353 struct btrfs_dir_item *dir_item; 6354 struct inode *di_inode; 6355 struct btrfs_key key; 6356 int log_mode = LOG_INODE_EXISTS; 6357 6358 dir_item = (struct btrfs_dir_item *)item->data; 6359 btrfs_disk_key_to_cpu(&key, &dir_item->location); 6360 6361 if (key.type == BTRFS_ROOT_ITEM_KEY) 6362 continue; 6363 6364 di_inode = btrfs_iget_logging(key.objectid, inode->root); 6365 if (IS_ERR(di_inode)) { 6366 ret = PTR_ERR(di_inode); 6367 break; 6368 } 6369 6370 if (!need_log_inode(trans, BTRFS_I(di_inode))) { 6371 btrfs_add_delayed_iput(BTRFS_I(di_inode)); 6372 continue; 6373 } 6374 6375 if (btrfs_stack_dir_ftype(dir_item) == BTRFS_FT_DIR) 6376 log_mode = LOG_INODE_ALL; 6377 6378 ctx->log_new_dentries = false; 6379 ret = btrfs_log_inode(trans, BTRFS_I(di_inode), log_mode, ctx); 6380 6381 if (!ret && ctx->log_new_dentries) 6382 ret = log_new_dir_dentries(trans, BTRFS_I(di_inode), ctx); 6383 6384 btrfs_add_delayed_iput(BTRFS_I(di_inode)); 6385 6386 if (ret) 6387 break; 6388 } 6389 6390 ctx->log_new_dentries = orig_log_new_dentries; 6391 ctx->logging_new_delayed_dentries = false; 6392 6393 return ret; 6394 } 6395 6396 /* log a single inode in the tree log. 6397 * At least one parent directory for this inode must exist in the tree 6398 * or be logged already. 6399 * 6400 * Any items from this inode changed by the current transaction are copied 6401 * to the log tree. An extra reference is taken on any extents in this 6402 * file, allowing us to avoid a whole pile of corner cases around logging 6403 * blocks that have been removed from the tree. 6404 * 6405 * See LOG_INODE_ALL and related defines for a description of what inode_only 6406 * does. 6407 * 6408 * This handles both files and directories. 6409 */ 6410 static int btrfs_log_inode(struct btrfs_trans_handle *trans, 6411 struct btrfs_inode *inode, 6412 int inode_only, 6413 struct btrfs_log_ctx *ctx) 6414 { 6415 struct btrfs_path *path; 6416 struct btrfs_path *dst_path; 6417 struct btrfs_key min_key; 6418 struct btrfs_key max_key; 6419 struct btrfs_root *log = inode->root->log_root; 6420 int ret; 6421 bool fast_search = false; 6422 u64 ino = btrfs_ino(inode); 6423 struct extent_map_tree *em_tree = &inode->extent_tree; 6424 u64 logged_isize = 0; 6425 bool need_log_inode_item = true; 6426 bool xattrs_logged = false; 6427 bool inode_item_dropped = true; 6428 bool full_dir_logging = false; 6429 LIST_HEAD(delayed_ins_list); 6430 LIST_HEAD(delayed_del_list); 6431 6432 path = btrfs_alloc_path(); 6433 if (!path) 6434 return -ENOMEM; 6435 dst_path = btrfs_alloc_path(); 6436 if (!dst_path) { 6437 btrfs_free_path(path); 6438 return -ENOMEM; 6439 } 6440 6441 min_key.objectid = ino; 6442 min_key.type = BTRFS_INODE_ITEM_KEY; 6443 min_key.offset = 0; 6444 6445 max_key.objectid = ino; 6446 6447 6448 /* today the code can only do partial logging of directories */ 6449 if (S_ISDIR(inode->vfs_inode.i_mode) || 6450 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 6451 &inode->runtime_flags) && 6452 inode_only >= LOG_INODE_EXISTS)) 6453 max_key.type = BTRFS_XATTR_ITEM_KEY; 6454 else 6455 max_key.type = (u8)-1; 6456 max_key.offset = (u64)-1; 6457 6458 if (S_ISDIR(inode->vfs_inode.i_mode) && inode_only == LOG_INODE_ALL) 6459 full_dir_logging = true; 6460 6461 /* 6462 * If we are logging a directory while we are logging dentries of the 6463 * delayed items of some other inode, then we need to flush the delayed 6464 * items of this directory and not log the delayed items directly. This 6465 * is to prevent more than one level of recursion into btrfs_log_inode() 6466 * by having something like this: 6467 * 6468 * $ mkdir -p a/b/c/d/e/f/g/h/... 6469 * $ xfs_io -c "fsync" a 6470 * 6471 * Where all directories in the path did not exist before and are 6472 * created in the current transaction. 6473 * So in such a case we directly log the delayed items of the main 6474 * directory ("a") without flushing them first, while for each of its 6475 * subdirectories we flush their delayed items before logging them. 6476 * This prevents a potential unbounded recursion like this: 6477 * 6478 * btrfs_log_inode() 6479 * log_new_delayed_dentries() 6480 * btrfs_log_inode() 6481 * log_new_delayed_dentries() 6482 * btrfs_log_inode() 6483 * log_new_delayed_dentries() 6484 * (...) 6485 * 6486 * We have thresholds for the maximum number of delayed items to have in 6487 * memory, and once they are hit, the items are flushed asynchronously. 6488 * However the limit is quite high, so lets prevent deep levels of 6489 * recursion to happen by limiting the maximum depth to be 1. 6490 */ 6491 if (full_dir_logging && ctx->logging_new_delayed_dentries) { 6492 ret = btrfs_commit_inode_delayed_items(trans, inode); 6493 if (ret) 6494 goto out; 6495 } 6496 6497 mutex_lock(&inode->log_mutex); 6498 6499 /* 6500 * For symlinks, we must always log their content, which is stored in an 6501 * inline extent, otherwise we could end up with an empty symlink after 6502 * log replay, which is invalid on linux (symlink(2) returns -ENOENT if 6503 * one attempts to create an empty symlink). 6504 * We don't need to worry about flushing delalloc, because when we create 6505 * the inline extent when the symlink is created (we never have delalloc 6506 * for symlinks). 6507 */ 6508 if (S_ISLNK(inode->vfs_inode.i_mode)) 6509 inode_only = LOG_INODE_ALL; 6510 6511 /* 6512 * Before logging the inode item, cache the value returned by 6513 * inode_logged(), because after that we have the need to figure out if 6514 * the inode was previously logged in this transaction. 6515 */ 6516 ret = inode_logged(trans, inode, path); 6517 if (ret < 0) 6518 goto out_unlock; 6519 ctx->logged_before = (ret == 1); 6520 ret = 0; 6521 6522 /* 6523 * This is for cases where logging a directory could result in losing a 6524 * a file after replaying the log. For example, if we move a file from a 6525 * directory A to a directory B, then fsync directory A, we have no way 6526 * to known the file was moved from A to B, so logging just A would 6527 * result in losing the file after a log replay. 6528 */ 6529 if (full_dir_logging && inode->last_unlink_trans >= trans->transid) { 6530 ret = BTRFS_LOG_FORCE_COMMIT; 6531 goto out_unlock; 6532 } 6533 6534 /* 6535 * a brute force approach to making sure we get the most uptodate 6536 * copies of everything. 6537 */ 6538 if (S_ISDIR(inode->vfs_inode.i_mode)) { 6539 clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags); 6540 if (ctx->logged_before) 6541 ret = drop_inode_items(trans, log, path, inode, 6542 BTRFS_XATTR_ITEM_KEY); 6543 } else { 6544 if (inode_only == LOG_INODE_EXISTS && ctx->logged_before) { 6545 /* 6546 * Make sure the new inode item we write to the log has 6547 * the same isize as the current one (if it exists). 6548 * This is necessary to prevent data loss after log 6549 * replay, and also to prevent doing a wrong expanding 6550 * truncate - for e.g. create file, write 4K into offset 6551 * 0, fsync, write 4K into offset 4096, add hard link, 6552 * fsync some other file (to sync log), power fail - if 6553 * we use the inode's current i_size, after log replay 6554 * we get a 8Kb file, with the last 4Kb extent as a hole 6555 * (zeroes), as if an expanding truncate happened, 6556 * instead of getting a file of 4Kb only. 6557 */ 6558 ret = logged_inode_size(log, inode, path, &logged_isize); 6559 if (ret) 6560 goto out_unlock; 6561 } 6562 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 6563 &inode->runtime_flags)) { 6564 if (inode_only == LOG_INODE_EXISTS) { 6565 max_key.type = BTRFS_XATTR_ITEM_KEY; 6566 if (ctx->logged_before) 6567 ret = drop_inode_items(trans, log, path, 6568 inode, max_key.type); 6569 } else { 6570 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 6571 &inode->runtime_flags); 6572 clear_bit(BTRFS_INODE_COPY_EVERYTHING, 6573 &inode->runtime_flags); 6574 if (ctx->logged_before) 6575 ret = truncate_inode_items(trans, log, 6576 inode, 0, 0); 6577 } 6578 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING, 6579 &inode->runtime_flags) || 6580 inode_only == LOG_INODE_EXISTS) { 6581 if (inode_only == LOG_INODE_ALL) 6582 fast_search = true; 6583 max_key.type = BTRFS_XATTR_ITEM_KEY; 6584 if (ctx->logged_before) 6585 ret = drop_inode_items(trans, log, path, inode, 6586 max_key.type); 6587 } else { 6588 if (inode_only == LOG_INODE_ALL) 6589 fast_search = true; 6590 inode_item_dropped = false; 6591 goto log_extents; 6592 } 6593 6594 } 6595 if (ret) 6596 goto out_unlock; 6597 6598 /* 6599 * If we are logging a directory in full mode, collect the delayed items 6600 * before iterating the subvolume tree, so that we don't miss any new 6601 * dir index items in case they get flushed while or right after we are 6602 * iterating the subvolume tree. 6603 */ 6604 if (full_dir_logging && !ctx->logging_new_delayed_dentries) 6605 btrfs_log_get_delayed_items(inode, &delayed_ins_list, 6606 &delayed_del_list); 6607 6608 ret = copy_inode_items_to_log(trans, inode, &min_key, &max_key, 6609 path, dst_path, logged_isize, 6610 inode_only, ctx, 6611 &need_log_inode_item); 6612 if (ret) 6613 goto out_unlock; 6614 6615 btrfs_release_path(path); 6616 btrfs_release_path(dst_path); 6617 ret = btrfs_log_all_xattrs(trans, inode, path, dst_path, ctx); 6618 if (ret) 6619 goto out_unlock; 6620 xattrs_logged = true; 6621 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) { 6622 btrfs_release_path(path); 6623 btrfs_release_path(dst_path); 6624 ret = btrfs_log_holes(trans, inode, path); 6625 if (ret) 6626 goto out_unlock; 6627 } 6628 log_extents: 6629 btrfs_release_path(path); 6630 btrfs_release_path(dst_path); 6631 if (need_log_inode_item) { 6632 ret = log_inode_item(trans, log, dst_path, inode, inode_item_dropped); 6633 if (ret) 6634 goto out_unlock; 6635 /* 6636 * If we are doing a fast fsync and the inode was logged before 6637 * in this transaction, we don't need to log the xattrs because 6638 * they were logged before. If xattrs were added, changed or 6639 * deleted since the last time we logged the inode, then we have 6640 * already logged them because the inode had the runtime flag 6641 * BTRFS_INODE_COPY_EVERYTHING set. 6642 */ 6643 if (!xattrs_logged && inode->logged_trans < trans->transid) { 6644 ret = btrfs_log_all_xattrs(trans, inode, path, dst_path, ctx); 6645 if (ret) 6646 goto out_unlock; 6647 btrfs_release_path(path); 6648 } 6649 } 6650 if (fast_search) { 6651 ret = btrfs_log_changed_extents(trans, inode, dst_path, ctx); 6652 if (ret) 6653 goto out_unlock; 6654 } else if (inode_only == LOG_INODE_ALL) { 6655 struct extent_map *em, *n; 6656 6657 write_lock(&em_tree->lock); 6658 list_for_each_entry_safe(em, n, &em_tree->modified_extents, list) 6659 list_del_init(&em->list); 6660 write_unlock(&em_tree->lock); 6661 } 6662 6663 if (full_dir_logging) { 6664 ret = log_directory_changes(trans, inode, path, dst_path, ctx); 6665 if (ret) 6666 goto out_unlock; 6667 ret = log_delayed_insertion_items(trans, inode, path, 6668 &delayed_ins_list, ctx); 6669 if (ret) 6670 goto out_unlock; 6671 ret = log_delayed_deletion_items(trans, inode, path, 6672 &delayed_del_list, ctx); 6673 if (ret) 6674 goto out_unlock; 6675 } 6676 6677 spin_lock(&inode->lock); 6678 inode->logged_trans = trans->transid; 6679 /* 6680 * Don't update last_log_commit if we logged that an inode exists. 6681 * We do this for three reasons: 6682 * 6683 * 1) We might have had buffered writes to this inode that were 6684 * flushed and had their ordered extents completed in this 6685 * transaction, but we did not previously log the inode with 6686 * LOG_INODE_ALL. Later the inode was evicted and after that 6687 * it was loaded again and this LOG_INODE_EXISTS log operation 6688 * happened. We must make sure that if an explicit fsync against 6689 * the inode is performed later, it logs the new extents, an 6690 * updated inode item, etc, and syncs the log. The same logic 6691 * applies to direct IO writes instead of buffered writes. 6692 * 6693 * 2) When we log the inode with LOG_INODE_EXISTS, its inode item 6694 * is logged with an i_size of 0 or whatever value was logged 6695 * before. If later the i_size of the inode is increased by a 6696 * truncate operation, the log is synced through an fsync of 6697 * some other inode and then finally an explicit fsync against 6698 * this inode is made, we must make sure this fsync logs the 6699 * inode with the new i_size, the hole between old i_size and 6700 * the new i_size, and syncs the log. 6701 * 6702 * 3) If we are logging that an ancestor inode exists as part of 6703 * logging a new name from a link or rename operation, don't update 6704 * its last_log_commit - otherwise if an explicit fsync is made 6705 * against an ancestor, the fsync considers the inode in the log 6706 * and doesn't sync the log, resulting in the ancestor missing after 6707 * a power failure unless the log was synced as part of an fsync 6708 * against any other unrelated inode. 6709 */ 6710 if (inode_only != LOG_INODE_EXISTS) 6711 inode->last_log_commit = inode->last_sub_trans; 6712 spin_unlock(&inode->lock); 6713 6714 /* 6715 * Reset the last_reflink_trans so that the next fsync does not need to 6716 * go through the slower path when logging extents and their checksums. 6717 */ 6718 if (inode_only == LOG_INODE_ALL) 6719 inode->last_reflink_trans = 0; 6720 6721 out_unlock: 6722 mutex_unlock(&inode->log_mutex); 6723 out: 6724 btrfs_free_path(path); 6725 btrfs_free_path(dst_path); 6726 6727 if (ret) 6728 free_conflicting_inodes(ctx); 6729 else 6730 ret = log_conflicting_inodes(trans, inode->root, ctx); 6731 6732 if (full_dir_logging && !ctx->logging_new_delayed_dentries) { 6733 if (!ret) 6734 ret = log_new_delayed_dentries(trans, inode, 6735 &delayed_ins_list, ctx); 6736 6737 btrfs_log_put_delayed_items(inode, &delayed_ins_list, 6738 &delayed_del_list); 6739 } 6740 6741 return ret; 6742 } 6743 6744 static int btrfs_log_all_parents(struct btrfs_trans_handle *trans, 6745 struct btrfs_inode *inode, 6746 struct btrfs_log_ctx *ctx) 6747 { 6748 int ret; 6749 struct btrfs_path *path; 6750 struct btrfs_key key; 6751 struct btrfs_root *root = inode->root; 6752 const u64 ino = btrfs_ino(inode); 6753 6754 path = btrfs_alloc_path(); 6755 if (!path) 6756 return -ENOMEM; 6757 path->skip_locking = 1; 6758 path->search_commit_root = 1; 6759 6760 key.objectid = ino; 6761 key.type = BTRFS_INODE_REF_KEY; 6762 key.offset = 0; 6763 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 6764 if (ret < 0) 6765 goto out; 6766 6767 while (true) { 6768 struct extent_buffer *leaf = path->nodes[0]; 6769 int slot = path->slots[0]; 6770 u32 cur_offset = 0; 6771 u32 item_size; 6772 unsigned long ptr; 6773 6774 if (slot >= btrfs_header_nritems(leaf)) { 6775 ret = btrfs_next_leaf(root, path); 6776 if (ret < 0) 6777 goto out; 6778 else if (ret > 0) 6779 break; 6780 continue; 6781 } 6782 6783 btrfs_item_key_to_cpu(leaf, &key, slot); 6784 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */ 6785 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY) 6786 break; 6787 6788 item_size = btrfs_item_size(leaf, slot); 6789 ptr = btrfs_item_ptr_offset(leaf, slot); 6790 while (cur_offset < item_size) { 6791 struct btrfs_key inode_key; 6792 struct inode *dir_inode; 6793 6794 inode_key.type = BTRFS_INODE_ITEM_KEY; 6795 inode_key.offset = 0; 6796 6797 if (key.type == BTRFS_INODE_EXTREF_KEY) { 6798 struct btrfs_inode_extref *extref; 6799 6800 extref = (struct btrfs_inode_extref *) 6801 (ptr + cur_offset); 6802 inode_key.objectid = btrfs_inode_extref_parent( 6803 leaf, extref); 6804 cur_offset += sizeof(*extref); 6805 cur_offset += btrfs_inode_extref_name_len(leaf, 6806 extref); 6807 } else { 6808 inode_key.objectid = key.offset; 6809 cur_offset = item_size; 6810 } 6811 6812 dir_inode = btrfs_iget_logging(inode_key.objectid, root); 6813 /* 6814 * If the parent inode was deleted, return an error to 6815 * fallback to a transaction commit. This is to prevent 6816 * getting an inode that was moved from one parent A to 6817 * a parent B, got its former parent A deleted and then 6818 * it got fsync'ed, from existing at both parents after 6819 * a log replay (and the old parent still existing). 6820 * Example: 6821 * 6822 * mkdir /mnt/A 6823 * mkdir /mnt/B 6824 * touch /mnt/B/bar 6825 * sync 6826 * mv /mnt/B/bar /mnt/A/bar 6827 * mv -T /mnt/A /mnt/B 6828 * fsync /mnt/B/bar 6829 * <power fail> 6830 * 6831 * If we ignore the old parent B which got deleted, 6832 * after a log replay we would have file bar linked 6833 * at both parents and the old parent B would still 6834 * exist. 6835 */ 6836 if (IS_ERR(dir_inode)) { 6837 ret = PTR_ERR(dir_inode); 6838 goto out; 6839 } 6840 6841 if (!need_log_inode(trans, BTRFS_I(dir_inode))) { 6842 btrfs_add_delayed_iput(BTRFS_I(dir_inode)); 6843 continue; 6844 } 6845 6846 ctx->log_new_dentries = false; 6847 ret = btrfs_log_inode(trans, BTRFS_I(dir_inode), 6848 LOG_INODE_ALL, ctx); 6849 if (!ret && ctx->log_new_dentries) 6850 ret = log_new_dir_dentries(trans, 6851 BTRFS_I(dir_inode), ctx); 6852 btrfs_add_delayed_iput(BTRFS_I(dir_inode)); 6853 if (ret) 6854 goto out; 6855 } 6856 path->slots[0]++; 6857 } 6858 ret = 0; 6859 out: 6860 btrfs_free_path(path); 6861 return ret; 6862 } 6863 6864 static int log_new_ancestors(struct btrfs_trans_handle *trans, 6865 struct btrfs_root *root, 6866 struct btrfs_path *path, 6867 struct btrfs_log_ctx *ctx) 6868 { 6869 struct btrfs_key found_key; 6870 6871 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]); 6872 6873 while (true) { 6874 struct extent_buffer *leaf; 6875 int slot; 6876 struct btrfs_key search_key; 6877 struct inode *inode; 6878 u64 ino; 6879 int ret = 0; 6880 6881 btrfs_release_path(path); 6882 6883 ino = found_key.offset; 6884 6885 search_key.objectid = found_key.offset; 6886 search_key.type = BTRFS_INODE_ITEM_KEY; 6887 search_key.offset = 0; 6888 inode = btrfs_iget_logging(ino, root); 6889 if (IS_ERR(inode)) 6890 return PTR_ERR(inode); 6891 6892 if (BTRFS_I(inode)->generation >= trans->transid && 6893 need_log_inode(trans, BTRFS_I(inode))) 6894 ret = btrfs_log_inode(trans, BTRFS_I(inode), 6895 LOG_INODE_EXISTS, ctx); 6896 btrfs_add_delayed_iput(BTRFS_I(inode)); 6897 if (ret) 6898 return ret; 6899 6900 if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID) 6901 break; 6902 6903 search_key.type = BTRFS_INODE_REF_KEY; 6904 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 6905 if (ret < 0) 6906 return ret; 6907 6908 leaf = path->nodes[0]; 6909 slot = path->slots[0]; 6910 if (slot >= btrfs_header_nritems(leaf)) { 6911 ret = btrfs_next_leaf(root, path); 6912 if (ret < 0) 6913 return ret; 6914 else if (ret > 0) 6915 return -ENOENT; 6916 leaf = path->nodes[0]; 6917 slot = path->slots[0]; 6918 } 6919 6920 btrfs_item_key_to_cpu(leaf, &found_key, slot); 6921 if (found_key.objectid != search_key.objectid || 6922 found_key.type != BTRFS_INODE_REF_KEY) 6923 return -ENOENT; 6924 } 6925 return 0; 6926 } 6927 6928 static int log_new_ancestors_fast(struct btrfs_trans_handle *trans, 6929 struct btrfs_inode *inode, 6930 struct dentry *parent, 6931 struct btrfs_log_ctx *ctx) 6932 { 6933 struct btrfs_root *root = inode->root; 6934 struct dentry *old_parent = NULL; 6935 struct super_block *sb = inode->vfs_inode.i_sb; 6936 int ret = 0; 6937 6938 while (true) { 6939 if (!parent || d_really_is_negative(parent) || 6940 sb != parent->d_sb) 6941 break; 6942 6943 inode = BTRFS_I(d_inode(parent)); 6944 if (root != inode->root) 6945 break; 6946 6947 if (inode->generation >= trans->transid && 6948 need_log_inode(trans, inode)) { 6949 ret = btrfs_log_inode(trans, inode, 6950 LOG_INODE_EXISTS, ctx); 6951 if (ret) 6952 break; 6953 } 6954 if (IS_ROOT(parent)) 6955 break; 6956 6957 parent = dget_parent(parent); 6958 dput(old_parent); 6959 old_parent = parent; 6960 } 6961 dput(old_parent); 6962 6963 return ret; 6964 } 6965 6966 static int log_all_new_ancestors(struct btrfs_trans_handle *trans, 6967 struct btrfs_inode *inode, 6968 struct dentry *parent, 6969 struct btrfs_log_ctx *ctx) 6970 { 6971 struct btrfs_root *root = inode->root; 6972 const u64 ino = btrfs_ino(inode); 6973 struct btrfs_path *path; 6974 struct btrfs_key search_key; 6975 int ret; 6976 6977 /* 6978 * For a single hard link case, go through a fast path that does not 6979 * need to iterate the fs/subvolume tree. 6980 */ 6981 if (inode->vfs_inode.i_nlink < 2) 6982 return log_new_ancestors_fast(trans, inode, parent, ctx); 6983 6984 path = btrfs_alloc_path(); 6985 if (!path) 6986 return -ENOMEM; 6987 6988 search_key.objectid = ino; 6989 search_key.type = BTRFS_INODE_REF_KEY; 6990 search_key.offset = 0; 6991 again: 6992 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 6993 if (ret < 0) 6994 goto out; 6995 if (ret == 0) 6996 path->slots[0]++; 6997 6998 while (true) { 6999 struct extent_buffer *leaf = path->nodes[0]; 7000 int slot = path->slots[0]; 7001 struct btrfs_key found_key; 7002 7003 if (slot >= btrfs_header_nritems(leaf)) { 7004 ret = btrfs_next_leaf(root, path); 7005 if (ret < 0) 7006 goto out; 7007 else if (ret > 0) 7008 break; 7009 continue; 7010 } 7011 7012 btrfs_item_key_to_cpu(leaf, &found_key, slot); 7013 if (found_key.objectid != ino || 7014 found_key.type > BTRFS_INODE_EXTREF_KEY) 7015 break; 7016 7017 /* 7018 * Don't deal with extended references because they are rare 7019 * cases and too complex to deal with (we would need to keep 7020 * track of which subitem we are processing for each item in 7021 * this loop, etc). So just return some error to fallback to 7022 * a transaction commit. 7023 */ 7024 if (found_key.type == BTRFS_INODE_EXTREF_KEY) { 7025 ret = -EMLINK; 7026 goto out; 7027 } 7028 7029 /* 7030 * Logging ancestors needs to do more searches on the fs/subvol 7031 * tree, so it releases the path as needed to avoid deadlocks. 7032 * Keep track of the last inode ref key and resume from that key 7033 * after logging all new ancestors for the current hard link. 7034 */ 7035 memcpy(&search_key, &found_key, sizeof(search_key)); 7036 7037 ret = log_new_ancestors(trans, root, path, ctx); 7038 if (ret) 7039 goto out; 7040 btrfs_release_path(path); 7041 goto again; 7042 } 7043 ret = 0; 7044 out: 7045 btrfs_free_path(path); 7046 return ret; 7047 } 7048 7049 /* 7050 * helper function around btrfs_log_inode to make sure newly created 7051 * parent directories also end up in the log. A minimal inode and backref 7052 * only logging is done of any parent directories that are older than 7053 * the last committed transaction 7054 */ 7055 static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, 7056 struct btrfs_inode *inode, 7057 struct dentry *parent, 7058 int inode_only, 7059 struct btrfs_log_ctx *ctx) 7060 { 7061 struct btrfs_root *root = inode->root; 7062 struct btrfs_fs_info *fs_info = root->fs_info; 7063 int ret = 0; 7064 bool log_dentries = false; 7065 7066 if (btrfs_test_opt(fs_info, NOTREELOG)) { 7067 ret = BTRFS_LOG_FORCE_COMMIT; 7068 goto end_no_trans; 7069 } 7070 7071 if (btrfs_root_refs(&root->root_item) == 0) { 7072 ret = BTRFS_LOG_FORCE_COMMIT; 7073 goto end_no_trans; 7074 } 7075 7076 /* 7077 * If we're logging an inode from a subvolume created in the current 7078 * transaction we must force a commit since the root is not persisted. 7079 */ 7080 if (btrfs_root_generation(&root->root_item) == trans->transid) { 7081 ret = BTRFS_LOG_FORCE_COMMIT; 7082 goto end_no_trans; 7083 } 7084 7085 /* 7086 * Skip already logged inodes or inodes corresponding to tmpfiles 7087 * (since logging them is pointless, a link count of 0 means they 7088 * will never be accessible). 7089 */ 7090 if ((btrfs_inode_in_log(inode, trans->transid) && 7091 list_empty(&ctx->ordered_extents)) || 7092 inode->vfs_inode.i_nlink == 0) { 7093 ret = BTRFS_NO_LOG_SYNC; 7094 goto end_no_trans; 7095 } 7096 7097 ret = start_log_trans(trans, root, ctx); 7098 if (ret) 7099 goto end_no_trans; 7100 7101 ret = btrfs_log_inode(trans, inode, inode_only, ctx); 7102 if (ret) 7103 goto end_trans; 7104 7105 /* 7106 * for regular files, if its inode is already on disk, we don't 7107 * have to worry about the parents at all. This is because 7108 * we can use the last_unlink_trans field to record renames 7109 * and other fun in this file. 7110 */ 7111 if (S_ISREG(inode->vfs_inode.i_mode) && 7112 inode->generation < trans->transid && 7113 inode->last_unlink_trans < trans->transid) { 7114 ret = 0; 7115 goto end_trans; 7116 } 7117 7118 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx->log_new_dentries) 7119 log_dentries = true; 7120 7121 /* 7122 * On unlink we must make sure all our current and old parent directory 7123 * inodes are fully logged. This is to prevent leaving dangling 7124 * directory index entries in directories that were our parents but are 7125 * not anymore. Not doing this results in old parent directory being 7126 * impossible to delete after log replay (rmdir will always fail with 7127 * error -ENOTEMPTY). 7128 * 7129 * Example 1: 7130 * 7131 * mkdir testdir 7132 * touch testdir/foo 7133 * ln testdir/foo testdir/bar 7134 * sync 7135 * unlink testdir/bar 7136 * xfs_io -c fsync testdir/foo 7137 * <power failure> 7138 * mount fs, triggers log replay 7139 * 7140 * If we don't log the parent directory (testdir), after log replay the 7141 * directory still has an entry pointing to the file inode using the bar 7142 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and 7143 * the file inode has a link count of 1. 7144 * 7145 * Example 2: 7146 * 7147 * mkdir testdir 7148 * touch foo 7149 * ln foo testdir/foo2 7150 * ln foo testdir/foo3 7151 * sync 7152 * unlink testdir/foo3 7153 * xfs_io -c fsync foo 7154 * <power failure> 7155 * mount fs, triggers log replay 7156 * 7157 * Similar as the first example, after log replay the parent directory 7158 * testdir still has an entry pointing to the inode file with name foo3 7159 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item 7160 * and has a link count of 2. 7161 */ 7162 if (inode->last_unlink_trans >= trans->transid) { 7163 ret = btrfs_log_all_parents(trans, inode, ctx); 7164 if (ret) 7165 goto end_trans; 7166 } 7167 7168 ret = log_all_new_ancestors(trans, inode, parent, ctx); 7169 if (ret) 7170 goto end_trans; 7171 7172 if (log_dentries) 7173 ret = log_new_dir_dentries(trans, inode, ctx); 7174 else 7175 ret = 0; 7176 end_trans: 7177 if (ret < 0) { 7178 btrfs_set_log_full_commit(trans); 7179 ret = BTRFS_LOG_FORCE_COMMIT; 7180 } 7181 7182 if (ret) 7183 btrfs_remove_log_ctx(root, ctx); 7184 btrfs_end_log_trans(root); 7185 end_no_trans: 7186 return ret; 7187 } 7188 7189 /* 7190 * it is not safe to log dentry if the chunk root has added new 7191 * chunks. This returns 0 if the dentry was logged, and 1 otherwise. 7192 * If this returns 1, you must commit the transaction to safely get your 7193 * data on disk. 7194 */ 7195 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, 7196 struct dentry *dentry, 7197 struct btrfs_log_ctx *ctx) 7198 { 7199 struct dentry *parent = dget_parent(dentry); 7200 int ret; 7201 7202 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent, 7203 LOG_INODE_ALL, ctx); 7204 dput(parent); 7205 7206 return ret; 7207 } 7208 7209 /* 7210 * should be called during mount to recover any replay any log trees 7211 * from the FS 7212 */ 7213 int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) 7214 { 7215 int ret; 7216 struct btrfs_path *path; 7217 struct btrfs_trans_handle *trans; 7218 struct btrfs_key key; 7219 struct btrfs_key found_key; 7220 struct btrfs_root *log; 7221 struct btrfs_fs_info *fs_info = log_root_tree->fs_info; 7222 struct walk_control wc = { 7223 .process_func = process_one_buffer, 7224 .stage = LOG_WALK_PIN_ONLY, 7225 }; 7226 7227 path = btrfs_alloc_path(); 7228 if (!path) 7229 return -ENOMEM; 7230 7231 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); 7232 7233 trans = btrfs_start_transaction(fs_info->tree_root, 0); 7234 if (IS_ERR(trans)) { 7235 ret = PTR_ERR(trans); 7236 goto error; 7237 } 7238 7239 wc.trans = trans; 7240 wc.pin = 1; 7241 7242 ret = walk_log_tree(trans, log_root_tree, &wc); 7243 if (ret) { 7244 btrfs_abort_transaction(trans, ret); 7245 goto error; 7246 } 7247 7248 again: 7249 key.objectid = BTRFS_TREE_LOG_OBJECTID; 7250 key.offset = (u64)-1; 7251 key.type = BTRFS_ROOT_ITEM_KEY; 7252 7253 while (1) { 7254 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); 7255 7256 if (ret < 0) { 7257 btrfs_abort_transaction(trans, ret); 7258 goto error; 7259 } 7260 if (ret > 0) { 7261 if (path->slots[0] == 0) 7262 break; 7263 path->slots[0]--; 7264 } 7265 btrfs_item_key_to_cpu(path->nodes[0], &found_key, 7266 path->slots[0]); 7267 btrfs_release_path(path); 7268 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) 7269 break; 7270 7271 log = btrfs_read_tree_root(log_root_tree, &found_key); 7272 if (IS_ERR(log)) { 7273 ret = PTR_ERR(log); 7274 btrfs_abort_transaction(trans, ret); 7275 goto error; 7276 } 7277 7278 wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset, 7279 true); 7280 if (IS_ERR(wc.replay_dest)) { 7281 ret = PTR_ERR(wc.replay_dest); 7282 7283 /* 7284 * We didn't find the subvol, likely because it was 7285 * deleted. This is ok, simply skip this log and go to 7286 * the next one. 7287 * 7288 * We need to exclude the root because we can't have 7289 * other log replays overwriting this log as we'll read 7290 * it back in a few more times. This will keep our 7291 * block from being modified, and we'll just bail for 7292 * each subsequent pass. 7293 */ 7294 if (ret == -ENOENT) 7295 ret = btrfs_pin_extent_for_log_replay(trans, log->node); 7296 btrfs_put_root(log); 7297 7298 if (!ret) 7299 goto next; 7300 btrfs_abort_transaction(trans, ret); 7301 goto error; 7302 } 7303 7304 wc.replay_dest->log_root = log; 7305 ret = btrfs_record_root_in_trans(trans, wc.replay_dest); 7306 if (ret) 7307 /* The loop needs to continue due to the root refs */ 7308 btrfs_abort_transaction(trans, ret); 7309 else 7310 ret = walk_log_tree(trans, log, &wc); 7311 7312 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { 7313 ret = fixup_inode_link_counts(trans, wc.replay_dest, 7314 path); 7315 if (ret) 7316 btrfs_abort_transaction(trans, ret); 7317 } 7318 7319 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { 7320 struct btrfs_root *root = wc.replay_dest; 7321 7322 btrfs_release_path(path); 7323 7324 /* 7325 * We have just replayed everything, and the highest 7326 * objectid of fs roots probably has changed in case 7327 * some inode_item's got replayed. 7328 * 7329 * root->objectid_mutex is not acquired as log replay 7330 * could only happen during mount. 7331 */ 7332 ret = btrfs_init_root_free_objectid(root); 7333 if (ret) 7334 btrfs_abort_transaction(trans, ret); 7335 } 7336 7337 wc.replay_dest->log_root = NULL; 7338 btrfs_put_root(wc.replay_dest); 7339 btrfs_put_root(log); 7340 7341 if (ret) 7342 goto error; 7343 next: 7344 if (found_key.offset == 0) 7345 break; 7346 key.offset = found_key.offset - 1; 7347 } 7348 btrfs_release_path(path); 7349 7350 /* step one is to pin it all, step two is to replay just inodes */ 7351 if (wc.pin) { 7352 wc.pin = 0; 7353 wc.process_func = replay_one_buffer; 7354 wc.stage = LOG_WALK_REPLAY_INODES; 7355 goto again; 7356 } 7357 /* step three is to replay everything */ 7358 if (wc.stage < LOG_WALK_REPLAY_ALL) { 7359 wc.stage++; 7360 goto again; 7361 } 7362 7363 btrfs_free_path(path); 7364 7365 /* step 4: commit the transaction, which also unpins the blocks */ 7366 ret = btrfs_commit_transaction(trans); 7367 if (ret) 7368 return ret; 7369 7370 log_root_tree->log_root = NULL; 7371 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); 7372 btrfs_put_root(log_root_tree); 7373 7374 return 0; 7375 error: 7376 if (wc.trans) 7377 btrfs_end_transaction(wc.trans); 7378 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); 7379 btrfs_free_path(path); 7380 return ret; 7381 } 7382 7383 /* 7384 * there are some corner cases where we want to force a full 7385 * commit instead of allowing a directory to be logged. 7386 * 7387 * They revolve around files there were unlinked from the directory, and 7388 * this function updates the parent directory so that a full commit is 7389 * properly done if it is fsync'd later after the unlinks are done. 7390 * 7391 * Must be called before the unlink operations (updates to the subvolume tree, 7392 * inodes, etc) are done. 7393 */ 7394 void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans, 7395 struct btrfs_inode *dir, struct btrfs_inode *inode, 7396 bool for_rename) 7397 { 7398 /* 7399 * when we're logging a file, if it hasn't been renamed 7400 * or unlinked, and its inode is fully committed on disk, 7401 * we don't have to worry about walking up the directory chain 7402 * to log its parents. 7403 * 7404 * So, we use the last_unlink_trans field to put this transid 7405 * into the file. When the file is logged we check it and 7406 * don't log the parents if the file is fully on disk. 7407 */ 7408 mutex_lock(&inode->log_mutex); 7409 inode->last_unlink_trans = trans->transid; 7410 mutex_unlock(&inode->log_mutex); 7411 7412 if (!for_rename) 7413 return; 7414 7415 /* 7416 * If this directory was already logged, any new names will be logged 7417 * with btrfs_log_new_name() and old names will be deleted from the log 7418 * tree with btrfs_del_dir_entries_in_log() or with 7419 * btrfs_del_inode_ref_in_log(). 7420 */ 7421 if (inode_logged(trans, dir, NULL) == 1) 7422 return; 7423 7424 /* 7425 * If the inode we're about to unlink was logged before, the log will be 7426 * properly updated with the new name with btrfs_log_new_name() and the 7427 * old name removed with btrfs_del_dir_entries_in_log() or with 7428 * btrfs_del_inode_ref_in_log(). 7429 */ 7430 if (inode_logged(trans, inode, NULL) == 1) 7431 return; 7432 7433 /* 7434 * when renaming files across directories, if the directory 7435 * there we're unlinking from gets fsync'd later on, there's 7436 * no way to find the destination directory later and fsync it 7437 * properly. So, we have to be conservative and force commits 7438 * so the new name gets discovered. 7439 */ 7440 mutex_lock(&dir->log_mutex); 7441 dir->last_unlink_trans = trans->transid; 7442 mutex_unlock(&dir->log_mutex); 7443 } 7444 7445 /* 7446 * Make sure that if someone attempts to fsync the parent directory of a deleted 7447 * snapshot, it ends up triggering a transaction commit. This is to guarantee 7448 * that after replaying the log tree of the parent directory's root we will not 7449 * see the snapshot anymore and at log replay time we will not see any log tree 7450 * corresponding to the deleted snapshot's root, which could lead to replaying 7451 * it after replaying the log tree of the parent directory (which would replay 7452 * the snapshot delete operation). 7453 * 7454 * Must be called before the actual snapshot destroy operation (updates to the 7455 * parent root and tree of tree roots trees, etc) are done. 7456 */ 7457 void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans, 7458 struct btrfs_inode *dir) 7459 { 7460 mutex_lock(&dir->log_mutex); 7461 dir->last_unlink_trans = trans->transid; 7462 mutex_unlock(&dir->log_mutex); 7463 } 7464 7465 /* 7466 * Call this when creating a subvolume in a directory. 7467 * Because we don't commit a transaction when creating a subvolume, we can't 7468 * allow the directory pointing to the subvolume to be logged with an entry that 7469 * points to an unpersisted root if we are still in the transaction used to 7470 * create the subvolume, so make any attempt to log the directory to result in a 7471 * full log sync. 7472 * Also we don't need to worry with renames, since btrfs_rename() marks the log 7473 * for full commit when renaming a subvolume. 7474 */ 7475 void btrfs_record_new_subvolume(const struct btrfs_trans_handle *trans, 7476 struct btrfs_inode *dir) 7477 { 7478 mutex_lock(&dir->log_mutex); 7479 dir->last_unlink_trans = trans->transid; 7480 mutex_unlock(&dir->log_mutex); 7481 } 7482 7483 /* 7484 * Update the log after adding a new name for an inode. 7485 * 7486 * @trans: Transaction handle. 7487 * @old_dentry: The dentry associated with the old name and the old 7488 * parent directory. 7489 * @old_dir: The inode of the previous parent directory for the case 7490 * of a rename. For a link operation, it must be NULL. 7491 * @old_dir_index: The index number associated with the old name, meaningful 7492 * only for rename operations (when @old_dir is not NULL). 7493 * Ignored for link operations. 7494 * @parent: The dentry associated with the directory under which the 7495 * new name is located. 7496 * 7497 * Call this after adding a new name for an inode, as a result of a link or 7498 * rename operation, and it will properly update the log to reflect the new name. 7499 */ 7500 void btrfs_log_new_name(struct btrfs_trans_handle *trans, 7501 struct dentry *old_dentry, struct btrfs_inode *old_dir, 7502 u64 old_dir_index, struct dentry *parent) 7503 { 7504 struct btrfs_inode *inode = BTRFS_I(d_inode(old_dentry)); 7505 struct btrfs_root *root = inode->root; 7506 struct btrfs_log_ctx ctx; 7507 bool log_pinned = false; 7508 int ret; 7509 7510 /* 7511 * this will force the logging code to walk the dentry chain 7512 * up for the file 7513 */ 7514 if (!S_ISDIR(inode->vfs_inode.i_mode)) 7515 inode->last_unlink_trans = trans->transid; 7516 7517 /* 7518 * if this inode hasn't been logged and directory we're renaming it 7519 * from hasn't been logged, we don't need to log it 7520 */ 7521 ret = inode_logged(trans, inode, NULL); 7522 if (ret < 0) { 7523 goto out; 7524 } else if (ret == 0) { 7525 if (!old_dir) 7526 return; 7527 /* 7528 * If the inode was not logged and we are doing a rename (old_dir is not 7529 * NULL), check if old_dir was logged - if it was not we can return and 7530 * do nothing. 7531 */ 7532 ret = inode_logged(trans, old_dir, NULL); 7533 if (ret < 0) 7534 goto out; 7535 else if (ret == 0) 7536 return; 7537 } 7538 ret = 0; 7539 7540 /* 7541 * If we are doing a rename (old_dir is not NULL) from a directory that 7542 * was previously logged, make sure that on log replay we get the old 7543 * dir entry deleted. This is needed because we will also log the new 7544 * name of the renamed inode, so we need to make sure that after log 7545 * replay we don't end up with both the new and old dir entries existing. 7546 */ 7547 if (old_dir && old_dir->logged_trans == trans->transid) { 7548 struct btrfs_root *log = old_dir->root->log_root; 7549 struct btrfs_path *path; 7550 struct fscrypt_name fname; 7551 7552 ASSERT(old_dir_index >= BTRFS_DIR_START_INDEX); 7553 7554 ret = fscrypt_setup_filename(&old_dir->vfs_inode, 7555 &old_dentry->d_name, 0, &fname); 7556 if (ret) 7557 goto out; 7558 /* 7559 * We have two inodes to update in the log, the old directory and 7560 * the inode that got renamed, so we must pin the log to prevent 7561 * anyone from syncing the log until we have updated both inodes 7562 * in the log. 7563 */ 7564 ret = join_running_log_trans(root); 7565 /* 7566 * At least one of the inodes was logged before, so this should 7567 * not fail, but if it does, it's not serious, just bail out and 7568 * mark the log for a full commit. 7569 */ 7570 if (WARN_ON_ONCE(ret < 0)) { 7571 fscrypt_free_filename(&fname); 7572 goto out; 7573 } 7574 7575 log_pinned = true; 7576 7577 path = btrfs_alloc_path(); 7578 if (!path) { 7579 ret = -ENOMEM; 7580 fscrypt_free_filename(&fname); 7581 goto out; 7582 } 7583 7584 /* 7585 * Other concurrent task might be logging the old directory, 7586 * as it can be triggered when logging other inode that had or 7587 * still has a dentry in the old directory. We lock the old 7588 * directory's log_mutex to ensure the deletion of the old 7589 * name is persisted, because during directory logging we 7590 * delete all BTRFS_DIR_LOG_INDEX_KEY keys and the deletion of 7591 * the old name's dir index item is in the delayed items, so 7592 * it could be missed by an in progress directory logging. 7593 */ 7594 mutex_lock(&old_dir->log_mutex); 7595 ret = del_logged_dentry(trans, log, path, btrfs_ino(old_dir), 7596 &fname.disk_name, old_dir_index); 7597 if (ret > 0) { 7598 /* 7599 * The dentry does not exist in the log, so record its 7600 * deletion. 7601 */ 7602 btrfs_release_path(path); 7603 ret = insert_dir_log_key(trans, log, path, 7604 btrfs_ino(old_dir), 7605 old_dir_index, old_dir_index); 7606 } 7607 mutex_unlock(&old_dir->log_mutex); 7608 7609 btrfs_free_path(path); 7610 fscrypt_free_filename(&fname); 7611 if (ret < 0) 7612 goto out; 7613 } 7614 7615 btrfs_init_log_ctx(&ctx, inode); 7616 ctx.logging_new_name = true; 7617 btrfs_init_log_ctx_scratch_eb(&ctx); 7618 /* 7619 * We don't care about the return value. If we fail to log the new name 7620 * then we know the next attempt to sync the log will fallback to a full 7621 * transaction commit (due to a call to btrfs_set_log_full_commit()), so 7622 * we don't need to worry about getting a log committed that has an 7623 * inconsistent state after a rename operation. 7624 */ 7625 btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx); 7626 free_extent_buffer(ctx.scratch_eb); 7627 ASSERT(list_empty(&ctx.conflict_inodes)); 7628 out: 7629 /* 7630 * If an error happened mark the log for a full commit because it's not 7631 * consistent and up to date or we couldn't find out if one of the 7632 * inodes was logged before in this transaction. Do it before unpinning 7633 * the log, to avoid any races with someone else trying to commit it. 7634 */ 7635 if (ret < 0) 7636 btrfs_set_log_full_commit(trans); 7637 if (log_pinned) 7638 btrfs_end_log_trans(root); 7639 } 7640 7641