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