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