File Index
Files and directories
Sign in
FILE

tree-log.c

File preview with syntax highlighting.

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