^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2008 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/list_sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "misc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "tree-log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "locking.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "print-tree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "backref.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "compression.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "qgroup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "inode-map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "block-group.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "space-info.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* magic values for the inode_only field in btrfs_log_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * LOG_INODE_ALL means to log everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * LOG_INODE_EXISTS means to log just enough to recreate the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * during log replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) LOG_INODE_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) LOG_INODE_EXISTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) LOG_OTHER_INODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) LOG_OTHER_INODE_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * directory trouble cases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * log, we must force a full commit before doing an fsync of the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * where the unlink was done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * ---> record transid of last unlink/rename per directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * mkdir foo/some_dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * normal commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * rename foo/some_dir foo2/some_dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * mkdir foo/some_dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * fsync foo/some_dir/some_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * The fsync above will unlink the original some_dir without recording
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * it in its new location (foo2). After a crash, some_dir will be gone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * unless the fsync of some_file forces a full commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * 2) we must log any new names for any file or dir that is in the fsync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * log. ---> check inode while renaming/linking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * 2a) we must log any new names for any file or dir during rename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * when the directory they are being removed from was logged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * ---> check inode and old parent dir during rename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * 2a is actually the more important variant. With the extra logging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * a crash might unlink the old name without recreating the new one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * 3) after a crash, we must go through any directories with a link count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * of zero and redo the rm -rf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * mkdir f1/foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * normal commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * rm -rf f1/foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * fsync(f1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * The directory f1 was fully removed from the FS, but fsync was never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * called on f1, only its parent dir. After a crash the rm -rf must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * be replayed. This must be able to recurse down the entire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * directory tree. The inode link count fixup code takes care of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * ugly details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * stages for the tree walking. The first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * stage (0) is to only pin down the blocks we find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * the second stage (1) is to make sure that all the inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * we find in the log are created in the subvolume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * The last stage is to deal with directories and links and extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * and all the other fun semantics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) LOG_WALK_PIN_ONLY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) LOG_WALK_REPLAY_INODES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) LOG_WALK_REPLAY_DIR_INDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) LOG_WALK_REPLAY_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static int btrfs_log_inode(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct btrfs_root *root, struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int inode_only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct btrfs_log_ctx *ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct btrfs_path *path, u64 objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct btrfs_root *log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) u64 dirid, int del_all);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * tree logging is a special write ahead log used to make sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * fsyncs and O_SYNCs can happen without doing full tree commits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * Full tree commits are expensive because they require commonly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * modified blocks to be recowed, creating many dirty pages in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * extent tree an 4x-6x higher write load than ext3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * Instead of doing a tree commit on every fsync, we use the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * key ranges and transaction ids to find items for a given file or directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * that have changed in this transaction. Those items are copied into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * a special tree (one per subvolume root), that tree is written to disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * and then the fsync is considered complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * After a crash, items are copied out of the log-tree back into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * subvolume tree. Any file data extents found are recorded in the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * allocation tree, and the log-tree freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * The log tree is read three times, once to pin down all the extents it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * using in ram and once, once to create all the inodes logged in the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * and once to do all the other items.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * start a sub transaction and setup the log tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * this increments the log tree writer count to make the people
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * syncing the tree wait for us to finish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int start_log_trans(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) mutex_lock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (root->log_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (btrfs_need_log_full_commit(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (!root->log_start_pid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) root->log_start_pid = current->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) } else if (root->log_start_pid != current->pid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) mutex_lock(&fs_info->tree_log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!fs_info->log_root_tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ret = btrfs_init_log_root_tree(trans, fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) mutex_unlock(&fs_info->tree_log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ret = btrfs_add_log_tree(trans, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) root->log_start_pid = current->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) atomic_inc(&root->log_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) atomic_inc(&root->log_writers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (ctx && !ctx->logging_new_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int index = root->log_transid % 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) list_add_tail(&ctx->list, &root->log_ctxs[index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ctx->log_transid = root->log_transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * returns 0 if there was a log transaction running and we were able
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * to join, or returns -ENOENT if there were not transactions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * in progress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int join_running_log_trans(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) mutex_lock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (root->log_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) atomic_inc(&root->log_writers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * This either makes the current running log transaction wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * until you call btrfs_end_log_trans() or it makes any future
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * log transactions wait until you call btrfs_end_log_trans()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) void btrfs_pin_log_trans(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) atomic_inc(&root->log_writers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * indicate we're done making changes to the log tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * and wake up anyone waiting to do a sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) void btrfs_end_log_trans(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (atomic_dec_and_test(&root->log_writers)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /* atomic_dec_and_test implies a barrier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) cond_wake_up_nomb(&root->log_writer_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static int btrfs_write_tree_block(struct extent_buffer *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) buf->start + buf->len - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static void btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) filemap_fdatawait_range(buf->pages[0]->mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) buf->start, buf->start + buf->len - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * the walk control struct is used to pass state down the chain when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * processing the log tree. The stage field tells us which part
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * of the log tree processing we are currently doing. The others
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * are state fields used for that specific part
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct walk_control {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /* should we free the extent on disk when done? This is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * at transaction commit time while freeing a log tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) int free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* should we write out the extent buffer? This is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * while flushing the log tree to disk during a sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* should we wait for the extent buffer io to finish? Also used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * while flushing the log tree to disk for a sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) int wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /* pin only walk, we record which extents on disk belong to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * log trees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /* what stage of the replay code we're currently in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) int stage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * Ignore any items from the inode currently being processed. Needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * the LOG_WALK_REPLAY_INODES stage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) bool ignore_cur_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /* the root we are currently replaying */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct btrfs_root *replay_dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /* the trans handle for the current replay */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /* the function that gets used to process blocks we find in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * tree. Note the extent_buffer might not be up to date when it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * passed in, and it must be checked or read if you need the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * inside it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct walk_control *wc, u64 gen, int level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * process_func used to pin down extents, write them or wait on them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static int process_one_buffer(struct btrfs_root *log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct walk_control *wc, u64 gen, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct btrfs_fs_info *fs_info = log->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * If this fs is mixed then we need to be able to process the leaves to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * pin down any logged extents, so we have to read the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) ret = btrfs_read_buffer(eb, gen, level, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (wc->pin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) ret = btrfs_pin_extent_for_log_replay(wc->trans, eb->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) eb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (wc->pin && btrfs_header_level(eb) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) ret = btrfs_exclude_logged_extents(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (wc->write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) btrfs_write_tree_block(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (wc->wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) btrfs_wait_tree_block_writeback(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * Item overwrite used by replay and tree logging. eb, slot and key all refer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * to the src data we are copying out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * root is the tree we are copying into, and path is a scratch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * path for use in this function (it should be released on entry and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * will be released on exit).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * If the key is already in the destination tree the existing item is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * overwritten. If the existing item isn't big enough, it is extended.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * If it is too large, it is truncated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * If the key isn't in the destination yet, a new item is inserted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static noinline int overwrite_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct extent_buffer *eb, int slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct btrfs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) u64 saved_i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int save_old_i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) unsigned long src_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) unsigned long dst_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) int overwrite_root = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) overwrite_root = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) item_size = btrfs_item_size_nr(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) src_ptr = btrfs_item_ptr_offset(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /* look for the key in the destination tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) char *src_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) char *dst_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) u32 dst_size = btrfs_item_size_nr(path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (dst_size != item_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) goto insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (item_size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) dst_copy = kmalloc(item_size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) src_copy = kmalloc(item_size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (!dst_copy || !src_copy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) kfree(dst_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) kfree(src_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) read_extent_buffer(eb, src_copy, src_ptr, item_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) item_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) ret = memcmp(dst_copy, src_copy, item_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) kfree(dst_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) kfree(src_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * they have the same contents, just return, this saves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * us from cowing blocks in the destination tree and doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * extra writes that may not have been done by a previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * We need to load the old nbytes into the inode so when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * replay the extents we've logged we get the right nbytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (inode_item) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) struct btrfs_inode_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) u64 nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) u32 mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) item = btrfs_item_ptr(path->nodes[0], path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) struct btrfs_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) nbytes = btrfs_inode_nbytes(path->nodes[0], item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) item = btrfs_item_ptr(eb, slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct btrfs_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) btrfs_set_inode_nbytes(eb, item, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * If this is a directory we need to reset the i_size to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * 0 so that we can set it up properly when replaying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * the rest of the items in this log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) mode = btrfs_inode_mode(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (S_ISDIR(mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) btrfs_set_inode_size(eb, item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) } else if (inode_item) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct btrfs_inode_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) u32 mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * New inode, set nbytes to 0 so that the nbytes comes out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * properly when we replay the extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) btrfs_set_inode_nbytes(eb, item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * If this is a directory we need to reset the i_size to 0 so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * that we can set it up properly when replaying the rest of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * the items in this log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) mode = btrfs_inode_mode(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (S_ISDIR(mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) btrfs_set_inode_size(eb, item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) insert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) /* try to insert the key into the destination tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) path->skip_release_on_error = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) ret = btrfs_insert_empty_item(trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) key, item_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) path->skip_release_on_error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) /* make sure any existing item is the correct size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (ret == -EEXIST || ret == -EOVERFLOW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) u32 found_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) found_size = btrfs_item_size_nr(path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (found_size > item_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) btrfs_truncate_item(path, item_size, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) else if (found_size < item_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) btrfs_extend_item(path, item_size - found_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) } else if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /* don't overwrite an existing inode if the generation number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * was logged as zero. This is done when the tree logging code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * is just logging an inode to make sure it exists after recovery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * Also, don't overwrite i_size on directories during replay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * log replay inserts and removes directory items based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * state of the tree found in the subvolume, and i_size is modified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * as it goes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) struct btrfs_inode_item *src_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) struct btrfs_inode_item *dst_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) src_item = (struct btrfs_inode_item *)src_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) dst_item = (struct btrfs_inode_item *)dst_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (btrfs_inode_generation(eb, src_item) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) struct extent_buffer *dst_eb = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) const u64 ino_size = btrfs_inode_size(eb, src_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * For regular files an ino_size == 0 is used only when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * logging that an inode exists, as part of a directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * fsync, and the inode wasn't fsynced before. In this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * case don't set the size of the inode in the fs/subvol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * tree, otherwise we would be throwing valid data away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) ino_size != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) btrfs_set_inode_size(dst_eb, dst_item, ino_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) goto no_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (overwrite_root &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) save_old_i_size = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) saved_i_size = btrfs_inode_size(path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) dst_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) copy_extent_buffer(path->nodes[0], eb, dst_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) src_ptr, item_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (save_old_i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct btrfs_inode_item *dst_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) dst_item = (struct btrfs_inode_item *)dst_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) /* make sure the generation is filled in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (key->type == BTRFS_INODE_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) struct btrfs_inode_item *dst_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) dst_item = (struct btrfs_inode_item *)dst_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) btrfs_set_inode_generation(path->nodes[0], dst_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) no_copy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) btrfs_mark_buffer_dirty(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * simple helper to read an inode off the disk from a given root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * This can only be called for subvolume roots and not for the log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) static noinline struct inode *read_one_inode(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) u64 objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) inode = btrfs_iget(root->fs_info->sb, objectid, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /* replays a single extent in 'eb' at 'slot' with 'key' into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * subvolume 'root'. path is released on entry and should be released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) * on exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * extents in the log tree have not been allocated out of the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) * tree yet. So, this completes the allocation, taking a reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) * as required if the extent already exists or creating a new extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) * if it isn't in the extent allocation tree yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * The extent is inserted into the file, dropping any existing extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * from the file that overlap the new one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) struct extent_buffer *eb, int slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) struct btrfs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) int found_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) u64 extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) u64 start = key->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) u64 nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) struct btrfs_file_extent_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) found_type = btrfs_file_extent_type(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (found_type == BTRFS_FILE_EXTENT_REG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) found_type == BTRFS_FILE_EXTENT_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) nbytes = btrfs_file_extent_num_bytes(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) extent_end = start + nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) * We don't add to the inodes nbytes if we are prealloc or a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) * hole.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) size = btrfs_file_extent_ram_bytes(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) nbytes = btrfs_file_extent_ram_bytes(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) extent_end = ALIGN(start + size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) inode = read_one_inode(root, key->objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) * first check to see if we already have this extent in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * file. This must be done before the btrfs_drop_extents run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * so we don't try to drop this extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) ret = btrfs_lookup_file_extent(trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) btrfs_ino(BTRFS_I(inode)), start, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) if (ret == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) (found_type == BTRFS_FILE_EXTENT_REG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) struct btrfs_file_extent_item cmp1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) struct btrfs_file_extent_item cmp2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) struct btrfs_file_extent_item *existing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) existing = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) read_extent_buffer(eb, &cmp1, (unsigned long)item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) sizeof(cmp1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) sizeof(cmp2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * we already have a pointer to this exact extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * we don't have to do anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) /* drop any overlapping extents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (found_type == BTRFS_FILE_EXTENT_REG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) found_type == BTRFS_FILE_EXTENT_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) unsigned long dest_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) struct btrfs_key ins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) btrfs_fs_incompat(fs_info, NO_HOLES))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) goto update_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) ret = btrfs_insert_empty_item(trans, root, path, key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) sizeof(*item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) dest_offset = btrfs_item_ptr_offset(path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) copy_extent_buffer(path->nodes[0], eb, dest_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) (unsigned long)item, sizeof(*item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) ins.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) offset = key->offset - btrfs_file_extent_offset(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) * Manually record dirty extent, as here we did a shallow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) * file extent item copy and skip normal backref update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) * but modifying extent tree all by ourselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * So need to manually record dirty extent for qgroup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * as the owner of the file extent changed from log tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * (doesn't affect qgroup) to fs/file tree(affects qgroup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) ret = btrfs_qgroup_trace_extent(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) btrfs_file_extent_disk_bytenr(eb, item),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) btrfs_file_extent_disk_num_bytes(eb, item),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) if (ins.objectid > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) struct btrfs_ref ref = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) u64 csum_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) u64 csum_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) LIST_HEAD(ordered_sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) * is this extent already allocated in the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) * allocation tree? If so, just add a reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) ins.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) } else if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) btrfs_init_generic_ref(&ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) BTRFS_ADD_DELAYED_REF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) ins.objectid, ins.offset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) btrfs_init_data_ref(&ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) root->root_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) key->objectid, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) ret = btrfs_inc_extent_ref(trans, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * insert the extent pointer in the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * allocation tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) ret = btrfs_alloc_logged_file_extent(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) root->root_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) key->objectid, offset, &ins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) if (btrfs_file_extent_compression(eb, item)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) csum_start = ins.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) csum_end = csum_start + ins.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) csum_start = ins.objectid +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) btrfs_file_extent_offset(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) csum_end = csum_start +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) btrfs_file_extent_num_bytes(eb, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) ret = btrfs_lookup_csums_range(root->log_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) csum_start, csum_end - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) &ordered_sums, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) * Now delete all existing cums in the csum root that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) * cover our range. We do this because we can have an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) * extent that is completely referenced by one file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * extent item and partially referenced by another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) * file extent item (like after using the clone or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * extent_same ioctls). In this case if we end up doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) * the replay of the one that partially references the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) * extent first, and we do not do the csum deletion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) * below, we can get 2 csum items in the csum tree that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) * overlap each other. For example, imagine our log has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) * the two following file extent items:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) * key (257 EXTENT_DATA 409600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) * extent data disk byte 12845056 nr 102400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * extent data offset 20480 nr 20480 ram 102400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * key (257 EXTENT_DATA 819200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) * extent data disk byte 12845056 nr 102400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * extent data offset 0 nr 102400 ram 102400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * Where the second one fully references the 100K extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * that starts at disk byte 12845056, and the log tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) * has a single csum item that covers the entire range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) * of the extent:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) * After the first file extent item is replayed, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) * csum tree gets the following csum item:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) * Which covers the 20K sub-range starting at offset 20K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) * of our extent. Now when we replay the second file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) * extent item, if we do not delete existing csum items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) * that cover any of its blocks, we end up getting two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) * csum items in our csum tree that overlap each other:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * Which is a problem, because after this anyone trying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) * to lookup up for the checksum of any block of our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) * extent starting at an offset of 40K or higher, will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) * end up looking at the second csum item only, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) * does not contain the checksum for any block starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * at offset 40K or higher of our extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) while (!list_empty(&ordered_sums)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) struct btrfs_ordered_sum *sums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) sums = list_entry(ordered_sums.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) struct btrfs_ordered_sum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) ret = btrfs_del_csums(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) fs_info->csum_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) sums->bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) sums->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) ret = btrfs_csum_file_blocks(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) fs_info->csum_root, sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) list_del(&sums->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) kfree(sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) /* inline extents are easy, we just overwrite them */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) ret = overwrite_item(trans, root, path, eb, slot, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) extent_end - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) inode_add_bytes(inode, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) update_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) * when cleaning up conflicts between the directory names in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) * subvolume, directory names in the log and directory names in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) * inode back references, we may have to unlink inodes from directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) * This is a helper function to do the unlink of a specific directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) * item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) struct btrfs_inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) struct btrfs_dir_item *di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) int name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) struct btrfs_key location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) btrfs_dir_item_key_to_cpu(leaf, di, &location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) name_len = btrfs_dir_name_len(leaf, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) name = kmalloc(name_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) inode = read_one_inode(root, location.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) ret = link_to_fixup_dir(trans, root, path, location.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) ret = btrfs_run_delayed_items(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) * See if a given name and sequence number found in an inode back reference are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) * already in a directory and correctly point to this inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) * Returns: < 0 on error, 0 if the directory entry does not exists and 1 if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) * exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) static noinline int inode_in_dir(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) u64 dirid, u64 objectid, u64 index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) const char *name, int name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) struct btrfs_key location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) index, name, name_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) if (IS_ERR(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) if (PTR_ERR(di) != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) ret = PTR_ERR(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) } else if (di) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) if (location.objectid != objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) if (IS_ERR(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) ret = PTR_ERR(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) } else if (di) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) if (location.objectid == objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) * helper function to check a log tree for a named back reference in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) * an inode. This is used to decide if a back reference that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) * found in the subvolume conflicts with what we find in the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) * inode backreferences may have multiple refs in a single item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) * during replay we process one reference at a time, and we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) * want to delete valid links to a file from the subvolume if that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) * link is also in the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) static noinline int backref_in_log(struct btrfs_root *log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) struct btrfs_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) u64 ref_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) const char *name, int namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) } else if (ret == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) if (key->type == BTRFS_INODE_EXTREF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) ref_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) ret = !!btrfs_find_name_in_backref(path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) struct btrfs_root *log_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) struct btrfs_inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) u64 inode_objectid, u64 parent_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) u64 ref_index, char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) int *search_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) char *victim_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) int victim_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) struct btrfs_key search_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) struct btrfs_inode_extref *extref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) /* Search old style refs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) search_key.objectid = inode_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) search_key.type = BTRFS_INODE_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) search_key.offset = parent_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) struct btrfs_inode_ref *victim_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) unsigned long ptr_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) /* are we trying to overwrite a back ref for the root directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) * if so, just jump out, we're done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) if (search_key.objectid == search_key.offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) /* check all the names in this back reference to see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) * if they are in the log. if so, we allow them to stay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) * otherwise they must be unlinked as a conflict
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) while (ptr < ptr_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) victim_ref = (struct btrfs_inode_ref *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) victim_name_len = btrfs_inode_ref_name_len(leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) victim_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) victim_name = kmalloc(victim_name_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) if (!victim_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) read_extent_buffer(leaf, victim_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) (unsigned long)(victim_ref + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) victim_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) ret = backref_in_log(log_root, &search_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) parent_objectid, victim_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) victim_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) kfree(victim_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) } else if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) inc_nlink(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) ret = btrfs_unlink_inode(trans, root, dir, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) victim_name, victim_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) kfree(victim_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) ret = btrfs_run_delayed_items(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) *search_done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) kfree(victim_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) * NOTE: we have searched root tree and checked the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) * corresponding ref, it does not need to check again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) *search_done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) /* Same search but for extended refs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) inode_objectid, parent_objectid, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) if (!IS_ERR_OR_NULL(extref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) u32 cur_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) unsigned long base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) struct inode *victim_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) item_size = btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) base = btrfs_item_ptr_offset(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) while (cur_offset < item_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) extref = (struct btrfs_inode_extref *)(base + cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) victim_name = kmalloc(victim_name_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) if (!victim_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) victim_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) search_key.objectid = inode_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) search_key.type = BTRFS_INODE_EXTREF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) search_key.offset = btrfs_extref_hash(parent_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) victim_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) victim_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) ret = backref_in_log(log_root, &search_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) parent_objectid, victim_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) victim_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) kfree(victim_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) } else if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) victim_parent = read_one_inode(root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) parent_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) if (victim_parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) inc_nlink(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) ret = btrfs_unlink_inode(trans, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) BTRFS_I(victim_parent),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) victim_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) victim_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) ret = btrfs_run_delayed_items(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) iput(victim_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) kfree(victim_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) *search_done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) kfree(victim_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) cur_offset += victim_name_len + sizeof(*extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) *search_done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) /* look for a conflicting sequence number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) ref_index, name, namelen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) if (IS_ERR(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) if (PTR_ERR(di) != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) return PTR_ERR(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) } else if (di) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) ret = drop_one_dir_item(trans, root, path, dir, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) /* look for a conflicting name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) name, namelen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) if (IS_ERR(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) return PTR_ERR(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) } else if (di) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) ret = drop_one_dir_item(trans, root, path, dir, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) u32 *namelen, char **name, u64 *index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) u64 *parent_objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) struct btrfs_inode_extref *extref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) extref = (struct btrfs_inode_extref *)ref_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) *namelen = btrfs_inode_extref_name_len(eb, extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) *name = kmalloc(*namelen, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) if (*name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) read_extent_buffer(eb, *name, (unsigned long)&extref->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) *namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) if (index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) *index = btrfs_inode_extref_index(eb, extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) if (parent_objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) *parent_objectid = btrfs_inode_extref_parent(eb, extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) u32 *namelen, char **name, u64 *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) struct btrfs_inode_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) ref = (struct btrfs_inode_ref *)ref_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) *namelen = btrfs_inode_ref_name_len(eb, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) *name = kmalloc(*namelen, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) if (*name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) if (index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) *index = btrfs_inode_ref_index(eb, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) * Take an inode reference item from the log tree and iterate all names from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) * inode reference item in the subvolume tree with the same key (if it exists).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) * For any name that is not in the inode reference item from the log tree, do a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) * proper unlink of that name (that is, remove its entry from the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) * reference item and both dir index keys).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) struct extent_buffer *log_eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) int log_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) struct btrfs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) unsigned long ref_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) unsigned long ref_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) eb = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) while (ref_ptr < ref_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) int namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) u64 parent_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) if (key->type == BTRFS_INODE_EXTREF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) NULL, &parent_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) parent_id = key->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) if (key->type == BTRFS_INODE_EXTREF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) parent_id, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) ret = !!btrfs_find_name_in_backref(log_eb, log_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) struct inode *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) dir = read_one_inode(root, parent_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) if (!dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) inode, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) iput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) * Whenever we need to check if a name exists or not, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) * check the subvolume tree. So after an unlink we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) * run delayed items, so that future checks for a name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) * during log replay see that the name does not exists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) * anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) ret = btrfs_run_delayed_items(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) ref_ptr += namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) if (key->type == BTRFS_INODE_EXTREF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) ref_ptr += sizeof(struct btrfs_inode_extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) ref_ptr += sizeof(struct btrfs_inode_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) const u8 ref_type, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) const int namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) const u64 parent_id = btrfs_ino(BTRFS_I(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) key.objectid = btrfs_ino(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) key.type = ref_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) if (key.type == BTRFS_INODE_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) key.offset = parent_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) key.offset = btrfs_extref_hash(parent_id, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) if (key.type == BTRFS_INODE_EXTREF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) path->slots[0], parent_id, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) ret = !!btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) static int add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) struct inode *dir, struct inode *inode, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) int namelen, u64 ref_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) struct btrfs_dir_item *dir_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) struct inode *other_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) dir_item = btrfs_lookup_dir_item(NULL, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) btrfs_ino(BTRFS_I(dir)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) name, namelen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) if (!dir_item) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) goto add_link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) } else if (IS_ERR(dir_item)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) ret = PTR_ERR(dir_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) * Our inode's dentry collides with the dentry of another inode which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) * in the log but not yet processed since it has a higher inode number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) * So delete that other dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) btrfs_dir_item_key_to_cpu(path->nodes[0], dir_item, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) other_inode = read_one_inode(root, key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) if (!other_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), BTRFS_I(other_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) * If we dropped the link count to 0, bump it so that later the iput()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) * on the inode will not free it. We will fixup the link count later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) if (other_inode->i_nlink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) inc_nlink(other_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) ret = btrfs_run_delayed_items(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) add_link:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) name, namelen, 0, ref_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) iput(other_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) * replay one inode back reference item found in the log tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) * eb, slot and key refer to the buffer and key found in the log tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) * root is the destination we are replaying into, and path is for temp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) * use by this function. (it should be released on return).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) struct btrfs_root *log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) struct extent_buffer *eb, int slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) struct btrfs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) struct inode *dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) unsigned long ref_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) unsigned long ref_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) int namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) int search_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) int log_ref_ver = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) u64 parent_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) u64 inode_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) u64 ref_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) int ref_struct_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) ref_ptr = btrfs_item_ptr_offset(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) if (key->type == BTRFS_INODE_EXTREF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) struct btrfs_inode_extref *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) ref_struct_size = sizeof(struct btrfs_inode_extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) log_ref_ver = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) r = (struct btrfs_inode_extref *)ref_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) parent_objectid = btrfs_inode_extref_parent(eb, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) ref_struct_size = sizeof(struct btrfs_inode_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) parent_objectid = key->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) inode_objectid = key->objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) * it is possible that we didn't log all the parent directories
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) * for a given inode. If we don't find the dir, just don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) * copy the back ref in. The link count fixup code will take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) * care of the rest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) dir = read_one_inode(root, parent_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) if (!dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) inode = read_one_inode(root, inode_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) while (ref_ptr < ref_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) if (log_ref_ver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) &ref_index, &parent_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) * parent object can change from one array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) * item to another.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) if (!dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) dir = read_one_inode(root, parent_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) if (!dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) &ref_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) ret = inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) btrfs_ino(BTRFS_I(inode)), ref_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) } else if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) * look for a conflicting back reference in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) * metadata. if we find one we have to unlink that name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) * of the file before we add our new link. Later on, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) * overwrite any existing back reference, and we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) * want to create dangling pointers in the directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) if (!search_done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) ret = __add_inode_ref(trans, root, path, log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) BTRFS_I(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) inode_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) parent_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) ref_index, name, namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) &search_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) * If a reference item already exists for this inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) * with the same parent and name, but different index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) * drop it and the corresponding directory index entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) * from the parent before adding the new reference item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) * and dir index entries, otherwise we would fail with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) * -EEXIST returned from btrfs_add_link() below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) ret = btrfs_inode_ref_exists(inode, dir, key->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) ret = btrfs_unlink_inode(trans, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) BTRFS_I(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) * If we dropped the link count to 0, bump it so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) * that later the iput() on the inode will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) * free it. We will fixup the link count later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) if (!ret && inode->i_nlink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) * Whenever we need to check if a name exists or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) * not, we check the subvolume tree. So after an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) * unlink we must run delayed items, so that future
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) * checks for a name during log replay see that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) * name does not exists anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) ret = btrfs_run_delayed_items(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) /* insert our name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) ret = add_link(trans, root, dir, inode, name, namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) ref_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) /* Else, ret == 1, we already have a perfect match, we're done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) if (log_ref_ver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) iput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) * Before we overwrite the inode reference item in the subvolume tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) * with the item from the log tree, we must unlink all names from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) * parent directory that are in the subvolume's tree inode reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) * item, otherwise we end up with an inconsistent subvolume tree where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) * dir index entries exist for a name but there is no inode reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) * item with the same name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) /* finally write the back reference in the inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) ret = overwrite_item(trans, root, path, eb, slot, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) iput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) static int insert_orphan_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) struct btrfs_root *root, u64 ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) ret = btrfs_insert_orphan_item(trans, root, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) if (ret == -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) static int count_inode_extrefs(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) struct btrfs_inode *inode, struct btrfs_path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) int name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) unsigned int nlink = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) u32 cur_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) u64 inode_objectid = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) u64 offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) struct btrfs_inode_extref *extref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) &extref, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) item_size = btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) cur_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) while (cur_offset < item_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) name_len = btrfs_inode_extref_name_len(leaf, extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) nlink++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) cur_offset += name_len + sizeof(*extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) if (ret < 0 && ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) return nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) static int count_inode_refs(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) struct btrfs_inode *inode, struct btrfs_path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) unsigned int nlink = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) unsigned long ptr_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) int name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) key.type = BTRFS_INODE_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) if (path->slots[0] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) process_slot:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) btrfs_item_key_to_cpu(path->nodes[0], &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) if (key.objectid != ino ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) key.type != BTRFS_INODE_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) while (ptr < ptr_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) struct btrfs_inode_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) ref = (struct btrfs_inode_ref *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) name_len = btrfs_inode_ref_name_len(path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) ptr = (unsigned long)(ref + 1) + name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) nlink++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) if (key.offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) if (path->slots[0] > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) goto process_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) key.offset--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) return nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) * There are a few corners where the link count of the file can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) * be properly maintained during replay. So, instead of adding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) * lots of complexity to the log code, we just scan the backrefs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) * for any file that has been through replay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) * The scan will update the link count on the inode to reflect the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) * number of back refs found. If it goes down to zero, the iput
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) * will free the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) u64 nlink = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) u64 ino = btrfs_ino(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) ret = count_inode_refs(root, BTRFS_I(inode), path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) nlink = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) ret = count_inode_extrefs(root, BTRFS_I(inode), path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) nlink += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) if (nlink != inode->i_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) set_nlink(inode, nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) BTRFS_I(inode)->index_cnt = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) if (inode->i_nlink == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) ret = replay_dir_deletes(trans, root, NULL, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) ino, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) ret = insert_orphan_item(trans, root, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) struct btrfs_path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) key.type = BTRFS_ORPHAN_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) if (ret == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) if (path->slots[0] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) key.type != BTRFS_ORPHAN_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) ret = btrfs_del_item(trans, root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) inode = read_one_inode(root, key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) ret = fixup_inode_link_count(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) * fixup on a directory may create new entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) * make sure we always look for the highset possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) * offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) * record a given inode in the fixup dir so we can check its link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) * count when replay is done. The link count is incremented here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) * so the inode won't go away until we check it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) u64 objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) inode = read_one_inode(root, objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) key.type = BTRFS_ORPHAN_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) key.offset = objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) if (!inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) set_nlink(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) } else if (ret == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) * when replaying the log for a directory, we only insert names
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) * for inodes that actually exist. This means an fsync on a directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) * does not implicitly fsync all the new files in it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) static noinline int insert_one_name(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) u64 dirid, u64 index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) char *name, int name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) struct btrfs_key *location)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) struct inode *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) inode = read_one_inode(root, location->objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) dir = read_one_inode(root, dirid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) if (!dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) name_len, 1, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) /* FIXME, put inode into FIXUP list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) iput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) * take a single entry in a log directory item and replay it into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) * the subvolume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) * if a conflicting item exists in the subdirectory already,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) * the inode it points to is unlinked and put into the link count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) * fix up tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) * If a name from the log points to a file or directory that does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) * not exist in the FS, it is skipped. fsyncs on directories
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) * do not force down inodes inside that directory, just changes to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) * names or unlinks in a directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) * non-existing inode) and 1 if the name was replayed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) static noinline int replay_one_name(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) struct btrfs_dir_item *di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) struct btrfs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) int name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) struct btrfs_dir_item *dst_di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) struct btrfs_key log_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) struct inode *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) u8 log_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) bool exists;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) bool name_added = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) dir = read_one_inode(root, key->objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) if (!dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) name_len = btrfs_dir_name_len(eb, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) name = kmalloc(name_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) log_type = btrfs_dir_type(eb, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) read_extent_buffer(eb, name, (unsigned long)(di + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) btrfs_dir_item_key_to_cpu(eb, di, &log_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) ret = btrfs_lookup_inode(trans, root, path, &log_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) exists = (ret == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) if (key->type == BTRFS_DIR_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) name, name_len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) } else if (key->type == BTRFS_DIR_INDEX_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) dst_di = btrfs_lookup_dir_index_item(trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) key->objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) key->offset, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) name_len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) /* Corruption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) if (dst_di == ERR_PTR(-ENOENT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) dst_di = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) if (IS_ERR(dst_di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) ret = PTR_ERR(dst_di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) } else if (!dst_di) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) /* we need a sequence number to insert, so we only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) * do inserts for the BTRFS_DIR_INDEX_KEY types
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) if (key->type != BTRFS_DIR_INDEX_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) goto insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) /* the existing item matches the logged item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) if (found_key.objectid == log_key.objectid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) found_key.type == log_key.type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) found_key.offset == log_key.offset &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) update_size = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) * don't drop the conflicting directory entry if the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) * for the new entry doesn't exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) if (!exists)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) if (key->type == BTRFS_DIR_INDEX_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) goto insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) if (!ret && update_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) ret = btrfs_update_inode(trans, root, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) iput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) if (!ret && name_added)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) insert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) * Check if the inode reference exists in the log for the given name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) * inode and parent inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) found_key.objectid = log_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) found_key.type = BTRFS_INODE_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) found_key.offset = key->objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) ret = backref_in_log(root->log_root, &found_key, 0, name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) } else if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) /* The dentry will be added later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) update_size = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) found_key.objectid = log_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) found_key.type = BTRFS_INODE_EXTREF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) found_key.offset = key->objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) ret = backref_in_log(root->log_root, &found_key, key->objectid, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) } else if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) /* The dentry will be added later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) update_size = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) ret = insert_one_name(trans, root, key->objectid, key->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) name, name_len, &log_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) if (ret && ret != -ENOENT && ret != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) name_added = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) update_size = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) * find all the names in a directory item and reconcile them into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) * one name in a directory item, but the same code gets used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) * both directory index types
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) struct extent_buffer *eb, int slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) struct btrfs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) u32 item_size = btrfs_item_size_nr(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) int name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) unsigned long ptr_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) struct btrfs_path *fixup_path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) ptr = btrfs_item_ptr_offset(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) ptr_end = ptr + item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) while (ptr < ptr_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) di = (struct btrfs_dir_item *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) name_len = btrfs_dir_name_len(eb, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) ret = replay_one_name(trans, root, path, eb, di, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) ptr = (unsigned long)(di + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) ptr += name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) * If this entry refers to a non-directory (directories can not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) * have a link count > 1) and it was added in the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) * that was not committed, make sure we fixup the link count of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) * the inode it the entry points to. Otherwise something like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) * the following would result in a directory pointing to an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) * inode with a wrong link that does not account for this dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) * entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) * mkdir testdir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) * touch testdir/foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) * touch testdir/bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) * sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) * ln testdir/bar testdir/bar_link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) * ln testdir/foo testdir/foo_link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) * xfs_io -c "fsync" testdir/bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) * <power failure>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) * mount fs, log replay happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) * File foo would remain with a link count of 1 when it has two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) * entries pointing to it in the directory testdir. This would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) * make it impossible to ever delete the parent directory has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) * it would result in stale dentries that can never be deleted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) struct btrfs_key di_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) if (!fixup_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) fixup_path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) if (!fixup_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) btrfs_dir_item_key_to_cpu(eb, di, &di_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) ret = link_to_fixup_dir(trans, root, fixup_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) di_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) btrfs_free_path(fixup_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) * directory replay has two parts. There are the standard directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) * items in the log copied from the subvolume, and range items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) * created in the log while the subvolume was logged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) * The range items tell us which parts of the key space the log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) * is authoritative for. During replay, if a key in the subvolume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) * directory is in a logged range item, but not actually in the log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) * that means it was deleted from the directory before the fsync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) * and should be removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) static noinline int find_dir_range(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) u64 dirid, int key_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) u64 *start_ret, u64 *end_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) u64 found_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) struct btrfs_dir_log_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) int nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) if (*start_ret == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) key.objectid = dirid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) key.type = key_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) key.offset = *start_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) if (path->slots[0] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) if (key.type != key_type || key.objectid != dirid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) item = btrfs_item_ptr(path->nodes[0], path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) struct btrfs_dir_log_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) found_end = btrfs_dir_log_end(path->nodes[0], item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) if (*start_ret >= key.offset && *start_ret <= found_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) *start_ret = key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) *end_ret = found_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) /* check the next slot in the tree to see if it is a valid item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) nritems = btrfs_header_nritems(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) if (path->slots[0] >= nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) if (key.type != key_type || key.objectid != dirid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) item = btrfs_item_ptr(path->nodes[0], path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) struct btrfs_dir_log_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) found_end = btrfs_dir_log_end(path->nodes[0], item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) *start_ret = key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) *end_ret = found_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) * this looks for a given directory item in the log. If the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) * item is not in the log, the item is removed and the inode it points
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) * to is unlinked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) struct btrfs_root *log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) struct btrfs_path *log_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) struct btrfs_key *dir_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) struct btrfs_dir_item *log_di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) int name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) unsigned long ptr_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) struct btrfs_key location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) eb = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) item_size = btrfs_item_size_nr(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) ptr = btrfs_item_ptr_offset(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) ptr_end = ptr + item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) while (ptr < ptr_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) di = (struct btrfs_dir_item *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) name_len = btrfs_dir_name_len(eb, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) name = kmalloc(name_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) read_extent_buffer(eb, name, (unsigned long)(di + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) log_di = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) log_di = btrfs_lookup_dir_item(trans, log, log_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) dir_key->objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) name, name_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) log_di = btrfs_lookup_dir_index_item(trans, log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) log_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) dir_key->objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) dir_key->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) name, name_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) if (!log_di || log_di == ERR_PTR(-ENOENT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) btrfs_dir_item_key_to_cpu(eb, di, &location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) btrfs_release_path(log_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) inode = read_one_inode(root, location.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) ret = link_to_fixup_dir(trans, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) path, location.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) BTRFS_I(inode), name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) ret = btrfs_run_delayed_items(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) /* there might still be more names under this key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) * check and repeat if required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) ret = btrfs_search_slot(NULL, root, dir_key, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) } else if (IS_ERR(log_di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) return PTR_ERR(log_di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) btrfs_release_path(log_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) ptr = (unsigned long)(di + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) ptr += name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) btrfs_release_path(log_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) struct btrfs_root *log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) const u64 ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) struct btrfs_key search_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) struct btrfs_path *log_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) int nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) log_path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) if (!log_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) search_key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) search_key.type = BTRFS_XATTR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) search_key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) process_leaf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) nritems = btrfs_header_nritems(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) for (i = path->slots[0]; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) struct btrfs_dir_item *log_di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) u32 total_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) u32 cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) btrfs_item_key_to_cpu(path->nodes[0], &key, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) total_size = btrfs_item_size_nr(path->nodes[0], i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) cur = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) while (cur < total_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) u32 this_len = sizeof(*di) + name_len + data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) name = kmalloc(name_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) read_extent_buffer(path->nodes[0], name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) (unsigned long)(di + 1), name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) name, name_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) btrfs_release_path(log_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) if (!log_di) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) /* Doesn't exist in log tree, so delete it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) di = btrfs_lookup_xattr(trans, root, path, ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) name, name_len, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) if (IS_ERR(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) ret = PTR_ERR(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) ASSERT(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) ret = btrfs_delete_one_dir_name(trans, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) path, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) search_key = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) if (IS_ERR(log_di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) ret = PTR_ERR(log_di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) cur += this_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) di = (struct btrfs_dir_item *)((char *)di + this_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) else if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) goto process_leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) btrfs_free_path(log_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) * deletion replay happens before we copy any new directory items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) * out of the log or out of backreferences from inodes. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) * scans the log to find ranges of keys that log is authoritative for,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) * and then scans the directory to find items in those ranges that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) * not present in the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) * Anything we don't find in the log is unlinked and removed from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) * directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) struct btrfs_root *log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) u64 dirid, int del_all)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) u64 range_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) u64 range_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) int key_type = BTRFS_DIR_LOG_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) struct btrfs_key dir_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) struct btrfs_path *log_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) struct inode *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) dir_key.objectid = dirid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) dir_key.type = BTRFS_DIR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) log_path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) if (!log_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) dir = read_one_inode(root, dirid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) /* it isn't an error if the inode isn't there, that can happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) * because we replay the deletes before we copy in the inode item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) * from the log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) if (!dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) btrfs_free_path(log_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) range_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) range_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) if (del_all)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) range_end = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) ret = find_dir_range(log, path, dirid, key_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) &range_start, &range_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) dir_key.offset = range_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) int nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) ret = btrfs_search_slot(NULL, root, &dir_key, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) nritems = btrfs_header_nritems(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) if (path->slots[0] >= nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) else if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) btrfs_item_key_to_cpu(path->nodes[0], &found_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) if (found_key.objectid != dirid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) found_key.type != dir_key.type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) goto next_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) if (found_key.offset > range_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) ret = check_item_in_log(trans, root, log, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) log_path, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) &found_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) if (found_key.offset == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) dir_key.offset = found_key.offset + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) if (range_end == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) range_start = range_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) next_type:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) key_type = BTRFS_DIR_LOG_INDEX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) dir_key.type = BTRFS_DIR_INDEX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) btrfs_free_path(log_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) iput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) * the process_func used to replay items from the log tree. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) * gets called in two different stages. The first stage just looks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) * for inodes and makes sure they are all copied into the subvolume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) * The second stage copies all the other item types from the log into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) * the subvolume. The two stage approach is slower, but gets rid of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) * lots of complexity around inodes referencing other inodes that exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) * only in the log (references come from either directory items or inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) * back refs).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) struct walk_control *wc, u64 gen, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) int nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) struct btrfs_root *root = wc->replay_dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) ret = btrfs_read_buffer(eb, gen, level, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) level = btrfs_header_level(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) if (level != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) nritems = btrfs_header_nritems(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) for (i = 0; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) btrfs_item_key_to_cpu(eb, &key, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) /* inode keys are done during the first stage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) if (key.type == BTRFS_INODE_ITEM_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) wc->stage == LOG_WALK_REPLAY_INODES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) struct btrfs_inode_item *inode_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) u32 mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) inode_item = btrfs_item_ptr(eb, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) struct btrfs_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) * If we have a tmpfile (O_TMPFILE) that got fsync'ed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) * and never got linked before the fsync, skip it, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) * replaying it is pointless since it would be deleted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) * later. We skip logging tmpfiles, but it's always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) * possible we are replaying a log created with a kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) * that used to log tmpfiles.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) if (btrfs_inode_nlink(eb, inode_item) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) wc->ignore_cur_inode = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) wc->ignore_cur_inode = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) ret = replay_xattr_deletes(wc->trans, root, log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) path, key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) mode = btrfs_inode_mode(eb, inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) if (S_ISDIR(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) ret = replay_dir_deletes(wc->trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) root, log, path, key.objectid, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) ret = overwrite_item(wc->trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) eb, i, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) * Before replaying extents, truncate the inode to its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) * size. We need to do it now and not after log replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) * because before an fsync we can have prealloc extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) * added beyond the inode's i_size. If we did it after,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) * through orphan cleanup for example, we would drop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) * those prealloc extents just after replaying them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) if (S_ISREG(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) u64 from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) inode = read_one_inode(root, key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) from = ALIGN(i_size_read(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) root->fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) ret = btrfs_drop_extents(wc->trans, root, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) from, (u64)-1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) /* Update the inode's nbytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) ret = btrfs_update_inode(wc->trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) ret = link_to_fixup_dir(wc->trans, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) path, key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) if (wc->ignore_cur_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) if (key.type == BTRFS_DIR_INDEX_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) ret = replay_one_dir_item(wc->trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) eb, i, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) if (wc->stage < LOG_WALK_REPLAY_ALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) /* these keys are simply copied */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) if (key.type == BTRFS_XATTR_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) ret = overwrite_item(wc->trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) eb, i, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) } else if (key.type == BTRFS_INODE_REF_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) key.type == BTRFS_INODE_EXTREF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) ret = add_inode_ref(wc->trans, root, log, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) eb, i, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) if (ret && ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) ret = replay_one_extent(wc->trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) eb, i, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) } else if (key.type == BTRFS_DIR_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) ret = replay_one_dir_item(wc->trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) eb, i, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) * Correctly adjust the reserved bytes occupied by a log tree extent buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) static void unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) struct btrfs_block_group *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) cache = btrfs_lookup_block_group(fs_info, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) if (!cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) btrfs_err(fs_info, "unable to find block group for %llu", start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) spin_lock(&cache->space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) spin_lock(&cache->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) cache->reserved -= fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) cache->space_info->bytes_reserved -= fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) spin_unlock(&cache->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) spin_unlock(&cache->space_info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) btrfs_put_block_group(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) struct btrfs_path *path, int *level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) struct walk_control *wc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) u64 bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) u64 ptr_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) struct extent_buffer *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) struct extent_buffer *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) u32 blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) while (*level > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) struct btrfs_key first_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) cur = path->nodes[*level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) WARN_ON(btrfs_header_level(cur) != *level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) if (path->slots[*level] >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) btrfs_header_nritems(cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) blocksize = fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) next = btrfs_find_create_tree_block(fs_info, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) if (IS_ERR(next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) return PTR_ERR(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) if (*level == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) ret = wc->process_func(root, next, wc, ptr_gen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) *level - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) free_extent_buffer(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) path->slots[*level]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) if (wc->free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) ret = btrfs_read_buffer(next, ptr_gen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) *level - 1, &first_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) free_extent_buffer(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) if (trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) btrfs_tree_lock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) btrfs_set_lock_blocking_write(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) btrfs_clean_tree_block(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) btrfs_wait_tree_block_writeback(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) btrfs_tree_unlock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) ret = btrfs_pin_reserved_extent(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) bytenr, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) free_extent_buffer(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) clear_extent_buffer_dirty(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) unaccount_log_buffer(fs_info, bytenr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) free_extent_buffer(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) free_extent_buffer(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) if (path->nodes[*level-1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) free_extent_buffer(path->nodes[*level-1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) path->nodes[*level-1] = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) *level = btrfs_header_level(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) path->slots[*level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) struct btrfs_path *path, int *level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) struct walk_control *wc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) slot = path->slots[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) path->slots[i]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) *level = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) WARN_ON(*level == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) ret = wc->process_func(root, path->nodes[*level], wc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) btrfs_header_generation(path->nodes[*level]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) *level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) if (wc->free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) struct extent_buffer *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) next = path->nodes[*level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) if (trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) btrfs_tree_lock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) btrfs_set_lock_blocking_write(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) btrfs_clean_tree_block(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) btrfs_wait_tree_block_writeback(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) btrfs_tree_unlock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) ret = btrfs_pin_reserved_extent(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) path->nodes[*level]->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) path->nodes[*level]->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) clear_extent_buffer_dirty(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) unaccount_log_buffer(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) path->nodes[*level]->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) free_extent_buffer(path->nodes[*level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) path->nodes[*level] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) *level = i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) * drop the reference count on the tree rooted at 'snap'. This traverses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) * the tree freeing any blocks that have a ref count of zero after being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) * decremented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) static int walk_log_tree(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) struct btrfs_root *log, struct walk_control *wc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) struct btrfs_fs_info *fs_info = log->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) int wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) int orig_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) level = btrfs_header_level(log->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) orig_level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) path->nodes[level] = log->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) atomic_inc(&log->node->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) path->slots[level] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) wret = walk_down_log_tree(trans, log, path, &level, wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) if (wret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) if (wret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) wret = walk_up_log_tree(trans, log, path, &level, wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) if (wret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) if (wret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) ret = wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) /* was the root node processed? if not, catch it here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) if (path->nodes[orig_level]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) ret = wc->process_func(log, path->nodes[orig_level], wc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) btrfs_header_generation(path->nodes[orig_level]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) orig_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) if (wc->free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) struct extent_buffer *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) next = path->nodes[orig_level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) if (trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) btrfs_tree_lock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) btrfs_set_lock_blocking_write(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) btrfs_clean_tree_block(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) btrfs_wait_tree_block_writeback(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) btrfs_tree_unlock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) ret = btrfs_pin_reserved_extent(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) next->start, next->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) clear_extent_buffer_dirty(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) unaccount_log_buffer(fs_info, next->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) * helper function to update the item for a given subvolumes log root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) * in the tree of log roots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) static int update_log_root(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) struct btrfs_root *log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) struct btrfs_root_item *root_item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) struct btrfs_fs_info *fs_info = log->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) if (log->log_transid == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) /* insert root item on the first sync */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) ret = btrfs_insert_root(trans, fs_info->log_root_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) &log->root_key, root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) ret = btrfs_update_root(trans, fs_info->log_root_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) &log->root_key, root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) static void wait_log_commit(struct btrfs_root *root, int transid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) int index = transid % 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) * we only allow two pending log transactions at a time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) * so we know that if ours is more than 2 older than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) * current transaction, we're done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) prepare_to_wait(&root->log_commit_wait[index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) &wait, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) if (!(root->log_transid_committed < transid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) atomic_read(&root->log_commit[index])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) mutex_lock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) finish_wait(&root->log_commit_wait[index], &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) static void wait_for_writer(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) prepare_to_wait(&root->log_writer_wait, &wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) if (!atomic_read(&root->log_writers))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) mutex_lock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) finish_wait(&root->log_writer_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) mutex_lock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) list_del_init(&ctx->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) * Invoked in log mutex context, or be sure there is no other task which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) * can access the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) int index, int error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) struct btrfs_log_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) struct btrfs_log_ctx *safe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) list_del_init(&ctx->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) ctx->log_ret = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) INIT_LIST_HEAD(&root->log_ctxs[index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) * btrfs_sync_log does sends a given tree log down to the disk and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) * updates the super blocks to record it. When this call is done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) * you know that any inodes previously logged are safely on disk only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) * if it returns 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) * Any other return value means you need to call btrfs_commit_transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) * Some of the edge cases for fsyncing directories that have had unlinks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) * or renames done in the past mean that sometimes the only safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) * that has happened.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) int btrfs_sync_log(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) struct btrfs_root *root, struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) int index1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) int index2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) int mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) struct btrfs_root *log = root->log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) struct btrfs_root *log_root_tree = fs_info->log_root_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) struct btrfs_root_item new_root_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) int log_transid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) struct btrfs_log_ctx root_log_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) struct blk_plug plug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) mutex_lock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) log_transid = ctx->log_transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) if (root->log_transid_committed >= log_transid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) return ctx->log_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) index1 = log_transid % 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) if (atomic_read(&root->log_commit[index1])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) wait_log_commit(root, log_transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) return ctx->log_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) ASSERT(log_transid == root->log_transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) atomic_set(&root->log_commit[index1], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) /* wait for previous tree log sync to complete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) wait_log_commit(root, log_transid - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) int batch = atomic_read(&root->log_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) /* when we're on an ssd, just kick the log commit out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) if (!btrfs_test_opt(fs_info, SSD) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) schedule_timeout_uninterruptible(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) mutex_lock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) wait_for_writer(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) if (batch == atomic_read(&root->log_batch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) /* bail out if we need to do a full commit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) if (btrfs_need_log_full_commit(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) if (log_transid % 2 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) mark = EXTENT_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) mark = EXTENT_NEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) /* we start IO on all the marked extents here, but we don't actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) * wait for them until later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) blk_start_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) * We _must_ update under the root->log_mutex in order to make sure we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) * have a consistent view of the log root we are trying to commit at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) * this moment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) * We _must_ copy this into a local copy, because we are not holding the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) * log_root_tree->log_mutex yet. This is important because when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) * commit the log_root_tree we must have a consistent view of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) * log_root_tree when we update the super block to point at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) * log_root_tree bytenr. If we update the log_root_tree here we'll race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) * with the commit and possibly point at the new block which we may not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) * have written out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) btrfs_set_root_node(&log->root_item, log->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) root->log_transid++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) log->log_transid = root->log_transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) root->log_start_pid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) * IO has been started, blocks of the log tree have WRITTEN flag set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) * in their headers. new modifications of the log will be written to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) * new positions. so it's safe to allow log writers to go in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) btrfs_init_log_ctx(&root_log_ctx, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) mutex_lock(&log_root_tree->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) index2 = log_root_tree->log_transid % 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) root_log_ctx.log_transid = log_root_tree->log_transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) * Now we are safe to update the log_root_tree because we're under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) * log_mutex, and we're a current writer so we're holding the commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) * open until we drop the log_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) ret = update_log_root(trans, log, &new_root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) if (!list_empty(&root_log_ctx.list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) list_del_init(&root_log_ctx.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) if (ret != -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) mutex_unlock(&log_root_tree->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) btrfs_wait_tree_log_extents(log, mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) mutex_unlock(&log_root_tree->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) list_del_init(&root_log_ctx.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) mutex_unlock(&log_root_tree->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) ret = root_log_ctx.log_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) index2 = root_log_ctx.log_transid % 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) if (atomic_read(&log_root_tree->log_commit[index2])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) ret = btrfs_wait_tree_log_extents(log, mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) wait_log_commit(log_root_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) root_log_ctx.log_transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) mutex_unlock(&log_root_tree->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) ret = root_log_ctx.log_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) atomic_set(&log_root_tree->log_commit[index2], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) wait_log_commit(log_root_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) root_log_ctx.log_transid - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) * now that we've moved on to the tree of log tree roots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) * check the full commit flag again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) if (btrfs_need_log_full_commit(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) btrfs_wait_tree_log_extents(log, mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) mutex_unlock(&log_root_tree->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) goto out_wake_log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) ret = btrfs_write_marked_extents(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) &log_root_tree->dirty_log_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) EXTENT_DIRTY | EXTENT_NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) mutex_unlock(&log_root_tree->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) goto out_wake_log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) ret = btrfs_wait_tree_log_extents(log, mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) ret = btrfs_wait_tree_log_extents(log_root_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) EXTENT_NEW | EXTENT_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) mutex_unlock(&log_root_tree->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) goto out_wake_log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) btrfs_set_super_log_root(fs_info->super_for_commit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) log_root_tree->node->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) btrfs_set_super_log_root_level(fs_info->super_for_commit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) btrfs_header_level(log_root_tree->node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) log_root_tree->log_transid++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) mutex_unlock(&log_root_tree->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) * Nobody else is going to jump in and write the ctree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) * super here because the log_commit atomic below is protecting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) * us. We must be called with a transaction handle pinning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) * the running transaction open, so a full commit can't hop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) * in and cause problems either.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) ret = write_all_supers(fs_info, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) goto out_wake_log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) mutex_lock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) if (root->last_log_commit < log_transid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) root->last_log_commit = log_transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) out_wake_log_root:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) mutex_lock(&log_root_tree->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) log_root_tree->log_transid_committed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) atomic_set(&log_root_tree->log_commit[index2], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) mutex_unlock(&log_root_tree->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) * The barrier before waitqueue_active (in cond_wake_up) is needed so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) * all the updates above are seen by the woken threads. It might not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) * necessary, but proving that seems to be hard.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) cond_wake_up(&log_root_tree->log_commit_wait[index2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) mutex_lock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) btrfs_remove_all_log_ctxs(root, index1, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) root->log_transid_committed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) atomic_set(&root->log_commit[index1], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) mutex_unlock(&root->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) * The barrier before waitqueue_active (in cond_wake_up) is needed so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) * all the updates above are seen by the woken threads. It might not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) * necessary, but proving that seems to be hard.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) cond_wake_up(&root->log_commit_wait[index1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) static void free_log_tree(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) struct btrfs_root *log)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) struct walk_control wc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) .free = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) .process_func = process_one_buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) ret = walk_log_tree(trans, log, &wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) if (trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) btrfs_handle_fs_error(log->fs_info, ret, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) extent_io_tree_release(&log->log_csum_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) btrfs_put_root(log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) * free all the extents used by the tree log. This should be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) * at commit time of the full transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) if (root->log_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) free_log_tree(trans, root->log_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) root->log_root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) if (fs_info->log_root_tree) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) free_log_tree(trans, fs_info->log_root_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) fs_info->log_root_tree = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) * Check if an inode was logged in the current transaction. We can't always rely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) * on an inode's logged_trans value, because it's an in-memory only field and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) * therefore not persisted. This means that its value is lost if the inode gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) * evicted and loaded again from disk (in which case it has a value of 0, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) * certainly it is smaller then any possible transaction ID), when that happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) * the full_sync flag is set in the inode's runtime flags, so on that case we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) * assume eviction happened and ignore the logged_trans value, assuming the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) * worst case, that the inode was logged before in the current transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) static bool inode_logged(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) if (inode->logged_trans == trans->transid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) if (inode->last_trans == trans->transid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) !test_bit(BTRFS_FS_LOG_RECOVERING, &trans->fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) * If both a file and directory are logged, and unlinks or renames are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) * mixed in, we have a few interesting corners:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) * create file X in dir Y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) * link file X to X.link in dir Y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) * fsync file X
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) * unlink file X but leave X.link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) * fsync dir Y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) * After a crash we would expect only X.link to exist. But file X
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) * didn't get fsync'd again so the log has back refs for X and X.link.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) * We solve this by removing directory entries and inode backrefs from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) * log when a file that was logged in the current transaction is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) * unlinked. Any later fsync will include the updated log entries, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) * we'll be able to reconstruct the proper directory items from backrefs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) * This optimizations allows us to avoid relogging the entire inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) * or the entire directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) const char *name, int name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) struct btrfs_inode *dir, u64 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) struct btrfs_root *log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) int bytes_del = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) u64 dir_ino = btrfs_ino(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) if (!inode_logged(trans, dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) ret = join_running_log_trans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) mutex_lock(&dir->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) log = root->log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) name, name_len, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) if (IS_ERR(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) err = PTR_ERR(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) if (di) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) ret = btrfs_delete_one_dir_name(trans, log, path, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) bytes_del += name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) index, name, name_len, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) if (IS_ERR(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) err = PTR_ERR(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) if (di) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) ret = btrfs_delete_one_dir_name(trans, log, path, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) bytes_del += name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) /* update the directory size in the log to reflect the names
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) * we have removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) if (bytes_del) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) key.objectid = dir_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) key.type = BTRFS_INODE_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) struct btrfs_inode_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) u64 i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) item = btrfs_item_ptr(path->nodes[0], path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) struct btrfs_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) i_size = btrfs_inode_size(path->nodes[0], item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) if (i_size > bytes_del)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) i_size -= bytes_del;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) btrfs_set_inode_size(path->nodes[0], item, i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) btrfs_mark_buffer_dirty(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) mutex_unlock(&dir->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) if (err == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) } else if (err < 0 && err != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) /* ENOENT can be returned if the entry hasn't been fsynced yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) btrfs_abort_transaction(trans, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) btrfs_end_log_trans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) /* see comments for btrfs_del_dir_entries_in_log */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) const char *name, int name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) struct btrfs_inode *inode, u64 dirid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) struct btrfs_root *log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) u64 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) if (!inode_logged(trans, inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) ret = join_running_log_trans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) log = root->log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) mutex_lock(&inode->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) dirid, &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) mutex_unlock(&inode->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) if (ret == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) } else if (ret < 0 && ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) btrfs_end_log_trans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) * creates a range item in the log for 'dirid'. first_offset and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) * last_offset tell us which parts of the key space the log should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) * be considered authoritative for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) struct btrfs_root *log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) int key_type, u64 dirid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) u64 first_offset, u64 last_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) struct btrfs_dir_log_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) key.objectid = dirid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) key.offset = first_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) if (key_type == BTRFS_DIR_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) key.type = BTRFS_DIR_LOG_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) key.type = BTRFS_DIR_LOG_INDEX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) item = btrfs_item_ptr(path->nodes[0], path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) struct btrfs_dir_log_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) btrfs_mark_buffer_dirty(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) * log all the items included in the current transaction for a given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) * directory. This also creates the range items in the log tree required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) * to replay anything deleted before the fsync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) static noinline int log_dir_items(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) struct btrfs_root *root, struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) struct btrfs_path *dst_path, int key_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) struct btrfs_log_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) u64 min_offset, u64 *last_offset_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) struct btrfs_key min_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) struct btrfs_root *log = root->log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) struct extent_buffer *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) int nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) u64 first_offset = min_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) u64 last_offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) log = root->log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) min_key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) min_key.type = key_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) min_key.offset = min_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) ret = btrfs_search_forward(root, &min_key, path, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) * we didn't find anything from this transaction, see if there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) * is anything at all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) min_key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) min_key.type = key_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) min_key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) ret = btrfs_previous_item(root, path, ino, key_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) /* if ret == 0 there are items for this type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) * create a range to tell us the last key of this type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) * otherwise, there are no items in this directory after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) * *min_offset, and we create a range to indicate that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) struct btrfs_key tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) btrfs_item_key_to_cpu(path->nodes[0], &tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) if (key_type == tmp.type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) first_offset = max(min_offset, tmp.offset) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) /* go backward to find any previous key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) ret = btrfs_previous_item(root, path, ino, key_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) struct btrfs_key tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) if (key_type == tmp.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) first_offset = tmp.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) ret = overwrite_item(trans, log, dst_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) path->nodes[0], path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) * Find the first key from this transaction again. See the note for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) * log_new_dir_dentries, if we're logging a directory recursively we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) * won't be holding its i_mutex, which means we can modify the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) * while we're logging it. If we remove an entry between our first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) * search and this search we'll not find the key again and can just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) * bail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) search:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) * we have a block from this transaction, log every item in it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) * from our directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) struct btrfs_key tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) src = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) nritems = btrfs_header_nritems(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) for (i = path->slots[0]; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) btrfs_item_key_to_cpu(src, &min_key, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) if (min_key.objectid != ino || min_key.type != key_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) if (need_resched()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) goto search;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) ret = overwrite_item(trans, log, dst_path, src, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) &min_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) * We must make sure that when we log a directory entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) * the corresponding inode, after log replay, has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) * matching link count. For example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) * touch foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) * mkdir mydir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) * sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) * ln foo mydir/bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) * xfs_io -c "fsync" mydir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) * <crash>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) * <mount fs and log replay>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) * Would result in a fsync log that when replayed, our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) * file inode would have a link count of 1, but we get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) * two directory entries pointing to the same inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) * After removing one of the names, it would not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) * possible to remove the other name, which resulted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) * always in stale file handle errors, and would not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) * be possible to rmdir the parent directory, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) * its i_size could never decrement to the value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) btrfs_dir_item_key_to_cpu(src, di, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) if (ctx &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) (btrfs_dir_transid(src, di) == trans->transid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) tmp.type != BTRFS_ROOT_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) ctx->log_new_dentries = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) path->slots[0] = nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) * look ahead to the next item and see if it is also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) * from this directory and from this transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) last_offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) if (tmp.objectid != ino || tmp.type != key_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) last_offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) ret = overwrite_item(trans, log, dst_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) path->nodes[0], path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) last_offset = tmp.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) btrfs_release_path(dst_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) *last_offset_ret = last_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) * insert the log range keys to indicate where the log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) * is valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) ret = insert_dir_log_key(trans, log, path, key_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) ino, first_offset, last_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) * logging directories is very similar to logging inodes, We find all the items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) * from the current transaction and write them to the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) * The recovery code scans the directory in the subvolume, and if it finds a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) * key in the range logged that is not present in the log tree, then it means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) * that dir entry was unlinked during the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) * In order for that scan to work, we must include one key smaller than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) * the smallest logged by this transaction and one key larger than the largest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) * key logged by this transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) struct btrfs_root *root, struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) struct btrfs_path *dst_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) u64 min_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) u64 max_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) int key_type = BTRFS_DIR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) min_key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) max_key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) ctx, min_key, &max_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) if (max_key == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) min_key = max_key + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) if (key_type == BTRFS_DIR_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) key_type = BTRFS_DIR_INDEX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) * a helper function to drop items from the log before we relog an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) * inode. max_key_type indicates the highest item type to remove.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) * This cannot be run for file data extents because it does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) * free the extents they point to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) static int drop_objectid_items(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) struct btrfs_root *log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) u64 objectid, int max_key_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) int start_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) key.objectid = objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) key.type = max_key_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) BUG_ON(ret == 0); /* Logic error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) if (path->slots[0] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) btrfs_item_key_to_cpu(path->nodes[0], &found_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) if (found_key.objectid != objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) found_key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) found_key.type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) ret = btrfs_bin_search(path->nodes[0], &found_key, &start_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) ret = btrfs_del_items(trans, log, path, start_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) path->slots[0] - start_slot + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) * If start slot isn't 0 then we don't need to re-search, we've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) * found the last guy with the objectid in this tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) if (ret || start_slot != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) static void fill_inode_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) struct extent_buffer *leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) struct btrfs_inode_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) struct inode *inode, int log_inode_only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) u64 logged_isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) struct btrfs_map_token token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) btrfs_init_map_token(&token, leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) if (log_inode_only) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) /* set the generation to zero so the recover code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) * can tell the difference between an logging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) * just to say 'this inode exists' and a logging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) * to say 'update this inode with these values'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) btrfs_set_token_inode_generation(&token, item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) btrfs_set_token_inode_size(&token, item, logged_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) btrfs_set_token_inode_generation(&token, item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) BTRFS_I(inode)->generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) btrfs_set_token_inode_size(&token, item, inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) btrfs_set_token_inode_mode(&token, item, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) btrfs_set_token_timespec_sec(&token, &item->atime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) inode->i_atime.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) btrfs_set_token_timespec_nsec(&token, &item->atime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) inode->i_atime.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) btrfs_set_token_timespec_sec(&token, &item->mtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) inode->i_mtime.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) btrfs_set_token_timespec_nsec(&token, &item->mtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) inode->i_mtime.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) btrfs_set_token_timespec_sec(&token, &item->ctime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) inode->i_ctime.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) btrfs_set_token_timespec_nsec(&token, &item->ctime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) inode->i_ctime.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) btrfs_set_token_inode_transid(&token, item, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) btrfs_set_token_inode_block_group(&token, item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) static int log_inode_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) struct btrfs_root *log, struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) struct btrfs_inode_item *inode_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) ret = btrfs_insert_empty_item(trans, log, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) &inode->location, sizeof(*inode_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) if (ret && ret != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) struct btrfs_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) static int log_csums(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) struct btrfs_root *log_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) struct btrfs_ordered_sum *sums)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) const u64 lock_end = sums->bytenr + sums->len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) * If this inode was not used for reflink operations in the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) * transaction with new extents, then do the fast path, no need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) * worry about logging checksum items with overlapping ranges.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) if (inode->last_reflink_trans < trans->transid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) return btrfs_csum_file_blocks(trans, log_root, sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) * Serialize logging for checksums. This is to avoid racing with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) * same checksum being logged by another task that is logging another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) * file which happens to refer to the same extent as well. Such races
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) * can leave checksum items in the log with overlapping ranges.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) ret = lock_extent_bits(&log_root->log_csum_range, sums->bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) lock_end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) * Due to extent cloning, we might have logged a csum item that covers a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) * subrange of a cloned extent, and later we can end up logging a csum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) * item for a larger subrange of the same extent or the entire range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) * This would leave csum items in the log tree that cover the same range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) * and break the searches for checksums in the log tree, resulting in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) * some checksums missing in the fs/subvolume tree. So just delete (or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) * trim and adjust) any existing csum items in the log for this range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) ret = btrfs_csum_file_blocks(trans, log_root, sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) unlock_extent_cached(&log_root->log_csum_range, sums->bytenr, lock_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) static noinline int copy_items(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) struct btrfs_path *dst_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) struct btrfs_path *src_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) int start_slot, int nr, int inode_only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) u64 logged_isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) unsigned long src_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) unsigned long dst_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) struct btrfs_root *log = inode->root->log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) struct btrfs_file_extent_item *extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) struct btrfs_inode_item *inode_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) struct extent_buffer *src = src_path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) struct btrfs_key *ins_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) u32 *ins_sizes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) char *ins_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) struct list_head ordered_sums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) INIT_LIST_HEAD(&ordered_sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) nr * sizeof(u32), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) if (!ins_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) ins_sizes = (u32 *)ins_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) ret = btrfs_insert_empty_items(trans, log, dst_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) ins_keys, ins_sizes, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) kfree(ins_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) for (i = 0; i < nr; i++, dst_path->slots[0]++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) dst_path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) src_offset = btrfs_item_ptr_offset(src, start_slot + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) inode_item = btrfs_item_ptr(dst_path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) dst_path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) struct btrfs_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) fill_inode_item(trans, dst_path->nodes[0], inode_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) &inode->vfs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) inode_only == LOG_INODE_EXISTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) logged_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) src_offset, ins_sizes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) /* take a reference on file data extents so that truncates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) * or deletes of this inode don't have to relog the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) * again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) !skip_csum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) int found_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) extent = btrfs_item_ptr(src, start_slot + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) if (btrfs_file_extent_generation(src, extent) < trans->transid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) found_type = btrfs_file_extent_type(src, extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) if (found_type == BTRFS_FILE_EXTENT_REG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) u64 ds, dl, cs, cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) ds = btrfs_file_extent_disk_bytenr(src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) /* ds == 0 is a hole */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) if (ds == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) dl = btrfs_file_extent_disk_num_bytes(src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) cs = btrfs_file_extent_offset(src, extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) cl = btrfs_file_extent_num_bytes(src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) if (btrfs_file_extent_compression(src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) extent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) cs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) cl = dl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) ret = btrfs_lookup_csums_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) fs_info->csum_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) ds + cs, ds + cs + cl - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) &ordered_sums, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) btrfs_mark_buffer_dirty(dst_path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) btrfs_release_path(dst_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) kfree(ins_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) * we have to do this after the loop above to avoid changing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) * log tree while trying to change the log tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) while (!list_empty(&ordered_sums)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) struct btrfs_ordered_sum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) ret = log_csums(trans, inode, log, sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) list_del(&sums->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) kfree(sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) struct extent_map *em1, *em2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) em1 = list_entry(a, struct extent_map, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) em2 = list_entry(b, struct extent_map, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) if (em1->start < em2->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) else if (em1->start > em2->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) static int log_extent_csums(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) struct btrfs_root *log_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) const struct extent_map *em,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) u64 csum_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) u64 csum_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) u64 mod_start = em->mod_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) u64 mod_len = em->mod_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) LIST_HEAD(ordered_sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) if (inode->flags & BTRFS_INODE_NODATASUM ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) em->block_start == EXTENT_MAP_HOLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) list_for_each_entry(ordered, &ctx->ordered_extents, log_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) const u64 ordered_end = ordered->file_offset + ordered->num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) const u64 mod_end = mod_start + mod_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) struct btrfs_ordered_sum *sums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) if (mod_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) if (ordered_end <= mod_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) if (mod_end <= ordered->file_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) * We are going to copy all the csums on this ordered extent, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) * go ahead and adjust mod_start and mod_len in case this ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) * extent has already been logged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) if (ordered->file_offset > mod_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) if (ordered_end >= mod_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) mod_len = ordered->file_offset - mod_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) * If we have this case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) * |--------- logged extent ---------|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) * |----- ordered extent ----|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) * Just don't mess with mod_start and mod_len, we'll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) * just end up logging more csums than we need and it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) * will be ok.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) if (ordered_end < mod_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) mod_len = mod_end - ordered_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) mod_start = ordered_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) mod_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) * To keep us from looping for the above case of an ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) * extent that falls inside of the logged extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) list_for_each_entry(sums, &ordered->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) ret = log_csums(trans, inode, log_root, sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) /* We're done, found all csums in the ordered extents. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) if (mod_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) /* If we're compressed we have to save the entire range of csums. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) if (em->compress_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) csum_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) csum_len = max(em->block_len, em->orig_block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) csum_offset = mod_start - em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) csum_len = mod_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) /* block start is already adjusted for the file extent offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) em->block_start + csum_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) em->block_start + csum_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) csum_len - 1, &ordered_sums, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) while (!list_empty(&ordered_sums)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) struct btrfs_ordered_sum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) ret = log_csums(trans, inode, log_root, sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) list_del(&sums->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) kfree(sums);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) static int log_one_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) struct btrfs_inode *inode, struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) const struct extent_map *em,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) struct btrfs_root *log = root->log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) struct btrfs_map_token token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) u64 extent_offset = em->start - em->orig_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) u64 block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) int extent_inserted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) ret = log_extent_csums(trans, inode, log, em, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) em->start + em->len, NULL, 0, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) sizeof(*fi), &extent_inserted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) if (!extent_inserted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) key.objectid = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) key.type = BTRFS_EXTENT_DATA_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) key.offset = em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) ret = btrfs_insert_empty_item(trans, log, path, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) sizeof(*fi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) btrfs_init_map_token(&token, leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) btrfs_set_token_file_extent_generation(&token, fi, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) btrfs_set_token_file_extent_type(&token, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) BTRFS_FILE_EXTENT_PREALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) btrfs_set_token_file_extent_type(&token, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) BTRFS_FILE_EXTENT_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) block_len = max(em->block_len, em->orig_block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) if (em->compress_type != BTRFS_COMPRESS_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) btrfs_set_token_file_extent_disk_bytenr(&token, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) em->block_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) btrfs_set_token_file_extent_disk_bytenr(&token, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) em->block_start -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) extent_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) btrfs_set_token_file_extent_disk_bytenr(&token, fi, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) btrfs_set_token_file_extent_disk_num_bytes(&token, fi, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) btrfs_set_token_file_extent_offset(&token, fi, extent_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) btrfs_set_token_file_extent_num_bytes(&token, fi, em->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) btrfs_set_token_file_extent_ram_bytes(&token, fi, em->ram_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) btrfs_set_token_file_extent_compression(&token, fi, em->compress_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) btrfs_set_token_file_extent_encryption(&token, fi, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) btrfs_set_token_file_extent_other_encoding(&token, fi, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) * Log all prealloc extents beyond the inode's i_size to make sure we do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) * lose them after doing a full/fast fsync and replaying the log. We scan the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) * subvolume's root instead of iterating the inode's extent map tree because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) * otherwise we can log incorrect extent items based on extent map conversion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) * That can happen due to the fact that extent maps are merged when they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) * are not in the extent map tree's list of modified extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) struct btrfs_path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) const u64 i_size = i_size_read(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) const u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) struct btrfs_path *dst_path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) bool dropped_extents = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) u64 truncate_offset = i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) int ins_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) int start_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) if (!(inode->flags & BTRFS_INODE_PREALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) key.type = BTRFS_EXTENT_DATA_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) key.offset = i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) * We must check if there is a prealloc extent that starts before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) * i_size and crosses the i_size boundary. This is to ensure later we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) * truncate down to the end of that extent and not to the i_size, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) * otherwise we end up losing part of the prealloc extent after a log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) * replay and with an implicit hole if there is another prealloc extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) * that starts at an offset beyond i_size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) struct btrfs_file_extent_item *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369) if (btrfs_file_extent_type(leaf, ei) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) BTRFS_FILE_EXTENT_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) u64 extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) extent_end = key.offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) btrfs_file_extent_num_bytes(leaf, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) if (extent_end > i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) truncate_offset = extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) if (slot >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) if (ins_nr > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) ret = copy_items(trans, inode, dst_path, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) start_slot, ins_nr, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) ins_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) if (key.objectid > ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) if (WARN_ON_ONCE(key.objectid < ino) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) key.type < BTRFS_EXTENT_DATA_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) key.offset < i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) if (!dropped_extents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) * Avoid logging extent items logged in past fsync calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) * and leading to duplicate keys in the log tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) ret = btrfs_truncate_inode_items(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) root->log_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) &inode->vfs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) truncate_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) BTRFS_EXTENT_DATA_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) } while (ret == -EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) dropped_extents = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) if (ins_nr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) start_slot = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) ins_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) if (!dst_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) dst_path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) if (!dst_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) if (ins_nr > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) ret = copy_items(trans, inode, dst_path, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) start_slot, ins_nr, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) btrfs_free_path(dst_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) struct btrfs_ordered_extent *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) struct extent_map *em, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) struct list_head extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) struct extent_map_tree *tree = &inode->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) u64 test_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) int num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) INIT_LIST_HEAD(&extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) write_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) test_gen = root->fs_info->last_trans_committed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) list_del_init(&em->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475) * Just an arbitrary number, this can be really CPU intensive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) * once we start getting a lot of extents, and really once we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) * have a bunch of extents we just want to commit since it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) * be faster.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) if (++num > 32768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) list_del_init(&tree->modified_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) ret = -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) goto process;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) if (em->generation <= test_gen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) /* We log prealloc extents beyond eof later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491) em->start >= i_size_read(&inode->vfs_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) /* Need a ref to keep it from getting evicted from cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) refcount_inc(&em->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) set_bit(EXTENT_FLAG_LOGGING, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) list_add_tail(&em->list, &extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) num++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) list_sort(NULL, &extents, extent_cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) process:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) while (!list_empty(&extents)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) em = list_entry(extents.next, struct extent_map, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) list_del_init(&em->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) * If we had an error we just need to delete everybody from our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) * private list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513) clear_em_logging(tree, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518) write_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) ret = log_one_extent(trans, inode, root, em, path, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) write_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) clear_em_logging(tree, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) WARN_ON(!list_empty(&extents));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526) write_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) ret = btrfs_log_prealloc_extents(trans, inode, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) * We have logged all extents successfully, now make sure the commit of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) * the current transaction waits for the ordered extents to complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537) * before it commits and wipes out the log trees, otherwise we would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538) * lose data if an ordered extents completes after the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) * commits and a power failure happens after the transaction commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541) list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) list_del_init(&ordered->log_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546) spin_lock_irq(&inode->ordered_tree.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) atomic_inc(&trans->transaction->pending_ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) spin_unlock_irq(&inode->ordered_tree.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) struct btrfs_path *path, u64 *size_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) key.objectid = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) key.type = BTRFS_INODE_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567) key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569) ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) } else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) *size_ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) struct btrfs_inode_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577) item = btrfs_item_ptr(path->nodes[0], path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) struct btrfs_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579) *size_ret = btrfs_inode_size(path->nodes[0], item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) * If the in-memory inode's i_size is smaller then the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582) * size stored in the btree, return the inode's i_size, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583) * that we get a correct inode size after replaying the log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) * when before a power failure we had a shrinking truncate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) * followed by addition of a new name (rename / new hard link).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) * Otherwise return the inode size from the btree, to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) * data loss when replaying a log due to previously doing a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) * write that expands the inode's size and logging a new name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) * immediately after.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) if (*size_ret > inode->vfs_inode.i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) *size_ret = inode->vfs_inode.i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600) * At the moment we always log all xattrs. This is to figure out at log replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) * time which xattrs must have their deletion replayed. If a xattr is missing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) * in the log tree and exists in the fs/subvol tree, we delete it. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) * because if a xattr is deleted, the inode is fsynced and a power failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604) * happens, causing the log to be replayed the next time the fs is mounted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) * we want the xattr to not exist anymore (same behaviour as other filesystems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) * with a journal, ext3/4, xfs, f2fs, etc).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612) struct btrfs_path *dst_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616) const u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617) int ins_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618) int start_slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) bool found_xattrs = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624) key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) key.type = BTRFS_XATTR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) int slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) struct extent_buffer *leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) int nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) if (slot >= nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) if (ins_nr > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639) ret = copy_items(trans, inode, dst_path, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) start_slot, ins_nr, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) ins_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654) if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657) if (ins_nr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658) start_slot = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659) ins_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) found_xattrs = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664) if (ins_nr > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) ret = copy_items(trans, inode, dst_path, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666) start_slot, ins_nr, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) if (!found_xattrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678) * When using the NO_HOLES feature if we punched a hole that causes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) * deletion of entire leafs or all the extent items of the first leaf (the one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680) * that contains the inode item and references) we may end up not processing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) * any extents, because there are no leafs with a generation matching the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) * current transaction that have extent items for our inode. So we need to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) * if any holes exist and then log them. We also need to log holes after any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) * truncate operation that changes the inode's size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) static int btrfs_log_holes(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689) struct btrfs_path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693) const u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) const u64 i_size = i_size_read(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) u64 prev_extent_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701) key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702) key.type = BTRFS_EXTENT_DATA_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703) key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709) while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710) struct extent_buffer *leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712) if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) /* We have a hole, log it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728) if (prev_extent_end < key.offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) const u64 hole_len = key.offset - prev_extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732) * Release the path to avoid deadlocks with other code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733) * paths that search the root while holding locks on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) * leafs from the log root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737) ret = btrfs_insert_file_extent(trans, root->log_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) ino, prev_extent_end, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) 0, hole_len, 0, hole_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740) 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745) * Search for the same key again in the root. Since it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) * an extent item and we are holding the inode lock, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747) * key must still exist. If it doesn't just emit warning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) * and return an error to fall back to a transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749) * commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754) if (WARN_ON(ret > 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759) prev_extent_end = btrfs_file_extent_end(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) if (prev_extent_end < i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765) u64 hole_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768) hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769) ret = btrfs_insert_file_extent(trans, root->log_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770) ino, prev_extent_end, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771) hole_len, 0, hole_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772) 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) * When we are logging a new inode X, check if it doesn't have a reference that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782) * matches the reference from some other inode Y created in a past transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783) * and that was renamed in the current transaction. If we don't do this, then at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) * log replay time we can lose inode Y (and all its files if it's a directory):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) * mkdir /mnt/x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787) * echo "hello world" > /mnt/x/foobar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) * sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789) * mv /mnt/x /mnt/y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) * mkdir /mnt/x # or touch /mnt/x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791) * xfs_io -c fsync /mnt/x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792) * <power fail>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) * mount fs, trigger log replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795) * After the log replay procedure, we would lose the first directory and all its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796) * files (file foobar).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797) * For the case where inode Y is not a directory we simply end up losing it:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799) * echo "123" > /mnt/foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800) * sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801) * mv /mnt/foo /mnt/bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802) * echo "abc" > /mnt/foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803) * xfs_io -c fsync /mnt/foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804) * <power fail>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806) * We also need this for cases where a snapshot entry is replaced by some other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807) * entry (file or directory) otherwise we end up with an unreplayable log due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808) * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4809) * if it were a regular entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4810) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4811) * mkdir /mnt/x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4812) * btrfs subvolume snapshot /mnt /mnt/x/snap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4813) * btrfs subvolume delete /mnt/x/snap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4814) * rmdir /mnt/x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4815) * mkdir /mnt/x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4816) * fsync /mnt/x or fsync some new file inside it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4817) * <power fail>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4818) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4819) * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4820) * the same transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4821) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4822) static int btrfs_check_ref_name_override(struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4823) const int slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4824) const struct btrfs_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4825) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4826) u64 *other_ino, u64 *other_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4828) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4829) struct btrfs_path *search_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4830) char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4831) u32 name_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4832) u32 item_size = btrfs_item_size_nr(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4833) u32 cur_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4834) unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4836) search_path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4837) if (!search_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4838) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4839) search_path->search_commit_root = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4840) search_path->skip_locking = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4842) while (cur_offset < item_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4843) u64 parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4844) u32 this_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4845) u32 this_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4846) unsigned long name_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4847) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4849) if (key->type == BTRFS_INODE_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4850) struct btrfs_inode_ref *iref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4852) iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4853) parent = key->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4854) this_name_len = btrfs_inode_ref_name_len(eb, iref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4855) name_ptr = (unsigned long)(iref + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4856) this_len = sizeof(*iref) + this_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4857) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4858) struct btrfs_inode_extref *extref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4860) extref = (struct btrfs_inode_extref *)(ptr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4861) cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4862) parent = btrfs_inode_extref_parent(eb, extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4863) this_name_len = btrfs_inode_extref_name_len(eb, extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4864) name_ptr = (unsigned long)&extref->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4865) this_len = sizeof(*extref) + this_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4868) if (this_name_len > name_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4869) char *new_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4871) new_name = krealloc(name, this_name_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4872) if (!new_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4873) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4874) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4876) name_len = this_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4877) name = new_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4880) read_extent_buffer(eb, name, name_ptr, this_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4881) di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4882) parent, name, this_name_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4883) if (di && !IS_ERR(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4884) struct btrfs_key di_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4886) btrfs_dir_item_key_to_cpu(search_path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4887) di, &di_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4888) if (di_key.type == BTRFS_INODE_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4889) if (di_key.objectid != key->objectid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4890) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4891) *other_ino = di_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4892) *other_parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4893) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4894) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4896) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4897) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4899) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4900) } else if (IS_ERR(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4901) ret = PTR_ERR(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4902) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4904) btrfs_release_path(search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4906) cur_offset += this_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4908) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4909) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4910) btrfs_free_path(search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4911) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4912) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4915) struct btrfs_ino_list {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4916) u64 ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4917) u64 parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4918) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4919) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4921) static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4922) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4923) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4924) struct btrfs_log_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4925) u64 ino, u64 parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4927) struct btrfs_ino_list *ino_elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4928) LIST_HEAD(inode_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4929) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4931) ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4932) if (!ino_elem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4933) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4934) ino_elem->ino = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4935) ino_elem->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4936) list_add_tail(&ino_elem->list, &inode_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4938) while (!list_empty(&inode_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4939) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4940) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4941) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4943) ino_elem = list_first_entry(&inode_list, struct btrfs_ino_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4944) list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4945) ino = ino_elem->ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4946) parent = ino_elem->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4947) list_del(&ino_elem->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4948) kfree(ino_elem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4949) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4950) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4952) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4954) inode = btrfs_iget(fs_info->sb, ino, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4955) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4956) * If the other inode that had a conflicting dir entry was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4957) * deleted in the current transaction, we need to log its parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4958) * directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4959) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4960) if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4961) ret = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4962) if (ret == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4963) inode = btrfs_iget(fs_info->sb, parent, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4964) if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4965) ret = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4966) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4967) ret = btrfs_log_inode(trans, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4968) BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4969) LOG_OTHER_INODE_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4970) ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4971) btrfs_add_delayed_iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4974) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4976) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4977) * If the inode was already logged skip it - otherwise we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4978) * hit an infinite loop. Example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4979) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4980) * From the commit root (previous transaction) we have the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4981) * following inodes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4982) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4983) * inode 257 a directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4984) * inode 258 with references "zz" and "zz_link" on inode 257
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4985) * inode 259 with reference "a" on inode 257
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4986) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4987) * And in the current (uncommitted) transaction we have:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4988) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4989) * inode 257 a directory, unchanged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4990) * inode 258 with references "a" and "a2" on inode 257
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4991) * inode 259 with reference "zz_link" on inode 257
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4992) * inode 261 with reference "zz" on inode 257
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4993) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4994) * When logging inode 261 the following infinite loop could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4995) * happen if we don't skip already logged inodes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4996) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4997) * - we detect inode 258 as a conflicting inode, with inode 261
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4998) * on reference "zz", and log it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4999) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5000) * - we detect inode 259 as a conflicting inode, with inode 258
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5001) * on reference "a", and log it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5002) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5003) * - we detect inode 258 as a conflicting inode, with inode 259
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5004) * on reference "zz_link", and log it - again! After this we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5005) * repeat the above steps forever.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5006) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5007) spin_lock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5008) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5009) * Check the inode's logged_trans only instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5010) * btrfs_inode_in_log(). This is because the last_log_commit of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5011) * the inode is not updated when we only log that it exists and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5012) * it has the full sync bit set (see btrfs_log_inode()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5013) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5014) if (BTRFS_I(inode)->logged_trans == trans->transid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5015) spin_unlock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5016) btrfs_add_delayed_iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5017) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5019) spin_unlock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5020) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5021) * We are safe logging the other inode without acquiring its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5022) * lock as long as we log with the LOG_INODE_EXISTS mode. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5023) * are safe against concurrent renames of the other inode as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5024) * well because during a rename we pin the log and update the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5025) * log with the new name before we unpin it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5026) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5027) ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5028) LOG_OTHER_INODE, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5029) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5030) btrfs_add_delayed_iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5031) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5034) key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5035) key.type = BTRFS_INODE_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5036) key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5037) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5038) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5039) btrfs_add_delayed_iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5040) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5041) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5043) while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5044) struct extent_buffer *leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5045) int slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5046) u64 other_ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5047) u64 other_parent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5049) if (slot >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5050) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5051) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5052) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5053) } else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5054) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5055) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5057) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5060) btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5061) if (key.objectid != ino ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5062) (key.type != BTRFS_INODE_REF_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5063) key.type != BTRFS_INODE_EXTREF_KEY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5064) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5065) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5066) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5068) ret = btrfs_check_ref_name_override(leaf, slot, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5069) BTRFS_I(inode), &other_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5070) &other_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5071) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5072) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5073) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5074) ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5075) if (!ino_elem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5076) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5077) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5079) ino_elem->ino = other_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5080) ino_elem->parent = other_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5081) list_add_tail(&ino_elem->list, &inode_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5082) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5083) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5084) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5086) btrfs_add_delayed_iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5089) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5092) static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5093) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5094) struct btrfs_key *min_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5095) const struct btrfs_key *max_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5096) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5097) struct btrfs_path *dst_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5098) const u64 logged_isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5099) const bool recursive_logging,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5100) const int inode_only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5101) struct btrfs_log_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5102) bool *need_log_inode_item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5104) const u64 i_size = i_size_read(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5105) struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5106) int ins_start_slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5107) int ins_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5108) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5110) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5111) ret = btrfs_search_forward(root, min_key, path, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5112) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5113) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5114) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5115) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5116) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5118) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5119) /* Note, ins_nr might be > 0 here, cleanup outside the loop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5120) if (min_key->objectid != max_key->objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5121) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5122) if (min_key->type > max_key->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5123) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5125) if (min_key->type == BTRFS_INODE_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5126) *need_log_inode_item = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5127) } else if (min_key->type == BTRFS_EXTENT_DATA_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5128) min_key->offset >= i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5129) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5130) * Extents at and beyond eof are logged with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5131) * btrfs_log_prealloc_extents().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5132) * Only regular files have BTRFS_EXTENT_DATA_KEY keys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5133) * and no keys greater than that, so bail out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5135) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5136) } else if ((min_key->type == BTRFS_INODE_REF_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5137) min_key->type == BTRFS_INODE_EXTREF_KEY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5138) inode->generation == trans->transid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5139) !recursive_logging) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5140) u64 other_ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5141) u64 other_parent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5143) ret = btrfs_check_ref_name_override(path->nodes[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5144) path->slots[0], min_key, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5145) &other_ino, &other_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5146) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5147) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5148) } else if (ret > 0 && ctx &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5149) other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5150) if (ins_nr > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5151) ins_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5152) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5153) ins_nr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5154) ins_start_slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5156) ret = copy_items(trans, inode, dst_path, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5157) ins_start_slot, ins_nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5158) inode_only, logged_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5159) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5160) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5161) ins_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5163) ret = log_conflicting_inodes(trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5164) ctx, other_ino, other_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5165) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5166) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5167) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5168) goto next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5170) } else if (min_key->type == BTRFS_XATTR_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5171) /* Skip xattrs, logged later with btrfs_log_all_xattrs() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5172) if (ins_nr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5173) goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5174) ret = copy_items(trans, inode, dst_path, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5175) ins_start_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5176) ins_nr, inode_only, logged_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5177) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5178) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5179) ins_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5180) goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5183) if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5184) ins_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5185) goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5186) } else if (!ins_nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5187) ins_start_slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5188) ins_nr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5189) goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5192) ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5193) ins_nr, inode_only, logged_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5194) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5195) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5196) ins_nr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5197) ins_start_slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5198) next_slot:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5199) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5200) if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5201) btrfs_item_key_to_cpu(path->nodes[0], min_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5202) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5203) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5205) if (ins_nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5206) ret = copy_items(trans, inode, dst_path, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5207) ins_start_slot, ins_nr, inode_only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5208) logged_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5209) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5210) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5211) ins_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5213) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5214) next_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5215) if (min_key->offset < (u64)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5216) min_key->offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5217) } else if (min_key->type < max_key->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5218) min_key->type++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5219) min_key->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5220) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5221) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5224) if (ins_nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5225) ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5226) ins_nr, inode_only, logged_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5227) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5228) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5231) if (inode_only == LOG_INODE_ALL && S_ISREG(inode->vfs_inode.i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5232) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5233) * Release the path because otherwise we might attempt to double
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5234) * lock the same leaf with btrfs_log_prealloc_extents() below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5235) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5236) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5237) ret = btrfs_log_prealloc_extents(trans, inode, dst_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5240) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5243) /* log a single inode in the tree log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5244) * At least one parent directory for this inode must exist in the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5245) * or be logged already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5246) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5247) * Any items from this inode changed by the current transaction are copied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5248) * to the log tree. An extra reference is taken on any extents in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5249) * file, allowing us to avoid a whole pile of corner cases around logging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5250) * blocks that have been removed from the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5251) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5252) * See LOG_INODE_ALL and related defines for a description of what inode_only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5253) * does.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5254) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5255) * This handles both files and directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5257) static int btrfs_log_inode(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5258) struct btrfs_root *root, struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5259) int inode_only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5260) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5262) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5263) struct btrfs_path *dst_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5264) struct btrfs_key min_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5265) struct btrfs_key max_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5266) struct btrfs_root *log = root->log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5267) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5268) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5269) bool fast_search = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5270) u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5271) struct extent_map_tree *em_tree = &inode->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5272) u64 logged_isize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5273) bool need_log_inode_item = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5274) bool xattrs_logged = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5275) bool recursive_logging = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5277) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5278) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5279) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5280) dst_path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5281) if (!dst_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5282) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5283) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5286) min_key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5287) min_key.type = BTRFS_INODE_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5288) min_key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5290) max_key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5293) /* today the code can only do partial logging of directories */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5294) if (S_ISDIR(inode->vfs_inode.i_mode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5295) (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5296) &inode->runtime_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5297) inode_only >= LOG_INODE_EXISTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5298) max_key.type = BTRFS_XATTR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5299) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5300) max_key.type = (u8)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5301) max_key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5303) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5304) * Only run delayed items if we are a directory. We want to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5305) * all directory indexes hit the fs/subvolume tree so we can find them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5306) * and figure out which index ranges have to be logged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5307) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5308) * Otherwise commit the delayed inode only if the full sync flag is set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5309) * as we want to make sure an up to date version is in the subvolume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5310) * tree so copy_inode_items_to_log() / copy_items() can find it and copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5311) * it to the log tree. For a non full sync, we always log the inode item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5312) * based on the in-memory struct btrfs_inode which is always up to date.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5313) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5314) if (S_ISDIR(inode->vfs_inode.i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5315) ret = btrfs_commit_inode_delayed_items(trans, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5316) else if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5317) ret = btrfs_commit_inode_delayed_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5319) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5320) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5321) btrfs_free_path(dst_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5322) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5325) if (inode_only == LOG_OTHER_INODE || inode_only == LOG_OTHER_INODE_ALL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5326) recursive_logging = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5327) if (inode_only == LOG_OTHER_INODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5328) inode_only = LOG_INODE_EXISTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5329) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5330) inode_only = LOG_INODE_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5331) mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5332) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5333) mutex_lock(&inode->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5336) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5337) * a brute force approach to making sure we get the most uptodate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5338) * copies of everything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5339) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5340) if (S_ISDIR(inode->vfs_inode.i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5341) int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5343) if (inode_only == LOG_INODE_EXISTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5344) max_key_type = BTRFS_XATTR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5345) ret = drop_objectid_items(trans, log, path, ino, max_key_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5346) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5347) if (inode_only == LOG_INODE_EXISTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5348) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5349) * Make sure the new inode item we write to the log has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5350) * the same isize as the current one (if it exists).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5351) * This is necessary to prevent data loss after log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5352) * replay, and also to prevent doing a wrong expanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5353) * truncate - for e.g. create file, write 4K into offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5354) * 0, fsync, write 4K into offset 4096, add hard link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5355) * fsync some other file (to sync log), power fail - if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5356) * we use the inode's current i_size, after log replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5357) * we get a 8Kb file, with the last 4Kb extent as a hole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5358) * (zeroes), as if an expanding truncate happened,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5359) * instead of getting a file of 4Kb only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5360) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5361) err = logged_inode_size(log, inode, path, &logged_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5362) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5363) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5365) if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5366) &inode->runtime_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5367) if (inode_only == LOG_INODE_EXISTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5368) max_key.type = BTRFS_XATTR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5369) ret = drop_objectid_items(trans, log, path, ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5370) max_key.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5371) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5372) clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5373) &inode->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5374) clear_bit(BTRFS_INODE_COPY_EVERYTHING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5375) &inode->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5376) while(1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5377) ret = btrfs_truncate_inode_items(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5378) log, &inode->vfs_inode, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5379) if (ret != -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5380) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5383) } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5384) &inode->runtime_flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5385) inode_only == LOG_INODE_EXISTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5386) if (inode_only == LOG_INODE_ALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5387) fast_search = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5388) max_key.type = BTRFS_XATTR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5389) ret = drop_objectid_items(trans, log, path, ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5390) max_key.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5391) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5392) if (inode_only == LOG_INODE_ALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5393) fast_search = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5394) goto log_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5398) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5399) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5400) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5403) err = copy_inode_items_to_log(trans, inode, &min_key, &max_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5404) path, dst_path, logged_isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5405) recursive_logging, inode_only, ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5406) &need_log_inode_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5407) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5408) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5410) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5411) btrfs_release_path(dst_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5412) err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5413) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5414) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5415) xattrs_logged = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5416) if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5417) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5418) btrfs_release_path(dst_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5419) err = btrfs_log_holes(trans, root, inode, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5420) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5421) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5423) log_extents:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5424) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5425) btrfs_release_path(dst_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5426) if (need_log_inode_item) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5427) err = log_inode_item(trans, log, dst_path, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5428) if (!err && !xattrs_logged) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5429) err = btrfs_log_all_xattrs(trans, root, inode, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5430) dst_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5431) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5433) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5434) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5436) if (fast_search) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5437) ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5438) ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5439) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5440) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5441) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5443) } else if (inode_only == LOG_INODE_ALL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5444) struct extent_map *em, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5446) write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5447) list_for_each_entry_safe(em, n, &em_tree->modified_extents, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5448) list_del_init(&em->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5449) write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5452) if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5453) ret = log_directory_changes(trans, root, inode, path, dst_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5454) ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5455) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5456) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5457) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5461) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5462) * If we are logging that an ancestor inode exists as part of logging a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5463) * new name from a link or rename operation, don't mark the inode as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5464) * logged - otherwise if an explicit fsync is made against an ancestor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5465) * the fsync considers the inode in the log and doesn't sync the log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5466) * resulting in the ancestor missing after a power failure unless the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5467) * log was synced as part of an fsync against any other unrelated inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5468) * So keep it simple for this case and just don't flag the ancestors as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5469) * logged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5470) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5471) if (!ctx ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5472) !(S_ISDIR(inode->vfs_inode.i_mode) && ctx->logging_new_name &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5473) &inode->vfs_inode != ctx->inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5474) spin_lock(&inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5475) inode->logged_trans = trans->transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5476) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5477) * Don't update last_log_commit if we logged that an inode exists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5478) * after it was loaded to memory (full_sync bit set).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5479) * This is to prevent data loss when we do a write to the inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5480) * then the inode gets evicted after all delalloc was flushed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5481) * then we log it exists (due to a rename for example) and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5482) * fsync it. This last fsync would do nothing (not logging the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5483) * extents previously written).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5484) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5485) if (inode_only != LOG_INODE_EXISTS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5486) !test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5487) inode->last_log_commit = inode->last_sub_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5488) spin_unlock(&inode->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5490) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5491) mutex_unlock(&inode->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5493) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5494) btrfs_free_path(dst_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5495) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5498) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5499) * Check if we must fallback to a transaction commit when logging an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5500) * This must be called after logging the inode and is used only in the context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5501) * when fsyncing an inode requires the need to log some other inode - in which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5502) * case we can't lock the i_mutex of each other inode we need to log as that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5503) * can lead to deadlocks with concurrent fsync against other inodes (as we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5504) * log inodes up or down in the hierarchy) or rename operations for example. So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5505) * we take the log_mutex of the inode after we have logged it and then check for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5506) * its last_unlink_trans value - this is safe because any task setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5507) * last_unlink_trans must take the log_mutex and it must do this before it does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5508) * the actual unlink operation, so if we do this check before a concurrent task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5509) * sets last_unlink_trans it means we've logged a consistent version/state of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5510) * all the inode items, otherwise we are not sure and must do a transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5511) * commit (the concurrent task might have only updated last_unlink_trans before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5512) * we logged the inode or it might have also done the unlink).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5513) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5514) static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5515) struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5517) struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5518) bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5520) mutex_lock(&inode->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5521) if (inode->last_unlink_trans > fs_info->last_trans_committed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5522) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5523) * Make sure any commits to the log are forced to be full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5524) * commits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5525) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5526) btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5527) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5529) mutex_unlock(&inode->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5531) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5534) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5535) * follow the dentry parent pointers up the chain and see if any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5536) * of the directories in it require a full commit before they can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5537) * be logged. Returns zero if nothing special needs to be done or 1 if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5538) * a full commit is required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5539) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5540) static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5541) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5542) struct dentry *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5543) struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5544) u64 last_committed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5546) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5547) struct dentry *old_parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5550) * for regular files, if its inode is already on disk, we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5551) * have to worry about the parents at all. This is because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5552) * we can use the last_unlink_trans field to record renames
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5553) * and other fun in this file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5554) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5555) if (S_ISREG(inode->vfs_inode.i_mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5556) inode->generation <= last_committed &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5557) inode->last_unlink_trans <= last_committed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5558) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5560) if (!S_ISDIR(inode->vfs_inode.i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5561) if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5562) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5563) inode = BTRFS_I(d_inode(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5566) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5567) if (btrfs_must_commit_transaction(trans, inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5568) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5569) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5572) if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5573) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5575) if (IS_ROOT(parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5576) inode = BTRFS_I(d_inode(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5577) if (btrfs_must_commit_transaction(trans, inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5578) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5579) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5582) parent = dget_parent(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5583) dput(old_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5584) old_parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5585) inode = BTRFS_I(d_inode(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5588) dput(old_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5589) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5590) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5593) struct btrfs_dir_list {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5594) u64 ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5595) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5596) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5598) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5599) * Log the inodes of the new dentries of a directory. See log_dir_items() for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5600) * details about the why it is needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5601) * This is a recursive operation - if an existing dentry corresponds to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5602) * directory, that directory's new entries are logged too (same behaviour as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5603) * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5604) * the dentries point to we do not lock their i_mutex, otherwise lockdep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5605) * complains about the following circular lock dependency / possible deadlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5606) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5607) * CPU0 CPU1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5608) * ---- ----
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5609) * lock(&type->i_mutex_dir_key#3/2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5610) * lock(sb_internal#2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5611) * lock(&type->i_mutex_dir_key#3/2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5612) * lock(&sb->s_type->i_mutex_key#14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5613) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5614) * Where sb_internal is the lock (a counter that works as a lock) acquired by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5615) * sb_start_intwrite() in btrfs_start_transaction().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5616) * Not locking i_mutex of the inodes is still safe because:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5617) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5618) * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5619) * that while logging the inode new references (names) are added or removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5620) * from the inode, leaving the logged inode item with a link count that does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5621) * not match the number of logged inode reference items. This is fine because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5622) * at log replay time we compute the real number of links and correct the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5623) * link count in the inode item (see replay_one_buffer() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5624) * link_to_fixup_dir());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5625) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5626) * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5627) * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5628) * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5629) * has a size that doesn't match the sum of the lengths of all the logged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5630) * names. This does not result in a problem because if a dir_item key is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5631) * logged but its matching dir_index key is not logged, at log replay time we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5632) * don't use it to replay the respective name (see replay_one_name()). On the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5633) * other hand if only the dir_index key ends up being logged, the respective
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5634) * name is added to the fs/subvol tree with both the dir_item and dir_index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5635) * keys created (see replay_one_name()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5636) * The directory's inode item with a wrong i_size is not a problem as well,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5637) * since we don't use it at log replay time to set the i_size in the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5638) * item of the fs/subvol tree (see overwrite_item()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5639) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5640) static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5641) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5642) struct btrfs_inode *start_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5643) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5645) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5646) struct btrfs_root *log = root->log_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5647) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5648) LIST_HEAD(dir_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5649) struct btrfs_dir_list *dir_elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5650) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5652) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5653) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5654) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5656) dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5657) if (!dir_elem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5658) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5659) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5661) dir_elem->ino = btrfs_ino(start_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5662) list_add_tail(&dir_elem->list, &dir_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5664) while (!list_empty(&dir_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5665) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5666) struct btrfs_key min_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5667) int nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5668) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5670) dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5671) list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5672) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5673) goto next_dir_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5675) min_key.objectid = dir_elem->ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5676) min_key.type = BTRFS_DIR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5677) min_key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5678) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5679) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5680) ret = btrfs_search_forward(log, &min_key, path, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5681) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5682) goto next_dir_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5683) } else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5684) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5685) goto next_dir_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5688) process_leaf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5689) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5690) nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5691) for (i = path->slots[0]; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5692) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5693) struct btrfs_key di_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5694) struct inode *di_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5695) struct btrfs_dir_list *new_dir_elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5696) int log_mode = LOG_INODE_EXISTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5697) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5699) btrfs_item_key_to_cpu(leaf, &min_key, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5700) if (min_key.objectid != dir_elem->ino ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5701) min_key.type != BTRFS_DIR_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5702) goto next_dir_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5704) di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5705) type = btrfs_dir_type(leaf, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5706) if (btrfs_dir_transid(leaf, di) < trans->transid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5707) type != BTRFS_FT_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5708) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5709) btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5710) if (di_key.type == BTRFS_ROOT_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5711) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5713) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5714) di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5715) if (IS_ERR(di_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5716) ret = PTR_ERR(di_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5717) goto next_dir_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5720) if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5721) btrfs_add_delayed_iput(di_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5722) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5725) ctx->log_new_dentries = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5726) if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5727) log_mode = LOG_INODE_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5728) ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5729) log_mode, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5730) if (!ret &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5731) btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5732) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5733) btrfs_add_delayed_iput(di_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5734) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5735) goto next_dir_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5736) if (ctx->log_new_dentries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5737) new_dir_elem = kmalloc(sizeof(*new_dir_elem),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5738) GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5739) if (!new_dir_elem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5740) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5741) goto next_dir_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5743) new_dir_elem->ino = di_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5744) list_add_tail(&new_dir_elem->list, &dir_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5746) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5748) if (i == nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5749) ret = btrfs_next_leaf(log, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5750) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5751) goto next_dir_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5752) } else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5753) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5754) goto next_dir_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5756) goto process_leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5758) if (min_key.offset < (u64)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5759) min_key.offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5760) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5762) next_dir_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5763) list_del(&dir_elem->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5764) kfree(dir_elem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5767) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5768) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5771) static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5772) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5773) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5774) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5775) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5776) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5777) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5778) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5779) struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5780) const u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5782) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5783) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5784) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5785) path->skip_locking = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5786) path->search_commit_root = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5788) key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5789) key.type = BTRFS_INODE_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5790) key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5791) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5792) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5793) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5795) while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5796) struct extent_buffer *leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5797) int slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5798) u32 cur_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5799) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5800) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5802) if (slot >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5803) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5804) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5805) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5806) else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5807) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5808) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5811) btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5812) /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5813) if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5814) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5816) item_size = btrfs_item_size_nr(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5817) ptr = btrfs_item_ptr_offset(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5818) while (cur_offset < item_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5819) struct btrfs_key inode_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5820) struct inode *dir_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5822) inode_key.type = BTRFS_INODE_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5823) inode_key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5825) if (key.type == BTRFS_INODE_EXTREF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5826) struct btrfs_inode_extref *extref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5828) extref = (struct btrfs_inode_extref *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5829) (ptr + cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5830) inode_key.objectid = btrfs_inode_extref_parent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5831) leaf, extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5832) cur_offset += sizeof(*extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5833) cur_offset += btrfs_inode_extref_name_len(leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5834) extref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5835) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5836) inode_key.objectid = key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5837) cur_offset = item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5840) dir_inode = btrfs_iget(fs_info->sb, inode_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5841) root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5842) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5843) * If the parent inode was deleted, return an error to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5844) * fallback to a transaction commit. This is to prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5845) * getting an inode that was moved from one parent A to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5846) * a parent B, got its former parent A deleted and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5847) * it got fsync'ed, from existing at both parents after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5848) * a log replay (and the old parent still existing).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5849) * Example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5850) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5851) * mkdir /mnt/A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5852) * mkdir /mnt/B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5853) * touch /mnt/B/bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5854) * sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5855) * mv /mnt/B/bar /mnt/A/bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5856) * mv -T /mnt/A /mnt/B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5857) * fsync /mnt/B/bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5858) * <power fail>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5859) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5860) * If we ignore the old parent B which got deleted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5861) * after a log replay we would have file bar linked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5862) * at both parents and the old parent B would still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5863) * exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5864) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5865) if (IS_ERR(dir_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5866) ret = PTR_ERR(dir_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5867) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5870) if (ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5871) ctx->log_new_dentries = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5872) ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5873) LOG_INODE_ALL, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5874) if (!ret &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5875) btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5876) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5877) if (!ret && ctx && ctx->log_new_dentries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5878) ret = log_new_dir_dentries(trans, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5879) BTRFS_I(dir_inode), ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5880) btrfs_add_delayed_iput(dir_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5881) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5882) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5884) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5886) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5887) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5888) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5889) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5892) static int log_new_ancestors(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5893) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5894) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5895) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5896) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5897) struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5899) btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5901) while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5902) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5903) const u64 last_committed = fs_info->last_trans_committed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5904) struct extent_buffer *leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5905) int slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5906) struct btrfs_key search_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5907) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5908) u64 ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5909) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5911) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5913) ino = found_key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5915) search_key.objectid = found_key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5916) search_key.type = BTRFS_INODE_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5917) search_key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5918) inode = btrfs_iget(fs_info->sb, ino, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5919) if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5920) return PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5922) if (BTRFS_I(inode)->generation > last_committed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5923) ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5924) LOG_INODE_EXISTS, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5925) btrfs_add_delayed_iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5926) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5927) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5929) if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5930) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5932) search_key.type = BTRFS_INODE_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5933) ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5934) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5935) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5937) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5938) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5939) if (slot >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5940) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5941) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5942) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5943) else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5944) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5945) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5946) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5949) btrfs_item_key_to_cpu(leaf, &found_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5950) if (found_key.objectid != search_key.objectid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5951) found_key.type != BTRFS_INODE_REF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5952) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5954) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5957) static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5958) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5959) struct dentry *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5960) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5961) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5962) struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5963) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5964) struct dentry *old_parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5965) struct super_block *sb = inode->vfs_inode.i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5966) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5968) while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5969) if (!parent || d_really_is_negative(parent) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5970) sb != parent->d_sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5971) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5973) inode = BTRFS_I(d_inode(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5974) if (root != inode->root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5975) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5977) if (inode->generation > fs_info->last_trans_committed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5978) ret = btrfs_log_inode(trans, root, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5979) LOG_INODE_EXISTS, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5980) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5981) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5982) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5983) if (IS_ROOT(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5984) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5986) parent = dget_parent(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5987) dput(old_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5988) old_parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5990) dput(old_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5992) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5995) static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5996) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5997) struct dentry *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5998) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6000) struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6001) const u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6002) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6003) struct btrfs_key search_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6004) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6006) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6007) * For a single hard link case, go through a fast path that does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6008) * need to iterate the fs/subvolume tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6009) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6010) if (inode->vfs_inode.i_nlink < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6011) return log_new_ancestors_fast(trans, inode, parent, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6013) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6014) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6015) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6017) search_key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6018) search_key.type = BTRFS_INODE_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6019) search_key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6020) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6021) ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6022) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6023) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6024) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6025) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6027) while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6028) struct extent_buffer *leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6029) int slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6030) struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6032) if (slot >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6033) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6034) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6035) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6036) else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6037) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6038) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6039) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6041) btrfs_item_key_to_cpu(leaf, &found_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6042) if (found_key.objectid != ino ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6043) found_key.type > BTRFS_INODE_EXTREF_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6044) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6046) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6047) * Don't deal with extended references because they are rare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6048) * cases and too complex to deal with (we would need to keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6049) * track of which subitem we are processing for each item in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6050) * this loop, etc). So just return some error to fallback to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6051) * a transaction commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6052) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6053) if (found_key.type == BTRFS_INODE_EXTREF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6054) ret = -EMLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6055) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6058) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6059) * Logging ancestors needs to do more searches on the fs/subvol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6060) * tree, so it releases the path as needed to avoid deadlocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6061) * Keep track of the last inode ref key and resume from that key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6062) * after logging all new ancestors for the current hard link.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6063) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6064) memcpy(&search_key, &found_key, sizeof(search_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6066) ret = log_new_ancestors(trans, root, path, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6067) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6068) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6069) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6070) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6072) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6073) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6074) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6075) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6078) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6079) * helper function around btrfs_log_inode to make sure newly created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6080) * parent directories also end up in the log. A minimal inode and backref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6081) * only logging is done of any parent directories that are older than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6082) * the last committed transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6083) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6084) static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6085) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6086) struct dentry *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6087) int inode_only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6088) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6089) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6090) struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6091) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6092) struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6093) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6094) u64 last_committed = fs_info->last_trans_committed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6095) bool log_dentries = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6097) sb = inode->vfs_inode.i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6099) if (btrfs_test_opt(fs_info, NOTREELOG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6100) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6101) goto end_no_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6105) * The prev transaction commit doesn't complete, we need do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6106) * full commit by ourselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6108) if (fs_info->last_trans_log_full_commit >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6109) fs_info->last_trans_committed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6110) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6111) goto end_no_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6114) if (btrfs_root_refs(&root->root_item) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6115) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6116) goto end_no_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6119) ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6120) last_committed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6121) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6122) goto end_no_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6125) * Skip already logged inodes or inodes corresponding to tmpfiles
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6126) * (since logging them is pointless, a link count of 0 means they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6127) * will never be accessible).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6128) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6129) if ((btrfs_inode_in_log(inode, trans->transid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6130) list_empty(&ctx->ordered_extents)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6131) inode->vfs_inode.i_nlink == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6132) ret = BTRFS_NO_LOG_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6133) goto end_no_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6136) ret = start_log_trans(trans, root, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6137) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6138) goto end_no_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6140) ret = btrfs_log_inode(trans, root, inode, inode_only, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6141) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6142) goto end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6145) * for regular files, if its inode is already on disk, we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6146) * have to worry about the parents at all. This is because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6147) * we can use the last_unlink_trans field to record renames
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6148) * and other fun in this file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6150) if (S_ISREG(inode->vfs_inode.i_mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6151) inode->generation <= last_committed &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6152) inode->last_unlink_trans <= last_committed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6153) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6154) goto end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6157) if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6158) log_dentries = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6160) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6161) * On unlink we must make sure all our current and old parent directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6162) * inodes are fully logged. This is to prevent leaving dangling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6163) * directory index entries in directories that were our parents but are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6164) * not anymore. Not doing this results in old parent directory being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6165) * impossible to delete after log replay (rmdir will always fail with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6166) * error -ENOTEMPTY).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6167) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6168) * Example 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6169) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6170) * mkdir testdir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6171) * touch testdir/foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6172) * ln testdir/foo testdir/bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6173) * sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6174) * unlink testdir/bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6175) * xfs_io -c fsync testdir/foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6176) * <power failure>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6177) * mount fs, triggers log replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6178) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6179) * If we don't log the parent directory (testdir), after log replay the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6180) * directory still has an entry pointing to the file inode using the bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6181) * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6182) * the file inode has a link count of 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6184) * Example 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6185) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6186) * mkdir testdir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6187) * touch foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6188) * ln foo testdir/foo2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6189) * ln foo testdir/foo3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6190) * sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6191) * unlink testdir/foo3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6192) * xfs_io -c fsync foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6193) * <power failure>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6194) * mount fs, triggers log replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6195) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6196) * Similar as the first example, after log replay the parent directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6197) * testdir still has an entry pointing to the inode file with name foo3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6198) * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6199) * and has a link count of 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6200) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6201) if (inode->last_unlink_trans > last_committed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6202) ret = btrfs_log_all_parents(trans, inode, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6203) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6204) goto end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6207) ret = log_all_new_ancestors(trans, inode, parent, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6208) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6209) goto end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6211) if (log_dentries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6212) ret = log_new_dir_dentries(trans, root, inode, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6213) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6214) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6215) end_trans:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6216) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6217) btrfs_set_log_full_commit(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6218) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6221) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6222) btrfs_remove_log_ctx(root, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6223) btrfs_end_log_trans(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6224) end_no_trans:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6225) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6228) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6229) * it is not safe to log dentry if the chunk root has added new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6230) * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6231) * If this returns 1, you must commit the transaction to safely get your
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6232) * data on disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6233) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6234) int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6235) struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6236) struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6238) struct dentry *parent = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6239) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6241) ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6242) LOG_INODE_ALL, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6243) dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6245) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6249) * should be called during mount to recover any replay any log trees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6250) * from the FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6251) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6252) int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6254) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6255) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6256) struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6257) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6258) struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6259) struct btrfs_root *log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6260) struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6261) struct walk_control wc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6262) .process_func = process_one_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6263) .stage = LOG_WALK_PIN_ONLY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6264) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6266) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6267) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6268) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6270) set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6272) trans = btrfs_start_transaction(fs_info->tree_root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6273) if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6274) ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6275) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6278) wc.trans = trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6279) wc.pin = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6281) ret = walk_log_tree(trans, log_root_tree, &wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6282) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6283) btrfs_handle_fs_error(fs_info, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6284) "Failed to pin buffers while recovering log root tree.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6285) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6288) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6289) key.objectid = BTRFS_TREE_LOG_OBJECTID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6290) key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6291) key.type = BTRFS_ROOT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6293) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6294) ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6296) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6297) btrfs_handle_fs_error(fs_info, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6298) "Couldn't find tree log root.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6299) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6301) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6302) if (path->slots[0] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6303) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6304) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6306) btrfs_item_key_to_cpu(path->nodes[0], &found_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6307) path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6308) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6309) if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6310) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6312) log = btrfs_read_tree_root(log_root_tree, &found_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6313) if (IS_ERR(log)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6314) ret = PTR_ERR(log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6315) btrfs_handle_fs_error(fs_info, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6316) "Couldn't read tree log root.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6317) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6320) wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6321) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6322) if (IS_ERR(wc.replay_dest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6323) ret = PTR_ERR(wc.replay_dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6325) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6326) * We didn't find the subvol, likely because it was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6327) * deleted. This is ok, simply skip this log and go to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6328) * the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6329) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6330) * We need to exclude the root because we can't have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6331) * other log replays overwriting this log as we'll read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6332) * it back in a few more times. This will keep our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6333) * block from being modified, and we'll just bail for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6334) * each subsequent pass.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6335) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6336) if (ret == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6337) ret = btrfs_pin_extent_for_log_replay(trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6338) log->node->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6339) log->node->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6340) btrfs_put_root(log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6342) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6343) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6344) btrfs_handle_fs_error(fs_info, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6345) "Couldn't read target root for tree log recovery.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6346) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6349) wc.replay_dest->log_root = log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6350) btrfs_record_root_in_trans(trans, wc.replay_dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6351) ret = walk_log_tree(trans, log, &wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6353) if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6354) ret = fixup_inode_link_counts(trans, wc.replay_dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6355) path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6358) if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6359) struct btrfs_root *root = wc.replay_dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6361) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6363) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6364) * We have just replayed everything, and the highest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6365) * objectid of fs roots probably has changed in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6366) * some inode_item's got replayed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6367) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6368) * root->objectid_mutex is not acquired as log replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6369) * could only happen during mount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6371) ret = btrfs_find_highest_objectid(root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6372) &root->highest_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6375) wc.replay_dest->log_root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6376) btrfs_put_root(wc.replay_dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6377) btrfs_put_root(log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6379) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6380) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6381) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6382) if (found_key.offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6383) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6384) key.offset = found_key.offset - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6386) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6388) /* step one is to pin it all, step two is to replay just inodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6389) if (wc.pin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6390) wc.pin = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6391) wc.process_func = replay_one_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6392) wc.stage = LOG_WALK_REPLAY_INODES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6393) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6395) /* step three is to replay everything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6396) if (wc.stage < LOG_WALK_REPLAY_ALL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6397) wc.stage++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6398) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6401) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6403) /* step 4: commit the transaction, which also unpins the blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6404) ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6405) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6406) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6408) log_root_tree->log_root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6409) clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6410) btrfs_put_root(log_root_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6412) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6413) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6414) if (wc.trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6415) btrfs_end_transaction(wc.trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6416) clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6417) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6418) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6421) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6422) * there are some corner cases where we want to force a full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6423) * commit instead of allowing a directory to be logged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6424) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6425) * They revolve around files there were unlinked from the directory, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6426) * this function updates the parent directory so that a full commit is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6427) * properly done if it is fsync'd later after the unlinks are done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6428) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6429) * Must be called before the unlink operations (updates to the subvolume tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6430) * inodes, etc) are done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6432) void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6433) struct btrfs_inode *dir, struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6434) int for_rename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6436) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6437) * when we're logging a file, if it hasn't been renamed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6438) * or unlinked, and its inode is fully committed on disk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6439) * we don't have to worry about walking up the directory chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6440) * to log its parents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6441) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6442) * So, we use the last_unlink_trans field to put this transid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6443) * into the file. When the file is logged we check it and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6444) * don't log the parents if the file is fully on disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6445) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6446) mutex_lock(&inode->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6447) inode->last_unlink_trans = trans->transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6448) mutex_unlock(&inode->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6450) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6451) * if this directory was already logged any new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6452) * names for this file/dir will get recorded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6453) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6454) if (dir->logged_trans == trans->transid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6455) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6457) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6458) * if the inode we're about to unlink was logged,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6459) * the log will be properly updated for any new names
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6460) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6461) if (inode->logged_trans == trans->transid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6462) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6464) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6465) * when renaming files across directories, if the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6466) * there we're unlinking from gets fsync'd later on, there's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6467) * no way to find the destination directory later and fsync it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6468) * properly. So, we have to be conservative and force commits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6469) * so the new name gets discovered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6470) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6471) if (for_rename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6472) goto record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6474) /* we can safely do the unlink without any special recording */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6475) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6477) record:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6478) mutex_lock(&dir->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6479) dir->last_unlink_trans = trans->transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6480) mutex_unlock(&dir->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6483) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6484) * Make sure that if someone attempts to fsync the parent directory of a deleted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6485) * snapshot, it ends up triggering a transaction commit. This is to guarantee
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6486) * that after replaying the log tree of the parent directory's root we will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6487) * see the snapshot anymore and at log replay time we will not see any log tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6488) * corresponding to the deleted snapshot's root, which could lead to replaying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6489) * it after replaying the log tree of the parent directory (which would replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6490) * the snapshot delete operation).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6491) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6492) * Must be called before the actual snapshot destroy operation (updates to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6493) * parent root and tree of tree roots trees, etc) are done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6494) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6495) void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6496) struct btrfs_inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6498) mutex_lock(&dir->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6499) dir->last_unlink_trans = trans->transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6500) mutex_unlock(&dir->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6503) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6504) * Call this after adding a new name for a file and it will properly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6505) * update the log to reflect the new name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6506) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6507) void btrfs_log_new_name(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6508) struct btrfs_inode *inode, struct btrfs_inode *old_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6509) struct dentry *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6511) struct btrfs_log_ctx ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6513) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6514) * this will force the logging code to walk the dentry chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6515) * up for the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6516) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6517) if (!S_ISDIR(inode->vfs_inode.i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6518) inode->last_unlink_trans = trans->transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6520) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6521) * if this inode hasn't been logged and directory we're renaming it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6522) * from hasn't been logged, we don't need to log it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6523) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6524) if (!inode_logged(trans, inode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6525) (!old_dir || !inode_logged(trans, old_dir)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6526) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6528) btrfs_init_log_ctx(&ctx, &inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6529) ctx.logging_new_name = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6530) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6531) * We don't care about the return value. If we fail to log the new name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6532) * then we know the next attempt to sync the log will fallback to a full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6533) * transaction commit (due to a call to btrfs_set_log_full_commit()), so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6534) * we don't need to worry about getting a log committed that has an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6535) * inconsistent state after a rename operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6536) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6537) btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6539)