David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008 Oracle. All rights reserved. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 8 | #include <linux/blkdev.h> |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9 | #include <linux/list_sort.h> |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 10 | #include <linux/iversion.h> |
David Sterba | 602cbe9 | 2019-08-21 18:48:25 +0200 | [diff] [blame] | 11 | #include "misc.h" |
Nikolay Borisov | 9678c54 | 2018-01-08 11:45:05 +0200 | [diff] [blame] | 12 | #include "ctree.h" |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 13 | #include "tree-log.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 14 | #include "disk-io.h" |
| 15 | #include "locking.h" |
| 16 | #include "print-tree.h" |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 17 | #include "backref.h" |
Anand Jain | ebb8765 | 2016-03-10 17:26:59 +0800 | [diff] [blame] | 18 | #include "compression.h" |
Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 19 | #include "qgroup.h" |
Nikolay Borisov | 6787bb9 | 2020-01-20 16:09:10 +0200 | [diff] [blame] | 20 | #include "block-group.h" |
| 21 | #include "space-info.h" |
Naohiro Aota | d3575156 | 2021-02-04 19:21:54 +0900 | [diff] [blame] | 22 | #include "zoned.h" |
Josef Bacik | 26c2c45 | 2021-12-03 17:18:03 -0500 | [diff] [blame] | 23 | #include "inode-item.h" |
Josef Bacik | c7f13d4 | 2022-10-19 10:50:47 -0400 | [diff] [blame] | 24 | #include "fs.h" |
Josef Bacik | ad1ac50 | 2022-10-19 10:50:59 -0400 | [diff] [blame] | 25 | #include "accessors.h" |
Josef Bacik | a023180 | 2022-10-24 14:46:57 -0400 | [diff] [blame] | 26 | #include "extent-tree.h" |
Josef Bacik | 45c40c8 | 2022-10-24 14:47:00 -0400 | [diff] [blame] | 27 | #include "root-tree.h" |
Josef Bacik | f2b3927 | 2022-10-26 15:08:26 -0400 | [diff] [blame] | 28 | #include "dir-item.h" |
Josef Bacik | 7c8ede1 | 2022-10-26 15:08:27 -0400 | [diff] [blame] | 29 | #include "file-item.h" |
Josef Bacik | af142b6 | 2022-10-26 15:08:30 -0400 | [diff] [blame] | 30 | #include "file.h" |
Josef Bacik | aa5d300 | 2022-10-26 15:08:41 -0400 | [diff] [blame] | 31 | #include "orphan.h" |
Christoph Hellwig | 103c197 | 2022-11-15 10:44:05 +0100 | [diff] [blame] | 32 | #include "tree-checker.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 33 | |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 34 | #define MAX_CONFLICT_INODES 10 |
| 35 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 36 | /* magic values for the inode_only field in btrfs_log_inode: |
| 37 | * |
| 38 | * LOG_INODE_ALL means to log everything |
| 39 | * LOG_INODE_EXISTS means to log just enough to recreate the inode |
| 40 | * during log replay |
| 41 | */ |
David Sterba | e13976c | 2019-08-01 14:50:30 +0200 | [diff] [blame] | 42 | enum { |
| 43 | LOG_INODE_ALL, |
| 44 | LOG_INODE_EXISTS, |
David Sterba | e13976c | 2019-08-01 14:50:30 +0200 | [diff] [blame] | 45 | }; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 46 | |
| 47 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 48 | * directory trouble cases |
| 49 | * |
| 50 | * 1) on rename or unlink, if the inode being unlinked isn't in the fsync |
| 51 | * log, we must force a full commit before doing an fsync of the directory |
| 52 | * where the unlink was done. |
| 53 | * ---> record transid of last unlink/rename per directory |
| 54 | * |
| 55 | * mkdir foo/some_dir |
| 56 | * normal commit |
| 57 | * rename foo/some_dir foo2/some_dir |
| 58 | * mkdir foo/some_dir |
| 59 | * fsync foo/some_dir/some_file |
| 60 | * |
| 61 | * The fsync above will unlink the original some_dir without recording |
| 62 | * it in its new location (foo2). After a crash, some_dir will be gone |
| 63 | * unless the fsync of some_file forces a full commit |
| 64 | * |
| 65 | * 2) we must log any new names for any file or dir that is in the fsync |
| 66 | * log. ---> check inode while renaming/linking. |
| 67 | * |
| 68 | * 2a) we must log any new names for any file or dir during rename |
| 69 | * when the directory they are being removed from was logged. |
| 70 | * ---> check inode and old parent dir during rename |
| 71 | * |
| 72 | * 2a is actually the more important variant. With the extra logging |
| 73 | * a crash might unlink the old name without recreating the new one |
| 74 | * |
| 75 | * 3) after a crash, we must go through any directories with a link count |
| 76 | * of zero and redo the rm -rf |
| 77 | * |
| 78 | * mkdir f1/foo |
| 79 | * normal commit |
| 80 | * rm -rf f1/foo |
| 81 | * fsync(f1) |
| 82 | * |
| 83 | * The directory f1 was fully removed from the FS, but fsync was never |
| 84 | * called on f1, only its parent dir. After a crash the rm -rf must |
| 85 | * be replayed. This must be able to recurse down the entire |
| 86 | * directory tree. The inode link count fixup code takes care of the |
| 87 | * ugly details. |
| 88 | */ |
| 89 | |
| 90 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 91 | * stages for the tree walking. The first |
| 92 | * stage (0) is to only pin down the blocks we find |
| 93 | * the second stage (1) is to make sure that all the inodes |
| 94 | * we find in the log are created in the subvolume. |
| 95 | * |
| 96 | * The last stage is to deal with directories and links and extents |
| 97 | * and all the other fun semantics |
| 98 | */ |
David Sterba | e13976c | 2019-08-01 14:50:30 +0200 | [diff] [blame] | 99 | enum { |
| 100 | LOG_WALK_PIN_ONLY, |
| 101 | LOG_WALK_REPLAY_INODES, |
| 102 | LOG_WALK_REPLAY_DIR_INDEX, |
| 103 | LOG_WALK_REPLAY_ALL, |
| 104 | }; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 105 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 106 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 107 | struct btrfs_inode *inode, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 108 | int inode_only, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 109 | struct btrfs_log_ctx *ctx); |
Yan Zheng | ec051c0 | 2009-01-05 15:43:42 -0500 | [diff] [blame] | 110 | static int link_to_fixup_dir(struct btrfs_trans_handle *trans, |
| 111 | struct btrfs_root *root, |
| 112 | struct btrfs_path *path, u64 objectid); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 113 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, |
| 114 | struct btrfs_root *root, |
| 115 | struct btrfs_root *log, |
| 116 | struct btrfs_path *path, |
| 117 | u64 dirid, int del_all); |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 118 | static void wait_log_commit(struct btrfs_root *root, int transid); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * tree logging is a special write ahead log used to make sure that |
| 122 | * fsyncs and O_SYNCs can happen without doing full tree commits. |
| 123 | * |
| 124 | * Full tree commits are expensive because they require commonly |
| 125 | * modified blocks to be recowed, creating many dirty pages in the |
| 126 | * extent tree an 4x-6x higher write load than ext3. |
| 127 | * |
| 128 | * Instead of doing a tree commit on every fsync, we use the |
| 129 | * key ranges and transaction ids to find items for a given file or directory |
| 130 | * that have changed in this transaction. Those items are copied into |
| 131 | * a special tree (one per subvolume root), that tree is written to disk |
| 132 | * and then the fsync is considered complete. |
| 133 | * |
| 134 | * After a crash, items are copied out of the log-tree back into the |
| 135 | * subvolume tree. Any file data extents found are recorded in the extent |
| 136 | * allocation tree, and the log-tree freed. |
| 137 | * |
| 138 | * The log tree is read three times, once to pin down all the extents it is |
| 139 | * using in ram and once, once to create all the inodes logged in the tree |
| 140 | * and once to do all the other items. |
| 141 | */ |
| 142 | |
| 143 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 144 | * start a sub transaction and setup the log tree |
| 145 | * this increments the log tree writer count to make the people |
| 146 | * syncing the tree wait for us to finish |
| 147 | */ |
| 148 | static int start_log_trans(struct btrfs_trans_handle *trans, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 149 | struct btrfs_root *root, |
| 150 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 151 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 152 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | 47876f7 | 2020-11-25 12:19:28 +0000 | [diff] [blame] | 153 | struct btrfs_root *tree_root = fs_info->tree_root; |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 154 | const bool zoned = btrfs_is_zoned(fs_info); |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 155 | int ret = 0; |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 156 | bool created = false; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 157 | |
Filipe Manana | 47876f7 | 2020-11-25 12:19:28 +0000 | [diff] [blame] | 158 | /* |
| 159 | * First check if the log root tree was already created. If not, create |
| 160 | * it before locking the root's log_mutex, just to keep lockdep happy. |
| 161 | */ |
| 162 | if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state)) { |
| 163 | mutex_lock(&tree_root->log_mutex); |
| 164 | if (!fs_info->log_root_tree) { |
| 165 | ret = btrfs_init_log_root_tree(trans, fs_info); |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 166 | if (!ret) { |
Filipe Manana | 47876f7 | 2020-11-25 12:19:28 +0000 | [diff] [blame] | 167 | set_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state); |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 168 | created = true; |
| 169 | } |
Filipe Manana | 47876f7 | 2020-11-25 12:19:28 +0000 | [diff] [blame] | 170 | } |
| 171 | mutex_unlock(&tree_root->log_mutex); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | } |
| 175 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 176 | mutex_lock(&root->log_mutex); |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 177 | |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 178 | again: |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 179 | if (root->log_root) { |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 180 | int index = (root->log_transid + 1) % 2; |
| 181 | |
David Sterba | 4884b8e | 2019-03-20 13:25:34 +0100 | [diff] [blame] | 182 | if (btrfs_need_log_full_commit(trans)) { |
Josef Bacik | f31f09f | 2022-06-13 15:09:48 -0400 | [diff] [blame] | 183 | ret = BTRFS_LOG_FORCE_COMMIT; |
Miao Xie | 50471a3 | 2014-02-20 18:08:57 +0800 | [diff] [blame] | 184 | goto out; |
| 185 | } |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 186 | |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 187 | if (zoned && atomic_read(&root->log_commit[index])) { |
| 188 | wait_log_commit(root, root->log_transid - 1); |
| 189 | goto again; |
| 190 | } |
| 191 | |
Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 192 | if (!root->log_start_pid) { |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 193 | clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 194 | root->log_start_pid = current->pid; |
Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 195 | } else if (root->log_start_pid != current->pid) { |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 196 | set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 197 | } |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 198 | } else { |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 199 | /* |
| 200 | * This means fs_info->log_root_tree was already created |
| 201 | * for some other FS trees. Do the full commit not to mix |
| 202 | * nodes from multiple log transactions to do sequential |
| 203 | * writing. |
| 204 | */ |
| 205 | if (zoned && !created) { |
Josef Bacik | f31f09f | 2022-06-13 15:09:48 -0400 | [diff] [blame] | 206 | ret = BTRFS_LOG_FORCE_COMMIT; |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 207 | goto out; |
| 208 | } |
| 209 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 210 | ret = btrfs_add_log_tree(trans, root); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 211 | if (ret) |
Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 212 | goto out; |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 213 | |
Filipe Manana | e7a7981 | 2020-06-15 10:38:44 +0100 | [diff] [blame] | 214 | set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state); |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 215 | clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
| 216 | root->log_start_pid = current->pid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 217 | } |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 218 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 219 | atomic_inc(&root->log_writers); |
Filipe Manana | 289cffc | 2021-08-31 15:30:32 +0100 | [diff] [blame] | 220 | if (!ctx->logging_new_name) { |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 221 | int index = root->log_transid % 2; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 222 | list_add_tail(&ctx->list, &root->log_ctxs[index]); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 223 | ctx->log_transid = root->log_transid; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 224 | } |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 225 | |
Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 226 | out: |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 227 | mutex_unlock(&root->log_mutex); |
Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 228 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* |
| 232 | * returns 0 if there was a log transaction running and we were able |
| 233 | * to join, or returns -ENOENT if there were not transactions |
| 234 | * in progress |
| 235 | */ |
| 236 | static int join_running_log_trans(struct btrfs_root *root) |
| 237 | { |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 238 | const bool zoned = btrfs_is_zoned(root->fs_info); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 239 | int ret = -ENOENT; |
| 240 | |
Filipe Manana | e7a7981 | 2020-06-15 10:38:44 +0100 | [diff] [blame] | 241 | if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state)) |
| 242 | return ret; |
| 243 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 244 | mutex_lock(&root->log_mutex); |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 245 | again: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 246 | if (root->log_root) { |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 247 | int index = (root->log_transid + 1) % 2; |
| 248 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 249 | ret = 0; |
Naohiro Aota | fa1a0f4 | 2021-02-04 19:22:19 +0900 | [diff] [blame] | 250 | if (zoned && atomic_read(&root->log_commit[index])) { |
| 251 | wait_log_commit(root, root->log_transid - 1); |
| 252 | goto again; |
| 253 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 254 | atomic_inc(&root->log_writers); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 255 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 256 | mutex_unlock(&root->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 261 | * This either makes the current running log transaction wait |
| 262 | * until you call btrfs_end_log_trans() or it makes any future |
| 263 | * log transactions wait until you call btrfs_end_log_trans() |
| 264 | */ |
zhong jiang | 45128b0 | 2018-08-17 00:37:15 +0800 | [diff] [blame] | 265 | void btrfs_pin_log_trans(struct btrfs_root *root) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 266 | { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 267 | atomic_inc(&root->log_writers); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 271 | * indicate we're done making changes to the log tree |
| 272 | * and wake up anyone waiting to do a sync |
| 273 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 274 | void btrfs_end_log_trans(struct btrfs_root *root) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 275 | { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 276 | if (atomic_dec_and_test(&root->log_writers)) { |
David Sterba | 093258e6 | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 277 | /* atomic_dec_and_test implies a barrier */ |
| 278 | cond_wake_up_nomb(&root->log_writer_wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 279 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 282 | /* |
| 283 | * the walk control struct is used to pass state down the chain when |
| 284 | * processing the log tree. The stage field tells us which part |
| 285 | * of the log tree processing we are currently doing. The others |
| 286 | * are state fields used for that specific part |
| 287 | */ |
| 288 | struct walk_control { |
| 289 | /* should we free the extent on disk when done? This is used |
| 290 | * at transaction commit time while freeing a log tree |
| 291 | */ |
| 292 | int free; |
| 293 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 294 | /* pin only walk, we record which extents on disk belong to the |
| 295 | * log trees |
| 296 | */ |
| 297 | int pin; |
| 298 | |
| 299 | /* what stage of the replay code we're currently in */ |
| 300 | int stage; |
| 301 | |
Filipe Manana | f2d72f4 | 2018-10-08 11:12:55 +0100 | [diff] [blame] | 302 | /* |
| 303 | * Ignore any items from the inode currently being processed. Needs |
| 304 | * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in |
| 305 | * the LOG_WALK_REPLAY_INODES stage. |
| 306 | */ |
| 307 | bool ignore_cur_inode; |
| 308 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 309 | /* the root we are currently replaying */ |
| 310 | struct btrfs_root *replay_dest; |
| 311 | |
| 312 | /* the trans handle for the current replay */ |
| 313 | struct btrfs_trans_handle *trans; |
| 314 | |
| 315 | /* the function that gets used to process blocks we find in the |
| 316 | * tree. Note the extent_buffer might not be up to date when it is |
| 317 | * passed in, and it must be checked or read if you need the data |
| 318 | * inside it |
| 319 | */ |
| 320 | int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 321 | struct walk_control *wc, u64 gen, int level); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | /* |
| 325 | * process_func used to pin down extents, write them or wait on them |
| 326 | */ |
| 327 | static int process_one_buffer(struct btrfs_root *log, |
| 328 | struct extent_buffer *eb, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 329 | struct walk_control *wc, u64 gen, int level) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 330 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 331 | struct btrfs_fs_info *fs_info = log->fs_info; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 332 | int ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 333 | |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 334 | /* |
| 335 | * If this fs is mixed then we need to be able to process the leaves to |
| 336 | * pin down any logged extents, so we have to read the block. |
| 337 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 338 | if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
Qu Wenruo | 789d6a3 | 2022-09-14 13:32:50 +0800 | [diff] [blame] | 339 | struct btrfs_tree_parent_check check = { |
| 340 | .level = level, |
| 341 | .transid = gen |
| 342 | }; |
| 343 | |
| 344 | ret = btrfs_read_extent_buffer(eb, &check); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 345 | if (ret) |
| 346 | return ret; |
| 347 | } |
| 348 | |
Filipe Manana | c816d70 | 2022-01-04 12:53:41 +0000 | [diff] [blame] | 349 | if (wc->pin) { |
Nikolay Borisov | 9fce570 | 2020-01-20 16:09:13 +0200 | [diff] [blame] | 350 | ret = btrfs_pin_extent_for_log_replay(wc->trans, eb->start, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 351 | eb->len); |
Filipe Manana | c816d70 | 2022-01-04 12:53:41 +0000 | [diff] [blame] | 352 | if (ret) |
| 353 | return ret; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 354 | |
Filipe Manana | c816d70 | 2022-01-04 12:53:41 +0000 | [diff] [blame] | 355 | if (btrfs_buffer_uptodate(eb, gen, 0) && |
| 356 | btrfs_header_level(eb) == 0) |
David Sterba | bcdc428 | 2019-03-20 12:14:33 +0100 | [diff] [blame] | 357 | ret = btrfs_exclude_logged_extents(eb); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 358 | } |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 359 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 360 | } |
| 361 | |
Filipe Manana | 3a8d1db | 2022-11-21 10:23:23 +0000 | [diff] [blame] | 362 | /* |
| 363 | * Item overwrite used by replay and tree logging. eb, slot and key all refer |
| 364 | * to the src data we are copying out. |
| 365 | * |
| 366 | * root is the tree we are copying into, and path is a scratch |
| 367 | * path for use in this function (it should be released on entry and |
| 368 | * will be released on exit). |
| 369 | * |
| 370 | * If the key is already in the destination tree the existing item is |
| 371 | * overwritten. If the existing item isn't big enough, it is extended. |
| 372 | * If it is too large, it is truncated. |
| 373 | * |
| 374 | * If the key isn't in the destination yet, a new item is inserted. |
| 375 | */ |
| 376 | static int overwrite_item(struct btrfs_trans_handle *trans, |
| 377 | struct btrfs_root *root, |
| 378 | struct btrfs_path *path, |
| 379 | struct extent_buffer *eb, int slot, |
| 380 | struct btrfs_key *key) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 381 | { |
| 382 | int ret; |
| 383 | u32 item_size; |
| 384 | u64 saved_i_size = 0; |
| 385 | int save_old_i_size = 0; |
| 386 | unsigned long src_ptr; |
| 387 | unsigned long dst_ptr; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 388 | bool inode_item = key->type == BTRFS_INODE_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 389 | |
Filipe Manana | 3eb4234 | 2022-11-21 10:23:24 +0000 | [diff] [blame] | 390 | /* |
| 391 | * This is only used during log replay, so the root is always from a |
| 392 | * fs/subvolume tree. In case we ever need to support a log root, then |
| 393 | * we'll have to clone the leaf in the path, release the path and use |
| 394 | * the leaf before writing into the log tree. See the comments at |
| 395 | * copy_items() for more details. |
| 396 | */ |
| 397 | ASSERT(root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 398 | |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 399 | item_size = btrfs_item_size(eb, slot); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 400 | src_ptr = btrfs_item_ptr_offset(eb, slot); |
| 401 | |
Filipe Manana | 3a8d1db | 2022-11-21 10:23:23 +0000 | [diff] [blame] | 402 | /* Look for the key in the destination tree. */ |
| 403 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 404 | if (ret < 0) |
| 405 | return ret; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 406 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 407 | if (ret == 0) { |
| 408 | char *src_copy; |
| 409 | char *dst_copy; |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 410 | u32 dst_size = btrfs_item_size(path->nodes[0], |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 411 | path->slots[0]); |
| 412 | if (dst_size != item_size) |
| 413 | goto insert; |
| 414 | |
| 415 | if (item_size == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 416 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | dst_copy = kmalloc(item_size, GFP_NOFS); |
| 420 | src_copy = kmalloc(item_size, GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 421 | if (!dst_copy || !src_copy) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 422 | btrfs_release_path(path); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 423 | kfree(dst_copy); |
| 424 | kfree(src_copy); |
| 425 | return -ENOMEM; |
| 426 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 427 | |
| 428 | read_extent_buffer(eb, src_copy, src_ptr, item_size); |
| 429 | |
| 430 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
| 431 | read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, |
| 432 | item_size); |
| 433 | ret = memcmp(dst_copy, src_copy, item_size); |
| 434 | |
| 435 | kfree(dst_copy); |
| 436 | kfree(src_copy); |
| 437 | /* |
| 438 | * they have the same contents, just return, this saves |
| 439 | * us from cowing blocks in the destination tree and doing |
| 440 | * extra writes that may not have been done by a previous |
| 441 | * sync |
| 442 | */ |
| 443 | if (ret == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 444 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 448 | /* |
| 449 | * We need to load the old nbytes into the inode so when we |
| 450 | * replay the extents we've logged we get the right nbytes. |
| 451 | */ |
| 452 | if (inode_item) { |
| 453 | struct btrfs_inode_item *item; |
| 454 | u64 nbytes; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 455 | u32 mode; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 456 | |
| 457 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 458 | struct btrfs_inode_item); |
| 459 | nbytes = btrfs_inode_nbytes(path->nodes[0], item); |
| 460 | item = btrfs_item_ptr(eb, slot, |
| 461 | struct btrfs_inode_item); |
| 462 | btrfs_set_inode_nbytes(eb, item, nbytes); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 463 | |
| 464 | /* |
| 465 | * If this is a directory we need to reset the i_size to |
| 466 | * 0 so that we can set it up properly when replaying |
| 467 | * the rest of the items in this log. |
| 468 | */ |
| 469 | mode = btrfs_inode_mode(eb, item); |
| 470 | if (S_ISDIR(mode)) |
| 471 | btrfs_set_inode_size(eb, item, 0); |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 472 | } |
| 473 | } else if (inode_item) { |
| 474 | struct btrfs_inode_item *item; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 475 | u32 mode; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 476 | |
| 477 | /* |
| 478 | * New inode, set nbytes to 0 so that the nbytes comes out |
| 479 | * properly when we replay the extents. |
| 480 | */ |
| 481 | item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); |
| 482 | btrfs_set_inode_nbytes(eb, item, 0); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 483 | |
| 484 | /* |
| 485 | * If this is a directory we need to reset the i_size to 0 so |
| 486 | * that we can set it up properly when replaying the rest of |
| 487 | * the items in this log. |
| 488 | */ |
| 489 | mode = btrfs_inode_mode(eb, item); |
| 490 | if (S_ISDIR(mode)) |
| 491 | btrfs_set_inode_size(eb, item, 0); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 492 | } |
| 493 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 494 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 495 | /* try to insert the key into the destination tree */ |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 496 | path->skip_release_on_error = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 497 | ret = btrfs_insert_empty_item(trans, root, path, |
| 498 | key, item_size); |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 499 | path->skip_release_on_error = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 500 | |
| 501 | /* make sure any existing item is the correct size */ |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 502 | if (ret == -EEXIST || ret == -EOVERFLOW) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 503 | u32 found_size; |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 504 | found_size = btrfs_item_size(path->nodes[0], |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 505 | path->slots[0]); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 506 | if (found_size > item_size) |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 507 | btrfs_truncate_item(path, item_size, 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 508 | else if (found_size < item_size) |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 509 | btrfs_extend_item(path, item_size - found_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 510 | } else if (ret) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 511 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 512 | } |
| 513 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], |
| 514 | path->slots[0]); |
| 515 | |
| 516 | /* don't overwrite an existing inode if the generation number |
| 517 | * was logged as zero. This is done when the tree logging code |
| 518 | * is just logging an inode to make sure it exists after recovery. |
| 519 | * |
| 520 | * Also, don't overwrite i_size on directories during replay. |
| 521 | * log replay inserts and removes directory items based on the |
| 522 | * state of the tree found in the subvolume, and i_size is modified |
| 523 | * as it goes |
| 524 | */ |
| 525 | if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) { |
| 526 | struct btrfs_inode_item *src_item; |
| 527 | struct btrfs_inode_item *dst_item; |
| 528 | |
| 529 | src_item = (struct btrfs_inode_item *)src_ptr; |
| 530 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 531 | |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 532 | if (btrfs_inode_generation(eb, src_item) == 0) { |
| 533 | struct extent_buffer *dst_eb = path->nodes[0]; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 534 | const u64 ino_size = btrfs_inode_size(eb, src_item); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 535 | |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 536 | /* |
| 537 | * For regular files an ino_size == 0 is used only when |
| 538 | * logging that an inode exists, as part of a directory |
| 539 | * fsync, and the inode wasn't fsynced before. In this |
| 540 | * case don't set the size of the inode in the fs/subvol |
| 541 | * tree, otherwise we would be throwing valid data away. |
| 542 | */ |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 543 | if (S_ISREG(btrfs_inode_mode(eb, src_item)) && |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 544 | S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) && |
David Sterba | 60d48e2 | 2020-04-29 15:29:53 +0200 | [diff] [blame] | 545 | ino_size != 0) |
| 546 | btrfs_set_inode_size(dst_eb, dst_item, ino_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 547 | goto no_copy; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 548 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 549 | |
Filipe Manana | 3eb4234 | 2022-11-21 10:23:24 +0000 | [diff] [blame] | 550 | if (S_ISDIR(btrfs_inode_mode(eb, src_item)) && |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 551 | S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { |
| 552 | save_old_i_size = 1; |
| 553 | saved_i_size = btrfs_inode_size(path->nodes[0], |
| 554 | dst_item); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | copy_extent_buffer(path->nodes[0], eb, dst_ptr, |
| 559 | src_ptr, item_size); |
| 560 | |
| 561 | if (save_old_i_size) { |
| 562 | struct btrfs_inode_item *dst_item; |
| 563 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 564 | btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); |
| 565 | } |
| 566 | |
| 567 | /* make sure the generation is filled in */ |
| 568 | if (key->type == BTRFS_INODE_ITEM_KEY) { |
| 569 | struct btrfs_inode_item *dst_item; |
| 570 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 571 | if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { |
| 572 | btrfs_set_inode_generation(path->nodes[0], dst_item, |
| 573 | trans->transid); |
| 574 | } |
| 575 | } |
| 576 | no_copy: |
| 577 | btrfs_mark_buffer_dirty(path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 578 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 579 | return 0; |
| 580 | } |
| 581 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 582 | static int read_alloc_one_name(struct extent_buffer *eb, void *start, int len, |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 583 | struct fscrypt_str *name) |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 584 | { |
| 585 | char *buf; |
| 586 | |
| 587 | buf = kmalloc(len, GFP_NOFS); |
| 588 | if (!buf) |
| 589 | return -ENOMEM; |
| 590 | |
| 591 | read_extent_buffer(eb, buf, (unsigned long)start, len); |
| 592 | name->name = buf; |
| 593 | name->len = len; |
| 594 | return 0; |
| 595 | } |
| 596 | |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 597 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 598 | * simple helper to read an inode off the disk from a given root |
| 599 | * This can only be called for subvolume roots and not for the log |
| 600 | */ |
| 601 | static noinline struct inode *read_one_inode(struct btrfs_root *root, |
| 602 | u64 objectid) |
| 603 | { |
| 604 | struct inode *inode; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 605 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 606 | inode = btrfs_iget(root->fs_info->sb, objectid, root); |
Al Viro | 2e19f1f | 2018-07-29 23:04:45 +0100 | [diff] [blame] | 607 | if (IS_ERR(inode)) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 608 | inode = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 609 | return inode; |
| 610 | } |
| 611 | |
| 612 | /* replays a single extent in 'eb' at 'slot' with 'key' into the |
| 613 | * subvolume 'root'. path is released on entry and should be released |
| 614 | * on exit. |
| 615 | * |
| 616 | * extents in the log tree have not been allocated out of the extent |
| 617 | * tree yet. So, this completes the allocation, taking a reference |
| 618 | * as required if the extent already exists or creating a new extent |
| 619 | * if it isn't in the extent allocation tree yet. |
| 620 | * |
| 621 | * The extent is inserted into the file, dropping any existing extents |
| 622 | * from the file that overlap the new one. |
| 623 | */ |
| 624 | static noinline int replay_one_extent(struct btrfs_trans_handle *trans, |
| 625 | struct btrfs_root *root, |
| 626 | struct btrfs_path *path, |
| 627 | struct extent_buffer *eb, int slot, |
| 628 | struct btrfs_key *key) |
| 629 | { |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 630 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 631 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 632 | int found_type; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 633 | u64 extent_end; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 634 | u64 start = key->offset; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 635 | u64 nbytes = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 636 | struct btrfs_file_extent_item *item; |
| 637 | struct inode *inode = NULL; |
| 638 | unsigned long size; |
| 639 | int ret = 0; |
| 640 | |
| 641 | item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 642 | found_type = btrfs_file_extent_type(eb, item); |
| 643 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 644 | if (found_type == BTRFS_FILE_EXTENT_REG || |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 645 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 646 | nbytes = btrfs_file_extent_num_bytes(eb, item); |
| 647 | extent_end = start + nbytes; |
| 648 | |
| 649 | /* |
| 650 | * We don't add to the inodes nbytes if we are prealloc or a |
| 651 | * hole. |
| 652 | */ |
| 653 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 654 | nbytes = 0; |
| 655 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 656 | size = btrfs_file_extent_ram_bytes(eb, item); |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 657 | nbytes = btrfs_file_extent_ram_bytes(eb, item); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 658 | extent_end = ALIGN(start + size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 659 | fs_info->sectorsize); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 660 | } else { |
| 661 | ret = 0; |
| 662 | goto out; |
| 663 | } |
| 664 | |
| 665 | inode = read_one_inode(root, key->objectid); |
| 666 | if (!inode) { |
| 667 | ret = -EIO; |
| 668 | goto out; |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * first check to see if we already have this extent in the |
| 673 | * file. This must be done before the btrfs_drop_extents run |
| 674 | * so we don't try to drop this extent. |
| 675 | */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 676 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 677 | btrfs_ino(BTRFS_I(inode)), start, 0); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 678 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 679 | if (ret == 0 && |
| 680 | (found_type == BTRFS_FILE_EXTENT_REG || |
| 681 | found_type == BTRFS_FILE_EXTENT_PREALLOC)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 682 | struct btrfs_file_extent_item cmp1; |
| 683 | struct btrfs_file_extent_item cmp2; |
| 684 | struct btrfs_file_extent_item *existing; |
| 685 | struct extent_buffer *leaf; |
| 686 | |
| 687 | leaf = path->nodes[0]; |
| 688 | existing = btrfs_item_ptr(leaf, path->slots[0], |
| 689 | struct btrfs_file_extent_item); |
| 690 | |
| 691 | read_extent_buffer(eb, &cmp1, (unsigned long)item, |
| 692 | sizeof(cmp1)); |
| 693 | read_extent_buffer(leaf, &cmp2, (unsigned long)existing, |
| 694 | sizeof(cmp2)); |
| 695 | |
| 696 | /* |
| 697 | * we already have a pointer to this exact extent, |
| 698 | * we don't have to do anything |
| 699 | */ |
| 700 | if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 701 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 702 | goto out; |
| 703 | } |
| 704 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 705 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 706 | |
| 707 | /* drop any overlapping extents */ |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 708 | drop_args.start = start; |
| 709 | drop_args.end = extent_end; |
| 710 | drop_args.drop_cache = true; |
| 711 | ret = btrfs_drop_extents(trans, root, BTRFS_I(inode), &drop_args); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 712 | if (ret) |
| 713 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 714 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 715 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 716 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 717 | u64 offset; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 718 | unsigned long dest_offset; |
| 719 | struct btrfs_key ins; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 720 | |
Filipe Manana | 3168021c | 2017-02-01 14:58:02 +0000 | [diff] [blame] | 721 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0 && |
| 722 | btrfs_fs_incompat(fs_info, NO_HOLES)) |
| 723 | goto update_inode; |
| 724 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 725 | ret = btrfs_insert_empty_item(trans, root, path, key, |
| 726 | sizeof(*item)); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 727 | if (ret) |
| 728 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 729 | dest_offset = btrfs_item_ptr_offset(path->nodes[0], |
| 730 | path->slots[0]); |
| 731 | copy_extent_buffer(path->nodes[0], eb, dest_offset, |
| 732 | (unsigned long)item, sizeof(*item)); |
| 733 | |
| 734 | ins.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 735 | ins.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
| 736 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 737 | offset = key->offset - btrfs_file_extent_offset(eb, item); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 738 | |
Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 739 | /* |
| 740 | * Manually record dirty extent, as here we did a shallow |
| 741 | * file extent item copy and skip normal backref update, |
| 742 | * but modifying extent tree all by ourselves. |
| 743 | * So need to manually record dirty extent for qgroup, |
| 744 | * as the owner of the file extent changed from log tree |
| 745 | * (doesn't affect qgroup) to fs/file tree(affects qgroup) |
| 746 | */ |
Lu Fengqi | a95f3aa | 2018-07-18 16:28:03 +0800 | [diff] [blame] | 747 | ret = btrfs_qgroup_trace_extent(trans, |
Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 748 | btrfs_file_extent_disk_bytenr(eb, item), |
David Sterba | e2896e7 | 2022-10-14 15:55:09 +0200 | [diff] [blame] | 749 | btrfs_file_extent_disk_num_bytes(eb, item)); |
Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 750 | if (ret < 0) |
| 751 | goto out; |
| 752 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 753 | if (ins.objectid > 0) { |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 754 | struct btrfs_ref ref = { 0 }; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 755 | u64 csum_start; |
| 756 | u64 csum_end; |
| 757 | LIST_HEAD(ordered_sums); |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 758 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 759 | /* |
| 760 | * is this extent already allocated in the extent |
| 761 | * allocation tree? If so, just add a reference |
| 762 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 763 | ret = btrfs_lookup_data_extent(fs_info, ins.objectid, |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 764 | ins.offset); |
Marcos Paulo de Souza | 3736127 | 2021-08-02 09:34:00 -0300 | [diff] [blame] | 765 | if (ret < 0) { |
| 766 | goto out; |
| 767 | } else if (ret == 0) { |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 768 | btrfs_init_generic_ref(&ref, |
| 769 | BTRFS_ADD_DELAYED_REF, |
| 770 | ins.objectid, ins.offset, 0); |
| 771 | btrfs_init_data_ref(&ref, |
| 772 | root->root_key.objectid, |
Nikolay Borisov | f42c5da | 2021-10-12 11:21:35 +0300 | [diff] [blame] | 773 | key->objectid, offset, 0, false); |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 774 | ret = btrfs_inc_extent_ref(trans, &ref); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 775 | if (ret) |
| 776 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 777 | } else { |
| 778 | /* |
| 779 | * insert the extent pointer in the extent |
| 780 | * allocation tree |
| 781 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 782 | ret = btrfs_alloc_logged_file_extent(trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 783 | root->root_key.objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 784 | key->objectid, offset, &ins); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 785 | if (ret) |
| 786 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 787 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 788 | btrfs_release_path(path); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 789 | |
| 790 | if (btrfs_file_extent_compression(eb, item)) { |
| 791 | csum_start = ins.objectid; |
| 792 | csum_end = csum_start + ins.offset; |
| 793 | } else { |
| 794 | csum_start = ins.objectid + |
| 795 | btrfs_file_extent_offset(eb, item); |
| 796 | csum_end = csum_start + |
| 797 | btrfs_file_extent_num_bytes(eb, item); |
| 798 | } |
| 799 | |
Qu Wenruo | 97e3823 | 2022-11-14 08:26:32 +0800 | [diff] [blame] | 800 | ret = btrfs_lookup_csums_list(root->log_root, |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 801 | csum_start, csum_end - 1, |
Josef Bacik | 26ce911 | 2022-09-12 12:27:43 -0700 | [diff] [blame] | 802 | &ordered_sums, 0, false); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 803 | if (ret) |
| 804 | goto out; |
Filipe Manana | b84b839 | 2015-08-19 11:09:40 +0100 | [diff] [blame] | 805 | /* |
| 806 | * Now delete all existing cums in the csum root that |
| 807 | * cover our range. We do this because we can have an |
| 808 | * extent that is completely referenced by one file |
| 809 | * extent item and partially referenced by another |
| 810 | * file extent item (like after using the clone or |
| 811 | * extent_same ioctls). In this case if we end up doing |
| 812 | * the replay of the one that partially references the |
| 813 | * extent first, and we do not do the csum deletion |
| 814 | * below, we can get 2 csum items in the csum tree that |
| 815 | * overlap each other. For example, imagine our log has |
| 816 | * the two following file extent items: |
| 817 | * |
| 818 | * key (257 EXTENT_DATA 409600) |
| 819 | * extent data disk byte 12845056 nr 102400 |
| 820 | * extent data offset 20480 nr 20480 ram 102400 |
| 821 | * |
| 822 | * key (257 EXTENT_DATA 819200) |
| 823 | * extent data disk byte 12845056 nr 102400 |
| 824 | * extent data offset 0 nr 102400 ram 102400 |
| 825 | * |
| 826 | * Where the second one fully references the 100K extent |
| 827 | * that starts at disk byte 12845056, and the log tree |
| 828 | * has a single csum item that covers the entire range |
| 829 | * of the extent: |
| 830 | * |
| 831 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 |
| 832 | * |
| 833 | * After the first file extent item is replayed, the |
| 834 | * csum tree gets the following csum item: |
| 835 | * |
| 836 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 |
| 837 | * |
| 838 | * Which covers the 20K sub-range starting at offset 20K |
| 839 | * of our extent. Now when we replay the second file |
| 840 | * extent item, if we do not delete existing csum items |
| 841 | * that cover any of its blocks, we end up getting two |
| 842 | * csum items in our csum tree that overlap each other: |
| 843 | * |
| 844 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 |
| 845 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 |
| 846 | * |
| 847 | * Which is a problem, because after this anyone trying |
| 848 | * to lookup up for the checksum of any block of our |
| 849 | * extent starting at an offset of 40K or higher, will |
| 850 | * end up looking at the second csum item only, which |
| 851 | * does not contain the checksum for any block starting |
| 852 | * at offset 40K or higher of our extent. |
| 853 | */ |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 854 | while (!list_empty(&ordered_sums)) { |
| 855 | struct btrfs_ordered_sum *sums; |
Josef Bacik | fc28b25 | 2021-11-05 16:45:48 -0400 | [diff] [blame] | 856 | struct btrfs_root *csum_root; |
| 857 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 858 | sums = list_entry(ordered_sums.next, |
| 859 | struct btrfs_ordered_sum, |
| 860 | list); |
Josef Bacik | fc28b25 | 2021-11-05 16:45:48 -0400 | [diff] [blame] | 861 | csum_root = btrfs_csum_root(fs_info, |
| 862 | sums->bytenr); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 863 | if (!ret) |
Josef Bacik | fc28b25 | 2021-11-05 16:45:48 -0400 | [diff] [blame] | 864 | ret = btrfs_del_csums(trans, csum_root, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 865 | sums->bytenr, |
| 866 | sums->len); |
Filipe Manana | b84b839 | 2015-08-19 11:09:40 +0100 | [diff] [blame] | 867 | if (!ret) |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 868 | ret = btrfs_csum_file_blocks(trans, |
Josef Bacik | fc28b25 | 2021-11-05 16:45:48 -0400 | [diff] [blame] | 869 | csum_root, |
| 870 | sums); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 871 | list_del(&sums->list); |
| 872 | kfree(sums); |
| 873 | } |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 874 | if (ret) |
| 875 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 876 | } else { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 877 | btrfs_release_path(path); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 878 | } |
| 879 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 880 | /* inline extents are easy, we just overwrite them */ |
| 881 | ret = overwrite_item(trans, root, path, eb, slot, key); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 882 | if (ret) |
| 883 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 884 | } |
| 885 | |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 886 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, |
| 887 | extent_end - start); |
| 888 | if (ret) |
| 889 | goto out; |
| 890 | |
Filipe Manana | 3168021c | 2017-02-01 14:58:02 +0000 | [diff] [blame] | 891 | update_inode: |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 892 | btrfs_update_inode_bytes(BTRFS_I(inode), nbytes, drop_args.bytes_found); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 893 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 894 | out: |
Lv Ruyi | 8aa1e49 | 2022-04-11 03:22:52 +0000 | [diff] [blame] | 895 | iput(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 896 | return ret; |
| 897 | } |
| 898 | |
Filipe Manana | 313ab75 | 2022-02-28 16:29:29 +0000 | [diff] [blame] | 899 | static int unlink_inode_for_log_replay(struct btrfs_trans_handle *trans, |
| 900 | struct btrfs_inode *dir, |
| 901 | struct btrfs_inode *inode, |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 902 | const struct fscrypt_str *name) |
Filipe Manana | 313ab75 | 2022-02-28 16:29:29 +0000 | [diff] [blame] | 903 | { |
| 904 | int ret; |
| 905 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 906 | ret = btrfs_unlink_inode(trans, dir, inode, name); |
Filipe Manana | 313ab75 | 2022-02-28 16:29:29 +0000 | [diff] [blame] | 907 | if (ret) |
| 908 | return ret; |
| 909 | /* |
| 910 | * Whenever we need to check if a name exists or not, we check the |
| 911 | * fs/subvolume tree. So after an unlink we must run delayed items, so |
| 912 | * that future checks for a name during log replay see that the name |
| 913 | * does not exists anymore. |
| 914 | */ |
| 915 | return btrfs_run_delayed_items(trans); |
| 916 | } |
| 917 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 918 | /* |
| 919 | * when cleaning up conflicts between the directory names in the |
| 920 | * subvolume, directory names in the log and directory names in the |
| 921 | * inode back references, we may have to unlink inodes from directories. |
| 922 | * |
| 923 | * This is a helper function to do the unlink of a specific directory |
| 924 | * item |
| 925 | */ |
| 926 | static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 927 | struct btrfs_path *path, |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 928 | struct btrfs_inode *dir, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 929 | struct btrfs_dir_item *di) |
| 930 | { |
Filipe Manana | 9798ba2 | 2021-10-25 17:31:49 +0100 | [diff] [blame] | 931 | struct btrfs_root *root = dir->root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 932 | struct inode *inode; |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 933 | struct fscrypt_str name; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 934 | struct extent_buffer *leaf; |
| 935 | struct btrfs_key location; |
| 936 | int ret; |
| 937 | |
| 938 | leaf = path->nodes[0]; |
| 939 | |
| 940 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 941 | ret = read_alloc_one_name(leaf, di + 1, btrfs_dir_name_len(leaf, di), &name); |
| 942 | if (ret) |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 943 | return -ENOMEM; |
| 944 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 945 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 946 | |
| 947 | inode = read_one_inode(root, location.objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 948 | if (!inode) { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 949 | ret = -EIO; |
| 950 | goto out; |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 951 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 952 | |
Yan Zheng | ec051c0 | 2009-01-05 15:43:42 -0500 | [diff] [blame] | 953 | ret = link_to_fixup_dir(trans, root, path, location.objectid); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 954 | if (ret) |
| 955 | goto out; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 956 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 957 | ret = unlink_inode_for_log_replay(trans, dir, BTRFS_I(inode), &name); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 958 | out: |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 959 | kfree(name.name); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 960 | iput(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 961 | return ret; |
| 962 | } |
| 963 | |
| 964 | /* |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 965 | * See if a given name and sequence number found in an inode back reference are |
| 966 | * already in a directory and correctly point to this inode. |
| 967 | * |
| 968 | * Returns: < 0 on error, 0 if the directory entry does not exists and 1 if it |
| 969 | * exists. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 970 | */ |
| 971 | static noinline int inode_in_dir(struct btrfs_root *root, |
| 972 | struct btrfs_path *path, |
| 973 | u64 dirid, u64 objectid, u64 index, |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 974 | struct fscrypt_str *name) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 975 | { |
| 976 | struct btrfs_dir_item *di; |
| 977 | struct btrfs_key location; |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 978 | int ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 979 | |
| 980 | di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 981 | index, name, 0); |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 982 | if (IS_ERR(di)) { |
Filipe Manana | 8dcbc26 | 2021-10-01 13:52:33 +0100 | [diff] [blame] | 983 | ret = PTR_ERR(di); |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 984 | goto out; |
| 985 | } else if (di) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 986 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
| 987 | if (location.objectid != objectid) |
| 988 | goto out; |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 989 | } else { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 990 | goto out; |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 991 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 992 | |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 993 | btrfs_release_path(path); |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 994 | di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, 0); |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 995 | if (IS_ERR(di)) { |
| 996 | ret = PTR_ERR(di); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 997 | goto out; |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 998 | } else if (di) { |
| 999 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
| 1000 | if (location.objectid == objectid) |
| 1001 | ret = 1; |
| 1002 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1003 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1004 | btrfs_release_path(path); |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 1005 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * helper function to check a log tree for a named back reference in |
| 1010 | * an inode. This is used to decide if a back reference that is |
| 1011 | * found in the subvolume conflicts with what we find in the log. |
| 1012 | * |
| 1013 | * inode backreferences may have multiple refs in a single item, |
| 1014 | * during replay we process one reference at a time, and we don't |
| 1015 | * want to delete valid links to a file from the subvolume if that |
| 1016 | * link is also in the log. |
| 1017 | */ |
| 1018 | static noinline int backref_in_log(struct btrfs_root *log, |
| 1019 | struct btrfs_key *key, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1020 | u64 ref_objectid, |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 1021 | const struct fscrypt_str *name) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1022 | { |
| 1023 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1024 | int ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1025 | |
| 1026 | path = btrfs_alloc_path(); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 1027 | if (!path) |
| 1028 | return -ENOMEM; |
| 1029 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1030 | ret = btrfs_search_slot(NULL, log, key, path, 0, 0); |
Nikolay Borisov | d3316c8 | 2019-09-25 14:03:03 +0300 | [diff] [blame] | 1031 | if (ret < 0) { |
| 1032 | goto out; |
| 1033 | } else if (ret == 1) { |
Nikolay Borisov | 89cbf5f6b | 2019-08-30 17:44:47 +0300 | [diff] [blame] | 1034 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1035 | goto out; |
Nikolay Borisov | 89cbf5f6b | 2019-08-30 17:44:47 +0300 | [diff] [blame] | 1036 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1037 | |
Nikolay Borisov | 89cbf5f6b | 2019-08-30 17:44:47 +0300 | [diff] [blame] | 1038 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
| 1039 | ret = !!btrfs_find_name_in_ext_backref(path->nodes[0], |
| 1040 | path->slots[0], |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1041 | ref_objectid, name); |
Nikolay Borisov | 89cbf5f6b | 2019-08-30 17:44:47 +0300 | [diff] [blame] | 1042 | else |
| 1043 | ret = !!btrfs_find_name_in_backref(path->nodes[0], |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1044 | path->slots[0], name); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1045 | out: |
| 1046 | btrfs_free_path(path); |
Nikolay Borisov | 89cbf5f6b | 2019-08-30 17:44:47 +0300 | [diff] [blame] | 1047 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1048 | } |
| 1049 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1050 | static inline int __add_inode_ref(struct btrfs_trans_handle *trans, |
| 1051 | struct btrfs_root *root, |
| 1052 | struct btrfs_path *path, |
| 1053 | struct btrfs_root *log_root, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1054 | struct btrfs_inode *dir, |
| 1055 | struct btrfs_inode *inode, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1056 | u64 inode_objectid, u64 parent_objectid, |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 1057 | u64 ref_index, struct fscrypt_str *name) |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1058 | { |
| 1059 | int ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1060 | struct extent_buffer *leaf; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1061 | struct btrfs_dir_item *di; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1062 | struct btrfs_key search_key; |
| 1063 | struct btrfs_inode_extref *extref; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1064 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1065 | again: |
| 1066 | /* Search old style refs */ |
| 1067 | search_key.objectid = inode_objectid; |
| 1068 | search_key.type = BTRFS_INODE_REF_KEY; |
| 1069 | search_key.offset = parent_objectid; |
| 1070 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1071 | if (ret == 0) { |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1072 | struct btrfs_inode_ref *victim_ref; |
| 1073 | unsigned long ptr; |
| 1074 | unsigned long ptr_end; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1075 | |
| 1076 | leaf = path->nodes[0]; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1077 | |
| 1078 | /* are we trying to overwrite a back ref for the root directory |
| 1079 | * if so, just jump out, we're done |
| 1080 | */ |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1081 | if (search_key.objectid == search_key.offset) |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1082 | return 1; |
| 1083 | |
| 1084 | /* check all the names in this back reference to see |
| 1085 | * if they are in the log. if so, we allow them to stay |
| 1086 | * otherwise they must be unlinked as a conflict |
| 1087 | */ |
| 1088 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 1089 | ptr_end = ptr + btrfs_item_size(leaf, path->slots[0]); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1090 | while (ptr < ptr_end) { |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 1091 | struct fscrypt_str victim_name; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1092 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1093 | victim_ref = (struct btrfs_inode_ref *)ptr; |
| 1094 | ret = read_alloc_one_name(leaf, (victim_ref + 1), |
| 1095 | btrfs_inode_ref_name_len(leaf, victim_ref), |
| 1096 | &victim_name); |
| 1097 | if (ret) |
| 1098 | return ret; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1099 | |
Nikolay Borisov | d3316c8 | 2019-09-25 14:03:03 +0300 | [diff] [blame] | 1100 | ret = backref_in_log(log_root, &search_key, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1101 | parent_objectid, &victim_name); |
Nikolay Borisov | d3316c8 | 2019-09-25 14:03:03 +0300 | [diff] [blame] | 1102 | if (ret < 0) { |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1103 | kfree(victim_name.name); |
Nikolay Borisov | d3316c8 | 2019-09-25 14:03:03 +0300 | [diff] [blame] | 1104 | return ret; |
| 1105 | } else if (!ret) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1106 | inc_nlink(&inode->vfs_inode); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1107 | btrfs_release_path(path); |
| 1108 | |
Filipe Manana | 313ab75 | 2022-02-28 16:29:29 +0000 | [diff] [blame] | 1109 | ret = unlink_inode_for_log_replay(trans, dir, inode, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1110 | &victim_name); |
| 1111 | kfree(victim_name.name); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1112 | if (ret) |
| 1113 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1114 | goto again; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1115 | } |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1116 | kfree(victim_name.name); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1117 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1118 | ptr = (unsigned long)(victim_ref + 1) + victim_name.len; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1119 | } |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1120 | } |
| 1121 | btrfs_release_path(path); |
| 1122 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1123 | /* Same search but for extended refs */ |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1124 | extref = btrfs_lookup_inode_extref(NULL, root, path, name, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1125 | inode_objectid, parent_objectid, 0, |
| 1126 | 0); |
Filipe Manana | 7a6b75b | 2022-08-01 14:57:51 +0100 | [diff] [blame] | 1127 | if (IS_ERR(extref)) { |
| 1128 | return PTR_ERR(extref); |
| 1129 | } else if (extref) { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1130 | u32 item_size; |
| 1131 | u32 cur_offset = 0; |
| 1132 | unsigned long base; |
| 1133 | struct inode *victim_parent; |
| 1134 | |
| 1135 | leaf = path->nodes[0]; |
| 1136 | |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 1137 | item_size = btrfs_item_size(leaf, path->slots[0]); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1138 | base = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1139 | |
| 1140 | while (cur_offset < item_size) { |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 1141 | struct fscrypt_str victim_name; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1142 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1143 | extref = (struct btrfs_inode_extref *)(base + cur_offset); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1144 | |
| 1145 | if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid) |
| 1146 | goto next; |
| 1147 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1148 | ret = read_alloc_one_name(leaf, &extref->name, |
| 1149 | btrfs_inode_extref_name_len(leaf, extref), |
| 1150 | &victim_name); |
| 1151 | if (ret) |
| 1152 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1153 | |
| 1154 | search_key.objectid = inode_objectid; |
| 1155 | search_key.type = BTRFS_INODE_EXTREF_KEY; |
| 1156 | search_key.offset = btrfs_extref_hash(parent_objectid, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1157 | victim_name.name, |
| 1158 | victim_name.len); |
Nikolay Borisov | d3316c8 | 2019-09-25 14:03:03 +0300 | [diff] [blame] | 1159 | ret = backref_in_log(log_root, &search_key, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1160 | parent_objectid, &victim_name); |
Nikolay Borisov | d3316c8 | 2019-09-25 14:03:03 +0300 | [diff] [blame] | 1161 | if (ret < 0) { |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1162 | kfree(victim_name.name); |
Nikolay Borisov | d3316c8 | 2019-09-25 14:03:03 +0300 | [diff] [blame] | 1163 | return ret; |
| 1164 | } else if (!ret) { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1165 | ret = -ENOENT; |
| 1166 | victim_parent = read_one_inode(root, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1167 | parent_objectid); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1168 | if (victim_parent) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1169 | inc_nlink(&inode->vfs_inode); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1170 | btrfs_release_path(path); |
| 1171 | |
Filipe Manana | 313ab75 | 2022-02-28 16:29:29 +0000 | [diff] [blame] | 1172 | ret = unlink_inode_for_log_replay(trans, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1173 | BTRFS_I(victim_parent), |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1174 | inode, &victim_name); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1175 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1176 | iput(victim_parent); |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1177 | kfree(victim_name.name); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1178 | if (ret) |
| 1179 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1180 | goto again; |
| 1181 | } |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1182 | kfree(victim_name.name); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1183 | next: |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1184 | cur_offset += victim_name.len + sizeof(*extref); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1185 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1186 | } |
| 1187 | btrfs_release_path(path); |
| 1188 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1189 | /* look for a conflicting sequence number */ |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1190 | di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir), |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1191 | ref_index, name, 0); |
Filipe Manana | 52db777 | 2021-10-01 13:52:32 +0100 | [diff] [blame] | 1192 | if (IS_ERR(di)) { |
Filipe Manana | 8dcbc26 | 2021-10-01 13:52:33 +0100 | [diff] [blame] | 1193 | return PTR_ERR(di); |
Filipe Manana | 52db777 | 2021-10-01 13:52:32 +0100 | [diff] [blame] | 1194 | } else if (di) { |
Filipe Manana | 9798ba2 | 2021-10-25 17:31:49 +0100 | [diff] [blame] | 1195 | ret = drop_one_dir_item(trans, path, dir, di); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1196 | if (ret) |
| 1197 | return ret; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1198 | } |
| 1199 | btrfs_release_path(path); |
| 1200 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 1201 | /* look for a conflicting name */ |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1202 | di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), name, 0); |
Filipe Manana | 52db777 | 2021-10-01 13:52:32 +0100 | [diff] [blame] | 1203 | if (IS_ERR(di)) { |
| 1204 | return PTR_ERR(di); |
| 1205 | } else if (di) { |
Filipe Manana | 9798ba2 | 2021-10-25 17:31:49 +0100 | [diff] [blame] | 1206 | ret = drop_one_dir_item(trans, path, dir, di); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1207 | if (ret) |
| 1208 | return ret; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1209 | } |
| 1210 | btrfs_release_path(path); |
| 1211 | |
| 1212 | return 0; |
| 1213 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1214 | |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1215 | static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 1216 | struct fscrypt_str *name, u64 *index, |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1217 | u64 *parent_objectid) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1218 | { |
| 1219 | struct btrfs_inode_extref *extref; |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1220 | int ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1221 | |
| 1222 | extref = (struct btrfs_inode_extref *)ref_ptr; |
| 1223 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1224 | ret = read_alloc_one_name(eb, &extref->name, |
| 1225 | btrfs_inode_extref_name_len(eb, extref), name); |
| 1226 | if (ret) |
| 1227 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1228 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1229 | if (index) |
| 1230 | *index = btrfs_inode_extref_index(eb, extref); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1231 | if (parent_objectid) |
| 1232 | *parent_objectid = btrfs_inode_extref_parent(eb, extref); |
| 1233 | |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1237 | static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 1238 | struct fscrypt_str *name, u64 *index) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1239 | { |
| 1240 | struct btrfs_inode_ref *ref; |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1241 | int ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1242 | |
| 1243 | ref = (struct btrfs_inode_ref *)ref_ptr; |
| 1244 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1245 | ret = read_alloc_one_name(eb, ref + 1, btrfs_inode_ref_name_len(eb, ref), |
| 1246 | name); |
| 1247 | if (ret) |
| 1248 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1249 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1250 | if (index) |
| 1251 | *index = btrfs_inode_ref_index(eb, ref); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1252 | |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1256 | /* |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1257 | * Take an inode reference item from the log tree and iterate all names from the |
| 1258 | * inode reference item in the subvolume tree with the same key (if it exists). |
| 1259 | * For any name that is not in the inode reference item from the log tree, do a |
| 1260 | * proper unlink of that name (that is, remove its entry from the inode |
| 1261 | * reference item and both dir index keys). |
| 1262 | */ |
| 1263 | static int unlink_old_inode_refs(struct btrfs_trans_handle *trans, |
| 1264 | struct btrfs_root *root, |
| 1265 | struct btrfs_path *path, |
| 1266 | struct btrfs_inode *inode, |
| 1267 | struct extent_buffer *log_eb, |
| 1268 | int log_slot, |
| 1269 | struct btrfs_key *key) |
| 1270 | { |
| 1271 | int ret; |
| 1272 | unsigned long ref_ptr; |
| 1273 | unsigned long ref_end; |
| 1274 | struct extent_buffer *eb; |
| 1275 | |
| 1276 | again: |
| 1277 | btrfs_release_path(path); |
| 1278 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 1279 | if (ret > 0) { |
| 1280 | ret = 0; |
| 1281 | goto out; |
| 1282 | } |
| 1283 | if (ret < 0) |
| 1284 | goto out; |
| 1285 | |
| 1286 | eb = path->nodes[0]; |
| 1287 | ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 1288 | ref_end = ref_ptr + btrfs_item_size(eb, path->slots[0]); |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1289 | while (ref_ptr < ref_end) { |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 1290 | struct fscrypt_str name; |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1291 | u64 parent_id; |
| 1292 | |
| 1293 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1294 | ret = extref_get_fields(eb, ref_ptr, &name, |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1295 | NULL, &parent_id); |
| 1296 | } else { |
| 1297 | parent_id = key->offset; |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1298 | ret = ref_get_fields(eb, ref_ptr, &name, NULL); |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1299 | } |
| 1300 | if (ret) |
| 1301 | goto out; |
| 1302 | |
| 1303 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
Nikolay Borisov | 6ff49c6 | 2019-08-27 14:46:29 +0300 | [diff] [blame] | 1304 | ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1305 | parent_id, &name); |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1306 | else |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1307 | ret = !!btrfs_find_name_in_backref(log_eb, log_slot, &name); |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1308 | |
| 1309 | if (!ret) { |
| 1310 | struct inode *dir; |
| 1311 | |
| 1312 | btrfs_release_path(path); |
| 1313 | dir = read_one_inode(root, parent_id); |
| 1314 | if (!dir) { |
| 1315 | ret = -ENOENT; |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1316 | kfree(name.name); |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1317 | goto out; |
| 1318 | } |
Filipe Manana | 313ab75 | 2022-02-28 16:29:29 +0000 | [diff] [blame] | 1319 | ret = unlink_inode_for_log_replay(trans, BTRFS_I(dir), |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1320 | inode, &name); |
| 1321 | kfree(name.name); |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1322 | iput(dir); |
| 1323 | if (ret) |
| 1324 | goto out; |
| 1325 | goto again; |
| 1326 | } |
| 1327 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1328 | kfree(name.name); |
| 1329 | ref_ptr += name.len; |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1330 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
| 1331 | ref_ptr += sizeof(struct btrfs_inode_extref); |
| 1332 | else |
| 1333 | ref_ptr += sizeof(struct btrfs_inode_ref); |
| 1334 | } |
| 1335 | ret = 0; |
| 1336 | out: |
| 1337 | btrfs_release_path(path); |
| 1338 | return ret; |
| 1339 | } |
| 1340 | |
| 1341 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1342 | * replay one inode back reference item found in the log tree. |
| 1343 | * eb, slot and key refer to the buffer and key found in the log tree. |
| 1344 | * root is the destination we are replaying into, and path is for temp |
| 1345 | * use by this function. (it should be released on return). |
| 1346 | */ |
| 1347 | static noinline int add_inode_ref(struct btrfs_trans_handle *trans, |
| 1348 | struct btrfs_root *root, |
| 1349 | struct btrfs_root *log, |
| 1350 | struct btrfs_path *path, |
| 1351 | struct extent_buffer *eb, int slot, |
| 1352 | struct btrfs_key *key) |
| 1353 | { |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1354 | struct inode *dir = NULL; |
| 1355 | struct inode *inode = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1356 | unsigned long ref_ptr; |
| 1357 | unsigned long ref_end; |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 1358 | struct fscrypt_str name; |
liubo | 34f3e4f | 2011-08-06 08:35:23 +0000 | [diff] [blame] | 1359 | int ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1360 | int log_ref_ver = 0; |
| 1361 | u64 parent_objectid; |
| 1362 | u64 inode_objectid; |
Chris Mason | f46dbe3 | 2012-10-09 11:17:20 -0400 | [diff] [blame] | 1363 | u64 ref_index = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1364 | int ref_struct_size; |
| 1365 | |
| 1366 | ref_ptr = btrfs_item_ptr_offset(eb, slot); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 1367 | ref_end = ref_ptr + btrfs_item_size(eb, slot); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1368 | |
| 1369 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
| 1370 | struct btrfs_inode_extref *r; |
| 1371 | |
| 1372 | ref_struct_size = sizeof(struct btrfs_inode_extref); |
| 1373 | log_ref_ver = 1; |
| 1374 | r = (struct btrfs_inode_extref *)ref_ptr; |
| 1375 | parent_objectid = btrfs_inode_extref_parent(eb, r); |
| 1376 | } else { |
| 1377 | ref_struct_size = sizeof(struct btrfs_inode_ref); |
| 1378 | parent_objectid = key->offset; |
| 1379 | } |
| 1380 | inode_objectid = key->objectid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1381 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1382 | /* |
| 1383 | * it is possible that we didn't log all the parent directories |
| 1384 | * for a given inode. If we don't find the dir, just don't |
| 1385 | * copy the back ref in. The link count fixup code will take |
| 1386 | * care of the rest |
| 1387 | */ |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1388 | dir = read_one_inode(root, parent_objectid); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1389 | if (!dir) { |
| 1390 | ret = -ENOENT; |
| 1391 | goto out; |
| 1392 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1393 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1394 | inode = read_one_inode(root, inode_objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1395 | if (!inode) { |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1396 | ret = -EIO; |
| 1397 | goto out; |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1398 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1399 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1400 | while (ref_ptr < ref_end) { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1401 | if (log_ref_ver) { |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1402 | ret = extref_get_fields(eb, ref_ptr, &name, |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1403 | &ref_index, &parent_objectid); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1404 | /* |
| 1405 | * parent object can change from one array |
| 1406 | * item to another. |
| 1407 | */ |
| 1408 | if (!dir) |
| 1409 | dir = read_one_inode(root, parent_objectid); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1410 | if (!dir) { |
| 1411 | ret = -ENOENT; |
| 1412 | goto out; |
| 1413 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1414 | } else { |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1415 | ret = ref_get_fields(eb, ref_ptr, &name, &ref_index); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1416 | } |
| 1417 | if (ret) |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1418 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1419 | |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 1420 | ret = inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)), |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1421 | btrfs_ino(BTRFS_I(inode)), ref_index, &name); |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 1422 | if (ret < 0) { |
| 1423 | goto out; |
| 1424 | } else if (ret == 0) { |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1425 | /* |
| 1426 | * look for a conflicting back reference in the |
| 1427 | * metadata. if we find one we have to unlink that name |
| 1428 | * of the file before we add our new link. Later on, we |
| 1429 | * overwrite any existing back reference, and we don't |
| 1430 | * want to create dangling pointers in the directory. |
| 1431 | */ |
Filipe Manana | 7059c65 | 2022-08-01 14:57:53 +0100 | [diff] [blame] | 1432 | ret = __add_inode_ref(trans, root, path, log, |
| 1433 | BTRFS_I(dir), BTRFS_I(inode), |
| 1434 | inode_objectid, parent_objectid, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1435 | ref_index, &name); |
Filipe Manana | 7059c65 | 2022-08-01 14:57:53 +0100 | [diff] [blame] | 1436 | if (ret) { |
| 1437 | if (ret == 1) |
| 1438 | ret = 0; |
Filipe Manana | 0d83639 | 2018-07-20 10:59:06 +0100 | [diff] [blame] | 1439 | goto out; |
Filipe Manana | 7059c65 | 2022-08-01 14:57:53 +0100 | [diff] [blame] | 1440 | } |
Filipe Manana | 0d83639 | 2018-07-20 10:59:06 +0100 | [diff] [blame] | 1441 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1442 | /* insert our name */ |
Filipe Manana | 7059c65 | 2022-08-01 14:57:53 +0100 | [diff] [blame] | 1443 | ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1444 | &name, 0, ref_index); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1445 | if (ret) |
| 1446 | goto out; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1447 | |
Josef Bacik | f96d447 | 2021-05-19 11:26:25 -0400 | [diff] [blame] | 1448 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
| 1449 | if (ret) |
| 1450 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1451 | } |
Filipe Manana | 77a5b9e | 2021-10-01 13:52:30 +0100 | [diff] [blame] | 1452 | /* Else, ret == 1, we already have a perfect match, we're done. */ |
liubo | c622ae6 | 2011-03-26 08:01:12 -0400 | [diff] [blame] | 1453 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1454 | ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + name.len; |
| 1455 | kfree(name.name); |
| 1456 | name.name = NULL; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1457 | if (log_ref_ver) { |
| 1458 | iput(dir); |
| 1459 | dir = NULL; |
| 1460 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1461 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1462 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1463 | /* |
| 1464 | * Before we overwrite the inode reference item in the subvolume tree |
| 1465 | * with the item from the log tree, we must unlink all names from the |
| 1466 | * parent directory that are in the subvolume's tree inode reference |
| 1467 | * item, otherwise we end up with an inconsistent subvolume tree where |
| 1468 | * dir index entries exist for a name but there is no inode reference |
| 1469 | * item with the same name. |
| 1470 | */ |
| 1471 | ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot, |
| 1472 | key); |
| 1473 | if (ret) |
| 1474 | goto out; |
| 1475 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1476 | /* finally write the back reference in the inode */ |
| 1477 | ret = overwrite_item(trans, root, path, eb, slot, key); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1478 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1479 | btrfs_release_path(path); |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1480 | kfree(name.name); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1481 | iput(dir); |
| 1482 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1483 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1484 | } |
| 1485 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1486 | static int count_inode_extrefs(struct btrfs_root *root, |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1487 | struct btrfs_inode *inode, struct btrfs_path *path) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1488 | { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1489 | int ret = 0; |
| 1490 | int name_len; |
| 1491 | unsigned int nlink = 0; |
| 1492 | u32 item_size; |
| 1493 | u32 cur_offset = 0; |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1494 | u64 inode_objectid = btrfs_ino(inode); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1495 | u64 offset = 0; |
| 1496 | unsigned long ptr; |
| 1497 | struct btrfs_inode_extref *extref; |
| 1498 | struct extent_buffer *leaf; |
| 1499 | |
| 1500 | while (1) { |
| 1501 | ret = btrfs_find_one_extref(root, inode_objectid, offset, path, |
| 1502 | &extref, &offset); |
| 1503 | if (ret) |
| 1504 | break; |
| 1505 | |
| 1506 | leaf = path->nodes[0]; |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 1507 | item_size = btrfs_item_size(leaf, path->slots[0]); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1508 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 1509 | cur_offset = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1510 | |
| 1511 | while (cur_offset < item_size) { |
| 1512 | extref = (struct btrfs_inode_extref *) (ptr + cur_offset); |
| 1513 | name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 1514 | |
| 1515 | nlink++; |
| 1516 | |
| 1517 | cur_offset += name_len + sizeof(*extref); |
| 1518 | } |
| 1519 | |
| 1520 | offset++; |
| 1521 | btrfs_release_path(path); |
| 1522 | } |
| 1523 | btrfs_release_path(path); |
| 1524 | |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 1525 | if (ret < 0 && ret != -ENOENT) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1526 | return ret; |
| 1527 | return nlink; |
| 1528 | } |
| 1529 | |
| 1530 | static int count_inode_refs(struct btrfs_root *root, |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1531 | struct btrfs_inode *inode, struct btrfs_path *path) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1532 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1533 | int ret; |
| 1534 | struct btrfs_key key; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1535 | unsigned int nlink = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1536 | unsigned long ptr; |
| 1537 | unsigned long ptr_end; |
| 1538 | int name_len; |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1539 | u64 ino = btrfs_ino(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1540 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1541 | key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1542 | key.type = BTRFS_INODE_REF_KEY; |
| 1543 | key.offset = (u64)-1; |
| 1544 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1545 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1546 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1547 | if (ret < 0) |
| 1548 | break; |
| 1549 | if (ret > 0) { |
| 1550 | if (path->slots[0] == 0) |
| 1551 | break; |
| 1552 | path->slots[0]--; |
| 1553 | } |
Filipe David Borba Manana | e93ae26 | 2013-10-14 22:49:11 +0100 | [diff] [blame] | 1554 | process_slot: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1555 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1556 | path->slots[0]); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1557 | if (key.objectid != ino || |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1558 | key.type != BTRFS_INODE_REF_KEY) |
| 1559 | break; |
| 1560 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 1561 | ptr_end = ptr + btrfs_item_size(path->nodes[0], |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1562 | path->slots[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1563 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1564 | struct btrfs_inode_ref *ref; |
| 1565 | |
| 1566 | ref = (struct btrfs_inode_ref *)ptr; |
| 1567 | name_len = btrfs_inode_ref_name_len(path->nodes[0], |
| 1568 | ref); |
| 1569 | ptr = (unsigned long)(ref + 1) + name_len; |
| 1570 | nlink++; |
| 1571 | } |
| 1572 | |
| 1573 | if (key.offset == 0) |
| 1574 | break; |
Filipe David Borba Manana | e93ae26 | 2013-10-14 22:49:11 +0100 | [diff] [blame] | 1575 | if (path->slots[0] > 0) { |
| 1576 | path->slots[0]--; |
| 1577 | goto process_slot; |
| 1578 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1579 | key.offset--; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1580 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1581 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1582 | btrfs_release_path(path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1583 | |
| 1584 | return nlink; |
| 1585 | } |
| 1586 | |
| 1587 | /* |
| 1588 | * There are a few corners where the link count of the file can't |
| 1589 | * be properly maintained during replay. So, instead of adding |
| 1590 | * lots of complexity to the log code, we just scan the backrefs |
| 1591 | * for any file that has been through replay. |
| 1592 | * |
| 1593 | * The scan will update the link count on the inode to reflect the |
| 1594 | * number of back refs found. If it goes down to zero, the iput |
| 1595 | * will free the inode. |
| 1596 | */ |
| 1597 | static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, |
| 1598 | struct btrfs_root *root, |
| 1599 | struct inode *inode) |
| 1600 | { |
| 1601 | struct btrfs_path *path; |
| 1602 | int ret; |
| 1603 | u64 nlink = 0; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1604 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1605 | |
| 1606 | path = btrfs_alloc_path(); |
| 1607 | if (!path) |
| 1608 | return -ENOMEM; |
| 1609 | |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1610 | ret = count_inode_refs(root, BTRFS_I(inode), path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1611 | if (ret < 0) |
| 1612 | goto out; |
| 1613 | |
| 1614 | nlink = ret; |
| 1615 | |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1616 | ret = count_inode_extrefs(root, BTRFS_I(inode), path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1617 | if (ret < 0) |
| 1618 | goto out; |
| 1619 | |
| 1620 | nlink += ret; |
| 1621 | |
| 1622 | ret = 0; |
| 1623 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1624 | if (nlink != inode->i_nlink) { |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 1625 | set_nlink(inode, nlink); |
Josef Bacik | f96d447 | 2021-05-19 11:26:25 -0400 | [diff] [blame] | 1626 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
| 1627 | if (ret) |
| 1628 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1629 | } |
Chris Mason | 8d5bf1c | 2008-09-11 15:51:21 -0400 | [diff] [blame] | 1630 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1631 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1632 | if (inode->i_nlink == 0) { |
| 1633 | if (S_ISDIR(inode->i_mode)) { |
| 1634 | ret = replay_dir_deletes(trans, root, NULL, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1635 | ino, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1636 | if (ret) |
| 1637 | goto out; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1638 | } |
Nikolay Borisov | ecdcf3c | 2020-10-22 18:40:46 +0300 | [diff] [blame] | 1639 | ret = btrfs_insert_orphan_item(trans, root, ino); |
| 1640 | if (ret == -EEXIST) |
| 1641 | ret = 0; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1642 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1643 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1644 | out: |
| 1645 | btrfs_free_path(path); |
| 1646 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, |
| 1650 | struct btrfs_root *root, |
| 1651 | struct btrfs_path *path) |
| 1652 | { |
| 1653 | int ret; |
| 1654 | struct btrfs_key key; |
| 1655 | struct inode *inode; |
| 1656 | |
| 1657 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; |
| 1658 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
| 1659 | key.offset = (u64)-1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1660 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1661 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1662 | if (ret < 0) |
| 1663 | break; |
| 1664 | |
| 1665 | if (ret == 1) { |
Josef Bacik | 011b28a | 2021-05-19 13:13:15 -0400 | [diff] [blame] | 1666 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1667 | if (path->slots[0] == 0) |
| 1668 | break; |
| 1669 | path->slots[0]--; |
| 1670 | } |
| 1671 | |
| 1672 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 1673 | if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || |
| 1674 | key.type != BTRFS_ORPHAN_ITEM_KEY) |
| 1675 | break; |
| 1676 | |
| 1677 | ret = btrfs_del_item(trans, root, path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1678 | if (ret) |
Josef Bacik | 011b28a | 2021-05-19 13:13:15 -0400 | [diff] [blame] | 1679 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1680 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1681 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1682 | inode = read_one_inode(root, key.offset); |
Josef Bacik | 011b28a | 2021-05-19 13:13:15 -0400 | [diff] [blame] | 1683 | if (!inode) { |
| 1684 | ret = -EIO; |
| 1685 | break; |
| 1686 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1687 | |
| 1688 | ret = fixup_inode_link_count(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1689 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1690 | if (ret) |
Josef Bacik | 011b28a | 2021-05-19 13:13:15 -0400 | [diff] [blame] | 1691 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1692 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1693 | /* |
| 1694 | * fixup on a directory may create new entries, |
| 1695 | * make sure we always look for the highset possible |
| 1696 | * offset |
| 1697 | */ |
| 1698 | key.offset = (u64)-1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1699 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1700 | btrfs_release_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1701 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | |
| 1705 | /* |
| 1706 | * record a given inode in the fixup dir so we can check its link |
| 1707 | * count when replay is done. The link count is incremented here |
| 1708 | * so the inode won't go away until we check it |
| 1709 | */ |
| 1710 | static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, |
| 1711 | struct btrfs_root *root, |
| 1712 | struct btrfs_path *path, |
| 1713 | u64 objectid) |
| 1714 | { |
| 1715 | struct btrfs_key key; |
| 1716 | int ret = 0; |
| 1717 | struct inode *inode; |
| 1718 | |
| 1719 | inode = read_one_inode(root, objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1720 | if (!inode) |
| 1721 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1722 | |
| 1723 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1724 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1725 | key.offset = objectid; |
| 1726 | |
| 1727 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
| 1728 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1729 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1730 | if (ret == 0) { |
Josef Bacik | 9bf7a48 | 2013-03-01 13:35:47 -0500 | [diff] [blame] | 1731 | if (!inode->i_nlink) |
| 1732 | set_nlink(inode, 1); |
| 1733 | else |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 1734 | inc_nlink(inode); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 1735 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1736 | } else if (ret == -EEXIST) { |
| 1737 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1738 | } |
| 1739 | iput(inode); |
| 1740 | |
| 1741 | return ret; |
| 1742 | } |
| 1743 | |
| 1744 | /* |
| 1745 | * when replaying the log for a directory, we only insert names |
| 1746 | * for inodes that actually exist. This means an fsync on a directory |
| 1747 | * does not implicitly fsync all the new files in it |
| 1748 | */ |
| 1749 | static noinline int insert_one_name(struct btrfs_trans_handle *trans, |
| 1750 | struct btrfs_root *root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1751 | u64 dirid, u64 index, |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 1752 | const struct fscrypt_str *name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1753 | struct btrfs_key *location) |
| 1754 | { |
| 1755 | struct inode *inode; |
| 1756 | struct inode *dir; |
| 1757 | int ret; |
| 1758 | |
| 1759 | inode = read_one_inode(root, location->objectid); |
| 1760 | if (!inode) |
| 1761 | return -ENOENT; |
| 1762 | |
| 1763 | dir = read_one_inode(root, dirid); |
| 1764 | if (!dir) { |
| 1765 | iput(inode); |
| 1766 | return -EIO; |
| 1767 | } |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1768 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 1769 | ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1770 | 1, index); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1771 | |
| 1772 | /* FIXME, put inode into FIXUP list */ |
| 1773 | |
| 1774 | iput(inode); |
| 1775 | iput(dir); |
| 1776 | return ret; |
| 1777 | } |
| 1778 | |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1779 | static int delete_conflicting_dir_entry(struct btrfs_trans_handle *trans, |
| 1780 | struct btrfs_inode *dir, |
| 1781 | struct btrfs_path *path, |
| 1782 | struct btrfs_dir_item *dst_di, |
| 1783 | const struct btrfs_key *log_key, |
Omar Sandoval | 94a48ae | 2022-10-20 12:58:28 -0400 | [diff] [blame] | 1784 | u8 log_flags, |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1785 | bool exists) |
| 1786 | { |
| 1787 | struct btrfs_key found_key; |
| 1788 | |
| 1789 | btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); |
| 1790 | /* The existing dentry points to the same inode, don't delete it. */ |
| 1791 | if (found_key.objectid == log_key->objectid && |
| 1792 | found_key.type == log_key->type && |
| 1793 | found_key.offset == log_key->offset && |
Omar Sandoval | 94a48ae | 2022-10-20 12:58:28 -0400 | [diff] [blame] | 1794 | btrfs_dir_flags(path->nodes[0], dst_di) == log_flags) |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1795 | return 1; |
| 1796 | |
| 1797 | /* |
| 1798 | * Don't drop the conflicting directory entry if the inode for the new |
| 1799 | * entry doesn't exist. |
| 1800 | */ |
| 1801 | if (!exists) |
| 1802 | return 0; |
| 1803 | |
| 1804 | return drop_one_dir_item(trans, path, dir, dst_di); |
| 1805 | } |
| 1806 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1807 | /* |
| 1808 | * take a single entry in a log directory item and replay it into |
| 1809 | * the subvolume. |
| 1810 | * |
| 1811 | * if a conflicting item exists in the subdirectory already, |
| 1812 | * the inode it points to is unlinked and put into the link count |
| 1813 | * fix up tree. |
| 1814 | * |
| 1815 | * If a name from the log points to a file or directory that does |
| 1816 | * not exist in the FS, it is skipped. fsyncs on directories |
| 1817 | * do not force down inodes inside that directory, just changes to the |
| 1818 | * names or unlinks in a directory. |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1819 | * |
| 1820 | * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a |
| 1821 | * non-existing inode) and 1 if the name was replayed. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1822 | */ |
| 1823 | static noinline int replay_one_name(struct btrfs_trans_handle *trans, |
| 1824 | struct btrfs_root *root, |
| 1825 | struct btrfs_path *path, |
| 1826 | struct extent_buffer *eb, |
| 1827 | struct btrfs_dir_item *di, |
| 1828 | struct btrfs_key *key) |
| 1829 | { |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 1830 | struct fscrypt_str name; |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1831 | struct btrfs_dir_item *dir_dst_di; |
| 1832 | struct btrfs_dir_item *index_dst_di; |
| 1833 | bool dir_dst_matches = false; |
| 1834 | bool index_dst_matches = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1835 | struct btrfs_key log_key; |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1836 | struct btrfs_key search_key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1837 | struct inode *dir; |
Omar Sandoval | 94a48ae | 2022-10-20 12:58:28 -0400 | [diff] [blame] | 1838 | u8 log_flags; |
Filipe Manana | cfd3126 | 2021-10-01 13:48:18 +0100 | [diff] [blame] | 1839 | bool exists; |
| 1840 | int ret; |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1841 | bool update_size = true; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1842 | bool name_added = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1843 | |
| 1844 | dir = read_one_inode(root, key->objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1845 | if (!dir) |
| 1846 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1847 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1848 | ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name); |
| 1849 | if (ret) |
Filipe David Borba Manana | 2bac325 | 2013-08-04 19:58:57 +0100 | [diff] [blame] | 1850 | goto out; |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 1851 | |
Omar Sandoval | 94a48ae | 2022-10-20 12:58:28 -0400 | [diff] [blame] | 1852 | log_flags = btrfs_dir_flags(eb, di); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1853 | btrfs_dir_item_key_to_cpu(eb, di, &log_key); |
Filipe Manana | cfd3126 | 2021-10-01 13:48:18 +0100 | [diff] [blame] | 1854 | ret = btrfs_lookup_inode(trans, root, path, &log_key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1855 | btrfs_release_path(path); |
Filipe Manana | cfd3126 | 2021-10-01 13:48:18 +0100 | [diff] [blame] | 1856 | if (ret < 0) |
| 1857 | goto out; |
| 1858 | exists = (ret == 0); |
| 1859 | ret = 0; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1860 | |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1861 | dir_dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1862 | &name, 1); |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1863 | if (IS_ERR(dir_dst_di)) { |
| 1864 | ret = PTR_ERR(dir_dst_di); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1865 | goto out; |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1866 | } else if (dir_dst_di) { |
| 1867 | ret = delete_conflicting_dir_entry(trans, BTRFS_I(dir), path, |
Omar Sandoval | 94a48ae | 2022-10-20 12:58:28 -0400 | [diff] [blame] | 1868 | dir_dst_di, &log_key, |
| 1869 | log_flags, exists); |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1870 | if (ret < 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1871 | goto out; |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1872 | dir_dst_matches = (ret == 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1873 | } |
| 1874 | |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1875 | btrfs_release_path(path); |
| 1876 | |
| 1877 | index_dst_di = btrfs_lookup_dir_index_item(trans, root, path, |
| 1878 | key->objectid, key->offset, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1879 | &name, 1); |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1880 | if (IS_ERR(index_dst_di)) { |
| 1881 | ret = PTR_ERR(index_dst_di); |
| 1882 | goto out; |
| 1883 | } else if (index_dst_di) { |
| 1884 | ret = delete_conflicting_dir_entry(trans, BTRFS_I(dir), path, |
| 1885 | index_dst_di, &log_key, |
Omar Sandoval | 94a48ae | 2022-10-20 12:58:28 -0400 | [diff] [blame] | 1886 | log_flags, exists); |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1887 | if (ret < 0) |
| 1888 | goto out; |
| 1889 | index_dst_matches = (ret == 1); |
| 1890 | } |
| 1891 | |
| 1892 | btrfs_release_path(path); |
| 1893 | |
| 1894 | if (dir_dst_matches && index_dst_matches) { |
| 1895 | ret = 0; |
Filipe Manana | a2cc11d | 2014-09-08 22:53:18 +0100 | [diff] [blame] | 1896 | update_size = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1897 | goto out; |
| 1898 | } |
| 1899 | |
| 1900 | /* |
Nikolay Borisov | 725af92 | 2019-08-30 17:44:49 +0300 | [diff] [blame] | 1901 | * Check if the inode reference exists in the log for the given name, |
| 1902 | * inode and parent inode |
| 1903 | */ |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1904 | search_key.objectid = log_key.objectid; |
| 1905 | search_key.type = BTRFS_INODE_REF_KEY; |
| 1906 | search_key.offset = key->objectid; |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1907 | ret = backref_in_log(root->log_root, &search_key, 0, &name); |
Nikolay Borisov | 725af92 | 2019-08-30 17:44:49 +0300 | [diff] [blame] | 1908 | if (ret < 0) { |
| 1909 | goto out; |
| 1910 | } else if (ret) { |
| 1911 | /* The dentry will be added later. */ |
| 1912 | ret = 0; |
| 1913 | update_size = false; |
| 1914 | goto out; |
| 1915 | } |
| 1916 | |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1917 | search_key.objectid = log_key.objectid; |
| 1918 | search_key.type = BTRFS_INODE_EXTREF_KEY; |
| 1919 | search_key.offset = key->objectid; |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1920 | ret = backref_in_log(root->log_root, &search_key, key->objectid, &name); |
Nikolay Borisov | 725af92 | 2019-08-30 17:44:49 +0300 | [diff] [blame] | 1921 | if (ret < 0) { |
| 1922 | goto out; |
| 1923 | } else if (ret) { |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1924 | /* The dentry will be added later. */ |
| 1925 | ret = 0; |
| 1926 | update_size = false; |
| 1927 | goto out; |
| 1928 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1929 | btrfs_release_path(path); |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 1930 | ret = insert_one_name(trans, root, key->objectid, key->offset, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1931 | &name, &log_key); |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1932 | if (ret && ret != -ENOENT && ret != -EEXIST) |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1933 | goto out; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1934 | if (!ret) |
| 1935 | name_added = true; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1936 | update_size = false; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1937 | ret = 0; |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1938 | |
| 1939 | out: |
| 1940 | if (!ret && update_size) { |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1941 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name.len * 2); |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1942 | ret = btrfs_update_inode(trans, root, BTRFS_I(dir)); |
| 1943 | } |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 1944 | kfree(name.name); |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1945 | iput(dir); |
| 1946 | if (!ret && name_added) |
| 1947 | ret = 1; |
| 1948 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1949 | } |
| 1950 | |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1951 | /* Replay one dir item from a BTRFS_DIR_INDEX_KEY key. */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1952 | static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, |
| 1953 | struct btrfs_root *root, |
| 1954 | struct btrfs_path *path, |
| 1955 | struct extent_buffer *eb, int slot, |
| 1956 | struct btrfs_key *key) |
| 1957 | { |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1958 | int ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1959 | struct btrfs_dir_item *di; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1960 | |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1961 | /* We only log dir index keys, which only contain a single dir item. */ |
| 1962 | ASSERT(key->type == BTRFS_DIR_INDEX_KEY); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1963 | |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1964 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
| 1965 | ret = replay_one_name(trans, root, path, eb, di, key); |
| 1966 | if (ret < 0) |
| 1967 | return ret; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1968 | |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1969 | /* |
| 1970 | * If this entry refers to a non-directory (directories can not have a |
| 1971 | * link count > 1) and it was added in the transaction that was not |
| 1972 | * committed, make sure we fixup the link count of the inode the entry |
| 1973 | * points to. Otherwise something like the following would result in a |
| 1974 | * directory pointing to an inode with a wrong link that does not account |
| 1975 | * for this dir entry: |
| 1976 | * |
| 1977 | * mkdir testdir |
| 1978 | * touch testdir/foo |
| 1979 | * touch testdir/bar |
| 1980 | * sync |
| 1981 | * |
| 1982 | * ln testdir/bar testdir/bar_link |
| 1983 | * ln testdir/foo testdir/foo_link |
| 1984 | * xfs_io -c "fsync" testdir/bar |
| 1985 | * |
| 1986 | * <power failure> |
| 1987 | * |
| 1988 | * mount fs, log replay happens |
| 1989 | * |
| 1990 | * File foo would remain with a link count of 1 when it has two entries |
| 1991 | * pointing to it in the directory testdir. This would make it impossible |
| 1992 | * to ever delete the parent directory has it would result in stale |
| 1993 | * dentries that can never be deleted. |
| 1994 | */ |
Omar Sandoval | 94a48ae | 2022-10-20 12:58:28 -0400 | [diff] [blame] | 1995 | if (ret == 1 && btrfs_dir_ftype(eb, di) != BTRFS_FT_DIR) { |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1996 | struct btrfs_path *fixup_path; |
| 1997 | struct btrfs_key di_key; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1998 | |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 1999 | fixup_path = btrfs_alloc_path(); |
| 2000 | if (!fixup_path) |
| 2001 | return -ENOMEM; |
| 2002 | |
| 2003 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 2004 | ret = link_to_fixup_dir(trans, root, fixup_path, di_key.objectid); |
| 2005 | btrfs_free_path(fixup_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2006 | } |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 2007 | |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 2008 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | /* |
| 2012 | * directory replay has two parts. There are the standard directory |
| 2013 | * items in the log copied from the subvolume, and range items |
| 2014 | * created in the log while the subvolume was logged. |
| 2015 | * |
| 2016 | * The range items tell us which parts of the key space the log |
| 2017 | * is authoritative for. During replay, if a key in the subvolume |
| 2018 | * directory is in a logged range item, but not actually in the log |
| 2019 | * that means it was deleted from the directory before the fsync |
| 2020 | * and should be removed. |
| 2021 | */ |
| 2022 | static noinline int find_dir_range(struct btrfs_root *root, |
| 2023 | struct btrfs_path *path, |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2024 | u64 dirid, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2025 | u64 *start_ret, u64 *end_ret) |
| 2026 | { |
| 2027 | struct btrfs_key key; |
| 2028 | u64 found_end; |
| 2029 | struct btrfs_dir_log_item *item; |
| 2030 | int ret; |
| 2031 | int nritems; |
| 2032 | |
| 2033 | if (*start_ret == (u64)-1) |
| 2034 | return 1; |
| 2035 | |
| 2036 | key.objectid = dirid; |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2037 | key.type = BTRFS_DIR_LOG_INDEX_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2038 | key.offset = *start_ret; |
| 2039 | |
| 2040 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2041 | if (ret < 0) |
| 2042 | goto out; |
| 2043 | if (ret > 0) { |
| 2044 | if (path->slots[0] == 0) |
| 2045 | goto out; |
| 2046 | path->slots[0]--; |
| 2047 | } |
| 2048 | if (ret != 0) |
| 2049 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 2050 | |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2051 | if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2052 | ret = 1; |
| 2053 | goto next; |
| 2054 | } |
| 2055 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2056 | struct btrfs_dir_log_item); |
| 2057 | found_end = btrfs_dir_log_end(path->nodes[0], item); |
| 2058 | |
| 2059 | if (*start_ret >= key.offset && *start_ret <= found_end) { |
| 2060 | ret = 0; |
| 2061 | *start_ret = key.offset; |
| 2062 | *end_ret = found_end; |
| 2063 | goto out; |
| 2064 | } |
| 2065 | ret = 1; |
| 2066 | next: |
| 2067 | /* check the next slot in the tree to see if it is a valid item */ |
| 2068 | nritems = btrfs_header_nritems(path->nodes[0]); |
Robbie Ko | 2a7bf53 | 2016-10-07 17:30:47 +0800 | [diff] [blame] | 2069 | path->slots[0]++; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2070 | if (path->slots[0] >= nritems) { |
| 2071 | ret = btrfs_next_leaf(root, path); |
| 2072 | if (ret) |
| 2073 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 2077 | |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2078 | if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2079 | ret = 1; |
| 2080 | goto out; |
| 2081 | } |
| 2082 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2083 | struct btrfs_dir_log_item); |
| 2084 | found_end = btrfs_dir_log_end(path->nodes[0], item); |
| 2085 | *start_ret = key.offset; |
| 2086 | *end_ret = found_end; |
| 2087 | ret = 0; |
| 2088 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2089 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2090 | return ret; |
| 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | * this looks for a given directory item in the log. If the directory |
| 2095 | * item is not in the log, the item is removed and the inode it points |
| 2096 | * to is unlinked |
| 2097 | */ |
| 2098 | static noinline int check_item_in_log(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2099 | struct btrfs_root *log, |
| 2100 | struct btrfs_path *path, |
| 2101 | struct btrfs_path *log_path, |
| 2102 | struct inode *dir, |
| 2103 | struct btrfs_key *dir_key) |
| 2104 | { |
Filipe Manana | d1ed82f | 2021-10-25 17:31:52 +0100 | [diff] [blame] | 2105 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2106 | int ret; |
| 2107 | struct extent_buffer *eb; |
| 2108 | int slot; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2109 | struct btrfs_dir_item *di; |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 2110 | struct fscrypt_str name; |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2111 | struct inode *inode = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2112 | struct btrfs_key location; |
| 2113 | |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2114 | /* |
David Sterba | 143823c | 2022-05-25 16:27:25 +0200 | [diff] [blame] | 2115 | * Currently we only log dir index keys. Even if we replay a log created |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2116 | * by an older kernel that logged both dir index and dir item keys, all |
| 2117 | * we need to do is process the dir index keys, we (and our caller) can |
| 2118 | * safely ignore dir item keys (key type BTRFS_DIR_ITEM_KEY). |
| 2119 | */ |
| 2120 | ASSERT(dir_key->type == BTRFS_DIR_INDEX_KEY); |
| 2121 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2122 | eb = path->nodes[0]; |
| 2123 | slot = path->slots[0]; |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2124 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 2125 | ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name); |
| 2126 | if (ret) |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2127 | goto out; |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2128 | |
| 2129 | if (log) { |
| 2130 | struct btrfs_dir_item *log_di; |
| 2131 | |
| 2132 | log_di = btrfs_lookup_dir_index_item(trans, log, log_path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2133 | dir_key->objectid, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 2134 | dir_key->offset, &name, 0); |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2135 | if (IS_ERR(log_di)) { |
| 2136 | ret = PTR_ERR(log_di); |
| 2137 | goto out; |
| 2138 | } else if (log_di) { |
| 2139 | /* The dentry exists in the log, we have nothing to do. */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2140 | ret = 0; |
| 2141 | goto out; |
| 2142 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2143 | } |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2144 | |
| 2145 | btrfs_dir_item_key_to_cpu(eb, di, &location); |
| 2146 | btrfs_release_path(path); |
| 2147 | btrfs_release_path(log_path); |
| 2148 | inode = read_one_inode(root, location.objectid); |
| 2149 | if (!inode) { |
| 2150 | ret = -EIO; |
| 2151 | goto out; |
| 2152 | } |
| 2153 | |
| 2154 | ret = link_to_fixup_dir(trans, root, path, location.objectid); |
| 2155 | if (ret) |
| 2156 | goto out; |
| 2157 | |
| 2158 | inc_nlink(inode); |
Filipe Manana | 313ab75 | 2022-02-28 16:29:29 +0000 | [diff] [blame] | 2159 | ret = unlink_inode_for_log_replay(trans, BTRFS_I(dir), BTRFS_I(inode), |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 2160 | &name); |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2161 | /* |
| 2162 | * Unlike dir item keys, dir index keys can only have one name (entry) in |
| 2163 | * them, as there are no key collisions since each key has a unique offset |
| 2164 | * (an index number), so we're done. |
| 2165 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2166 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2167 | btrfs_release_path(path); |
| 2168 | btrfs_release_path(log_path); |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 2169 | kfree(name.name); |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2170 | iput(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2171 | return ret; |
| 2172 | } |
| 2173 | |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2174 | static int replay_xattr_deletes(struct btrfs_trans_handle *trans, |
| 2175 | struct btrfs_root *root, |
| 2176 | struct btrfs_root *log, |
| 2177 | struct btrfs_path *path, |
| 2178 | const u64 ino) |
| 2179 | { |
| 2180 | struct btrfs_key search_key; |
| 2181 | struct btrfs_path *log_path; |
| 2182 | int i; |
| 2183 | int nritems; |
| 2184 | int ret; |
| 2185 | |
| 2186 | log_path = btrfs_alloc_path(); |
| 2187 | if (!log_path) |
| 2188 | return -ENOMEM; |
| 2189 | |
| 2190 | search_key.objectid = ino; |
| 2191 | search_key.type = BTRFS_XATTR_ITEM_KEY; |
| 2192 | search_key.offset = 0; |
| 2193 | again: |
| 2194 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
| 2195 | if (ret < 0) |
| 2196 | goto out; |
| 2197 | process_leaf: |
| 2198 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2199 | for (i = path->slots[0]; i < nritems; i++) { |
| 2200 | struct btrfs_key key; |
| 2201 | struct btrfs_dir_item *di; |
| 2202 | struct btrfs_dir_item *log_di; |
| 2203 | u32 total_size; |
| 2204 | u32 cur; |
| 2205 | |
| 2206 | btrfs_item_key_to_cpu(path->nodes[0], &key, i); |
| 2207 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) { |
| 2208 | ret = 0; |
| 2209 | goto out; |
| 2210 | } |
| 2211 | |
| 2212 | di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 2213 | total_size = btrfs_item_size(path->nodes[0], i); |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2214 | cur = 0; |
| 2215 | while (cur < total_size) { |
| 2216 | u16 name_len = btrfs_dir_name_len(path->nodes[0], di); |
| 2217 | u16 data_len = btrfs_dir_data_len(path->nodes[0], di); |
| 2218 | u32 this_len = sizeof(*di) + name_len + data_len; |
| 2219 | char *name; |
| 2220 | |
| 2221 | name = kmalloc(name_len, GFP_NOFS); |
| 2222 | if (!name) { |
| 2223 | ret = -ENOMEM; |
| 2224 | goto out; |
| 2225 | } |
| 2226 | read_extent_buffer(path->nodes[0], name, |
| 2227 | (unsigned long)(di + 1), name_len); |
| 2228 | |
| 2229 | log_di = btrfs_lookup_xattr(NULL, log, log_path, ino, |
| 2230 | name, name_len, 0); |
| 2231 | btrfs_release_path(log_path); |
| 2232 | if (!log_di) { |
| 2233 | /* Doesn't exist in log tree, so delete it. */ |
| 2234 | btrfs_release_path(path); |
| 2235 | di = btrfs_lookup_xattr(trans, root, path, ino, |
| 2236 | name, name_len, -1); |
| 2237 | kfree(name); |
| 2238 | if (IS_ERR(di)) { |
| 2239 | ret = PTR_ERR(di); |
| 2240 | goto out; |
| 2241 | } |
| 2242 | ASSERT(di); |
| 2243 | ret = btrfs_delete_one_dir_name(trans, root, |
| 2244 | path, di); |
| 2245 | if (ret) |
| 2246 | goto out; |
| 2247 | btrfs_release_path(path); |
| 2248 | search_key = key; |
| 2249 | goto again; |
| 2250 | } |
| 2251 | kfree(name); |
| 2252 | if (IS_ERR(log_di)) { |
| 2253 | ret = PTR_ERR(log_di); |
| 2254 | goto out; |
| 2255 | } |
| 2256 | cur += this_len; |
| 2257 | di = (struct btrfs_dir_item *)((char *)di + this_len); |
| 2258 | } |
| 2259 | } |
| 2260 | ret = btrfs_next_leaf(root, path); |
| 2261 | if (ret > 0) |
| 2262 | ret = 0; |
| 2263 | else if (ret == 0) |
| 2264 | goto process_leaf; |
| 2265 | out: |
| 2266 | btrfs_free_path(log_path); |
| 2267 | btrfs_release_path(path); |
| 2268 | return ret; |
| 2269 | } |
| 2270 | |
| 2271 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2272 | /* |
| 2273 | * deletion replay happens before we copy any new directory items |
| 2274 | * out of the log or out of backreferences from inodes. It |
| 2275 | * scans the log to find ranges of keys that log is authoritative for, |
| 2276 | * and then scans the directory to find items in those ranges that are |
| 2277 | * not present in the log. |
| 2278 | * |
| 2279 | * Anything we don't find in the log is unlinked and removed from the |
| 2280 | * directory. |
| 2281 | */ |
| 2282 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, |
| 2283 | struct btrfs_root *root, |
| 2284 | struct btrfs_root *log, |
| 2285 | struct btrfs_path *path, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2286 | u64 dirid, int del_all) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2287 | { |
| 2288 | u64 range_start; |
| 2289 | u64 range_end; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2290 | int ret = 0; |
| 2291 | struct btrfs_key dir_key; |
| 2292 | struct btrfs_key found_key; |
| 2293 | struct btrfs_path *log_path; |
| 2294 | struct inode *dir; |
| 2295 | |
| 2296 | dir_key.objectid = dirid; |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2297 | dir_key.type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2298 | log_path = btrfs_alloc_path(); |
| 2299 | if (!log_path) |
| 2300 | return -ENOMEM; |
| 2301 | |
| 2302 | dir = read_one_inode(root, dirid); |
| 2303 | /* it isn't an error if the inode isn't there, that can happen |
| 2304 | * because we replay the deletes before we copy in the inode item |
| 2305 | * from the log |
| 2306 | */ |
| 2307 | if (!dir) { |
| 2308 | btrfs_free_path(log_path); |
| 2309 | return 0; |
| 2310 | } |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2311 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2312 | range_start = 0; |
| 2313 | range_end = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2314 | while (1) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2315 | if (del_all) |
| 2316 | range_end = (u64)-1; |
| 2317 | else { |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2318 | ret = find_dir_range(log, path, dirid, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2319 | &range_start, &range_end); |
Filipe Manana | 10adb11 | 2021-10-14 17:26:04 +0100 | [diff] [blame] | 2320 | if (ret < 0) |
| 2321 | goto out; |
| 2322 | else if (ret > 0) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2323 | break; |
| 2324 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2325 | |
| 2326 | dir_key.offset = range_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2327 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2328 | int nritems; |
| 2329 | ret = btrfs_search_slot(NULL, root, &dir_key, path, |
| 2330 | 0, 0); |
| 2331 | if (ret < 0) |
| 2332 | goto out; |
| 2333 | |
| 2334 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2335 | if (path->slots[0] >= nritems) { |
| 2336 | ret = btrfs_next_leaf(root, path); |
Liu Bo | b98def7 | 2018-04-03 01:59:48 +0800 | [diff] [blame] | 2337 | if (ret == 1) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2338 | break; |
Liu Bo | b98def7 | 2018-04-03 01:59:48 +0800 | [diff] [blame] | 2339 | else if (ret < 0) |
| 2340 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2341 | } |
| 2342 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2343 | path->slots[0]); |
| 2344 | if (found_key.objectid != dirid || |
Filipe Manana | ccae4a1 | 2021-10-25 17:31:54 +0100 | [diff] [blame] | 2345 | found_key.type != dir_key.type) { |
| 2346 | ret = 0; |
| 2347 | goto out; |
| 2348 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2349 | |
| 2350 | if (found_key.offset > range_end) |
| 2351 | break; |
| 2352 | |
Filipe Manana | d1ed82f | 2021-10-25 17:31:52 +0100 | [diff] [blame] | 2353 | ret = check_item_in_log(trans, log, path, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2354 | log_path, dir, |
| 2355 | &found_key); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2356 | if (ret) |
| 2357 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2358 | if (found_key.offset == (u64)-1) |
| 2359 | break; |
| 2360 | dir_key.offset = found_key.offset + 1; |
| 2361 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2362 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2363 | if (range_end == (u64)-1) |
| 2364 | break; |
| 2365 | range_start = range_end + 1; |
| 2366 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2367 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2368 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2369 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2370 | btrfs_free_path(log_path); |
| 2371 | iput(dir); |
| 2372 | return ret; |
| 2373 | } |
| 2374 | |
| 2375 | /* |
| 2376 | * the process_func used to replay items from the log tree. This |
| 2377 | * gets called in two different stages. The first stage just looks |
| 2378 | * for inodes and makes sure they are all copied into the subvolume. |
| 2379 | * |
| 2380 | * The second stage copies all the other item types from the log into |
| 2381 | * the subvolume. The two stage approach is slower, but gets rid of |
| 2382 | * lots of complexity around inodes referencing other inodes that exist |
| 2383 | * only in the log (references come from either directory items or inode |
| 2384 | * back refs). |
| 2385 | */ |
| 2386 | static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2387 | struct walk_control *wc, u64 gen, int level) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2388 | { |
| 2389 | int nritems; |
Qu Wenruo | 789d6a3 | 2022-09-14 13:32:50 +0800 | [diff] [blame] | 2390 | struct btrfs_tree_parent_check check = { |
| 2391 | .transid = gen, |
| 2392 | .level = level |
| 2393 | }; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2394 | struct btrfs_path *path; |
| 2395 | struct btrfs_root *root = wc->replay_dest; |
| 2396 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2397 | int i; |
| 2398 | int ret; |
| 2399 | |
Qu Wenruo | 789d6a3 | 2022-09-14 13:32:50 +0800 | [diff] [blame] | 2400 | ret = btrfs_read_extent_buffer(eb, &check); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2401 | if (ret) |
| 2402 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2403 | |
| 2404 | level = btrfs_header_level(eb); |
| 2405 | |
| 2406 | if (level != 0) |
| 2407 | return 0; |
| 2408 | |
| 2409 | path = btrfs_alloc_path(); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2410 | if (!path) |
| 2411 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2412 | |
| 2413 | nritems = btrfs_header_nritems(eb); |
| 2414 | for (i = 0; i < nritems; i++) { |
| 2415 | btrfs_item_key_to_cpu(eb, &key, i); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2416 | |
| 2417 | /* inode keys are done during the first stage */ |
| 2418 | if (key.type == BTRFS_INODE_ITEM_KEY && |
| 2419 | wc->stage == LOG_WALK_REPLAY_INODES) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2420 | struct btrfs_inode_item *inode_item; |
| 2421 | u32 mode; |
| 2422 | |
| 2423 | inode_item = btrfs_item_ptr(eb, i, |
| 2424 | struct btrfs_inode_item); |
Filipe Manana | f2d72f4 | 2018-10-08 11:12:55 +0100 | [diff] [blame] | 2425 | /* |
| 2426 | * If we have a tmpfile (O_TMPFILE) that got fsync'ed |
| 2427 | * and never got linked before the fsync, skip it, as |
| 2428 | * replaying it is pointless since it would be deleted |
| 2429 | * later. We skip logging tmpfiles, but it's always |
| 2430 | * possible we are replaying a log created with a kernel |
| 2431 | * that used to log tmpfiles. |
| 2432 | */ |
| 2433 | if (btrfs_inode_nlink(eb, inode_item) == 0) { |
| 2434 | wc->ignore_cur_inode = true; |
| 2435 | continue; |
| 2436 | } else { |
| 2437 | wc->ignore_cur_inode = false; |
| 2438 | } |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2439 | ret = replay_xattr_deletes(wc->trans, root, log, |
| 2440 | path, key.objectid); |
| 2441 | if (ret) |
| 2442 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2443 | mode = btrfs_inode_mode(eb, inode_item); |
| 2444 | if (S_ISDIR(mode)) { |
| 2445 | ret = replay_dir_deletes(wc->trans, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2446 | root, log, path, key.objectid, 0); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2447 | if (ret) |
| 2448 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2449 | } |
| 2450 | ret = overwrite_item(wc->trans, root, path, |
| 2451 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2452 | if (ret) |
| 2453 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2454 | |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2455 | /* |
| 2456 | * Before replaying extents, truncate the inode to its |
| 2457 | * size. We need to do it now and not after log replay |
| 2458 | * because before an fsync we can have prealloc extents |
| 2459 | * added beyond the inode's i_size. If we did it after, |
| 2460 | * through orphan cleanup for example, we would drop |
| 2461 | * those prealloc extents just after replaying them. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2462 | */ |
| 2463 | if (S_ISREG(mode)) { |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2464 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2465 | struct inode *inode; |
| 2466 | u64 from; |
| 2467 | |
| 2468 | inode = read_one_inode(root, key.objectid); |
| 2469 | if (!inode) { |
| 2470 | ret = -EIO; |
| 2471 | break; |
| 2472 | } |
| 2473 | from = ALIGN(i_size_read(inode), |
| 2474 | root->fs_info->sectorsize); |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2475 | drop_args.start = from; |
| 2476 | drop_args.end = (u64)-1; |
| 2477 | drop_args.drop_cache = true; |
| 2478 | ret = btrfs_drop_extents(wc->trans, root, |
| 2479 | BTRFS_I(inode), |
| 2480 | &drop_args); |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2481 | if (!ret) { |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2482 | inode_sub_bytes(inode, |
| 2483 | drop_args.bytes_found); |
Filipe Manana | f2d72f4 | 2018-10-08 11:12:55 +0100 | [diff] [blame] | 2484 | /* Update the inode's nbytes. */ |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2485 | ret = btrfs_update_inode(wc->trans, |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 2486 | root, BTRFS_I(inode)); |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2487 | } |
| 2488 | iput(inode); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2489 | if (ret) |
| 2490 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2491 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2492 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2493 | ret = link_to_fixup_dir(wc->trans, root, |
| 2494 | path, key.objectid); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2495 | if (ret) |
| 2496 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2497 | } |
Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2498 | |
Filipe Manana | f2d72f4 | 2018-10-08 11:12:55 +0100 | [diff] [blame] | 2499 | if (wc->ignore_cur_inode) |
| 2500 | continue; |
| 2501 | |
Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2502 | if (key.type == BTRFS_DIR_INDEX_KEY && |
| 2503 | wc->stage == LOG_WALK_REPLAY_DIR_INDEX) { |
| 2504 | ret = replay_one_dir_item(wc->trans, root, path, |
| 2505 | eb, i, &key); |
| 2506 | if (ret) |
| 2507 | break; |
| 2508 | } |
| 2509 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2510 | if (wc->stage < LOG_WALK_REPLAY_ALL) |
| 2511 | continue; |
| 2512 | |
| 2513 | /* these keys are simply copied */ |
| 2514 | if (key.type == BTRFS_XATTR_ITEM_KEY) { |
| 2515 | ret = overwrite_item(wc->trans, root, path, |
| 2516 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2517 | if (ret) |
| 2518 | break; |
Liu Bo | 2da1c66 | 2013-05-26 13:50:29 +0000 | [diff] [blame] | 2519 | } else if (key.type == BTRFS_INODE_REF_KEY || |
| 2520 | key.type == BTRFS_INODE_EXTREF_KEY) { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 2521 | ret = add_inode_ref(wc->trans, root, log, path, |
| 2522 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2523 | if (ret && ret != -ENOENT) |
| 2524 | break; |
| 2525 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2526 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 2527 | ret = replay_one_extent(wc->trans, root, path, |
| 2528 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2529 | if (ret) |
| 2530 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2531 | } |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 2532 | /* |
| 2533 | * We don't log BTRFS_DIR_ITEM_KEY keys anymore, only the |
| 2534 | * BTRFS_DIR_INDEX_KEY items which we use to derive the |
| 2535 | * BTRFS_DIR_ITEM_KEY items. If we are replaying a log from an |
| 2536 | * older kernel with such keys, ignore them. |
| 2537 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2538 | } |
| 2539 | btrfs_free_path(path); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2540 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2541 | } |
| 2542 | |
Nikolay Borisov | 6787bb9 | 2020-01-20 16:09:10 +0200 | [diff] [blame] | 2543 | /* |
| 2544 | * Correctly adjust the reserved bytes occupied by a log tree extent buffer |
| 2545 | */ |
| 2546 | static void unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start) |
| 2547 | { |
| 2548 | struct btrfs_block_group *cache; |
| 2549 | |
| 2550 | cache = btrfs_lookup_block_group(fs_info, start); |
| 2551 | if (!cache) { |
| 2552 | btrfs_err(fs_info, "unable to find block group for %llu", start); |
| 2553 | return; |
| 2554 | } |
| 2555 | |
| 2556 | spin_lock(&cache->space_info->lock); |
| 2557 | spin_lock(&cache->lock); |
| 2558 | cache->reserved -= fs_info->nodesize; |
| 2559 | cache->space_info->bytes_reserved -= fs_info->nodesize; |
| 2560 | spin_unlock(&cache->lock); |
| 2561 | spin_unlock(&cache->space_info->lock); |
| 2562 | |
| 2563 | btrfs_put_block_group(cache); |
| 2564 | } |
| 2565 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2566 | static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2567 | struct btrfs_root *root, |
| 2568 | struct btrfs_path *path, int *level, |
| 2569 | struct walk_control *wc) |
| 2570 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2571 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2572 | u64 bytenr; |
| 2573 | u64 ptr_gen; |
| 2574 | struct extent_buffer *next; |
| 2575 | struct extent_buffer *cur; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2576 | u32 blocksize; |
| 2577 | int ret = 0; |
| 2578 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2579 | while (*level > 0) { |
Qu Wenruo | 789d6a3 | 2022-09-14 13:32:50 +0800 | [diff] [blame] | 2580 | struct btrfs_tree_parent_check check = { 0 }; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2581 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2582 | cur = path->nodes[*level]; |
| 2583 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2584 | WARN_ON(btrfs_header_level(cur) != *level); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2585 | |
| 2586 | if (path->slots[*level] >= |
| 2587 | btrfs_header_nritems(cur)) |
| 2588 | break; |
| 2589 | |
| 2590 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 2591 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Qu Wenruo | 789d6a3 | 2022-09-14 13:32:50 +0800 | [diff] [blame] | 2592 | check.transid = ptr_gen; |
| 2593 | check.level = *level - 1; |
| 2594 | check.has_first_key = true; |
| 2595 | btrfs_node_key_to_cpu(cur, &check.first_key, path->slots[*level]); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2596 | blocksize = fs_info->nodesize; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2597 | |
Josef Bacik | 3fbaf25 | 2020-11-05 10:45:20 -0500 | [diff] [blame] | 2598 | next = btrfs_find_create_tree_block(fs_info, bytenr, |
| 2599 | btrfs_header_owner(cur), |
| 2600 | *level - 1); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 2601 | if (IS_ERR(next)) |
| 2602 | return PTR_ERR(next); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2603 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2604 | if (*level == 1) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2605 | ret = wc->process_func(root, next, wc, ptr_gen, |
| 2606 | *level - 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2607 | if (ret) { |
| 2608 | free_extent_buffer(next); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2609 | return ret; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2610 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2611 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2612 | path->slots[*level]++; |
| 2613 | if (wc->free) { |
Qu Wenruo | 789d6a3 | 2022-09-14 13:32:50 +0800 | [diff] [blame] | 2614 | ret = btrfs_read_extent_buffer(next, &check); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2615 | if (ret) { |
| 2616 | free_extent_buffer(next); |
| 2617 | return ret; |
| 2618 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2619 | |
Josef Bacik | c4e54a6 | 2023-01-26 16:00:56 -0500 | [diff] [blame] | 2620 | btrfs_tree_lock(next); |
Josef Bacik | 190a833 | 2023-01-26 16:00:58 -0500 | [diff] [blame] | 2621 | btrfs_clear_buffer_dirty(trans, next); |
Josef Bacik | 79b02ec | 2023-01-26 16:01:00 -0500 | [diff] [blame] | 2622 | wait_on_extent_buffer_writeback(next); |
Josef Bacik | c4e54a6 | 2023-01-26 16:00:56 -0500 | [diff] [blame] | 2623 | btrfs_tree_unlock(next); |
| 2624 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2625 | if (trans) { |
Nikolay Borisov | 7bfc100 | 2020-01-20 16:09:12 +0200 | [diff] [blame] | 2626 | ret = btrfs_pin_reserved_extent(trans, |
Nikolay Borisov | 10e958d | 2020-01-20 16:09:11 +0200 | [diff] [blame] | 2627 | bytenr, blocksize); |
| 2628 | if (ret) { |
| 2629 | free_extent_buffer(next); |
| 2630 | return ret; |
| 2631 | } |
Naohiro Aota | d3575156 | 2021-02-04 19:21:54 +0900 | [diff] [blame] | 2632 | btrfs_redirty_list_add( |
| 2633 | trans->transaction, next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2634 | } else { |
Nikolay Borisov | 10e958d | 2020-01-20 16:09:11 +0200 | [diff] [blame] | 2635 | unaccount_log_buffer(fs_info, bytenr); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2636 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2637 | } |
| 2638 | free_extent_buffer(next); |
| 2639 | continue; |
| 2640 | } |
Qu Wenruo | 789d6a3 | 2022-09-14 13:32:50 +0800 | [diff] [blame] | 2641 | ret = btrfs_read_extent_buffer(next, &check); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2642 | if (ret) { |
| 2643 | free_extent_buffer(next); |
| 2644 | return ret; |
| 2645 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2646 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2647 | if (path->nodes[*level-1]) |
| 2648 | free_extent_buffer(path->nodes[*level-1]); |
| 2649 | path->nodes[*level-1] = next; |
| 2650 | *level = btrfs_header_level(next); |
| 2651 | path->slots[*level] = 0; |
| 2652 | cond_resched(); |
| 2653 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2654 | path->slots[*level] = btrfs_header_nritems(path->nodes[*level]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2655 | |
| 2656 | cond_resched(); |
| 2657 | return 0; |
| 2658 | } |
| 2659 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2660 | static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2661 | struct btrfs_root *root, |
| 2662 | struct btrfs_path *path, int *level, |
| 2663 | struct walk_control *wc) |
| 2664 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2665 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2666 | int i; |
| 2667 | int slot; |
| 2668 | int ret; |
| 2669 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2670 | for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2671 | slot = path->slots[i]; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2672 | if (slot + 1 < btrfs_header_nritems(path->nodes[i])) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2673 | path->slots[i]++; |
| 2674 | *level = i; |
| 2675 | WARN_ON(*level == 0); |
| 2676 | return 0; |
| 2677 | } else { |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2678 | ret = wc->process_func(root, path->nodes[*level], wc, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2679 | btrfs_header_generation(path->nodes[*level]), |
| 2680 | *level); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2681 | if (ret) |
| 2682 | return ret; |
| 2683 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2684 | if (wc->free) { |
| 2685 | struct extent_buffer *next; |
| 2686 | |
| 2687 | next = path->nodes[*level]; |
| 2688 | |
Josef Bacik | c4e54a6 | 2023-01-26 16:00:56 -0500 | [diff] [blame] | 2689 | btrfs_tree_lock(next); |
Josef Bacik | 190a833 | 2023-01-26 16:00:58 -0500 | [diff] [blame] | 2690 | btrfs_clear_buffer_dirty(trans, next); |
Josef Bacik | 79b02ec | 2023-01-26 16:01:00 -0500 | [diff] [blame] | 2691 | wait_on_extent_buffer_writeback(next); |
Josef Bacik | c4e54a6 | 2023-01-26 16:00:56 -0500 | [diff] [blame] | 2692 | btrfs_tree_unlock(next); |
| 2693 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2694 | if (trans) { |
Nikolay Borisov | 7bfc100 | 2020-01-20 16:09:12 +0200 | [diff] [blame] | 2695 | ret = btrfs_pin_reserved_extent(trans, |
Nikolay Borisov | 10e958d | 2020-01-20 16:09:11 +0200 | [diff] [blame] | 2696 | path->nodes[*level]->start, |
| 2697 | path->nodes[*level]->len); |
| 2698 | if (ret) |
| 2699 | return ret; |
Naohiro Aota | 84c2544 | 2021-11-30 12:40:21 +0900 | [diff] [blame] | 2700 | btrfs_redirty_list_add(trans->transaction, |
| 2701 | next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2702 | } else { |
Nikolay Borisov | 10e958d | 2020-01-20 16:09:11 +0200 | [diff] [blame] | 2703 | unaccount_log_buffer(fs_info, |
| 2704 | path->nodes[*level]->start); |
| 2705 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2706 | } |
| 2707 | free_extent_buffer(path->nodes[*level]); |
| 2708 | path->nodes[*level] = NULL; |
| 2709 | *level = i + 1; |
| 2710 | } |
| 2711 | } |
| 2712 | return 1; |
| 2713 | } |
| 2714 | |
| 2715 | /* |
| 2716 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 2717 | * the tree freeing any blocks that have a ref count of zero after being |
| 2718 | * decremented. |
| 2719 | */ |
| 2720 | static int walk_log_tree(struct btrfs_trans_handle *trans, |
| 2721 | struct btrfs_root *log, struct walk_control *wc) |
| 2722 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2723 | struct btrfs_fs_info *fs_info = log->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2724 | int ret = 0; |
| 2725 | int wret; |
| 2726 | int level; |
| 2727 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2728 | int orig_level; |
| 2729 | |
| 2730 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 2731 | if (!path) |
| 2732 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2733 | |
| 2734 | level = btrfs_header_level(log->node); |
| 2735 | orig_level = level; |
| 2736 | path->nodes[level] = log->node; |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 2737 | atomic_inc(&log->node->refs); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2738 | path->slots[level] = 0; |
| 2739 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2740 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2741 | wret = walk_down_log_tree(trans, log, path, &level, wc); |
| 2742 | if (wret > 0) |
| 2743 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2744 | if (wret < 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2745 | ret = wret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2746 | goto out; |
| 2747 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2748 | |
| 2749 | wret = walk_up_log_tree(trans, log, path, &level, wc); |
| 2750 | if (wret > 0) |
| 2751 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2752 | if (wret < 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2753 | ret = wret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2754 | goto out; |
| 2755 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | /* was the root node processed? if not, catch it here */ |
| 2759 | if (path->nodes[orig_level]) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2760 | ret = wc->process_func(log, path->nodes[orig_level], wc, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2761 | btrfs_header_generation(path->nodes[orig_level]), |
| 2762 | orig_level); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2763 | if (ret) |
| 2764 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2765 | if (wc->free) { |
| 2766 | struct extent_buffer *next; |
| 2767 | |
| 2768 | next = path->nodes[orig_level]; |
| 2769 | |
Josef Bacik | c4e54a6 | 2023-01-26 16:00:56 -0500 | [diff] [blame] | 2770 | btrfs_tree_lock(next); |
Josef Bacik | 190a833 | 2023-01-26 16:00:58 -0500 | [diff] [blame] | 2771 | btrfs_clear_buffer_dirty(trans, next); |
Josef Bacik | 79b02ec | 2023-01-26 16:01:00 -0500 | [diff] [blame] | 2772 | wait_on_extent_buffer_writeback(next); |
Josef Bacik | c4e54a6 | 2023-01-26 16:00:56 -0500 | [diff] [blame] | 2773 | btrfs_tree_unlock(next); |
| 2774 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2775 | if (trans) { |
Nikolay Borisov | 7bfc100 | 2020-01-20 16:09:12 +0200 | [diff] [blame] | 2776 | ret = btrfs_pin_reserved_extent(trans, |
Nikolay Borisov | 10e958d | 2020-01-20 16:09:11 +0200 | [diff] [blame] | 2777 | next->start, next->len); |
| 2778 | if (ret) |
| 2779 | goto out; |
Naohiro Aota | 84c2544 | 2021-11-30 12:40:21 +0900 | [diff] [blame] | 2780 | btrfs_redirty_list_add(trans->transaction, next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2781 | } else { |
Nikolay Borisov | 10e958d | 2020-01-20 16:09:11 +0200 | [diff] [blame] | 2782 | unaccount_log_buffer(fs_info, next->start); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2783 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2784 | } |
| 2785 | } |
| 2786 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2787 | out: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2788 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2789 | return ret; |
| 2790 | } |
| 2791 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2792 | /* |
| 2793 | * helper function to update the item for a given subvolumes log root |
| 2794 | * in the tree of log roots |
| 2795 | */ |
| 2796 | static int update_log_root(struct btrfs_trans_handle *trans, |
Josef Bacik | 4203e96 | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 2797 | struct btrfs_root *log, |
| 2798 | struct btrfs_root_item *root_item) |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2799 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2800 | struct btrfs_fs_info *fs_info = log->fs_info; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2801 | int ret; |
| 2802 | |
| 2803 | if (log->log_transid == 1) { |
| 2804 | /* insert root item on the first sync */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2805 | ret = btrfs_insert_root(trans, fs_info->log_root_tree, |
Josef Bacik | 4203e96 | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 2806 | &log->root_key, root_item); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2807 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2808 | ret = btrfs_update_root(trans, fs_info->log_root_tree, |
Josef Bacik | 4203e96 | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 2809 | &log->root_key, root_item); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2810 | } |
| 2811 | return ret; |
| 2812 | } |
| 2813 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2814 | static void wait_log_commit(struct btrfs_root *root, int transid) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2815 | { |
| 2816 | DEFINE_WAIT(wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2817 | int index = transid % 2; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2818 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2819 | /* |
| 2820 | * we only allow two pending log transactions at a time, |
| 2821 | * so we know that if ours is more than 2 older than the |
| 2822 | * current transaction, we're done |
| 2823 | */ |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2824 | for (;;) { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2825 | prepare_to_wait(&root->log_commit_wait[index], |
| 2826 | &wait, TASK_UNINTERRUPTIBLE); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2827 | |
| 2828 | if (!(root->log_transid_committed < transid && |
| 2829 | atomic_read(&root->log_commit[index]))) |
| 2830 | break; |
| 2831 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2832 | mutex_unlock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2833 | schedule(); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2834 | mutex_lock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2835 | } |
| 2836 | finish_wait(&root->log_commit_wait[index], &wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2837 | } |
| 2838 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2839 | static void wait_for_writer(struct btrfs_root *root) |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2840 | { |
| 2841 | DEFINE_WAIT(wait); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2842 | |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2843 | for (;;) { |
| 2844 | prepare_to_wait(&root->log_writer_wait, &wait, |
| 2845 | TASK_UNINTERRUPTIBLE); |
| 2846 | if (!atomic_read(&root->log_writers)) |
| 2847 | break; |
| 2848 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2849 | mutex_unlock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2850 | schedule(); |
Filipe Manana | 575849e | 2015-02-11 11:12:39 +0000 | [diff] [blame] | 2851 | mutex_lock(&root->log_mutex); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2852 | } |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2853 | finish_wait(&root->log_writer_wait, &wait); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2854 | } |
| 2855 | |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2856 | static inline void btrfs_remove_log_ctx(struct btrfs_root *root, |
| 2857 | struct btrfs_log_ctx *ctx) |
| 2858 | { |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2859 | mutex_lock(&root->log_mutex); |
| 2860 | list_del_init(&ctx->list); |
| 2861 | mutex_unlock(&root->log_mutex); |
| 2862 | } |
| 2863 | |
| 2864 | /* |
| 2865 | * Invoked in log mutex context, or be sure there is no other task which |
| 2866 | * can access the list. |
| 2867 | */ |
| 2868 | static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root, |
| 2869 | int index, int error) |
| 2870 | { |
| 2871 | struct btrfs_log_ctx *ctx; |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2872 | struct btrfs_log_ctx *safe; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2873 | |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2874 | list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) { |
| 2875 | list_del_init(&ctx->list); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2876 | ctx->log_ret = error; |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2877 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2878 | } |
| 2879 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2880 | /* |
| 2881 | * btrfs_sync_log does sends a given tree log down to the disk and |
| 2882 | * updates the super blocks to record it. When this call is done, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2883 | * you know that any inodes previously logged are safely on disk only |
| 2884 | * if it returns 0. |
| 2885 | * |
| 2886 | * Any other return value means you need to call btrfs_commit_transaction. |
| 2887 | * Some of the edge cases for fsyncing directories that have had unlinks |
| 2888 | * or renames done in the past mean that sometimes the only safe |
| 2889 | * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN, |
| 2890 | * that has happened. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2891 | */ |
| 2892 | int btrfs_sync_log(struct btrfs_trans_handle *trans, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2893 | struct btrfs_root *root, struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2894 | { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2895 | int index1; |
| 2896 | int index2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2897 | int mark; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2898 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2899 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2900 | struct btrfs_root *log = root->log_root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2901 | struct btrfs_root *log_root_tree = fs_info->log_root_tree; |
Josef Bacik | 4203e96 | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 2902 | struct btrfs_root_item new_root_item; |
Miao Xie | bb14a59 | 2014-02-20 18:08:56 +0800 | [diff] [blame] | 2903 | int log_transid = 0; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2904 | struct btrfs_log_ctx root_log_ctx; |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2905 | struct blk_plug plug; |
Filipe Manana | 47876f7 | 2020-11-25 12:19:28 +0000 | [diff] [blame] | 2906 | u64 log_root_start; |
| 2907 | u64 log_root_level; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2908 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2909 | mutex_lock(&root->log_mutex); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2910 | log_transid = ctx->log_transid; |
| 2911 | if (root->log_transid_committed >= log_transid) { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2912 | mutex_unlock(&root->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2913 | return ctx->log_ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2914 | } |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2915 | |
| 2916 | index1 = log_transid % 2; |
| 2917 | if (atomic_read(&root->log_commit[index1])) { |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2918 | wait_log_commit(root, log_transid); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2919 | mutex_unlock(&root->log_mutex); |
| 2920 | return ctx->log_ret; |
| 2921 | } |
| 2922 | ASSERT(log_transid == root->log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2923 | atomic_set(&root->log_commit[index1], 1); |
| 2924 | |
| 2925 | /* wait for previous tree log sync to complete */ |
| 2926 | if (atomic_read(&root->log_commit[(index1 + 1) % 2])) |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2927 | wait_log_commit(root, log_transid - 1); |
Miao Xie | 48cab2e | 2014-02-20 18:08:52 +0800 | [diff] [blame] | 2928 | |
Yan, Zheng | 86df7eb | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2929 | while (1) { |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2930 | int batch = atomic_read(&root->log_batch); |
Chris Mason | cd354ad | 2011-10-20 15:45:37 -0400 | [diff] [blame] | 2931 | /* when we're on an ssd, just kick the log commit out */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2932 | if (!btrfs_test_opt(fs_info, SSD) && |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 2933 | test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) { |
Yan, Zheng | 86df7eb | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2934 | mutex_unlock(&root->log_mutex); |
| 2935 | schedule_timeout_uninterruptible(1); |
| 2936 | mutex_lock(&root->log_mutex); |
| 2937 | } |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2938 | wait_for_writer(root); |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2939 | if (batch == atomic_read(&root->log_batch)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2940 | break; |
| 2941 | } |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2942 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2943 | /* bail out if we need to do a full commit */ |
David Sterba | 4884b8e | 2019-03-20 13:25:34 +0100 | [diff] [blame] | 2944 | if (btrfs_need_log_full_commit(trans)) { |
Josef Bacik | f31f09f | 2022-06-13 15:09:48 -0400 | [diff] [blame] | 2945 | ret = BTRFS_LOG_FORCE_COMMIT; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2946 | mutex_unlock(&root->log_mutex); |
| 2947 | goto out; |
| 2948 | } |
| 2949 | |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2950 | if (log_transid % 2 == 0) |
| 2951 | mark = EXTENT_DIRTY; |
| 2952 | else |
| 2953 | mark = EXTENT_NEW; |
| 2954 | |
Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 2955 | /* we start IO on all the marked extents here, but we don't actually |
| 2956 | * wait for them until later. |
| 2957 | */ |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2958 | blk_start_plug(&plug); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2959 | ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark); |
Naohiro Aota | b528f46 | 2021-02-05 23:58:36 +0900 | [diff] [blame] | 2960 | /* |
| 2961 | * -EAGAIN happens when someone, e.g., a concurrent transaction |
| 2962 | * commit, writes a dirty extent in this tree-log commit. This |
| 2963 | * concurrent write will create a hole writing out the extents, |
| 2964 | * and we cannot proceed on a zoned filesystem, requiring |
| 2965 | * sequential writing. While we can bail out to a full commit |
| 2966 | * here, but we can continue hoping the concurrent writing fills |
| 2967 | * the hole. |
| 2968 | */ |
| 2969 | if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) |
| 2970 | ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2971 | if (ret) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2972 | blk_finish_plug(&plug); |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 2973 | btrfs_set_log_full_commit(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2974 | mutex_unlock(&root->log_mutex); |
| 2975 | goto out; |
| 2976 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2977 | |
Josef Bacik | 4203e96 | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 2978 | /* |
| 2979 | * We _must_ update under the root->log_mutex in order to make sure we |
| 2980 | * have a consistent view of the log root we are trying to commit at |
| 2981 | * this moment. |
| 2982 | * |
| 2983 | * We _must_ copy this into a local copy, because we are not holding the |
| 2984 | * log_root_tree->log_mutex yet. This is important because when we |
| 2985 | * commit the log_root_tree we must have a consistent view of the |
| 2986 | * log_root_tree when we update the super block to point at the |
| 2987 | * log_root_tree bytenr. If we update the log_root_tree here we'll race |
| 2988 | * with the commit and possibly point at the new block which we may not |
| 2989 | * have written out. |
| 2990 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2991 | btrfs_set_root_node(&log->root_item, log->node); |
Josef Bacik | 4203e96 | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 2992 | memcpy(&new_root_item, &log->root_item, sizeof(new_root_item)); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2993 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2994 | root->log_transid++; |
| 2995 | log->log_transid = root->log_transid; |
Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 2996 | root->log_start_pid = 0; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2997 | /* |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2998 | * IO has been started, blocks of the log tree have WRITTEN flag set |
| 2999 | * in their headers. new modifications of the log will be written to |
| 3000 | * new positions. so it's safe to allow log writers to go in. |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3001 | */ |
| 3002 | mutex_unlock(&root->log_mutex); |
| 3003 | |
Naohiro Aota | 3ddebf2 | 2021-02-04 19:22:20 +0900 | [diff] [blame] | 3004 | if (btrfs_is_zoned(fs_info)) { |
Naohiro Aota | e75f9fd | 2021-03-24 23:23:11 +0900 | [diff] [blame] | 3005 | mutex_lock(&fs_info->tree_root->log_mutex); |
Naohiro Aota | 3ddebf2 | 2021-02-04 19:22:20 +0900 | [diff] [blame] | 3006 | if (!log_root_tree->node) { |
| 3007 | ret = btrfs_alloc_log_tree_node(trans, log_root_tree); |
| 3008 | if (ret) { |
Filipe Manana | ea32af4 | 2021-07-07 12:23:45 +0100 | [diff] [blame] | 3009 | mutex_unlock(&fs_info->tree_root->log_mutex); |
Filipe Manana | 50ff578 | 2022-04-06 17:07:54 +0100 | [diff] [blame] | 3010 | blk_finish_plug(&plug); |
Naohiro Aota | 3ddebf2 | 2021-02-04 19:22:20 +0900 | [diff] [blame] | 3011 | goto out; |
| 3012 | } |
| 3013 | } |
Naohiro Aota | e75f9fd | 2021-03-24 23:23:11 +0900 | [diff] [blame] | 3014 | mutex_unlock(&fs_info->tree_root->log_mutex); |
Naohiro Aota | 3ddebf2 | 2021-02-04 19:22:20 +0900 | [diff] [blame] | 3015 | } |
| 3016 | |
Naohiro Aota | e75f9fd | 2021-03-24 23:23:11 +0900 | [diff] [blame] | 3017 | btrfs_init_log_ctx(&root_log_ctx, NULL); |
| 3018 | |
| 3019 | mutex_lock(&log_root_tree->log_mutex); |
| 3020 | |
Filipe Manana | e3d3b41 | 2021-03-11 15:13:30 +0000 | [diff] [blame] | 3021 | index2 = log_root_tree->log_transid % 2; |
| 3022 | list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]); |
| 3023 | root_log_ctx.log_transid = log_root_tree->log_transid; |
| 3024 | |
Josef Bacik | 4203e96 | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 3025 | /* |
| 3026 | * Now we are safe to update the log_root_tree because we're under the |
| 3027 | * log_mutex, and we're a current writer so we're holding the commit |
| 3028 | * open until we drop the log_mutex. |
| 3029 | */ |
| 3030 | ret = update_log_root(trans, log, &new_root_item); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3031 | if (ret) { |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3032 | if (!list_empty(&root_log_ctx.list)) |
| 3033 | list_del_init(&root_log_ctx.list); |
| 3034 | |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3035 | blk_finish_plug(&plug); |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 3036 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 09e4486 | 2023-01-10 14:56:38 +0000 | [diff] [blame] | 3037 | if (ret != -ENOSPC) |
| 3038 | btrfs_err(fs_info, |
| 3039 | "failed to update log for root %llu ret %d", |
| 3040 | root->root_key.objectid, ret); |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3041 | btrfs_wait_tree_log_extents(log, mark); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3042 | mutex_unlock(&log_root_tree->log_mutex); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3043 | goto out; |
| 3044 | } |
| 3045 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3046 | if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) { |
Forrest Liu | 3da5ab5 | 2015-01-30 19:42:12 +0800 | [diff] [blame] | 3047 | blk_finish_plug(&plug); |
Chris Mason | cbd60aa | 2016-09-06 05:37:40 -0700 | [diff] [blame] | 3048 | list_del_init(&root_log_ctx.list); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3049 | mutex_unlock(&log_root_tree->log_mutex); |
| 3050 | ret = root_log_ctx.log_ret; |
| 3051 | goto out; |
| 3052 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3053 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3054 | index2 = root_log_ctx.log_transid % 2; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3055 | if (atomic_read(&log_root_tree->log_commit[index2])) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3056 | blk_finish_plug(&plug); |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3057 | ret = btrfs_wait_tree_log_extents(log, mark); |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3058 | wait_log_commit(log_root_tree, |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3059 | root_log_ctx.log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3060 | mutex_unlock(&log_root_tree->log_mutex); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3061 | if (!ret) |
| 3062 | ret = root_log_ctx.log_ret; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3063 | goto out; |
| 3064 | } |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3065 | ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3066 | atomic_set(&log_root_tree->log_commit[index2], 1); |
| 3067 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3068 | if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) { |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3069 | wait_log_commit(log_root_tree, |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3070 | root_log_ctx.log_transid - 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3071 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3072 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3073 | /* |
| 3074 | * now that we've moved on to the tree of log tree roots, |
| 3075 | * check the full commit flag again |
| 3076 | */ |
David Sterba | 4884b8e | 2019-03-20 13:25:34 +0100 | [diff] [blame] | 3077 | if (btrfs_need_log_full_commit(trans)) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3078 | blk_finish_plug(&plug); |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3079 | btrfs_wait_tree_log_extents(log, mark); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3080 | mutex_unlock(&log_root_tree->log_mutex); |
Josef Bacik | f31f09f | 2022-06-13 15:09:48 -0400 | [diff] [blame] | 3081 | ret = BTRFS_LOG_FORCE_COMMIT; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3082 | goto out_wake_log_root; |
| 3083 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3084 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3085 | ret = btrfs_write_marked_extents(fs_info, |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3086 | &log_root_tree->dirty_log_pages, |
| 3087 | EXTENT_DIRTY | EXTENT_NEW); |
| 3088 | blk_finish_plug(&plug); |
Naohiro Aota | b528f46 | 2021-02-05 23:58:36 +0900 | [diff] [blame] | 3089 | /* |
| 3090 | * As described above, -EAGAIN indicates a hole in the extents. We |
| 3091 | * cannot wait for these write outs since the waiting cause a |
| 3092 | * deadlock. Bail out to the full commit instead. |
| 3093 | */ |
| 3094 | if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) { |
| 3095 | btrfs_set_log_full_commit(trans); |
| 3096 | btrfs_wait_tree_log_extents(log, mark); |
| 3097 | mutex_unlock(&log_root_tree->log_mutex); |
| 3098 | goto out_wake_log_root; |
| 3099 | } else if (ret) { |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 3100 | btrfs_set_log_full_commit(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3101 | mutex_unlock(&log_root_tree->log_mutex); |
| 3102 | goto out_wake_log_root; |
| 3103 | } |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3104 | ret = btrfs_wait_tree_log_extents(log, mark); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3105 | if (!ret) |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3106 | ret = btrfs_wait_tree_log_extents(log_root_tree, |
| 3107 | EXTENT_NEW | EXTENT_DIRTY); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3108 | if (ret) { |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 3109 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3110 | mutex_unlock(&log_root_tree->log_mutex); |
| 3111 | goto out_wake_log_root; |
| 3112 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3113 | |
Filipe Manana | 47876f7 | 2020-11-25 12:19:28 +0000 | [diff] [blame] | 3114 | log_root_start = log_root_tree->node->start; |
| 3115 | log_root_level = btrfs_header_level(log_root_tree->node); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3116 | log_root_tree->log_transid++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3117 | mutex_unlock(&log_root_tree->log_mutex); |
| 3118 | |
| 3119 | /* |
Filipe Manana | 47876f7 | 2020-11-25 12:19:28 +0000 | [diff] [blame] | 3120 | * Here we are guaranteed that nobody is going to write the superblock |
| 3121 | * for the current transaction before us and that neither we do write |
| 3122 | * our superblock before the previous transaction finishes its commit |
| 3123 | * and writes its superblock, because: |
| 3124 | * |
| 3125 | * 1) We are holding a handle on the current transaction, so no body |
| 3126 | * can commit it until we release the handle; |
| 3127 | * |
| 3128 | * 2) Before writing our superblock we acquire the tree_log_mutex, so |
| 3129 | * if the previous transaction is still committing, and hasn't yet |
| 3130 | * written its superblock, we wait for it to do it, because a |
| 3131 | * transaction commit acquires the tree_log_mutex when the commit |
| 3132 | * begins and releases it only after writing its superblock. |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3133 | */ |
Filipe Manana | 47876f7 | 2020-11-25 12:19:28 +0000 | [diff] [blame] | 3134 | mutex_lock(&fs_info->tree_log_mutex); |
Josef Bacik | 165ea85 | 2021-05-19 17:15:53 -0400 | [diff] [blame] | 3135 | |
| 3136 | /* |
| 3137 | * The previous transaction writeout phase could have failed, and thus |
| 3138 | * marked the fs in an error state. We must not commit here, as we |
| 3139 | * could have updated our generation in the super_for_commit and |
| 3140 | * writing the super here would result in transid mismatches. If there |
| 3141 | * is an error here just bail. |
| 3142 | */ |
Josef Bacik | 8496153 | 2021-10-05 16:35:25 -0400 | [diff] [blame] | 3143 | if (BTRFS_FS_ERROR(fs_info)) { |
Josef Bacik | 165ea85 | 2021-05-19 17:15:53 -0400 | [diff] [blame] | 3144 | ret = -EIO; |
| 3145 | btrfs_set_log_full_commit(trans); |
| 3146 | btrfs_abort_transaction(trans, ret); |
| 3147 | mutex_unlock(&fs_info->tree_log_mutex); |
| 3148 | goto out_wake_log_root; |
| 3149 | } |
| 3150 | |
Filipe Manana | 47876f7 | 2020-11-25 12:19:28 +0000 | [diff] [blame] | 3151 | btrfs_set_super_log_root(fs_info->super_for_commit, log_root_start); |
| 3152 | btrfs_set_super_log_root_level(fs_info->super_for_commit, log_root_level); |
David Sterba | eece6a9 | 2017-02-10 19:04:32 +0100 | [diff] [blame] | 3153 | ret = write_all_supers(fs_info, 1); |
Filipe Manana | 47876f7 | 2020-11-25 12:19:28 +0000 | [diff] [blame] | 3154 | mutex_unlock(&fs_info->tree_log_mutex); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3155 | if (ret) { |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 3156 | btrfs_set_log_full_commit(trans); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3157 | btrfs_abort_transaction(trans, ret); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3158 | goto out_wake_log_root; |
| 3159 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3160 | |
Filipe Manana | e1a6d26 | 2021-07-20 16:03:41 +0100 | [diff] [blame] | 3161 | /* |
| 3162 | * We know there can only be one task here, since we have not yet set |
| 3163 | * root->log_commit[index1] to 0 and any task attempting to sync the |
| 3164 | * log must wait for the previous log transaction to commit if it's |
| 3165 | * still in progress or wait for the current log transaction commit if |
| 3166 | * someone else already started it. We use <= and not < because the |
| 3167 | * first log transaction has an ID of 0. |
| 3168 | */ |
| 3169 | ASSERT(root->last_log_commit <= log_transid); |
| 3170 | root->last_log_commit = log_transid; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 3171 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3172 | out_wake_log_root: |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 3173 | mutex_lock(&log_root_tree->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3174 | btrfs_remove_all_log_ctxs(log_root_tree, index2, ret); |
| 3175 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3176 | log_root_tree->log_transid_committed++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3177 | atomic_set(&log_root_tree->log_commit[index2], 0); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3178 | mutex_unlock(&log_root_tree->log_mutex); |
| 3179 | |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3180 | /* |
David Sterba | 093258e6 | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3181 | * The barrier before waitqueue_active (in cond_wake_up) is needed so |
| 3182 | * all the updates above are seen by the woken threads. It might not be |
| 3183 | * necessary, but proving that seems to be hard. |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3184 | */ |
David Sterba | 093258e6 | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3185 | cond_wake_up(&log_root_tree->log_commit_wait[index2]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3186 | out: |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3187 | mutex_lock(&root->log_mutex); |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 3188 | btrfs_remove_all_log_ctxs(root, index1, ret); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3189 | root->log_transid_committed++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3190 | atomic_set(&root->log_commit[index1], 0); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3191 | mutex_unlock(&root->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3192 | |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3193 | /* |
David Sterba | 093258e6 | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3194 | * The barrier before waitqueue_active (in cond_wake_up) is needed so |
| 3195 | * all the updates above are seen by the woken threads. It might not be |
| 3196 | * necessary, but proving that seems to be hard. |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3197 | */ |
David Sterba | 093258e6 | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3198 | cond_wake_up(&root->log_commit_wait[index1]); |
Chris Mason | b31eabd | 2011-01-31 16:48:24 -0500 | [diff] [blame] | 3199 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3200 | } |
| 3201 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3202 | static void free_log_tree(struct btrfs_trans_handle *trans, |
| 3203 | struct btrfs_root *log) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3204 | { |
| 3205 | int ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3206 | struct walk_control wc = { |
| 3207 | .free = 1, |
| 3208 | .process_func = process_one_buffer |
| 3209 | }; |
| 3210 | |
Naohiro Aota | 3ddebf2 | 2021-02-04 19:22:20 +0900 | [diff] [blame] | 3211 | if (log->node) { |
| 3212 | ret = walk_log_tree(trans, log, &wc); |
| 3213 | if (ret) { |
Filipe Manana | 40cdc50 | 2022-01-18 13:39:34 +0000 | [diff] [blame] | 3214 | /* |
| 3215 | * We weren't able to traverse the entire log tree, the |
| 3216 | * typical scenario is getting an -EIO when reading an |
| 3217 | * extent buffer of the tree, due to a previous writeback |
| 3218 | * failure of it. |
| 3219 | */ |
| 3220 | set_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR, |
| 3221 | &log->fs_info->fs_state); |
| 3222 | |
| 3223 | /* |
| 3224 | * Some extent buffers of the log tree may still be dirty |
| 3225 | * and not yet written back to storage, because we may |
| 3226 | * have updates to a log tree without syncing a log tree, |
| 3227 | * such as during rename and link operations. So flush |
| 3228 | * them out and wait for their writeback to complete, so |
| 3229 | * that we properly cleanup their state and pages. |
| 3230 | */ |
| 3231 | btrfs_write_marked_extents(log->fs_info, |
| 3232 | &log->dirty_log_pages, |
| 3233 | EXTENT_DIRTY | EXTENT_NEW); |
| 3234 | btrfs_wait_tree_log_extents(log, |
| 3235 | EXTENT_DIRTY | EXTENT_NEW); |
| 3236 | |
Naohiro Aota | 3ddebf2 | 2021-02-04 19:22:20 +0900 | [diff] [blame] | 3237 | if (trans) |
| 3238 | btrfs_abort_transaction(trans, ret); |
| 3239 | else |
| 3240 | btrfs_handle_fs_error(log->fs_info, ret, NULL); |
| 3241 | } |
Jeff Mahoney | 374b0e2 | 2018-09-06 16:59:33 -0400 | [diff] [blame] | 3242 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3243 | |
Filipe Manana | 59b0713 | 2018-11-09 10:43:08 +0000 | [diff] [blame] | 3244 | clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1, |
| 3245 | EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT); |
Filipe Manana | e289f03 | 2020-05-18 12:14:50 +0100 | [diff] [blame] | 3246 | extent_io_tree_release(&log->log_csum_range); |
Naohiro Aota | d3575156 | 2021-02-04 19:21:54 +0900 | [diff] [blame] | 3247 | |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 3248 | btrfs_put_root(log); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | /* |
| 3252 | * free all the extents used by the tree log. This should be called |
| 3253 | * at commit time of the full transaction |
| 3254 | */ |
| 3255 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) |
| 3256 | { |
| 3257 | if (root->log_root) { |
| 3258 | free_log_tree(trans, root->log_root); |
| 3259 | root->log_root = NULL; |
Filipe Manana | e7a7981 | 2020-06-15 10:38:44 +0100 | [diff] [blame] | 3260 | clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3261 | } |
| 3262 | return 0; |
| 3263 | } |
| 3264 | |
| 3265 | int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, |
| 3266 | struct btrfs_fs_info *fs_info) |
| 3267 | { |
| 3268 | if (fs_info->log_root_tree) { |
| 3269 | free_log_tree(trans, fs_info->log_root_tree); |
| 3270 | fs_info->log_root_tree = NULL; |
Filipe Manana | 47876f7 | 2020-11-25 12:19:28 +0000 | [diff] [blame] | 3271 | clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &fs_info->tree_root->state); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3272 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3273 | return 0; |
| 3274 | } |
| 3275 | |
| 3276 | /* |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 3277 | * Check if an inode was logged in the current transaction. This correctly deals |
| 3278 | * with the case where the inode was logged but has a logged_trans of 0, which |
| 3279 | * happens if the inode is evicted and loaded again, as logged_trans is an in |
| 3280 | * memory only field (not persisted). |
| 3281 | * |
| 3282 | * Returns 1 if the inode was logged before in the transaction, 0 if it was not, |
| 3283 | * and < 0 on error. |
Filipe Manana | 803f0f6 | 2019-06-19 13:05:39 +0100 | [diff] [blame] | 3284 | */ |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 3285 | static int inode_logged(struct btrfs_trans_handle *trans, |
| 3286 | struct btrfs_inode *inode, |
| 3287 | struct btrfs_path *path_in) |
Filipe Manana | 803f0f6 | 2019-06-19 13:05:39 +0100 | [diff] [blame] | 3288 | { |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 3289 | struct btrfs_path *path = path_in; |
| 3290 | struct btrfs_key key; |
| 3291 | int ret; |
Filipe Manana | 803f0f6 | 2019-06-19 13:05:39 +0100 | [diff] [blame] | 3292 | |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 3293 | if (inode->logged_trans == trans->transid) |
| 3294 | return 1; |
Filipe Manana | 1e0860f | 2021-08-31 15:30:31 +0100 | [diff] [blame] | 3295 | |
Filipe Manana | 6e8e777 | 2021-07-27 11:24:44 +0100 | [diff] [blame] | 3296 | /* |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 3297 | * If logged_trans is not 0, then we know the inode logged was not logged |
| 3298 | * in this transaction, so we can return false right away. |
Filipe Manana | 6e8e777 | 2021-07-27 11:24:44 +0100 | [diff] [blame] | 3299 | */ |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 3300 | if (inode->logged_trans > 0) |
| 3301 | return 0; |
Filipe Manana | 803f0f6 | 2019-06-19 13:05:39 +0100 | [diff] [blame] | 3302 | |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 3303 | /* |
| 3304 | * If no log tree was created for this root in this transaction, then |
| 3305 | * the inode can not have been logged in this transaction. In that case |
| 3306 | * set logged_trans to anything greater than 0 and less than the current |
| 3307 | * transaction's ID, to avoid the search below in a future call in case |
| 3308 | * a log tree gets created after this. |
| 3309 | */ |
| 3310 | if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state)) { |
| 3311 | inode->logged_trans = trans->transid - 1; |
| 3312 | return 0; |
| 3313 | } |
| 3314 | |
| 3315 | /* |
| 3316 | * We have a log tree and the inode's logged_trans is 0. We can't tell |
| 3317 | * for sure if the inode was logged before in this transaction by looking |
| 3318 | * only at logged_trans. We could be pessimistic and assume it was, but |
| 3319 | * that can lead to unnecessarily logging an inode during rename and link |
| 3320 | * operations, and then further updating the log in followup rename and |
| 3321 | * link operations, specially if it's a directory, which adds latency |
| 3322 | * visible to applications doing a series of rename or link operations. |
| 3323 | * |
| 3324 | * A logged_trans of 0 here can mean several things: |
| 3325 | * |
| 3326 | * 1) The inode was never logged since the filesystem was mounted, and may |
| 3327 | * or may have not been evicted and loaded again; |
| 3328 | * |
| 3329 | * 2) The inode was logged in a previous transaction, then evicted and |
| 3330 | * then loaded again; |
| 3331 | * |
| 3332 | * 3) The inode was logged in the current transaction, then evicted and |
| 3333 | * then loaded again. |
| 3334 | * |
| 3335 | * For cases 1) and 2) we don't want to return true, but we need to detect |
| 3336 | * case 3) and return true. So we do a search in the log root for the inode |
| 3337 | * item. |
| 3338 | */ |
| 3339 | key.objectid = btrfs_ino(inode); |
| 3340 | key.type = BTRFS_INODE_ITEM_KEY; |
| 3341 | key.offset = 0; |
| 3342 | |
| 3343 | if (!path) { |
| 3344 | path = btrfs_alloc_path(); |
| 3345 | if (!path) |
| 3346 | return -ENOMEM; |
| 3347 | } |
| 3348 | |
| 3349 | ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0); |
| 3350 | |
| 3351 | if (path_in) |
| 3352 | btrfs_release_path(path); |
| 3353 | else |
| 3354 | btrfs_free_path(path); |
| 3355 | |
| 3356 | /* |
| 3357 | * Logging an inode always results in logging its inode item. So if we |
| 3358 | * did not find the item we know the inode was not logged for sure. |
| 3359 | */ |
| 3360 | if (ret < 0) { |
| 3361 | return ret; |
| 3362 | } else if (ret > 0) { |
| 3363 | /* |
| 3364 | * Set logged_trans to a value greater than 0 and less then the |
| 3365 | * current transaction to avoid doing the search in future calls. |
| 3366 | */ |
| 3367 | inode->logged_trans = trans->transid - 1; |
| 3368 | return 0; |
| 3369 | } |
| 3370 | |
| 3371 | /* |
| 3372 | * The inode was previously logged and then evicted, set logged_trans to |
| 3373 | * the current transacion's ID, to avoid future tree searches as long as |
| 3374 | * the inode is not evicted again. |
| 3375 | */ |
| 3376 | inode->logged_trans = trans->transid; |
| 3377 | |
| 3378 | /* |
| 3379 | * If it's a directory, then we must set last_dir_index_offset to the |
| 3380 | * maximum possible value, so that the next attempt to log the inode does |
| 3381 | * not skip checking if dir index keys found in modified subvolume tree |
| 3382 | * leaves have been logged before, otherwise it would result in attempts |
| 3383 | * to insert duplicate dir index keys in the log tree. This must be done |
| 3384 | * because last_dir_index_offset is an in-memory only field, not persisted |
| 3385 | * in the inode item or any other on-disk structure, so its value is lost |
| 3386 | * once the inode is evicted. |
| 3387 | */ |
| 3388 | if (S_ISDIR(inode->vfs_inode.i_mode)) |
| 3389 | inode->last_dir_index_offset = (u64)-1; |
| 3390 | |
| 3391 | return 1; |
Filipe Manana | 803f0f6 | 2019-06-19 13:05:39 +0100 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | /* |
Filipe Manana | 839061f | 2022-01-20 11:00:06 +0000 | [diff] [blame] | 3395 | * Delete a directory entry from the log if it exists. |
| 3396 | * |
| 3397 | * Returns < 0 on error |
| 3398 | * 1 if the entry does not exists |
| 3399 | * 0 if the entry existed and was successfully deleted |
| 3400 | */ |
| 3401 | static int del_logged_dentry(struct btrfs_trans_handle *trans, |
| 3402 | struct btrfs_root *log, |
| 3403 | struct btrfs_path *path, |
| 3404 | u64 dir_ino, |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 3405 | const struct fscrypt_str *name, |
Filipe Manana | 839061f | 2022-01-20 11:00:06 +0000 | [diff] [blame] | 3406 | u64 index) |
| 3407 | { |
| 3408 | struct btrfs_dir_item *di; |
| 3409 | |
| 3410 | /* |
| 3411 | * We only log dir index items of a directory, so we don't need to look |
| 3412 | * for dir item keys. |
| 3413 | */ |
| 3414 | di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 3415 | index, name, -1); |
Filipe Manana | 839061f | 2022-01-20 11:00:06 +0000 | [diff] [blame] | 3416 | if (IS_ERR(di)) |
| 3417 | return PTR_ERR(di); |
| 3418 | else if (!di) |
| 3419 | return 1; |
| 3420 | |
| 3421 | /* |
| 3422 | * We do not need to update the size field of the directory's |
| 3423 | * inode item because on log replay we update the field to reflect |
| 3424 | * all existing entries in the directory (see overwrite_item()). |
| 3425 | */ |
| 3426 | return btrfs_delete_one_dir_name(trans, log, path, di); |
| 3427 | } |
| 3428 | |
| 3429 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3430 | * If both a file and directory are logged, and unlinks or renames are |
| 3431 | * mixed in, we have a few interesting corners: |
| 3432 | * |
| 3433 | * create file X in dir Y |
| 3434 | * link file X to X.link in dir Y |
| 3435 | * fsync file X |
| 3436 | * unlink file X but leave X.link |
| 3437 | * fsync dir Y |
| 3438 | * |
| 3439 | * After a crash we would expect only X.link to exist. But file X |
| 3440 | * didn't get fsync'd again so the log has back refs for X and X.link. |
| 3441 | * |
| 3442 | * We solve this by removing directory entries and inode backrefs from the |
| 3443 | * log when a file that was logged in the current transaction is |
| 3444 | * unlinked. Any later fsync will include the updated log entries, and |
| 3445 | * we'll be able to reconstruct the proper directory items from backrefs. |
| 3446 | * |
| 3447 | * This optimizations allows us to avoid relogging the entire inode |
| 3448 | * or the entire directory. |
| 3449 | */ |
Josef Bacik | 9a35fc9 | 2021-10-05 16:35:24 -0400 | [diff] [blame] | 3450 | void btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, |
| 3451 | struct btrfs_root *root, |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 3452 | const struct fscrypt_str *name, |
Josef Bacik | 9a35fc9 | 2021-10-05 16:35:24 -0400 | [diff] [blame] | 3453 | struct btrfs_inode *dir, u64 index) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3454 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3455 | struct btrfs_path *path; |
| 3456 | int ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3457 | |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 3458 | ret = inode_logged(trans, dir, NULL); |
| 3459 | if (ret == 0) |
Josef Bacik | 9a35fc9 | 2021-10-05 16:35:24 -0400 | [diff] [blame] | 3460 | return; |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 3461 | else if (ret < 0) { |
| 3462 | btrfs_set_log_full_commit(trans); |
| 3463 | return; |
| 3464 | } |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 3465 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3466 | ret = join_running_log_trans(root); |
| 3467 | if (ret) |
Josef Bacik | 9a35fc9 | 2021-10-05 16:35:24 -0400 | [diff] [blame] | 3468 | return; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3469 | |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3470 | mutex_lock(&dir->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3471 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3472 | path = btrfs_alloc_path(); |
Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3473 | if (!path) { |
Filipe Manana | 839061f | 2022-01-20 11:00:06 +0000 | [diff] [blame] | 3474 | ret = -ENOMEM; |
Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3475 | goto out_unlock; |
| 3476 | } |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 3477 | |
Filipe Manana | 839061f | 2022-01-20 11:00:06 +0000 | [diff] [blame] | 3478 | ret = del_logged_dentry(trans, root->log_root, path, btrfs_ino(dir), |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 3479 | name, index); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3480 | btrfs_free_path(path); |
Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3481 | out_unlock: |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3482 | mutex_unlock(&dir->log_mutex); |
Filipe Manana | 839061f | 2022-01-20 11:00:06 +0000 | [diff] [blame] | 3483 | if (ret < 0) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 3484 | btrfs_set_log_full_commit(trans); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3485 | btrfs_end_log_trans(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | /* see comments for btrfs_del_dir_entries_in_log */ |
Josef Bacik | 9a35fc9 | 2021-10-05 16:35:24 -0400 | [diff] [blame] | 3489 | void btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, |
| 3490 | struct btrfs_root *root, |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 3491 | const struct fscrypt_str *name, |
Josef Bacik | 9a35fc9 | 2021-10-05 16:35:24 -0400 | [diff] [blame] | 3492 | struct btrfs_inode *inode, u64 dirid) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3493 | { |
| 3494 | struct btrfs_root *log; |
| 3495 | u64 index; |
| 3496 | int ret; |
| 3497 | |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 3498 | ret = inode_logged(trans, inode, NULL); |
| 3499 | if (ret == 0) |
Josef Bacik | 9a35fc9 | 2021-10-05 16:35:24 -0400 | [diff] [blame] | 3500 | return; |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 3501 | else if (ret < 0) { |
| 3502 | btrfs_set_log_full_commit(trans); |
| 3503 | return; |
| 3504 | } |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 3505 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3506 | ret = join_running_log_trans(root); |
| 3507 | if (ret) |
Josef Bacik | 9a35fc9 | 2021-10-05 16:35:24 -0400 | [diff] [blame] | 3508 | return; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3509 | log = root->log_root; |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3510 | mutex_lock(&inode->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3511 | |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 3512 | ret = btrfs_del_inode_ref(trans, log, name, btrfs_ino(inode), |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3513 | dirid, &index); |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3514 | mutex_unlock(&inode->log_mutex); |
Josef Bacik | 9a35fc9 | 2021-10-05 16:35:24 -0400 | [diff] [blame] | 3515 | if (ret < 0 && ret != -ENOENT) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 3516 | btrfs_set_log_full_commit(trans); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3517 | btrfs_end_log_trans(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | /* |
| 3521 | * creates a range item in the log for 'dirid'. first_offset and |
| 3522 | * last_offset tell us which parts of the key space the log should |
| 3523 | * be considered authoritative for. |
| 3524 | */ |
| 3525 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, |
| 3526 | struct btrfs_root *log, |
| 3527 | struct btrfs_path *path, |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 3528 | u64 dirid, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3529 | u64 first_offset, u64 last_offset) |
| 3530 | { |
| 3531 | int ret; |
| 3532 | struct btrfs_key key; |
| 3533 | struct btrfs_dir_log_item *item; |
| 3534 | |
| 3535 | key.objectid = dirid; |
| 3536 | key.offset = first_offset; |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 3537 | key.type = BTRFS_DIR_LOG_INDEX_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3538 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); |
Filipe Manana | 750ee45 | 2022-05-03 11:57:02 +0100 | [diff] [blame] | 3539 | /* |
| 3540 | * -EEXIST is fine and can happen sporadically when we are logging a |
| 3541 | * directory and have concurrent insertions in the subvolume's tree for |
| 3542 | * items from other inodes and that result in pushing off some dir items |
| 3543 | * from one leaf to another in order to accommodate for the new items. |
| 3544 | * This results in logging the same dir index range key. |
| 3545 | */ |
| 3546 | if (ret && ret != -EEXIST) |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3547 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3548 | |
| 3549 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3550 | struct btrfs_dir_log_item); |
Filipe Manana | 750ee45 | 2022-05-03 11:57:02 +0100 | [diff] [blame] | 3551 | if (ret == -EEXIST) { |
| 3552 | const u64 curr_end = btrfs_dir_log_end(path->nodes[0], item); |
| 3553 | |
| 3554 | /* |
| 3555 | * btrfs_del_dir_entries_in_log() might have been called during |
| 3556 | * an unlink between the initial insertion of this key and the |
| 3557 | * current update, or we might be logging a single entry deletion |
| 3558 | * during a rename, so set the new last_offset to the max value. |
| 3559 | */ |
| 3560 | last_offset = max(last_offset, curr_end); |
| 3561 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3562 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); |
| 3563 | btrfs_mark_buffer_dirty(path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3564 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3565 | return 0; |
| 3566 | } |
| 3567 | |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3568 | static int flush_dir_items_batch(struct btrfs_trans_handle *trans, |
Filipe Manana | 6afaed5 | 2023-01-10 14:56:39 +0000 | [diff] [blame] | 3569 | struct btrfs_inode *inode, |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3570 | struct extent_buffer *src, |
| 3571 | struct btrfs_path *dst_path, |
| 3572 | int start_slot, |
| 3573 | int count) |
| 3574 | { |
Filipe Manana | 6afaed5 | 2023-01-10 14:56:39 +0000 | [diff] [blame] | 3575 | struct btrfs_root *log = inode->root->log_root; |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3576 | char *ins_data = NULL; |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 3577 | struct btrfs_item_batch batch; |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3578 | struct extent_buffer *dst; |
Filipe Manana | da1b811 | 2021-09-24 12:28:15 +0100 | [diff] [blame] | 3579 | unsigned long src_offset; |
| 3580 | unsigned long dst_offset; |
Filipe Manana | 6afaed5 | 2023-01-10 14:56:39 +0000 | [diff] [blame] | 3581 | u64 last_index; |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3582 | struct btrfs_key key; |
| 3583 | u32 item_size; |
| 3584 | int ret; |
| 3585 | int i; |
| 3586 | |
| 3587 | ASSERT(count > 0); |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 3588 | batch.nr = count; |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3589 | |
| 3590 | if (count == 1) { |
| 3591 | btrfs_item_key_to_cpu(src, &key, start_slot); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 3592 | item_size = btrfs_item_size(src, start_slot); |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 3593 | batch.keys = &key; |
| 3594 | batch.data_sizes = &item_size; |
| 3595 | batch.total_data_size = item_size; |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3596 | } else { |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 3597 | struct btrfs_key *ins_keys; |
| 3598 | u32 *ins_sizes; |
| 3599 | |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3600 | ins_data = kmalloc(count * sizeof(u32) + |
| 3601 | count * sizeof(struct btrfs_key), GFP_NOFS); |
| 3602 | if (!ins_data) |
| 3603 | return -ENOMEM; |
| 3604 | |
| 3605 | ins_sizes = (u32 *)ins_data; |
| 3606 | ins_keys = (struct btrfs_key *)(ins_data + count * sizeof(u32)); |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 3607 | batch.keys = ins_keys; |
| 3608 | batch.data_sizes = ins_sizes; |
| 3609 | batch.total_data_size = 0; |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3610 | |
| 3611 | for (i = 0; i < count; i++) { |
| 3612 | const int slot = start_slot + i; |
| 3613 | |
| 3614 | btrfs_item_key_to_cpu(src, &ins_keys[i], slot); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 3615 | ins_sizes[i] = btrfs_item_size(src, slot); |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 3616 | batch.total_data_size += ins_sizes[i]; |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3617 | } |
| 3618 | } |
| 3619 | |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 3620 | ret = btrfs_insert_empty_items(trans, log, dst_path, &batch); |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3621 | if (ret) |
| 3622 | goto out; |
| 3623 | |
| 3624 | dst = dst_path->nodes[0]; |
Filipe Manana | da1b811 | 2021-09-24 12:28:15 +0100 | [diff] [blame] | 3625 | /* |
| 3626 | * Copy all the items in bulk, in a single copy operation. Item data is |
| 3627 | * organized such that it's placed at the end of a leaf and from right |
| 3628 | * to left. For example, the data for the second item ends at an offset |
| 3629 | * that matches the offset where the data for the first item starts, the |
| 3630 | * data for the third item ends at an offset that matches the offset |
| 3631 | * where the data of the second items starts, and so on. |
| 3632 | * Therefore our source and destination start offsets for copy match the |
| 3633 | * offsets of the last items (highest slots). |
| 3634 | */ |
| 3635 | dst_offset = btrfs_item_ptr_offset(dst, dst_path->slots[0] + count - 1); |
| 3636 | src_offset = btrfs_item_ptr_offset(src, start_slot + count - 1); |
| 3637 | copy_extent_buffer(dst, src, dst_offset, src_offset, batch.total_data_size); |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3638 | btrfs_release_path(dst_path); |
Filipe Manana | 6afaed5 | 2023-01-10 14:56:39 +0000 | [diff] [blame] | 3639 | |
| 3640 | last_index = batch.keys[count - 1].offset; |
| 3641 | ASSERT(last_index > inode->last_dir_index_offset); |
| 3642 | |
| 3643 | /* |
| 3644 | * If for some unexpected reason the last item's index is not greater |
Filipe Manana | 5cce178 | 2023-01-10 14:56:40 +0000 | [diff] [blame] | 3645 | * than the last index we logged, warn and force a transaction commit. |
Filipe Manana | 6afaed5 | 2023-01-10 14:56:39 +0000 | [diff] [blame] | 3646 | */ |
| 3647 | if (WARN_ON(last_index <= inode->last_dir_index_offset)) |
Filipe Manana | 5cce178 | 2023-01-10 14:56:40 +0000 | [diff] [blame] | 3648 | ret = BTRFS_LOG_FORCE_COMMIT; |
Filipe Manana | 6afaed5 | 2023-01-10 14:56:39 +0000 | [diff] [blame] | 3649 | else |
| 3650 | inode->last_dir_index_offset = last_index; |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3651 | out: |
| 3652 | kfree(ins_data); |
| 3653 | |
| 3654 | return ret; |
| 3655 | } |
| 3656 | |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3657 | static int process_dir_items_leaf(struct btrfs_trans_handle *trans, |
| 3658 | struct btrfs_inode *inode, |
| 3659 | struct btrfs_path *path, |
| 3660 | struct btrfs_path *dst_path, |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3661 | struct btrfs_log_ctx *ctx, |
| 3662 | u64 *last_old_dentry_offset) |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3663 | { |
| 3664 | struct btrfs_root *log = inode->root->log_root; |
Filipe Manana | 796787c | 2022-11-21 10:23:22 +0000 | [diff] [blame] | 3665 | struct extent_buffer *src; |
| 3666 | const int nritems = btrfs_header_nritems(path->nodes[0]); |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3667 | const u64 ino = btrfs_ino(inode); |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3668 | bool last_found = false; |
| 3669 | int batch_start = 0; |
| 3670 | int batch_size = 0; |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3671 | int i; |
| 3672 | |
Filipe Manana | 796787c | 2022-11-21 10:23:22 +0000 | [diff] [blame] | 3673 | /* |
| 3674 | * We need to clone the leaf, release the read lock on it, and use the |
| 3675 | * clone before modifying the log tree. See the comment at copy_items() |
| 3676 | * about why we need to do this. |
| 3677 | */ |
| 3678 | src = btrfs_clone_extent_buffer(path->nodes[0]); |
| 3679 | if (!src) |
| 3680 | return -ENOMEM; |
| 3681 | |
| 3682 | i = path->slots[0]; |
| 3683 | btrfs_release_path(path); |
| 3684 | path->nodes[0] = src; |
| 3685 | path->slots[0] = i; |
| 3686 | |
| 3687 | for (; i < nritems; i++) { |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3688 | struct btrfs_dir_item *di; |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3689 | struct btrfs_key key; |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3690 | int ret; |
| 3691 | |
| 3692 | btrfs_item_key_to_cpu(src, &key, i); |
| 3693 | |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 3694 | if (key.objectid != ino || key.type != BTRFS_DIR_INDEX_KEY) { |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3695 | last_found = true; |
| 3696 | break; |
| 3697 | } |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3698 | |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3699 | di = btrfs_item_ptr(src, i, struct btrfs_dir_item); |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3700 | |
| 3701 | /* |
| 3702 | * Skip ranges of items that consist only of dir item keys created |
| 3703 | * in past transactions. However if we find a gap, we must log a |
| 3704 | * dir index range item for that gap, so that index keys in that |
| 3705 | * gap are deleted during log replay. |
| 3706 | */ |
| 3707 | if (btrfs_dir_transid(src, di) < trans->transid) { |
| 3708 | if (key.offset > *last_old_dentry_offset + 1) { |
| 3709 | ret = insert_dir_log_key(trans, log, dst_path, |
| 3710 | ino, *last_old_dentry_offset + 1, |
| 3711 | key.offset - 1); |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3712 | if (ret < 0) |
| 3713 | return ret; |
| 3714 | } |
| 3715 | |
| 3716 | *last_old_dentry_offset = key.offset; |
| 3717 | continue; |
| 3718 | } |
Filipe Manana | 193df62 | 2022-08-22 11:51:39 +0100 | [diff] [blame] | 3719 | |
| 3720 | /* If we logged this dir index item before, we can skip it. */ |
| 3721 | if (key.offset <= inode->last_dir_index_offset) |
| 3722 | continue; |
| 3723 | |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3724 | /* |
| 3725 | * We must make sure that when we log a directory entry, the |
| 3726 | * corresponding inode, after log replay, has a matching link |
| 3727 | * count. For example: |
| 3728 | * |
| 3729 | * touch foo |
| 3730 | * mkdir mydir |
| 3731 | * sync |
| 3732 | * ln foo mydir/bar |
| 3733 | * xfs_io -c "fsync" mydir |
| 3734 | * <crash> |
| 3735 | * <mount fs and log replay> |
| 3736 | * |
| 3737 | * Would result in a fsync log that when replayed, our file inode |
| 3738 | * would have a link count of 1, but we get two directory entries |
| 3739 | * pointing to the same inode. After removing one of the names, |
| 3740 | * it would not be possible to remove the other name, which |
| 3741 | * resulted always in stale file handle errors, and would not be |
| 3742 | * possible to rmdir the parent directory, since its i_size could |
| 3743 | * never be decremented to the value BTRFS_EMPTY_DIR_SIZE, |
| 3744 | * resulting in -ENOTEMPTY errors. |
| 3745 | */ |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3746 | if (!ctx->log_new_dentries) { |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3747 | struct btrfs_key di_key; |
| 3748 | |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3749 | btrfs_dir_item_key_to_cpu(src, di, &di_key); |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3750 | if (di_key.type != BTRFS_ROOT_ITEM_KEY) |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3751 | ctx->log_new_dentries = true; |
| 3752 | } |
| 3753 | |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3754 | if (batch_size == 0) |
| 3755 | batch_start = i; |
| 3756 | batch_size++; |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3757 | } |
| 3758 | |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3759 | if (batch_size > 0) { |
| 3760 | int ret; |
| 3761 | |
Filipe Manana | 6afaed5 | 2023-01-10 14:56:39 +0000 | [diff] [blame] | 3762 | ret = flush_dir_items_batch(trans, inode, src, dst_path, |
Filipe Manana | 086dcbf | 2021-09-16 11:32:13 +0100 | [diff] [blame] | 3763 | batch_start, batch_size); |
| 3764 | if (ret < 0) |
| 3765 | return ret; |
| 3766 | } |
| 3767 | |
| 3768 | return last_found ? 1 : 0; |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3769 | } |
| 3770 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3771 | /* |
| 3772 | * log all the items included in the current transaction for a given |
| 3773 | * directory. This also creates the range items in the log tree required |
| 3774 | * to replay anything deleted before the fsync |
| 3775 | */ |
| 3776 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 3777 | struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3778 | struct btrfs_path *path, |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 3779 | struct btrfs_path *dst_path, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3780 | struct btrfs_log_ctx *ctx, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3781 | u64 min_offset, u64 *last_offset_ret) |
| 3782 | { |
| 3783 | struct btrfs_key min_key; |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 3784 | struct btrfs_root *root = inode->root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3785 | struct btrfs_root *log = root->log_root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3786 | int ret; |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3787 | u64 last_old_dentry_offset = min_offset - 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3788 | u64 last_offset = (u64)-1; |
Nikolay Borisov | 684a577 | 2017-01-18 00:31:41 +0200 | [diff] [blame] | 3789 | u64 ino = btrfs_ino(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3790 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3791 | min_key.objectid = ino; |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 3792 | min_key.type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3793 | min_key.offset = min_offset; |
| 3794 | |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3795 | ret = btrfs_search_forward(root, &min_key, path, trans->transid); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3796 | |
| 3797 | /* |
| 3798 | * we didn't find anything from this transaction, see if there |
| 3799 | * is anything at all |
| 3800 | */ |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 3801 | if (ret != 0 || min_key.objectid != ino || |
| 3802 | min_key.type != BTRFS_DIR_INDEX_KEY) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3803 | min_key.objectid = ino; |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 3804 | min_key.type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3805 | min_key.offset = (u64)-1; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3806 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3807 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
| 3808 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3809 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3810 | return ret; |
| 3811 | } |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 3812 | ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3813 | |
| 3814 | /* if ret == 0 there are items for this type, |
| 3815 | * create a range to tell us the last key of this type. |
| 3816 | * otherwise, there are no items in this directory after |
| 3817 | * *min_offset, and we create a range to indicate that. |
| 3818 | */ |
| 3819 | if (ret == 0) { |
| 3820 | struct btrfs_key tmp; |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3821 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3822 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, |
| 3823 | path->slots[0]); |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 3824 | if (tmp.type == BTRFS_DIR_INDEX_KEY) |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3825 | last_old_dentry_offset = tmp.offset; |
Filipe Manana | 235e1c7 | 2023-01-10 14:56:41 +0000 | [diff] [blame] | 3826 | } else if (ret > 0) { |
| 3827 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3828 | } |
Filipe Manana | 6d3d970 | 2023-01-10 14:56:34 +0000 | [diff] [blame] | 3829 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3830 | goto done; |
| 3831 | } |
| 3832 | |
| 3833 | /* go backward to find any previous key */ |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 3834 | ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3835 | if (ret == 0) { |
| 3836 | struct btrfs_key tmp; |
Filipe Manana | a450a4a | 2021-12-15 12:19:58 +0000 | [diff] [blame] | 3837 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3838 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
Filipe Manana | a450a4a | 2021-12-15 12:19:58 +0000 | [diff] [blame] | 3839 | /* |
| 3840 | * The dir index key before the first one we found that needs to |
| 3841 | * be logged might be in a previous leaf, and there might be a |
| 3842 | * gap between these keys, meaning that we had deletions that |
| 3843 | * happened. So the key range item we log (key type |
| 3844 | * BTRFS_DIR_LOG_INDEX_KEY) must cover a range that starts at the |
| 3845 | * previous key's offset plus 1, so that those deletes are replayed. |
| 3846 | */ |
| 3847 | if (tmp.type == BTRFS_DIR_INDEX_KEY) |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3848 | last_old_dentry_offset = tmp.offset; |
Filipe Manana | 6d3d970 | 2023-01-10 14:56:34 +0000 | [diff] [blame] | 3849 | } else if (ret < 0) { |
Filipe Manana | 6d3d970 | 2023-01-10 14:56:34 +0000 | [diff] [blame] | 3850 | goto done; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3851 | } |
Filipe Manana | 6d3d970 | 2023-01-10 14:56:34 +0000 | [diff] [blame] | 3852 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3853 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3854 | |
Josef Bacik | 2cc8334 | 2019-03-06 17:13:04 -0500 | [diff] [blame] | 3855 | /* |
Filipe Manana | 8bb6898 | 2023-01-10 14:56:35 +0000 | [diff] [blame] | 3856 | * Find the first key from this transaction again or the one we were at |
| 3857 | * in the loop below in case we had to reschedule. We may be logging the |
| 3858 | * directory without holding its VFS lock, which happen when logging new |
| 3859 | * dentries (through log_new_dir_dentries()) or in some cases when we |
| 3860 | * need to log the parent directory of an inode. This means a dir index |
| 3861 | * key might be deleted from the inode's root, and therefore we may not |
| 3862 | * find it anymore. If we can't find it, just move to the next key. We |
| 3863 | * can not bail out and ignore, because if we do that we will simply |
| 3864 | * not log dir index keys that come after the one that was just deleted |
| 3865 | * and we can end up logging a dir index range that ends at (u64)-1 |
| 3866 | * (@last_offset is initialized to that), resulting in removing dir |
| 3867 | * entries we should not remove at log replay time. |
Josef Bacik | 2cc8334 | 2019-03-06 17:13:04 -0500 | [diff] [blame] | 3868 | */ |
Filipe Manana | bb56f02 | 2020-09-14 15:27:50 +0100 | [diff] [blame] | 3869 | search: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3870 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
Filipe Manana | 235e1c7 | 2023-01-10 14:56:41 +0000 | [diff] [blame] | 3871 | if (ret > 0) { |
Filipe Manana | 8bb6898 | 2023-01-10 14:56:35 +0000 | [diff] [blame] | 3872 | ret = btrfs_next_item(root, path); |
Filipe Manana | 235e1c7 | 2023-01-10 14:56:41 +0000 | [diff] [blame] | 3873 | if (ret > 0) { |
| 3874 | /* There are no more keys in the inode's root. */ |
| 3875 | ret = 0; |
| 3876 | goto done; |
| 3877 | } |
| 3878 | } |
Filipe Manana | 6d3d970 | 2023-01-10 14:56:34 +0000 | [diff] [blame] | 3879 | if (ret < 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3880 | goto done; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3881 | |
| 3882 | /* |
| 3883 | * we have a block from this transaction, log every item in it |
| 3884 | * from our directory |
| 3885 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3886 | while (1) { |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3887 | ret = process_dir_items_leaf(trans, inode, path, dst_path, ctx, |
| 3888 | &last_old_dentry_offset); |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3889 | if (ret != 0) { |
Filipe Manana | 235e1c7 | 2023-01-10 14:56:41 +0000 | [diff] [blame] | 3890 | if (ret > 0) |
| 3891 | ret = 0; |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3892 | goto done; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3893 | } |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3894 | path->slots[0] = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3895 | |
| 3896 | /* |
| 3897 | * look ahead to the next item and see if it is also |
| 3898 | * from this directory and from this transaction |
| 3899 | */ |
| 3900 | ret = btrfs_next_leaf(root, path); |
Liu Bo | 80c0b42 | 2018-04-03 01:59:47 +0800 | [diff] [blame] | 3901 | if (ret) { |
Filipe Manana | 235e1c7 | 2023-01-10 14:56:41 +0000 | [diff] [blame] | 3902 | if (ret == 1) { |
Liu Bo | 80c0b42 | 2018-04-03 01:59:47 +0800 | [diff] [blame] | 3903 | last_offset = (u64)-1; |
Filipe Manana | 235e1c7 | 2023-01-10 14:56:41 +0000 | [diff] [blame] | 3904 | ret = 0; |
| 3905 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3906 | goto done; |
| 3907 | } |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3908 | btrfs_item_key_to_cpu(path->nodes[0], &min_key, path->slots[0]); |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 3909 | if (min_key.objectid != ino || min_key.type != BTRFS_DIR_INDEX_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3910 | last_offset = (u64)-1; |
| 3911 | goto done; |
| 3912 | } |
| 3913 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { |
Filipe Manana | a450a4a | 2021-12-15 12:19:58 +0000 | [diff] [blame] | 3914 | /* |
| 3915 | * The next leaf was not changed in the current transaction |
| 3916 | * and has at least one dir index key. |
| 3917 | * We check for the next key because there might have been |
| 3918 | * one or more deletions between the last key we logged and |
| 3919 | * that next key. So the key range item we log (key type |
| 3920 | * BTRFS_DIR_LOG_INDEX_KEY) must end at the next key's |
| 3921 | * offset minus 1, so that those deletes are replayed. |
| 3922 | */ |
| 3923 | last_offset = min_key.offset - 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3924 | goto done; |
| 3925 | } |
Filipe Manana | eb10d85 | 2021-09-16 11:32:12 +0100 | [diff] [blame] | 3926 | if (need_resched()) { |
| 3927 | btrfs_release_path(path); |
| 3928 | cond_resched(); |
| 3929 | goto search; |
| 3930 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3931 | } |
| 3932 | done: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3933 | btrfs_release_path(path); |
| 3934 | btrfs_release_path(dst_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3935 | |
Filipe Manana | 235e1c7 | 2023-01-10 14:56:41 +0000 | [diff] [blame] | 3936 | if (ret == 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3937 | *last_offset_ret = last_offset; |
| 3938 | /* |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3939 | * In case the leaf was changed in the current transaction but |
| 3940 | * all its dir items are from a past transaction, the last item |
| 3941 | * in the leaf is a dir item and there's no gap between that last |
| 3942 | * dir item and the first one on the next leaf (which did not |
| 3943 | * change in the current transaction), then we don't need to log |
| 3944 | * a range, last_old_dentry_offset is == to last_offset. |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3945 | */ |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3946 | ASSERT(last_old_dentry_offset <= last_offset); |
Filipe Manana | 235e1c7 | 2023-01-10 14:56:41 +0000 | [diff] [blame] | 3947 | if (last_old_dentry_offset < last_offset) |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 3948 | ret = insert_dir_log_key(trans, log, path, ino, |
| 3949 | last_old_dentry_offset + 1, |
| 3950 | last_offset); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3951 | } |
Filipe Manana | 235e1c7 | 2023-01-10 14:56:41 +0000 | [diff] [blame] | 3952 | |
| 3953 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3954 | } |
| 3955 | |
| 3956 | /* |
Filipe Manana | 193df62 | 2022-08-22 11:51:39 +0100 | [diff] [blame] | 3957 | * If the inode was logged before and it was evicted, then its |
| 3958 | * last_dir_index_offset is (u64)-1, so we don't the value of the last index |
| 3959 | * key offset. If that's the case, search for it and update the inode. This |
| 3960 | * is to avoid lookups in the log tree every time we try to insert a dir index |
| 3961 | * key from a leaf changed in the current transaction, and to allow us to always |
| 3962 | * do batch insertions of dir index keys. |
| 3963 | */ |
| 3964 | static int update_last_dir_index_offset(struct btrfs_inode *inode, |
| 3965 | struct btrfs_path *path, |
| 3966 | const struct btrfs_log_ctx *ctx) |
| 3967 | { |
| 3968 | const u64 ino = btrfs_ino(inode); |
| 3969 | struct btrfs_key key; |
| 3970 | int ret; |
| 3971 | |
| 3972 | lockdep_assert_held(&inode->log_mutex); |
| 3973 | |
| 3974 | if (inode->last_dir_index_offset != (u64)-1) |
| 3975 | return 0; |
| 3976 | |
| 3977 | if (!ctx->logged_before) { |
| 3978 | inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1; |
| 3979 | return 0; |
| 3980 | } |
| 3981 | |
| 3982 | key.objectid = ino; |
| 3983 | key.type = BTRFS_DIR_INDEX_KEY; |
| 3984 | key.offset = (u64)-1; |
| 3985 | |
| 3986 | ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0); |
| 3987 | /* |
| 3988 | * An error happened or we actually have an index key with an offset |
| 3989 | * value of (u64)-1. Bail out, we're done. |
| 3990 | */ |
| 3991 | if (ret <= 0) |
| 3992 | goto out; |
| 3993 | |
| 3994 | ret = 0; |
| 3995 | inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1; |
| 3996 | |
| 3997 | /* |
| 3998 | * No dir index items, bail out and leave last_dir_index_offset with |
| 3999 | * the value right before the first valid index value. |
| 4000 | */ |
| 4001 | if (path->slots[0] == 0) |
| 4002 | goto out; |
| 4003 | |
| 4004 | /* |
| 4005 | * btrfs_search_slot() left us at one slot beyond the slot with the last |
| 4006 | * index key, or beyond the last key of the directory that is not an |
| 4007 | * index key. If we have an index key before, set last_dir_index_offset |
| 4008 | * to its offset value, otherwise leave it with a value right before the |
| 4009 | * first valid index value, as it means we have an empty directory. |
| 4010 | */ |
| 4011 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1); |
| 4012 | if (key.objectid == ino && key.type == BTRFS_DIR_INDEX_KEY) |
| 4013 | inode->last_dir_index_offset = key.offset; |
| 4014 | |
| 4015 | out: |
| 4016 | btrfs_release_path(path); |
| 4017 | |
| 4018 | return ret; |
| 4019 | } |
| 4020 | |
| 4021 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4022 | * logging directories is very similar to logging inodes, We find all the items |
| 4023 | * from the current transaction and write them to the log. |
| 4024 | * |
| 4025 | * The recovery code scans the directory in the subvolume, and if it finds a |
| 4026 | * key in the range logged that is not present in the log tree, then it means |
| 4027 | * that dir entry was unlinked during the transaction. |
| 4028 | * |
| 4029 | * In order for that scan to work, we must include one key smaller than |
| 4030 | * the smallest logged by this transaction and one key larger than the largest |
| 4031 | * key logged by this transaction. |
| 4032 | */ |
| 4033 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 4034 | struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4035 | struct btrfs_path *path, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 4036 | struct btrfs_path *dst_path, |
| 4037 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4038 | { |
| 4039 | u64 min_key; |
| 4040 | u64 max_key; |
| 4041 | int ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4042 | |
Filipe Manana | 193df62 | 2022-08-22 11:51:39 +0100 | [diff] [blame] | 4043 | ret = update_last_dir_index_offset(inode, path, ctx); |
| 4044 | if (ret) |
| 4045 | return ret; |
| 4046 | |
Filipe Manana | 732d591 | 2021-12-15 12:20:00 +0000 | [diff] [blame] | 4047 | min_key = BTRFS_DIR_START_INDEX; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4048 | max_key = 0; |
Filipe Manana | dc28722 | 2021-09-16 11:32:14 +0100 | [diff] [blame] | 4049 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4050 | while (1) { |
Filipe Manana | 339d035 | 2021-10-25 17:31:53 +0100 | [diff] [blame] | 4051 | ret = log_dir_items(trans, inode, path, dst_path, |
Nikolay Borisov | dbf39ea | 2017-01-18 00:31:42 +0200 | [diff] [blame] | 4052 | ctx, min_key, &max_key); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4053 | if (ret) |
| 4054 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4055 | if (max_key == (u64)-1) |
| 4056 | break; |
| 4057 | min_key = max_key + 1; |
| 4058 | } |
| 4059 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4060 | return 0; |
| 4061 | } |
| 4062 | |
| 4063 | /* |
| 4064 | * a helper function to drop items from the log before we relog an |
| 4065 | * inode. max_key_type indicates the highest item type to remove. |
| 4066 | * This cannot be run for file data extents because it does not |
| 4067 | * free the extents they point to. |
| 4068 | */ |
Filipe Manana | 88e221c | 2021-08-31 15:30:35 +0100 | [diff] [blame] | 4069 | static int drop_inode_items(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4070 | struct btrfs_root *log, |
| 4071 | struct btrfs_path *path, |
Filipe Manana | 88e221c | 2021-08-31 15:30:35 +0100 | [diff] [blame] | 4072 | struct btrfs_inode *inode, |
| 4073 | int max_key_type) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4074 | { |
| 4075 | int ret; |
| 4076 | struct btrfs_key key; |
| 4077 | struct btrfs_key found_key; |
Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 4078 | int start_slot; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4079 | |
Filipe Manana | 88e221c | 2021-08-31 15:30:35 +0100 | [diff] [blame] | 4080 | key.objectid = btrfs_ino(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4081 | key.type = max_key_type; |
| 4082 | key.offset = (u64)-1; |
| 4083 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4084 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4085 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 4086 | BUG_ON(ret == 0); /* Logic error */ |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4087 | if (ret < 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4088 | break; |
| 4089 | |
| 4090 | if (path->slots[0] == 0) |
| 4091 | break; |
| 4092 | |
| 4093 | path->slots[0]--; |
| 4094 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 4095 | path->slots[0]); |
| 4096 | |
Filipe Manana | 88e221c | 2021-08-31 15:30:35 +0100 | [diff] [blame] | 4097 | if (found_key.objectid != key.objectid) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4098 | break; |
| 4099 | |
Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 4100 | found_key.offset = 0; |
| 4101 | found_key.type = 0; |
Qu Wenruo | e3b8336 | 2020-04-17 15:08:21 +0800 | [diff] [blame] | 4102 | ret = btrfs_bin_search(path->nodes[0], &found_key, &start_slot); |
Filipe Manana | cbca7d5 | 2019-02-18 16:57:26 +0000 | [diff] [blame] | 4103 | if (ret < 0) |
| 4104 | break; |
Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 4105 | |
| 4106 | ret = btrfs_del_items(trans, log, path, start_slot, |
| 4107 | path->slots[0] - start_slot + 1); |
| 4108 | /* |
| 4109 | * If start slot isn't 0 then we don't need to re-search, we've |
| 4110 | * found the last guy with the objectid in this tree. |
| 4111 | */ |
| 4112 | if (ret || start_slot != 0) |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 4113 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4114 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4115 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4116 | btrfs_release_path(path); |
Josef Bacik | 5bdbeb2 | 2012-05-29 16:59:49 -0400 | [diff] [blame] | 4117 | if (ret > 0) |
| 4118 | ret = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4119 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4120 | } |
| 4121 | |
Filipe Manana | 8a2b3da | 2021-08-31 15:30:36 +0100 | [diff] [blame] | 4122 | static int truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4123 | struct btrfs_root *log_root, |
| 4124 | struct btrfs_inode *inode, |
| 4125 | u64 new_size, u32 min_type) |
| 4126 | { |
Josef Bacik | d9ac19c | 2021-12-03 17:18:09 -0500 | [diff] [blame] | 4127 | struct btrfs_truncate_control control = { |
| 4128 | .new_size = new_size, |
Josef Bacik | 487e81d | 2021-12-03 17:18:14 -0500 | [diff] [blame] | 4129 | .ino = btrfs_ino(inode), |
Josef Bacik | d9ac19c | 2021-12-03 17:18:09 -0500 | [diff] [blame] | 4130 | .min_type = min_type, |
Josef Bacik | 5caa490 | 2021-12-03 17:18:12 -0500 | [diff] [blame] | 4131 | .skip_ref_updates = true, |
Josef Bacik | d9ac19c | 2021-12-03 17:18:09 -0500 | [diff] [blame] | 4132 | }; |
Filipe Manana | 8a2b3da | 2021-08-31 15:30:36 +0100 | [diff] [blame] | 4133 | |
Josef Bacik | 8697b8f | 2021-12-03 17:18:20 -0500 | [diff] [blame] | 4134 | return btrfs_truncate_inode_items(trans, log_root, &control); |
Filipe Manana | 8a2b3da | 2021-08-31 15:30:36 +0100 | [diff] [blame] | 4135 | } |
| 4136 | |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 4137 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 4138 | struct extent_buffer *leaf, |
| 4139 | struct btrfs_inode_item *item, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4140 | struct inode *inode, int log_inode_only, |
| 4141 | u64 logged_isize) |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 4142 | { |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 4143 | struct btrfs_map_token token; |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 4144 | u64 flags; |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 4145 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 4146 | btrfs_init_map_token(&token, leaf); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 4147 | |
| 4148 | if (log_inode_only) { |
| 4149 | /* set the generation to zero so the recover code |
| 4150 | * can tell the difference between an logging |
| 4151 | * just to say 'this inode exists' and a logging |
| 4152 | * to say 'update this inode with these values' |
| 4153 | */ |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4154 | btrfs_set_token_inode_generation(&token, item, 0); |
| 4155 | btrfs_set_token_inode_size(&token, item, logged_isize); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 4156 | } else { |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4157 | btrfs_set_token_inode_generation(&token, item, |
| 4158 | BTRFS_I(inode)->generation); |
| 4159 | btrfs_set_token_inode_size(&token, item, inode->i_size); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 4160 | } |
| 4161 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4162 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
| 4163 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); |
| 4164 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); |
| 4165 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 4166 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4167 | btrfs_set_token_timespec_sec(&token, &item->atime, |
| 4168 | inode->i_atime.tv_sec); |
| 4169 | btrfs_set_token_timespec_nsec(&token, &item->atime, |
| 4170 | inode->i_atime.tv_nsec); |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 4171 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4172 | btrfs_set_token_timespec_sec(&token, &item->mtime, |
| 4173 | inode->i_mtime.tv_sec); |
| 4174 | btrfs_set_token_timespec_nsec(&token, &item->mtime, |
| 4175 | inode->i_mtime.tv_nsec); |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 4176 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4177 | btrfs_set_token_timespec_sec(&token, &item->ctime, |
| 4178 | inode->i_ctime.tv_sec); |
| 4179 | btrfs_set_token_timespec_nsec(&token, &item->ctime, |
| 4180 | inode->i_ctime.tv_nsec); |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 4181 | |
Filipe Manana | e593e54 | 2021-01-27 10:34:55 +0000 | [diff] [blame] | 4182 | /* |
| 4183 | * We do not need to set the nbytes field, in fact during a fast fsync |
| 4184 | * its value may not even be correct, since a fast fsync does not wait |
| 4185 | * for ordered extent completion, which is where we update nbytes, it |
| 4186 | * only waits for writeback to complete. During log replay as we find |
| 4187 | * file extent items and replay them, we adjust the nbytes field of the |
| 4188 | * inode item in subvolume tree as needed (see overwrite_item()). |
| 4189 | */ |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 4190 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4191 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); |
| 4192 | btrfs_set_token_inode_transid(&token, item, trans->transid); |
| 4193 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 4194 | flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags, |
| 4195 | BTRFS_I(inode)->ro_flags); |
| 4196 | btrfs_set_token_inode_flags(&token, item, flags); |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4197 | btrfs_set_token_inode_block_group(&token, item, 0); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 4198 | } |
| 4199 | |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4200 | static int log_inode_item(struct btrfs_trans_handle *trans, |
| 4201 | struct btrfs_root *log, struct btrfs_path *path, |
Filipe Manana | 2ac691d | 2021-07-20 16:03:43 +0100 | [diff] [blame] | 4202 | struct btrfs_inode *inode, bool inode_item_dropped) |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4203 | { |
| 4204 | struct btrfs_inode_item *inode_item; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4205 | int ret; |
| 4206 | |
Filipe Manana | 2ac691d | 2021-07-20 16:03:43 +0100 | [diff] [blame] | 4207 | /* |
| 4208 | * If we are doing a fast fsync and the inode was logged before in the |
| 4209 | * current transaction, then we know the inode was previously logged and |
| 4210 | * it exists in the log tree. For performance reasons, in this case use |
| 4211 | * btrfs_search_slot() directly with ins_len set to 0 so that we never |
| 4212 | * attempt a write lock on the leaf's parent, which adds unnecessary lock |
| 4213 | * contention in case there are concurrent fsyncs for other inodes of the |
| 4214 | * same subvolume. Using btrfs_insert_empty_item() when the inode item |
| 4215 | * already exists can also result in unnecessarily splitting a leaf. |
| 4216 | */ |
| 4217 | if (!inode_item_dropped && inode->logged_trans == trans->transid) { |
| 4218 | ret = btrfs_search_slot(trans, log, &inode->location, path, 0, 1); |
| 4219 | ASSERT(ret <= 0); |
| 4220 | if (ret > 0) |
| 4221 | ret = -ENOENT; |
| 4222 | } else { |
| 4223 | /* |
| 4224 | * This means it is the first fsync in the current transaction, |
| 4225 | * so the inode item is not in the log and we need to insert it. |
| 4226 | * We can never get -EEXIST because we are only called for a fast |
| 4227 | * fsync and in case an inode eviction happens after the inode was |
| 4228 | * logged before in the current transaction, when we load again |
| 4229 | * the inode, we set BTRFS_INODE_NEEDS_FULL_SYNC on its runtime |
| 4230 | * flags and set ->logged_trans to 0. |
| 4231 | */ |
| 4232 | ret = btrfs_insert_empty_item(trans, log, path, &inode->location, |
| 4233 | sizeof(*inode_item)); |
| 4234 | ASSERT(ret != -EEXIST); |
| 4235 | } |
| 4236 | if (ret) |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4237 | return ret; |
| 4238 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4239 | struct btrfs_inode_item); |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 4240 | fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode, |
| 4241 | 0, 0); |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4242 | btrfs_release_path(path); |
| 4243 | return 0; |
| 4244 | } |
| 4245 | |
Filipe Manana | 40e046a | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 4246 | static int log_csums(struct btrfs_trans_handle *trans, |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 4247 | struct btrfs_inode *inode, |
Filipe Manana | 40e046a | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 4248 | struct btrfs_root *log_root, |
| 4249 | struct btrfs_ordered_sum *sums) |
| 4250 | { |
Filipe Manana | e289f03 | 2020-05-18 12:14:50 +0100 | [diff] [blame] | 4251 | const u64 lock_end = sums->bytenr + sums->len - 1; |
| 4252 | struct extent_state *cached_state = NULL; |
Filipe Manana | 40e046a | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 4253 | int ret; |
| 4254 | |
| 4255 | /* |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 4256 | * If this inode was not used for reflink operations in the current |
| 4257 | * transaction with new extents, then do the fast path, no need to |
| 4258 | * worry about logging checksum items with overlapping ranges. |
| 4259 | */ |
| 4260 | if (inode->last_reflink_trans < trans->transid) |
| 4261 | return btrfs_csum_file_blocks(trans, log_root, sums); |
| 4262 | |
| 4263 | /* |
Filipe Manana | e289f03 | 2020-05-18 12:14:50 +0100 | [diff] [blame] | 4264 | * Serialize logging for checksums. This is to avoid racing with the |
| 4265 | * same checksum being logged by another task that is logging another |
| 4266 | * file which happens to refer to the same extent as well. Such races |
| 4267 | * can leave checksum items in the log with overlapping ranges. |
| 4268 | */ |
Josef Bacik | 570eb97 | 2022-09-09 17:53:43 -0400 | [diff] [blame] | 4269 | ret = lock_extent(&log_root->log_csum_range, sums->bytenr, lock_end, |
| 4270 | &cached_state); |
Filipe Manana | e289f03 | 2020-05-18 12:14:50 +0100 | [diff] [blame] | 4271 | if (ret) |
| 4272 | return ret; |
| 4273 | /* |
Filipe Manana | 40e046a | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 4274 | * Due to extent cloning, we might have logged a csum item that covers a |
| 4275 | * subrange of a cloned extent, and later we can end up logging a csum |
| 4276 | * item for a larger subrange of the same extent or the entire range. |
| 4277 | * This would leave csum items in the log tree that cover the same range |
| 4278 | * and break the searches for checksums in the log tree, resulting in |
| 4279 | * some checksums missing in the fs/subvolume tree. So just delete (or |
| 4280 | * trim and adjust) any existing csum items in the log for this range. |
| 4281 | */ |
| 4282 | ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len); |
Filipe Manana | e289f03 | 2020-05-18 12:14:50 +0100 | [diff] [blame] | 4283 | if (!ret) |
| 4284 | ret = btrfs_csum_file_blocks(trans, log_root, sums); |
Filipe Manana | 40e046a | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 4285 | |
Josef Bacik | 570eb97 | 2022-09-09 17:53:43 -0400 | [diff] [blame] | 4286 | unlock_extent(&log_root->log_csum_range, sums->bytenr, lock_end, |
| 4287 | &cached_state); |
Filipe Manana | e289f03 | 2020-05-18 12:14:50 +0100 | [diff] [blame] | 4288 | |
| 4289 | return ret; |
Filipe Manana | 40e046a | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4292 | static noinline int copy_items(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 4293 | struct btrfs_inode *inode, |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4294 | struct btrfs_path *dst_path, |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4295 | struct btrfs_path *src_path, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4296 | int start_slot, int nr, int inode_only, |
| 4297 | u64 logged_isize) |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4298 | { |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 4299 | struct btrfs_root *log = inode->root->log_root; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4300 | struct btrfs_file_extent_item *extent; |
Filipe Manana | 796787c | 2022-11-21 10:23:22 +0000 | [diff] [blame] | 4301 | struct extent_buffer *src; |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4302 | int ret = 0; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4303 | struct btrfs_key *ins_keys; |
| 4304 | u32 *ins_sizes; |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 4305 | struct btrfs_item_batch batch; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4306 | char *ins_data; |
| 4307 | int i; |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4308 | int dst_index; |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4309 | const bool skip_csum = (inode->flags & BTRFS_INODE_NODATASUM); |
| 4310 | const u64 i_size = i_size_read(&inode->vfs_inode); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 4311 | |
Filipe Manana | 796787c | 2022-11-21 10:23:22 +0000 | [diff] [blame] | 4312 | /* |
| 4313 | * To keep lockdep happy and avoid deadlocks, clone the source leaf and |
| 4314 | * use the clone. This is because otherwise we would be changing the log |
| 4315 | * tree, to insert items from the subvolume tree or insert csum items, |
| 4316 | * while holding a read lock on a leaf from the subvolume tree, which |
| 4317 | * creates a nasty lock dependency when COWing log tree nodes/leaves: |
| 4318 | * |
| 4319 | * 1) Modifying the log tree triggers an extent buffer allocation while |
| 4320 | * holding a write lock on a parent extent buffer from the log tree. |
| 4321 | * Allocating the pages for an extent buffer, or the extent buffer |
| 4322 | * struct, can trigger inode eviction and finally the inode eviction |
| 4323 | * will trigger a release/remove of a delayed node, which requires |
| 4324 | * taking the delayed node's mutex; |
| 4325 | * |
| 4326 | * 2) Allocating a metadata extent for a log tree can trigger the async |
| 4327 | * reclaim thread and make us wait for it to release enough space and |
| 4328 | * unblock our reservation ticket. The reclaim thread can start |
| 4329 | * flushing delayed items, and that in turn results in the need to |
| 4330 | * lock delayed node mutexes and in the need to write lock extent |
| 4331 | * buffers of a subvolume tree - all this while holding a write lock |
| 4332 | * on the parent extent buffer in the log tree. |
| 4333 | * |
| 4334 | * So one task in scenario 1) running in parallel with another task in |
| 4335 | * scenario 2) could lead to a deadlock, one wanting to lock a delayed |
| 4336 | * node mutex while having a read lock on a leaf from the subvolume, |
| 4337 | * while the other is holding the delayed node's mutex and wants to |
| 4338 | * write lock the same subvolume leaf for flushing delayed items. |
| 4339 | */ |
| 4340 | src = btrfs_clone_extent_buffer(src_path->nodes[0]); |
| 4341 | if (!src) |
| 4342 | return -ENOMEM; |
| 4343 | |
| 4344 | i = src_path->slots[0]; |
| 4345 | btrfs_release_path(src_path); |
| 4346 | src_path->nodes[0] = src; |
| 4347 | src_path->slots[0] = i; |
| 4348 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4349 | ins_data = kmalloc(nr * sizeof(struct btrfs_key) + |
| 4350 | nr * sizeof(u32), GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 4351 | if (!ins_data) |
| 4352 | return -ENOMEM; |
| 4353 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4354 | ins_sizes = (u32 *)ins_data; |
| 4355 | ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 4356 | batch.keys = ins_keys; |
| 4357 | batch.data_sizes = ins_sizes; |
| 4358 | batch.total_data_size = 0; |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4359 | batch.nr = 0; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4360 | |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4361 | dst_index = 0; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4362 | for (i = 0; i < nr; i++) { |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4363 | const int src_slot = start_slot + i; |
| 4364 | struct btrfs_root *csum_root; |
Filipe Manana | 5b7ce5e | 2022-02-17 12:12:04 +0000 | [diff] [blame] | 4365 | struct btrfs_ordered_sum *sums; |
| 4366 | struct btrfs_ordered_sum *sums_next; |
| 4367 | LIST_HEAD(ordered_sums); |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4368 | u64 disk_bytenr; |
| 4369 | u64 disk_num_bytes; |
| 4370 | u64 extent_offset; |
| 4371 | u64 extent_num_bytes; |
| 4372 | bool is_old_extent; |
| 4373 | |
| 4374 | btrfs_item_key_to_cpu(src, &ins_keys[dst_index], src_slot); |
| 4375 | |
| 4376 | if (ins_keys[dst_index].type != BTRFS_EXTENT_DATA_KEY) |
| 4377 | goto add_to_batch; |
| 4378 | |
| 4379 | extent = btrfs_item_ptr(src, src_slot, |
| 4380 | struct btrfs_file_extent_item); |
| 4381 | |
| 4382 | is_old_extent = (btrfs_file_extent_generation(src, extent) < |
| 4383 | trans->transid); |
| 4384 | |
| 4385 | /* |
| 4386 | * Don't copy extents from past generations. That would make us |
| 4387 | * log a lot more metadata for common cases like doing only a |
| 4388 | * few random writes into a file and then fsync it for the first |
| 4389 | * time or after the full sync flag is set on the inode. We can |
| 4390 | * get leaves full of extent items, most of which are from past |
| 4391 | * generations, so we can skip them - as long as the inode has |
| 4392 | * not been the target of a reflink operation in this transaction, |
| 4393 | * as in that case it might have had file extent items with old |
| 4394 | * generations copied into it. We also must always log prealloc |
| 4395 | * extents that start at or beyond eof, otherwise we would lose |
| 4396 | * them on log replay. |
| 4397 | */ |
| 4398 | if (is_old_extent && |
| 4399 | ins_keys[dst_index].offset < i_size && |
| 4400 | inode->last_reflink_trans < trans->transid) |
| 4401 | continue; |
| 4402 | |
| 4403 | if (skip_csum) |
| 4404 | goto add_to_batch; |
| 4405 | |
| 4406 | /* Only regular extents have checksums. */ |
| 4407 | if (btrfs_file_extent_type(src, extent) != BTRFS_FILE_EXTENT_REG) |
| 4408 | goto add_to_batch; |
| 4409 | |
| 4410 | /* |
| 4411 | * If it's an extent created in a past transaction, then its |
| 4412 | * checksums are already accessible from the committed csum tree, |
| 4413 | * no need to log them. |
| 4414 | */ |
| 4415 | if (is_old_extent) |
| 4416 | goto add_to_batch; |
| 4417 | |
| 4418 | disk_bytenr = btrfs_file_extent_disk_bytenr(src, extent); |
| 4419 | /* If it's an explicit hole, there are no checksums. */ |
| 4420 | if (disk_bytenr == 0) |
| 4421 | goto add_to_batch; |
| 4422 | |
| 4423 | disk_num_bytes = btrfs_file_extent_disk_num_bytes(src, extent); |
| 4424 | |
| 4425 | if (btrfs_file_extent_compression(src, extent)) { |
| 4426 | extent_offset = 0; |
| 4427 | extent_num_bytes = disk_num_bytes; |
| 4428 | } else { |
| 4429 | extent_offset = btrfs_file_extent_offset(src, extent); |
| 4430 | extent_num_bytes = btrfs_file_extent_num_bytes(src, extent); |
| 4431 | } |
| 4432 | |
| 4433 | csum_root = btrfs_csum_root(trans->fs_info, disk_bytenr); |
| 4434 | disk_bytenr += extent_offset; |
Qu Wenruo | 97e3823 | 2022-11-14 08:26:32 +0800 | [diff] [blame] | 4435 | ret = btrfs_lookup_csums_list(csum_root, disk_bytenr, |
| 4436 | disk_bytenr + extent_num_bytes - 1, |
| 4437 | &ordered_sums, 0, false); |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4438 | if (ret) |
| 4439 | goto out; |
| 4440 | |
Filipe Manana | 5b7ce5e | 2022-02-17 12:12:04 +0000 | [diff] [blame] | 4441 | list_for_each_entry_safe(sums, sums_next, &ordered_sums, list) { |
| 4442 | if (!ret) |
| 4443 | ret = log_csums(trans, inode, log, sums); |
| 4444 | list_del(&sums->list); |
| 4445 | kfree(sums); |
| 4446 | } |
| 4447 | if (ret) |
| 4448 | goto out; |
| 4449 | |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4450 | add_to_batch: |
| 4451 | ins_sizes[dst_index] = btrfs_item_size(src, src_slot); |
| 4452 | batch.total_data_size += ins_sizes[dst_index]; |
| 4453 | batch.nr++; |
| 4454 | dst_index++; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4455 | } |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4456 | |
| 4457 | /* |
| 4458 | * We have a leaf full of old extent items that don't need to be logged, |
| 4459 | * so we don't need to do anything. |
| 4460 | */ |
| 4461 | if (batch.nr == 0) |
| 4462 | goto out; |
| 4463 | |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 4464 | ret = btrfs_insert_empty_items(trans, log, dst_path, &batch); |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4465 | if (ret) |
| 4466 | goto out; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4467 | |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4468 | dst_index = 0; |
| 4469 | for (i = 0; i < nr; i++) { |
| 4470 | const int src_slot = start_slot + i; |
| 4471 | const int dst_slot = dst_path->slots[0] + dst_index; |
| 4472 | struct btrfs_key key; |
| 4473 | unsigned long src_offset; |
| 4474 | unsigned long dst_offset; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4475 | |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4476 | /* |
| 4477 | * We're done, all the remaining items in the source leaf |
| 4478 | * correspond to old file extent items. |
| 4479 | */ |
| 4480 | if (dst_index >= batch.nr) |
| 4481 | break; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4482 | |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4483 | btrfs_item_key_to_cpu(src, &key, src_slot); |
| 4484 | |
| 4485 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 4486 | goto copy_item; |
| 4487 | |
| 4488 | extent = btrfs_item_ptr(src, src_slot, |
| 4489 | struct btrfs_file_extent_item); |
| 4490 | |
| 4491 | /* See the comment in the previous loop, same logic. */ |
| 4492 | if (btrfs_file_extent_generation(src, extent) < trans->transid && |
| 4493 | key.offset < i_size && |
| 4494 | inode->last_reflink_trans < trans->transid) |
| 4495 | continue; |
| 4496 | |
| 4497 | copy_item: |
| 4498 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], dst_slot); |
| 4499 | src_offset = btrfs_item_ptr_offset(src, src_slot); |
| 4500 | |
| 4501 | if (key.type == BTRFS_INODE_ITEM_KEY) { |
| 4502 | struct btrfs_inode_item *inode_item; |
| 4503 | |
| 4504 | inode_item = btrfs_item_ptr(dst_path->nodes[0], dst_slot, |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4505 | struct btrfs_inode_item); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 4506 | fill_inode_item(trans, dst_path->nodes[0], inode_item, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4507 | &inode->vfs_inode, |
| 4508 | inode_only == LOG_INODE_EXISTS, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4509 | logged_isize); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 4510 | } else { |
| 4511 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4512 | src_offset, ins_sizes[dst_index]); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4513 | } |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 4514 | |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4515 | dst_index++; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | btrfs_mark_buffer_dirty(dst_path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4519 | btrfs_release_path(dst_path); |
Filipe Manana | 7f30c07 | 2022-02-17 12:12:03 +0000 | [diff] [blame] | 4520 | out: |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4521 | kfree(ins_data); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 4522 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4523 | return ret; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4524 | } |
| 4525 | |
Sami Tolvanen | 4f0f586 | 2021-04-08 11:28:34 -0700 | [diff] [blame] | 4526 | static int extent_cmp(void *priv, const struct list_head *a, |
| 4527 | const struct list_head *b) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4528 | { |
David Sterba | 214cc18 | 2021-07-26 14:15:26 +0200 | [diff] [blame] | 4529 | const struct extent_map *em1, *em2; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4530 | |
| 4531 | em1 = list_entry(a, struct extent_map, list); |
| 4532 | em2 = list_entry(b, struct extent_map, list); |
| 4533 | |
| 4534 | if (em1->start < em2->start) |
| 4535 | return -1; |
| 4536 | else if (em1->start > em2->start) |
| 4537 | return 1; |
| 4538 | return 0; |
| 4539 | } |
| 4540 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4541 | static int log_extent_csums(struct btrfs_trans_handle *trans, |
| 4542 | struct btrfs_inode *inode, |
Nikolay Borisov | a9ecb65 | 2018-06-20 17:26:42 +0300 | [diff] [blame] | 4543 | struct btrfs_root *log_root, |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 4544 | const struct extent_map *em, |
| 4545 | struct btrfs_log_ctx *ctx) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4546 | { |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 4547 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | fc28b25 | 2021-11-05 16:45:48 -0400 | [diff] [blame] | 4548 | struct btrfs_root *csum_root; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4549 | u64 csum_offset; |
| 4550 | u64 csum_len; |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 4551 | u64 mod_start = em->mod_start; |
| 4552 | u64 mod_len = em->mod_len; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4553 | LIST_HEAD(ordered_sums); |
| 4554 | int ret = 0; |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4555 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4556 | if (inode->flags & BTRFS_INODE_NODATASUM || |
| 4557 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4558 | em->block_start == EXTENT_MAP_HOLE) |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4559 | return 0; |
| 4560 | |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 4561 | list_for_each_entry(ordered, &ctx->ordered_extents, log_list) { |
| 4562 | const u64 ordered_end = ordered->file_offset + ordered->num_bytes; |
| 4563 | const u64 mod_end = mod_start + mod_len; |
| 4564 | struct btrfs_ordered_sum *sums; |
| 4565 | |
| 4566 | if (mod_len == 0) |
| 4567 | break; |
| 4568 | |
| 4569 | if (ordered_end <= mod_start) |
| 4570 | continue; |
| 4571 | if (mod_end <= ordered->file_offset) |
| 4572 | break; |
| 4573 | |
| 4574 | /* |
| 4575 | * We are going to copy all the csums on this ordered extent, so |
| 4576 | * go ahead and adjust mod_start and mod_len in case this ordered |
| 4577 | * extent has already been logged. |
| 4578 | */ |
| 4579 | if (ordered->file_offset > mod_start) { |
| 4580 | if (ordered_end >= mod_end) |
| 4581 | mod_len = ordered->file_offset - mod_start; |
| 4582 | /* |
| 4583 | * If we have this case |
| 4584 | * |
| 4585 | * |--------- logged extent ---------| |
| 4586 | * |----- ordered extent ----| |
| 4587 | * |
| 4588 | * Just don't mess with mod_start and mod_len, we'll |
| 4589 | * just end up logging more csums than we need and it |
| 4590 | * will be ok. |
| 4591 | */ |
| 4592 | } else { |
| 4593 | if (ordered_end < mod_end) { |
| 4594 | mod_len = mod_end - ordered_end; |
| 4595 | mod_start = ordered_end; |
| 4596 | } else { |
| 4597 | mod_len = 0; |
| 4598 | } |
| 4599 | } |
| 4600 | |
| 4601 | /* |
| 4602 | * To keep us from looping for the above case of an ordered |
| 4603 | * extent that falls inside of the logged extent. |
| 4604 | */ |
| 4605 | if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags)) |
| 4606 | continue; |
| 4607 | |
| 4608 | list_for_each_entry(sums, &ordered->list, list) { |
| 4609 | ret = log_csums(trans, inode, log_root, sums); |
| 4610 | if (ret) |
| 4611 | return ret; |
| 4612 | } |
| 4613 | } |
| 4614 | |
| 4615 | /* We're done, found all csums in the ordered extents. */ |
| 4616 | if (mod_len == 0) |
| 4617 | return 0; |
| 4618 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4619 | /* If we're compressed we have to save the entire range of csums. */ |
Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4620 | if (em->compress_type) { |
| 4621 | csum_offset = 0; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4622 | csum_len = max(em->block_len, em->orig_block_len); |
Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4623 | } else { |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 4624 | csum_offset = mod_start - em->start; |
| 4625 | csum_len = mod_len; |
Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4626 | } |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4627 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4628 | /* block start is already adjusted for the file extent offset. */ |
Josef Bacik | fc28b25 | 2021-11-05 16:45:48 -0400 | [diff] [blame] | 4629 | csum_root = btrfs_csum_root(trans->fs_info, em->block_start); |
Qu Wenruo | 97e3823 | 2022-11-14 08:26:32 +0800 | [diff] [blame] | 4630 | ret = btrfs_lookup_csums_list(csum_root, em->block_start + csum_offset, |
| 4631 | em->block_start + csum_offset + |
| 4632 | csum_len - 1, &ordered_sums, 0, false); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4633 | if (ret) |
| 4634 | return ret; |
| 4635 | |
| 4636 | while (!list_empty(&ordered_sums)) { |
| 4637 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, |
| 4638 | struct btrfs_ordered_sum, |
| 4639 | list); |
| 4640 | if (!ret) |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 4641 | ret = log_csums(trans, inode, log_root, sums); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4642 | list_del(&sums->list); |
| 4643 | kfree(sums); |
| 4644 | } |
| 4645 | |
| 4646 | return ret; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4647 | } |
| 4648 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4649 | static int log_one_extent(struct btrfs_trans_handle *trans, |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 4650 | struct btrfs_inode *inode, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4651 | const struct extent_map *em, |
| 4652 | struct btrfs_path *path, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4653 | struct btrfs_log_ctx *ctx) |
| 4654 | { |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 4655 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 4656 | struct btrfs_root *log = inode->root->log_root; |
Filipe Manana | e1f53ed | 2022-02-03 14:55:50 +0000 | [diff] [blame] | 4657 | struct btrfs_file_extent_item fi = { 0 }; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4658 | struct extent_buffer *leaf; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4659 | struct btrfs_key key; |
| 4660 | u64 extent_offset = em->start - em->orig_start; |
| 4661 | u64 block_len; |
| 4662 | int ret; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4663 | |
Filipe Manana | e1f53ed | 2022-02-03 14:55:50 +0000 | [diff] [blame] | 4664 | btrfs_set_stack_file_extent_generation(&fi, trans->transid); |
| 4665 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 4666 | btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_PREALLOC); |
| 4667 | else |
| 4668 | btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_REG); |
| 4669 | |
| 4670 | block_len = max(em->block_len, em->orig_block_len); |
| 4671 | if (em->compress_type != BTRFS_COMPRESS_NONE) { |
| 4672 | btrfs_set_stack_file_extent_disk_bytenr(&fi, em->block_start); |
| 4673 | btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len); |
| 4674 | } else if (em->block_start < EXTENT_MAP_LAST_BYTE) { |
| 4675 | btrfs_set_stack_file_extent_disk_bytenr(&fi, em->block_start - |
| 4676 | extent_offset); |
| 4677 | btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len); |
| 4678 | } |
| 4679 | |
| 4680 | btrfs_set_stack_file_extent_offset(&fi, extent_offset); |
| 4681 | btrfs_set_stack_file_extent_num_bytes(&fi, em->len); |
| 4682 | btrfs_set_stack_file_extent_ram_bytes(&fi, em->ram_bytes); |
| 4683 | btrfs_set_stack_file_extent_compression(&fi, em->compress_type); |
| 4684 | |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 4685 | ret = log_extent_csums(trans, inode, log, em, ctx); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4686 | if (ret) |
| 4687 | return ret; |
| 4688 | |
Filipe Manana | 5328b2a | 2021-08-31 15:30:39 +0100 | [diff] [blame] | 4689 | /* |
| 4690 | * If this is the first time we are logging the inode in the current |
| 4691 | * transaction, we can avoid btrfs_drop_extents(), which is expensive |
| 4692 | * because it does a deletion search, which always acquires write locks |
| 4693 | * for extent buffers at levels 2, 1 and 0. This not only wastes time |
| 4694 | * but also adds significant contention in a log tree, since log trees |
| 4695 | * are small, with a root at level 2 or 3 at most, due to their short |
| 4696 | * life span. |
| 4697 | */ |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 4698 | if (ctx->logged_before) { |
Filipe Manana | 5328b2a | 2021-08-31 15:30:39 +0100 | [diff] [blame] | 4699 | drop_args.path = path; |
| 4700 | drop_args.start = em->start; |
| 4701 | drop_args.end = em->start + em->len; |
| 4702 | drop_args.replace_extent = true; |
Filipe Manana | e1f53ed | 2022-02-03 14:55:50 +0000 | [diff] [blame] | 4703 | drop_args.extent_item_size = sizeof(fi); |
Filipe Manana | 5328b2a | 2021-08-31 15:30:39 +0100 | [diff] [blame] | 4704 | ret = btrfs_drop_extents(trans, log, inode, &drop_args); |
| 4705 | if (ret) |
| 4706 | return ret; |
| 4707 | } |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4708 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 4709 | if (!drop_args.extent_inserted) { |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4710 | key.objectid = btrfs_ino(inode); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4711 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4712 | key.offset = em->start; |
| 4713 | |
| 4714 | ret = btrfs_insert_empty_item(trans, log, path, &key, |
Filipe Manana | e1f53ed | 2022-02-03 14:55:50 +0000 | [diff] [blame] | 4715 | sizeof(fi)); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4716 | if (ret) |
| 4717 | return ret; |
| 4718 | } |
| 4719 | leaf = path->nodes[0]; |
Filipe Manana | e1f53ed | 2022-02-03 14:55:50 +0000 | [diff] [blame] | 4720 | write_extent_buffer(leaf, &fi, |
| 4721 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4722 | sizeof(fi)); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4723 | btrfs_mark_buffer_dirty(leaf); |
| 4724 | |
| 4725 | btrfs_release_path(path); |
| 4726 | |
| 4727 | return ret; |
| 4728 | } |
| 4729 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4730 | /* |
| 4731 | * Log all prealloc extents beyond the inode's i_size to make sure we do not |
Filipe Manana | d994788 | 2022-02-17 12:12:02 +0000 | [diff] [blame] | 4732 | * lose them after doing a full/fast fsync and replaying the log. We scan the |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4733 | * subvolume's root instead of iterating the inode's extent map tree because |
| 4734 | * otherwise we can log incorrect extent items based on extent map conversion. |
| 4735 | * That can happen due to the fact that extent maps are merged when they |
| 4736 | * are not in the extent map tree's list of modified extents. |
| 4737 | */ |
| 4738 | static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans, |
| 4739 | struct btrfs_inode *inode, |
| 4740 | struct btrfs_path *path) |
| 4741 | { |
| 4742 | struct btrfs_root *root = inode->root; |
| 4743 | struct btrfs_key key; |
| 4744 | const u64 i_size = i_size_read(&inode->vfs_inode); |
| 4745 | const u64 ino = btrfs_ino(inode); |
| 4746 | struct btrfs_path *dst_path = NULL; |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4747 | bool dropped_extents = false; |
Filipe Manana | f135cea | 2020-04-23 16:30:53 +0100 | [diff] [blame] | 4748 | u64 truncate_offset = i_size; |
| 4749 | struct extent_buffer *leaf; |
| 4750 | int slot; |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4751 | int ins_nr = 0; |
| 4752 | int start_slot; |
| 4753 | int ret; |
| 4754 | |
| 4755 | if (!(inode->flags & BTRFS_INODE_PREALLOC)) |
| 4756 | return 0; |
| 4757 | |
| 4758 | key.objectid = ino; |
| 4759 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4760 | key.offset = i_size; |
| 4761 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4762 | if (ret < 0) |
| 4763 | goto out; |
| 4764 | |
Filipe Manana | f135cea | 2020-04-23 16:30:53 +0100 | [diff] [blame] | 4765 | /* |
| 4766 | * We must check if there is a prealloc extent that starts before the |
| 4767 | * i_size and crosses the i_size boundary. This is to ensure later we |
| 4768 | * truncate down to the end of that extent and not to the i_size, as |
| 4769 | * otherwise we end up losing part of the prealloc extent after a log |
| 4770 | * replay and with an implicit hole if there is another prealloc extent |
| 4771 | * that starts at an offset beyond i_size. |
| 4772 | */ |
| 4773 | ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY); |
| 4774 | if (ret < 0) |
| 4775 | goto out; |
| 4776 | |
| 4777 | if (ret == 0) { |
| 4778 | struct btrfs_file_extent_item *ei; |
| 4779 | |
| 4780 | leaf = path->nodes[0]; |
| 4781 | slot = path->slots[0]; |
| 4782 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 4783 | |
| 4784 | if (btrfs_file_extent_type(leaf, ei) == |
| 4785 | BTRFS_FILE_EXTENT_PREALLOC) { |
| 4786 | u64 extent_end; |
| 4787 | |
| 4788 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 4789 | extent_end = key.offset + |
| 4790 | btrfs_file_extent_num_bytes(leaf, ei); |
| 4791 | |
| 4792 | if (extent_end > i_size) |
| 4793 | truncate_offset = extent_end; |
| 4794 | } |
| 4795 | } else { |
| 4796 | ret = 0; |
| 4797 | } |
| 4798 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4799 | while (true) { |
Filipe Manana | f135cea | 2020-04-23 16:30:53 +0100 | [diff] [blame] | 4800 | leaf = path->nodes[0]; |
| 4801 | slot = path->slots[0]; |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4802 | |
| 4803 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4804 | if (ins_nr > 0) { |
| 4805 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4806 | start_slot, ins_nr, 1, 0); |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4807 | if (ret < 0) |
| 4808 | goto out; |
| 4809 | ins_nr = 0; |
| 4810 | } |
| 4811 | ret = btrfs_next_leaf(root, path); |
| 4812 | if (ret < 0) |
| 4813 | goto out; |
| 4814 | if (ret > 0) { |
| 4815 | ret = 0; |
| 4816 | break; |
| 4817 | } |
| 4818 | continue; |
| 4819 | } |
| 4820 | |
| 4821 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 4822 | if (key.objectid > ino) |
| 4823 | break; |
| 4824 | if (WARN_ON_ONCE(key.objectid < ino) || |
| 4825 | key.type < BTRFS_EXTENT_DATA_KEY || |
| 4826 | key.offset < i_size) { |
| 4827 | path->slots[0]++; |
| 4828 | continue; |
| 4829 | } |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4830 | if (!dropped_extents) { |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4831 | /* |
| 4832 | * Avoid logging extent items logged in past fsync calls |
| 4833 | * and leading to duplicate keys in the log tree. |
| 4834 | */ |
Filipe Manana | 8a2b3da | 2021-08-31 15:30:36 +0100 | [diff] [blame] | 4835 | ret = truncate_inode_items(trans, root->log_root, inode, |
| 4836 | truncate_offset, |
| 4837 | BTRFS_EXTENT_DATA_KEY); |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4838 | if (ret) |
| 4839 | goto out; |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4840 | dropped_extents = true; |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4841 | } |
| 4842 | if (ins_nr == 0) |
| 4843 | start_slot = slot; |
| 4844 | ins_nr++; |
| 4845 | path->slots[0]++; |
| 4846 | if (!dst_path) { |
| 4847 | dst_path = btrfs_alloc_path(); |
| 4848 | if (!dst_path) { |
| 4849 | ret = -ENOMEM; |
| 4850 | goto out; |
| 4851 | } |
| 4852 | } |
| 4853 | } |
Filipe Manana | 0bc2d3c | 2020-04-21 11:25:31 +0100 | [diff] [blame] | 4854 | if (ins_nr > 0) |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4855 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4856 | start_slot, ins_nr, 1, 0); |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4857 | out: |
| 4858 | btrfs_release_path(path); |
| 4859 | btrfs_free_path(dst_path); |
| 4860 | return ret; |
| 4861 | } |
| 4862 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4863 | static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4864 | struct btrfs_inode *inode, |
Miao Xie | 827463c | 2014-01-14 20:31:51 +0800 | [diff] [blame] | 4865 | struct btrfs_path *path, |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 4866 | struct btrfs_log_ctx *ctx) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4867 | { |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 4868 | struct btrfs_ordered_extent *ordered; |
| 4869 | struct btrfs_ordered_extent *tmp; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4870 | struct extent_map *em, *n; |
| 4871 | struct list_head extents; |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4872 | struct extent_map_tree *tree = &inode->extent_tree; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4873 | int ret = 0; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4874 | int num = 0; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4875 | |
| 4876 | INIT_LIST_HEAD(&extents); |
| 4877 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4878 | write_lock(&tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4879 | |
| 4880 | list_for_each_entry_safe(em, n, &tree->modified_extents, list) { |
| 4881 | list_del_init(&em->list); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4882 | /* |
| 4883 | * Just an arbitrary number, this can be really CPU intensive |
| 4884 | * once we start getting a lot of extents, and really once we |
| 4885 | * have a bunch of extents we just want to commit since it will |
| 4886 | * be faster. |
| 4887 | */ |
| 4888 | if (++num > 32768) { |
| 4889 | list_del_init(&tree->modified_extents); |
| 4890 | ret = -EFBIG; |
| 4891 | goto process; |
| 4892 | } |
| 4893 | |
Filipe Manana | 5f96bfb | 2020-11-25 12:19:24 +0000 | [diff] [blame] | 4894 | if (em->generation < trans->transid) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4895 | continue; |
Josef Bacik | 8c6c5928 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4896 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4897 | /* We log prealloc extents beyond eof later. */ |
| 4898 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && |
| 4899 | em->start >= i_size_read(&inode->vfs_inode)) |
| 4900 | continue; |
| 4901 | |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4902 | /* Need a ref to keep it from getting evicted from cache */ |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 4903 | refcount_inc(&em->refs); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4904 | set_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4905 | list_add_tail(&em->list, &extents); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4906 | num++; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | list_sort(NULL, &extents, extent_cmp); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4910 | process: |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4911 | while (!list_empty(&extents)) { |
| 4912 | em = list_entry(extents.next, struct extent_map, list); |
| 4913 | |
| 4914 | list_del_init(&em->list); |
| 4915 | |
| 4916 | /* |
| 4917 | * If we had an error we just need to delete everybody from our |
| 4918 | * private list. |
| 4919 | */ |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4920 | if (ret) { |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 4921 | clear_em_logging(tree, em); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4922 | free_extent_map(em); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4923 | continue; |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4924 | } |
| 4925 | |
| 4926 | write_unlock(&tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4927 | |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 4928 | ret = log_one_extent(trans, inode, em, path, ctx); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4929 | write_lock(&tree->lock); |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 4930 | clear_em_logging(tree, em); |
| 4931 | free_extent_map(em); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4932 | } |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4933 | WARN_ON(!list_empty(&extents)); |
| 4934 | write_unlock(&tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4935 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4936 | if (!ret) |
| 4937 | ret = btrfs_log_prealloc_extents(trans, inode, path); |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 4938 | if (ret) |
| 4939 | return ret; |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4940 | |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 4941 | /* |
| 4942 | * We have logged all extents successfully, now make sure the commit of |
| 4943 | * the current transaction waits for the ordered extents to complete |
| 4944 | * before it commits and wipes out the log trees, otherwise we would |
| 4945 | * lose data if an ordered extents completes after the transaction |
| 4946 | * commits and a power failure happens after the transaction commit. |
| 4947 | */ |
| 4948 | list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) { |
| 4949 | list_del_init(&ordered->log_list); |
| 4950 | set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags); |
| 4951 | |
| 4952 | if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) { |
| 4953 | spin_lock_irq(&inode->ordered_tree.lock); |
| 4954 | if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) { |
| 4955 | set_bit(BTRFS_ORDERED_PENDING, &ordered->flags); |
| 4956 | atomic_inc(&trans->transaction->pending_ordered); |
| 4957 | } |
| 4958 | spin_unlock_irq(&inode->ordered_tree.lock); |
| 4959 | } |
| 4960 | btrfs_put_ordered_extent(ordered); |
| 4961 | } |
| 4962 | |
| 4963 | return 0; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4964 | } |
| 4965 | |
Nikolay Borisov | 481b01c | 2017-01-18 00:31:34 +0200 | [diff] [blame] | 4966 | static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4967 | struct btrfs_path *path, u64 *size_ret) |
| 4968 | { |
| 4969 | struct btrfs_key key; |
| 4970 | int ret; |
| 4971 | |
Nikolay Borisov | 481b01c | 2017-01-18 00:31:34 +0200 | [diff] [blame] | 4972 | key.objectid = btrfs_ino(inode); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4973 | key.type = BTRFS_INODE_ITEM_KEY; |
| 4974 | key.offset = 0; |
| 4975 | |
| 4976 | ret = btrfs_search_slot(NULL, log, &key, path, 0, 0); |
| 4977 | if (ret < 0) { |
| 4978 | return ret; |
| 4979 | } else if (ret > 0) { |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 4980 | *size_ret = 0; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4981 | } else { |
| 4982 | struct btrfs_inode_item *item; |
| 4983 | |
| 4984 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4985 | struct btrfs_inode_item); |
| 4986 | *size_ret = btrfs_inode_size(path->nodes[0], item); |
Filipe Manana | bf50411 | 2019-03-04 14:06:12 +0000 | [diff] [blame] | 4987 | /* |
| 4988 | * If the in-memory inode's i_size is smaller then the inode |
| 4989 | * size stored in the btree, return the inode's i_size, so |
| 4990 | * that we get a correct inode size after replaying the log |
| 4991 | * when before a power failure we had a shrinking truncate |
| 4992 | * followed by addition of a new name (rename / new hard link). |
| 4993 | * Otherwise return the inode size from the btree, to avoid |
| 4994 | * data loss when replaying a log due to previously doing a |
| 4995 | * write that expands the inode's size and logging a new name |
| 4996 | * immediately after. |
| 4997 | */ |
| 4998 | if (*size_ret > inode->vfs_inode.i_size) |
| 4999 | *size_ret = inode->vfs_inode.i_size; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | btrfs_release_path(path); |
| 5003 | return 0; |
| 5004 | } |
| 5005 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5006 | /* |
| 5007 | * At the moment we always log all xattrs. This is to figure out at log replay |
| 5008 | * time which xattrs must have their deletion replayed. If a xattr is missing |
| 5009 | * in the log tree and exists in the fs/subvol tree, we delete it. This is |
| 5010 | * because if a xattr is deleted, the inode is fsynced and a power failure |
| 5011 | * happens, causing the log to be replayed the next time the fs is mounted, |
| 5012 | * we want the xattr to not exist anymore (same behaviour as other filesystems |
| 5013 | * with a journal, ext3/4, xfs, f2fs, etc). |
| 5014 | */ |
| 5015 | static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 5016 | struct btrfs_inode *inode, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5017 | struct btrfs_path *path, |
| 5018 | struct btrfs_path *dst_path) |
| 5019 | { |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 5020 | struct btrfs_root *root = inode->root; |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5021 | int ret; |
| 5022 | struct btrfs_key key; |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 5023 | const u64 ino = btrfs_ino(inode); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5024 | int ins_nr = 0; |
| 5025 | int start_slot = 0; |
Filipe Manana | f2f121a | 2020-11-13 11:21:49 +0000 | [diff] [blame] | 5026 | bool found_xattrs = false; |
| 5027 | |
| 5028 | if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags)) |
| 5029 | return 0; |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5030 | |
| 5031 | key.objectid = ino; |
| 5032 | key.type = BTRFS_XATTR_ITEM_KEY; |
| 5033 | key.offset = 0; |
| 5034 | |
| 5035 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5036 | if (ret < 0) |
| 5037 | return ret; |
| 5038 | |
| 5039 | while (true) { |
| 5040 | int slot = path->slots[0]; |
| 5041 | struct extent_buffer *leaf = path->nodes[0]; |
| 5042 | int nritems = btrfs_header_nritems(leaf); |
| 5043 | |
| 5044 | if (slot >= nritems) { |
| 5045 | if (ins_nr > 0) { |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 5046 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5047 | start_slot, ins_nr, 1, 0); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5048 | if (ret < 0) |
| 5049 | return ret; |
| 5050 | ins_nr = 0; |
| 5051 | } |
| 5052 | ret = btrfs_next_leaf(root, path); |
| 5053 | if (ret < 0) |
| 5054 | return ret; |
| 5055 | else if (ret > 0) |
| 5056 | break; |
| 5057 | continue; |
| 5058 | } |
| 5059 | |
| 5060 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5061 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) |
| 5062 | break; |
| 5063 | |
| 5064 | if (ins_nr == 0) |
| 5065 | start_slot = slot; |
| 5066 | ins_nr++; |
| 5067 | path->slots[0]++; |
Filipe Manana | f2f121a | 2020-11-13 11:21:49 +0000 | [diff] [blame] | 5068 | found_xattrs = true; |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5069 | cond_resched(); |
| 5070 | } |
| 5071 | if (ins_nr > 0) { |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 5072 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5073 | start_slot, ins_nr, 1, 0); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5074 | if (ret < 0) |
| 5075 | return ret; |
| 5076 | } |
| 5077 | |
Filipe Manana | f2f121a | 2020-11-13 11:21:49 +0000 | [diff] [blame] | 5078 | if (!found_xattrs) |
| 5079 | set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags); |
| 5080 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5081 | return 0; |
| 5082 | } |
| 5083 | |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5084 | /* |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5085 | * When using the NO_HOLES feature if we punched a hole that causes the |
| 5086 | * deletion of entire leafs or all the extent items of the first leaf (the one |
| 5087 | * that contains the inode item and references) we may end up not processing |
| 5088 | * any extents, because there are no leafs with a generation matching the |
| 5089 | * current transaction that have extent items for our inode. So we need to find |
| 5090 | * if any holes exist and then log them. We also need to log holes after any |
| 5091 | * truncate operation that changes the inode's size. |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5092 | */ |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5093 | static int btrfs_log_holes(struct btrfs_trans_handle *trans, |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5094 | struct btrfs_inode *inode, |
Filipe Manana | 7af5974 | 2020-04-07 11:37:44 +0100 | [diff] [blame] | 5095 | struct btrfs_path *path) |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5096 | { |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 5097 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5098 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5099 | struct btrfs_key key; |
Nikolay Borisov | a0308dd | 2017-01-18 00:31:38 +0200 | [diff] [blame] | 5100 | const u64 ino = btrfs_ino(inode); |
| 5101 | const u64 i_size = i_size_read(&inode->vfs_inode); |
Filipe Manana | 7af5974 | 2020-04-07 11:37:44 +0100 | [diff] [blame] | 5102 | u64 prev_extent_end = 0; |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5103 | int ret; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5104 | |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5105 | if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0) |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5106 | return 0; |
| 5107 | |
| 5108 | key.objectid = ino; |
| 5109 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe Manana | 7af5974 | 2020-04-07 11:37:44 +0100 | [diff] [blame] | 5110 | key.offset = 0; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5111 | |
| 5112 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5113 | if (ret < 0) |
| 5114 | return ret; |
| 5115 | |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5116 | while (true) { |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5117 | struct extent_buffer *leaf = path->nodes[0]; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5118 | |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5119 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 5120 | ret = btrfs_next_leaf(root, path); |
| 5121 | if (ret < 0) |
| 5122 | return ret; |
| 5123 | if (ret > 0) { |
| 5124 | ret = 0; |
| 5125 | break; |
| 5126 | } |
| 5127 | leaf = path->nodes[0]; |
| 5128 | } |
| 5129 | |
| 5130 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 5131 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) |
| 5132 | break; |
| 5133 | |
| 5134 | /* We have a hole, log it. */ |
| 5135 | if (prev_extent_end < key.offset) { |
Filipe Manana | 7af5974 | 2020-04-07 11:37:44 +0100 | [diff] [blame] | 5136 | const u64 hole_len = key.offset - prev_extent_end; |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5137 | |
| 5138 | /* |
| 5139 | * Release the path to avoid deadlocks with other code |
| 5140 | * paths that search the root while holding locks on |
| 5141 | * leafs from the log root. |
| 5142 | */ |
| 5143 | btrfs_release_path(path); |
Omar Sandoval | d1f68ba | 2022-07-23 18:25:29 -0400 | [diff] [blame] | 5144 | ret = btrfs_insert_hole_extent(trans, root->log_root, |
| 5145 | ino, prev_extent_end, |
| 5146 | hole_len); |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5147 | if (ret < 0) |
| 5148 | return ret; |
| 5149 | |
| 5150 | /* |
| 5151 | * Search for the same key again in the root. Since it's |
| 5152 | * an extent item and we are holding the inode lock, the |
| 5153 | * key must still exist. If it doesn't just emit warning |
| 5154 | * and return an error to fall back to a transaction |
| 5155 | * commit. |
| 5156 | */ |
| 5157 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5158 | if (ret < 0) |
| 5159 | return ret; |
| 5160 | if (WARN_ON(ret > 0)) |
| 5161 | return -ENOENT; |
| 5162 | leaf = path->nodes[0]; |
| 5163 | } |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5164 | |
Filipe Manana | 7af5974 | 2020-04-07 11:37:44 +0100 | [diff] [blame] | 5165 | prev_extent_end = btrfs_file_extent_end(path); |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5166 | path->slots[0]++; |
| 5167 | cond_resched(); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5168 | } |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5169 | |
Filipe Manana | 7af5974 | 2020-04-07 11:37:44 +0100 | [diff] [blame] | 5170 | if (prev_extent_end < i_size) { |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5171 | u64 hole_len; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5172 | |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5173 | btrfs_release_path(path); |
Filipe Manana | 7af5974 | 2020-04-07 11:37:44 +0100 | [diff] [blame] | 5174 | hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize); |
Omar Sandoval | d1f68ba | 2022-07-23 18:25:29 -0400 | [diff] [blame] | 5175 | ret = btrfs_insert_hole_extent(trans, root->log_root, ino, |
| 5176 | prev_extent_end, hole_len); |
Filipe Manana | 0e56315 | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5177 | if (ret < 0) |
| 5178 | return ret; |
| 5179 | } |
| 5180 | |
| 5181 | return 0; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5182 | } |
| 5183 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5184 | /* |
| 5185 | * When we are logging a new inode X, check if it doesn't have a reference that |
| 5186 | * matches the reference from some other inode Y created in a past transaction |
| 5187 | * and that was renamed in the current transaction. If we don't do this, then at |
| 5188 | * log replay time we can lose inode Y (and all its files if it's a directory): |
| 5189 | * |
| 5190 | * mkdir /mnt/x |
| 5191 | * echo "hello world" > /mnt/x/foobar |
| 5192 | * sync |
| 5193 | * mv /mnt/x /mnt/y |
| 5194 | * mkdir /mnt/x # or touch /mnt/x |
| 5195 | * xfs_io -c fsync /mnt/x |
| 5196 | * <power fail> |
| 5197 | * mount fs, trigger log replay |
| 5198 | * |
| 5199 | * After the log replay procedure, we would lose the first directory and all its |
| 5200 | * files (file foobar). |
| 5201 | * For the case where inode Y is not a directory we simply end up losing it: |
| 5202 | * |
| 5203 | * echo "123" > /mnt/foo |
| 5204 | * sync |
| 5205 | * mv /mnt/foo /mnt/bar |
| 5206 | * echo "abc" > /mnt/foo |
| 5207 | * xfs_io -c fsync /mnt/foo |
| 5208 | * <power fail> |
| 5209 | * |
| 5210 | * We also need this for cases where a snapshot entry is replaced by some other |
| 5211 | * entry (file or directory) otherwise we end up with an unreplayable log due to |
| 5212 | * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as |
| 5213 | * if it were a regular entry: |
| 5214 | * |
| 5215 | * mkdir /mnt/x |
| 5216 | * btrfs subvolume snapshot /mnt /mnt/x/snap |
| 5217 | * btrfs subvolume delete /mnt/x/snap |
| 5218 | * rmdir /mnt/x |
| 5219 | * mkdir /mnt/x |
| 5220 | * fsync /mnt/x or fsync some new file inside it |
| 5221 | * <power fail> |
| 5222 | * |
| 5223 | * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in |
| 5224 | * the same transaction. |
| 5225 | */ |
| 5226 | static int btrfs_check_ref_name_override(struct extent_buffer *eb, |
| 5227 | const int slot, |
| 5228 | const struct btrfs_key *key, |
Nikolay Borisov | 4791c8f | 2017-01-18 00:31:35 +0200 | [diff] [blame] | 5229 | struct btrfs_inode *inode, |
Filipe Manana | a3baaf0 | 2019-02-13 12:14:09 +0000 | [diff] [blame] | 5230 | u64 *other_ino, u64 *other_parent) |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5231 | { |
| 5232 | int ret; |
| 5233 | struct btrfs_path *search_path; |
| 5234 | char *name = NULL; |
| 5235 | u32 name_len = 0; |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 5236 | u32 item_size = btrfs_item_size(eb, slot); |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5237 | u32 cur_offset = 0; |
| 5238 | unsigned long ptr = btrfs_item_ptr_offset(eb, slot); |
| 5239 | |
| 5240 | search_path = btrfs_alloc_path(); |
| 5241 | if (!search_path) |
| 5242 | return -ENOMEM; |
| 5243 | search_path->search_commit_root = 1; |
| 5244 | search_path->skip_locking = 1; |
| 5245 | |
| 5246 | while (cur_offset < item_size) { |
| 5247 | u64 parent; |
| 5248 | u32 this_name_len; |
| 5249 | u32 this_len; |
| 5250 | unsigned long name_ptr; |
| 5251 | struct btrfs_dir_item *di; |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 5252 | struct fscrypt_str name_str; |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5253 | |
| 5254 | if (key->type == BTRFS_INODE_REF_KEY) { |
| 5255 | struct btrfs_inode_ref *iref; |
| 5256 | |
| 5257 | iref = (struct btrfs_inode_ref *)(ptr + cur_offset); |
| 5258 | parent = key->offset; |
| 5259 | this_name_len = btrfs_inode_ref_name_len(eb, iref); |
| 5260 | name_ptr = (unsigned long)(iref + 1); |
| 5261 | this_len = sizeof(*iref) + this_name_len; |
| 5262 | } else { |
| 5263 | struct btrfs_inode_extref *extref; |
| 5264 | |
| 5265 | extref = (struct btrfs_inode_extref *)(ptr + |
| 5266 | cur_offset); |
| 5267 | parent = btrfs_inode_extref_parent(eb, extref); |
| 5268 | this_name_len = btrfs_inode_extref_name_len(eb, extref); |
| 5269 | name_ptr = (unsigned long)&extref->name; |
| 5270 | this_len = sizeof(*extref) + this_name_len; |
| 5271 | } |
| 5272 | |
| 5273 | if (this_name_len > name_len) { |
| 5274 | char *new_name; |
| 5275 | |
| 5276 | new_name = krealloc(name, this_name_len, GFP_NOFS); |
| 5277 | if (!new_name) { |
| 5278 | ret = -ENOMEM; |
| 5279 | goto out; |
| 5280 | } |
| 5281 | name_len = this_name_len; |
| 5282 | name = new_name; |
| 5283 | } |
| 5284 | |
| 5285 | read_extent_buffer(eb, name, name_ptr, this_name_len); |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 5286 | |
| 5287 | name_str.name = name; |
| 5288 | name_str.len = this_name_len; |
Nikolay Borisov | 4791c8f | 2017-01-18 00:31:35 +0200 | [diff] [blame] | 5289 | di = btrfs_lookup_dir_item(NULL, inode->root, search_path, |
Sweet Tea Dorminy | e43eec8 | 2022-10-20 12:58:25 -0400 | [diff] [blame] | 5290 | parent, &name_str, 0); |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5291 | if (di && !IS_ERR(di)) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5292 | struct btrfs_key di_key; |
| 5293 | |
| 5294 | btrfs_dir_item_key_to_cpu(search_path->nodes[0], |
| 5295 | di, &di_key); |
| 5296 | if (di_key.type == BTRFS_INODE_ITEM_KEY) { |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5297 | if (di_key.objectid != key->objectid) { |
| 5298 | ret = 1; |
| 5299 | *other_ino = di_key.objectid; |
Filipe Manana | a3baaf0 | 2019-02-13 12:14:09 +0000 | [diff] [blame] | 5300 | *other_parent = parent; |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5301 | } else { |
| 5302 | ret = 0; |
| 5303 | } |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5304 | } else { |
| 5305 | ret = -EAGAIN; |
| 5306 | } |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5307 | goto out; |
| 5308 | } else if (IS_ERR(di)) { |
| 5309 | ret = PTR_ERR(di); |
| 5310 | goto out; |
| 5311 | } |
| 5312 | btrfs_release_path(search_path); |
| 5313 | |
| 5314 | cur_offset += this_len; |
| 5315 | } |
| 5316 | ret = 0; |
| 5317 | out: |
| 5318 | btrfs_free_path(search_path); |
| 5319 | kfree(name); |
| 5320 | return ret; |
| 5321 | } |
| 5322 | |
Filipe Manana | a375102 | 2022-08-17 12:22:44 +0100 | [diff] [blame] | 5323 | /* |
| 5324 | * Check if we need to log an inode. This is used in contexts where while |
| 5325 | * logging an inode we need to log another inode (either that it exists or in |
| 5326 | * full mode). This is used instead of btrfs_inode_in_log() because the later |
| 5327 | * requires the inode to be in the log and have the log transaction committed, |
| 5328 | * while here we do not care if the log transaction was already committed - our |
| 5329 | * caller will commit the log later - and we want to avoid logging an inode |
| 5330 | * multiple times when multiple tasks have joined the same log transaction. |
| 5331 | */ |
| 5332 | static bool need_log_inode(const struct btrfs_trans_handle *trans, |
| 5333 | const struct btrfs_inode *inode) |
| 5334 | { |
| 5335 | /* |
| 5336 | * If a directory was not modified, no dentries added or removed, we can |
| 5337 | * and should avoid logging it. |
| 5338 | */ |
| 5339 | if (S_ISDIR(inode->vfs_inode.i_mode) && inode->last_trans < trans->transid) |
| 5340 | return false; |
| 5341 | |
| 5342 | /* |
| 5343 | * If this inode does not have new/updated/deleted xattrs since the last |
| 5344 | * time it was logged and is flagged as logged in the current transaction, |
| 5345 | * we can skip logging it. As for new/deleted names, those are updated in |
| 5346 | * the log by link/unlink/rename operations. |
| 5347 | * In case the inode was logged and then evicted and reloaded, its |
| 5348 | * logged_trans will be 0, in which case we have to fully log it since |
| 5349 | * logged_trans is a transient field, not persisted. |
| 5350 | */ |
| 5351 | if (inode->logged_trans == trans->transid && |
| 5352 | !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags)) |
| 5353 | return false; |
| 5354 | |
| 5355 | return true; |
| 5356 | } |
| 5357 | |
Filipe Manana | f6d86db | 2022-08-17 12:22:45 +0100 | [diff] [blame] | 5358 | struct btrfs_dir_list { |
| 5359 | u64 ino; |
| 5360 | struct list_head list; |
| 5361 | }; |
| 5362 | |
| 5363 | /* |
| 5364 | * Log the inodes of the new dentries of a directory. |
| 5365 | * See process_dir_items_leaf() for details about why it is needed. |
| 5366 | * This is a recursive operation - if an existing dentry corresponds to a |
| 5367 | * directory, that directory's new entries are logged too (same behaviour as |
| 5368 | * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes |
| 5369 | * the dentries point to we do not acquire their VFS lock, otherwise lockdep |
| 5370 | * complains about the following circular lock dependency / possible deadlock: |
| 5371 | * |
| 5372 | * CPU0 CPU1 |
| 5373 | * ---- ---- |
| 5374 | * lock(&type->i_mutex_dir_key#3/2); |
| 5375 | * lock(sb_internal#2); |
| 5376 | * lock(&type->i_mutex_dir_key#3/2); |
| 5377 | * lock(&sb->s_type->i_mutex_key#14); |
| 5378 | * |
| 5379 | * Where sb_internal is the lock (a counter that works as a lock) acquired by |
| 5380 | * sb_start_intwrite() in btrfs_start_transaction(). |
| 5381 | * Not acquiring the VFS lock of the inodes is still safe because: |
| 5382 | * |
| 5383 | * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible |
| 5384 | * that while logging the inode new references (names) are added or removed |
| 5385 | * from the inode, leaving the logged inode item with a link count that does |
| 5386 | * not match the number of logged inode reference items. This is fine because |
| 5387 | * at log replay time we compute the real number of links and correct the |
| 5388 | * link count in the inode item (see replay_one_buffer() and |
| 5389 | * link_to_fixup_dir()); |
| 5390 | * |
| 5391 | * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that |
| 5392 | * while logging the inode's items new index items (key type |
| 5393 | * BTRFS_DIR_INDEX_KEY) are added to fs/subvol tree and the logged inode item |
| 5394 | * has a size that doesn't match the sum of the lengths of all the logged |
| 5395 | * names - this is ok, not a problem, because at log replay time we set the |
| 5396 | * directory's i_size to the correct value (see replay_one_name() and |
Filipe Manana | 3a8d1db | 2022-11-21 10:23:23 +0000 | [diff] [blame] | 5397 | * overwrite_item()). |
Filipe Manana | f6d86db | 2022-08-17 12:22:45 +0100 | [diff] [blame] | 5398 | */ |
| 5399 | static int log_new_dir_dentries(struct btrfs_trans_handle *trans, |
| 5400 | struct btrfs_inode *start_inode, |
| 5401 | struct btrfs_log_ctx *ctx) |
| 5402 | { |
| 5403 | struct btrfs_root *root = start_inode->root; |
| 5404 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5405 | struct btrfs_path *path; |
| 5406 | LIST_HEAD(dir_list); |
| 5407 | struct btrfs_dir_list *dir_elem; |
| 5408 | u64 ino = btrfs_ino(start_inode); |
| 5409 | int ret = 0; |
| 5410 | |
| 5411 | /* |
| 5412 | * If we are logging a new name, as part of a link or rename operation, |
| 5413 | * don't bother logging new dentries, as we just want to log the names |
| 5414 | * of an inode and that any new parents exist. |
| 5415 | */ |
| 5416 | if (ctx->logging_new_name) |
| 5417 | return 0; |
| 5418 | |
| 5419 | path = btrfs_alloc_path(); |
| 5420 | if (!path) |
| 5421 | return -ENOMEM; |
| 5422 | |
| 5423 | while (true) { |
| 5424 | struct extent_buffer *leaf; |
| 5425 | struct btrfs_key min_key; |
| 5426 | bool continue_curr_inode = true; |
| 5427 | int nritems; |
| 5428 | int i; |
| 5429 | |
| 5430 | min_key.objectid = ino; |
| 5431 | min_key.type = BTRFS_DIR_INDEX_KEY; |
| 5432 | min_key.offset = 0; |
| 5433 | again: |
| 5434 | btrfs_release_path(path); |
| 5435 | ret = btrfs_search_forward(root, &min_key, path, trans->transid); |
| 5436 | if (ret < 0) { |
| 5437 | break; |
| 5438 | } else if (ret > 0) { |
| 5439 | ret = 0; |
| 5440 | goto next; |
| 5441 | } |
| 5442 | |
| 5443 | leaf = path->nodes[0]; |
| 5444 | nritems = btrfs_header_nritems(leaf); |
| 5445 | for (i = path->slots[0]; i < nritems; i++) { |
| 5446 | struct btrfs_dir_item *di; |
| 5447 | struct btrfs_key di_key; |
| 5448 | struct inode *di_inode; |
| 5449 | int log_mode = LOG_INODE_EXISTS; |
| 5450 | int type; |
| 5451 | |
| 5452 | btrfs_item_key_to_cpu(leaf, &min_key, i); |
| 5453 | if (min_key.objectid != ino || |
| 5454 | min_key.type != BTRFS_DIR_INDEX_KEY) { |
| 5455 | continue_curr_inode = false; |
| 5456 | break; |
| 5457 | } |
| 5458 | |
| 5459 | di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item); |
Omar Sandoval | 94a48ae | 2022-10-20 12:58:28 -0400 | [diff] [blame] | 5460 | type = btrfs_dir_ftype(leaf, di); |
Filipe Manana | f6d86db | 2022-08-17 12:22:45 +0100 | [diff] [blame] | 5461 | if (btrfs_dir_transid(leaf, di) < trans->transid) |
| 5462 | continue; |
| 5463 | btrfs_dir_item_key_to_cpu(leaf, di, &di_key); |
| 5464 | if (di_key.type == BTRFS_ROOT_ITEM_KEY) |
| 5465 | continue; |
| 5466 | |
| 5467 | btrfs_release_path(path); |
| 5468 | di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root); |
| 5469 | if (IS_ERR(di_inode)) { |
| 5470 | ret = PTR_ERR(di_inode); |
| 5471 | goto out; |
| 5472 | } |
| 5473 | |
| 5474 | if (!need_log_inode(trans, BTRFS_I(di_inode))) { |
David Sterba | e55cf7c | 2022-10-28 03:53:04 +0200 | [diff] [blame] | 5475 | btrfs_add_delayed_iput(BTRFS_I(di_inode)); |
Filipe Manana | f6d86db | 2022-08-17 12:22:45 +0100 | [diff] [blame] | 5476 | break; |
| 5477 | } |
| 5478 | |
| 5479 | ctx->log_new_dentries = false; |
| 5480 | if (type == BTRFS_FT_DIR) |
| 5481 | log_mode = LOG_INODE_ALL; |
| 5482 | ret = btrfs_log_inode(trans, BTRFS_I(di_inode), |
| 5483 | log_mode, ctx); |
David Sterba | e55cf7c | 2022-10-28 03:53:04 +0200 | [diff] [blame] | 5484 | btrfs_add_delayed_iput(BTRFS_I(di_inode)); |
Filipe Manana | f6d86db | 2022-08-17 12:22:45 +0100 | [diff] [blame] | 5485 | if (ret) |
| 5486 | goto out; |
| 5487 | if (ctx->log_new_dentries) { |
| 5488 | dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS); |
| 5489 | if (!dir_elem) { |
| 5490 | ret = -ENOMEM; |
| 5491 | goto out; |
| 5492 | } |
| 5493 | dir_elem->ino = di_key.objectid; |
| 5494 | list_add_tail(&dir_elem->list, &dir_list); |
| 5495 | } |
| 5496 | break; |
| 5497 | } |
| 5498 | |
| 5499 | if (continue_curr_inode && min_key.offset < (u64)-1) { |
| 5500 | min_key.offset++; |
| 5501 | goto again; |
| 5502 | } |
| 5503 | |
| 5504 | next: |
| 5505 | if (list_empty(&dir_list)) |
| 5506 | break; |
| 5507 | |
| 5508 | dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, list); |
| 5509 | ino = dir_elem->ino; |
| 5510 | list_del(&dir_elem->list); |
| 5511 | kfree(dir_elem); |
| 5512 | } |
| 5513 | out: |
| 5514 | btrfs_free_path(path); |
| 5515 | if (ret) { |
| 5516 | struct btrfs_dir_list *next; |
| 5517 | |
| 5518 | list_for_each_entry_safe(dir_elem, next, &dir_list, list) |
| 5519 | kfree(dir_elem); |
| 5520 | } |
| 5521 | |
| 5522 | return ret; |
| 5523 | } |
| 5524 | |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5525 | struct btrfs_ino_list { |
| 5526 | u64 ino; |
Filipe Manana | a3baaf0 | 2019-02-13 12:14:09 +0000 | [diff] [blame] | 5527 | u64 parent; |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5528 | struct list_head list; |
| 5529 | }; |
| 5530 | |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5531 | static void free_conflicting_inodes(struct btrfs_log_ctx *ctx) |
| 5532 | { |
| 5533 | struct btrfs_ino_list *curr; |
| 5534 | struct btrfs_ino_list *next; |
| 5535 | |
| 5536 | list_for_each_entry_safe(curr, next, &ctx->conflict_inodes, list) { |
| 5537 | list_del(&curr->list); |
| 5538 | kfree(curr); |
| 5539 | } |
| 5540 | } |
| 5541 | |
Filipe Manana | 5557a06 | 2022-08-17 12:22:47 +0100 | [diff] [blame] | 5542 | static int conflicting_inode_is_dir(struct btrfs_root *root, u64 ino, |
| 5543 | struct btrfs_path *path) |
| 5544 | { |
| 5545 | struct btrfs_key key; |
| 5546 | int ret; |
| 5547 | |
| 5548 | key.objectid = ino; |
| 5549 | key.type = BTRFS_INODE_ITEM_KEY; |
| 5550 | key.offset = 0; |
| 5551 | |
| 5552 | path->search_commit_root = 1; |
| 5553 | path->skip_locking = 1; |
| 5554 | |
| 5555 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5556 | if (WARN_ON_ONCE(ret > 0)) { |
| 5557 | /* |
| 5558 | * We have previously found the inode through the commit root |
| 5559 | * so this should not happen. If it does, just error out and |
| 5560 | * fallback to a transaction commit. |
| 5561 | */ |
| 5562 | ret = -ENOENT; |
| 5563 | } else if (ret == 0) { |
| 5564 | struct btrfs_inode_item *item; |
| 5565 | |
| 5566 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5567 | struct btrfs_inode_item); |
| 5568 | if (S_ISDIR(btrfs_inode_mode(path->nodes[0], item))) |
| 5569 | ret = 1; |
| 5570 | } |
| 5571 | |
| 5572 | btrfs_release_path(path); |
| 5573 | path->search_commit_root = 0; |
| 5574 | path->skip_locking = 0; |
| 5575 | |
| 5576 | return ret; |
| 5577 | } |
| 5578 | |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5579 | static int add_conflicting_inode(struct btrfs_trans_handle *trans, |
| 5580 | struct btrfs_root *root, |
Filipe Manana | 5557a06 | 2022-08-17 12:22:47 +0100 | [diff] [blame] | 5581 | struct btrfs_path *path, |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5582 | u64 ino, u64 parent, |
| 5583 | struct btrfs_log_ctx *ctx) |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5584 | { |
| 5585 | struct btrfs_ino_list *ino_elem; |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5586 | struct inode *inode; |
| 5587 | |
| 5588 | /* |
| 5589 | * It's rare to have a lot of conflicting inodes, in practice it is not |
| 5590 | * common to have more than 1 or 2. We don't want to collect too many, |
| 5591 | * as we could end up logging too many inodes (even if only in |
| 5592 | * LOG_INODE_EXISTS mode) and slow down other fsyncs or transaction |
| 5593 | * commits. |
| 5594 | */ |
Filipe Manana | 5cce178 | 2023-01-10 14:56:40 +0000 | [diff] [blame] | 5595 | if (ctx->num_conflict_inodes >= MAX_CONFLICT_INODES) |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5596 | return BTRFS_LOG_FORCE_COMMIT; |
| 5597 | |
| 5598 | inode = btrfs_iget(root->fs_info->sb, ino, root); |
| 5599 | /* |
| 5600 | * If the other inode that had a conflicting dir entry was deleted in |
Filipe Manana | 5557a06 | 2022-08-17 12:22:47 +0100 | [diff] [blame] | 5601 | * the current transaction then we either: |
| 5602 | * |
| 5603 | * 1) Log the parent directory (later after adding it to the list) if |
| 5604 | * the inode is a directory. This is because it may be a deleted |
| 5605 | * subvolume/snapshot or it may be a regular directory that had |
| 5606 | * deleted subvolumes/snapshots (or subdirectories that had them), |
| 5607 | * and at the moment we can't deal with dropping subvolumes/snapshots |
| 5608 | * during log replay. So we just log the parent, which will result in |
| 5609 | * a fallback to a transaction commit if we are dealing with those |
| 5610 | * cases (last_unlink_trans will match the current transaction); |
| 5611 | * |
| 5612 | * 2) Do nothing if it's not a directory. During log replay we simply |
| 5613 | * unlink the conflicting dentry from the parent directory and then |
| 5614 | * add the dentry for our inode. Like this we can avoid logging the |
| 5615 | * parent directory (and maybe fallback to a transaction commit in |
| 5616 | * case it has a last_unlink_trans == trans->transid, due to moving |
| 5617 | * some inode from it to some other directory). |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5618 | */ |
| 5619 | if (IS_ERR(inode)) { |
| 5620 | int ret = PTR_ERR(inode); |
| 5621 | |
| 5622 | if (ret != -ENOENT) |
| 5623 | return ret; |
| 5624 | |
Filipe Manana | 5557a06 | 2022-08-17 12:22:47 +0100 | [diff] [blame] | 5625 | ret = conflicting_inode_is_dir(root, ino, path); |
| 5626 | /* Not a directory or we got an error. */ |
| 5627 | if (ret <= 0) |
| 5628 | return ret; |
| 5629 | |
| 5630 | /* Conflicting inode is a directory, so we'll log its parent. */ |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5631 | ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS); |
| 5632 | if (!ino_elem) |
| 5633 | return -ENOMEM; |
| 5634 | ino_elem->ino = ino; |
| 5635 | ino_elem->parent = parent; |
| 5636 | list_add_tail(&ino_elem->list, &ctx->conflict_inodes); |
| 5637 | ctx->num_conflict_inodes++; |
| 5638 | |
| 5639 | return 0; |
| 5640 | } |
| 5641 | |
| 5642 | /* |
| 5643 | * If the inode was already logged skip it - otherwise we can hit an |
| 5644 | * infinite loop. Example: |
| 5645 | * |
| 5646 | * From the commit root (previous transaction) we have the following |
| 5647 | * inodes: |
| 5648 | * |
| 5649 | * inode 257 a directory |
| 5650 | * inode 258 with references "zz" and "zz_link" on inode 257 |
| 5651 | * inode 259 with reference "a" on inode 257 |
| 5652 | * |
| 5653 | * And in the current (uncommitted) transaction we have: |
| 5654 | * |
| 5655 | * inode 257 a directory, unchanged |
| 5656 | * inode 258 with references "a" and "a2" on inode 257 |
| 5657 | * inode 259 with reference "zz_link" on inode 257 |
| 5658 | * inode 261 with reference "zz" on inode 257 |
| 5659 | * |
| 5660 | * When logging inode 261 the following infinite loop could |
| 5661 | * happen if we don't skip already logged inodes: |
| 5662 | * |
| 5663 | * - we detect inode 258 as a conflicting inode, with inode 261 |
| 5664 | * on reference "zz", and log it; |
| 5665 | * |
| 5666 | * - we detect inode 259 as a conflicting inode, with inode 258 |
| 5667 | * on reference "a", and log it; |
| 5668 | * |
| 5669 | * - we detect inode 258 as a conflicting inode, with inode 259 |
| 5670 | * on reference "zz_link", and log it - again! After this we |
| 5671 | * repeat the above steps forever. |
| 5672 | * |
| 5673 | * Here we can use need_log_inode() because we only need to log the |
| 5674 | * inode in LOG_INODE_EXISTS mode and rename operations update the log, |
| 5675 | * so that the log ends up with the new name and without the old name. |
| 5676 | */ |
| 5677 | if (!need_log_inode(trans, BTRFS_I(inode))) { |
David Sterba | e55cf7c | 2022-10-28 03:53:04 +0200 | [diff] [blame] | 5678 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5679 | return 0; |
| 5680 | } |
| 5681 | |
David Sterba | e55cf7c | 2022-10-28 03:53:04 +0200 | [diff] [blame] | 5682 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5683 | |
| 5684 | ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS); |
| 5685 | if (!ino_elem) |
| 5686 | return -ENOMEM; |
| 5687 | ino_elem->ino = ino; |
Filipe Manana | a3baaf0 | 2019-02-13 12:14:09 +0000 | [diff] [blame] | 5688 | ino_elem->parent = parent; |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5689 | list_add_tail(&ino_elem->list, &ctx->conflict_inodes); |
| 5690 | ctx->num_conflict_inodes++; |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5691 | |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5692 | return 0; |
| 5693 | } |
| 5694 | |
| 5695 | static int log_conflicting_inodes(struct btrfs_trans_handle *trans, |
| 5696 | struct btrfs_root *root, |
| 5697 | struct btrfs_log_ctx *ctx) |
| 5698 | { |
| 5699 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5700 | int ret = 0; |
| 5701 | |
| 5702 | /* |
| 5703 | * Conflicting inodes are logged by the first call to btrfs_log_inode(), |
| 5704 | * otherwise we could have unbounded recursion of btrfs_log_inode() |
| 5705 | * calls. This check guarantees we can have only 1 level of recursion. |
| 5706 | */ |
| 5707 | if (ctx->logging_conflict_inodes) |
| 5708 | return 0; |
| 5709 | |
| 5710 | ctx->logging_conflict_inodes = true; |
| 5711 | |
| 5712 | /* |
| 5713 | * New conflicting inodes may be found and added to the list while we |
| 5714 | * are logging a conflicting inode, so keep iterating while the list is |
| 5715 | * not empty. |
| 5716 | */ |
| 5717 | while (!list_empty(&ctx->conflict_inodes)) { |
| 5718 | struct btrfs_ino_list *curr; |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5719 | struct inode *inode; |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5720 | u64 ino; |
| 5721 | u64 parent; |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5722 | |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5723 | curr = list_first_entry(&ctx->conflict_inodes, |
| 5724 | struct btrfs_ino_list, list); |
| 5725 | ino = curr->ino; |
| 5726 | parent = curr->parent; |
| 5727 | list_del(&curr->list); |
| 5728 | kfree(curr); |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5729 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5730 | inode = btrfs_iget(fs_info->sb, ino, root); |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5731 | /* |
| 5732 | * If the other inode that had a conflicting dir entry was |
Filipe Manana | a3baaf0 | 2019-02-13 12:14:09 +0000 | [diff] [blame] | 5733 | * deleted in the current transaction, we need to log its parent |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5734 | * directory. See the comment at add_conflicting_inode(). |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5735 | */ |
| 5736 | if (IS_ERR(inode)) { |
| 5737 | ret = PTR_ERR(inode); |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5738 | if (ret != -ENOENT) |
| 5739 | break; |
| 5740 | |
| 5741 | inode = btrfs_iget(fs_info->sb, parent, root); |
| 5742 | if (IS_ERR(inode)) { |
| 5743 | ret = PTR_ERR(inode); |
| 5744 | break; |
Filipe Manana | a3baaf0 | 2019-02-13 12:14:09 +0000 | [diff] [blame] | 5745 | } |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5746 | |
| 5747 | /* |
| 5748 | * Always log the directory, we cannot make this |
| 5749 | * conditional on need_log_inode() because the directory |
| 5750 | * might have been logged in LOG_INODE_EXISTS mode or |
| 5751 | * the dir index of the conflicting inode is not in a |
| 5752 | * dir index key range logged for the directory. So we |
| 5753 | * must make sure the deletion is recorded. |
| 5754 | */ |
| 5755 | ret = btrfs_log_inode(trans, BTRFS_I(inode), |
| 5756 | LOG_INODE_ALL, ctx); |
David Sterba | e55cf7c | 2022-10-28 03:53:04 +0200 | [diff] [blame] | 5757 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5758 | if (ret) |
| 5759 | break; |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5760 | continue; |
| 5761 | } |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5762 | |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5763 | /* |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5764 | * Here we can use need_log_inode() because we only need to log |
| 5765 | * the inode in LOG_INODE_EXISTS mode and rename operations |
| 5766 | * update the log, so that the log ends up with the new name and |
| 5767 | * without the old name. |
Filipe Manana | b5e4ff9 | 2020-01-15 13:21:35 +0000 | [diff] [blame] | 5768 | * |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5769 | * We did this check at add_conflicting_inode(), but here we do |
| 5770 | * it again because if some other task logged the inode after |
| 5771 | * that, we can avoid doing it again. |
Filipe Manana | b5e4ff9 | 2020-01-15 13:21:35 +0000 | [diff] [blame] | 5772 | */ |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5773 | if (!need_log_inode(trans, BTRFS_I(inode))) { |
David Sterba | e55cf7c | 2022-10-28 03:53:04 +0200 | [diff] [blame] | 5774 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
Filipe Manana | b5e4ff9 | 2020-01-15 13:21:35 +0000 | [diff] [blame] | 5775 | continue; |
| 5776 | } |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5777 | |
Filipe Manana | b5e4ff9 | 2020-01-15 13:21:35 +0000 | [diff] [blame] | 5778 | /* |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5779 | * We are safe logging the other inode without acquiring its |
| 5780 | * lock as long as we log with the LOG_INODE_EXISTS mode. We |
| 5781 | * are safe against concurrent renames of the other inode as |
| 5782 | * well because during a rename we pin the log and update the |
| 5783 | * log with the new name before we unpin it. |
| 5784 | */ |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5785 | ret = btrfs_log_inode(trans, BTRFS_I(inode), LOG_INODE_EXISTS, ctx); |
David Sterba | e55cf7c | 2022-10-28 03:53:04 +0200 | [diff] [blame] | 5786 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5787 | if (ret) |
| 5788 | break; |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5789 | } |
| 5790 | |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5791 | ctx->logging_conflict_inodes = false; |
| 5792 | if (ret) |
| 5793 | free_conflicting_inodes(ctx); |
| 5794 | |
Filipe Manana | 6b5fc43 | 2019-02-13 12:14:03 +0000 | [diff] [blame] | 5795 | return ret; |
| 5796 | } |
| 5797 | |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 5798 | static int copy_inode_items_to_log(struct btrfs_trans_handle *trans, |
| 5799 | struct btrfs_inode *inode, |
| 5800 | struct btrfs_key *min_key, |
| 5801 | const struct btrfs_key *max_key, |
| 5802 | struct btrfs_path *path, |
| 5803 | struct btrfs_path *dst_path, |
| 5804 | const u64 logged_isize, |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 5805 | const int inode_only, |
| 5806 | struct btrfs_log_ctx *ctx, |
| 5807 | bool *need_log_inode_item) |
| 5808 | { |
Filipe Manana | d994788 | 2022-02-17 12:12:02 +0000 | [diff] [blame] | 5809 | const u64 i_size = i_size_read(&inode->vfs_inode); |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 5810 | struct btrfs_root *root = inode->root; |
| 5811 | int ins_start_slot = 0; |
| 5812 | int ins_nr = 0; |
| 5813 | int ret; |
| 5814 | |
| 5815 | while (1) { |
| 5816 | ret = btrfs_search_forward(root, min_key, path, trans->transid); |
| 5817 | if (ret < 0) |
| 5818 | return ret; |
| 5819 | if (ret > 0) { |
| 5820 | ret = 0; |
| 5821 | break; |
| 5822 | } |
| 5823 | again: |
| 5824 | /* Note, ins_nr might be > 0 here, cleanup outside the loop */ |
| 5825 | if (min_key->objectid != max_key->objectid) |
| 5826 | break; |
| 5827 | if (min_key->type > max_key->type) |
| 5828 | break; |
| 5829 | |
Filipe Manana | d994788 | 2022-02-17 12:12:02 +0000 | [diff] [blame] | 5830 | if (min_key->type == BTRFS_INODE_ITEM_KEY) { |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 5831 | *need_log_inode_item = false; |
Filipe Manana | d994788 | 2022-02-17 12:12:02 +0000 | [diff] [blame] | 5832 | } else if (min_key->type == BTRFS_EXTENT_DATA_KEY && |
| 5833 | min_key->offset >= i_size) { |
| 5834 | /* |
| 5835 | * Extents at and beyond eof are logged with |
| 5836 | * btrfs_log_prealloc_extents(). |
| 5837 | * Only regular files have BTRFS_EXTENT_DATA_KEY keys, |
| 5838 | * and no keys greater than that, so bail out. |
| 5839 | */ |
| 5840 | break; |
| 5841 | } else if ((min_key->type == BTRFS_INODE_REF_KEY || |
| 5842 | min_key->type == BTRFS_INODE_EXTREF_KEY) && |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5843 | (inode->generation == trans->transid || |
| 5844 | ctx->logging_conflict_inodes)) { |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 5845 | u64 other_ino = 0; |
| 5846 | u64 other_parent = 0; |
| 5847 | |
| 5848 | ret = btrfs_check_ref_name_override(path->nodes[0], |
| 5849 | path->slots[0], min_key, inode, |
| 5850 | &other_ino, &other_parent); |
| 5851 | if (ret < 0) { |
| 5852 | return ret; |
Filipe Manana | 289cffc | 2021-08-31 15:30:32 +0100 | [diff] [blame] | 5853 | } else if (ret > 0 && |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 5854 | other_ino != btrfs_ino(BTRFS_I(ctx->inode))) { |
| 5855 | if (ins_nr > 0) { |
| 5856 | ins_nr++; |
| 5857 | } else { |
| 5858 | ins_nr = 1; |
| 5859 | ins_start_slot = path->slots[0]; |
| 5860 | } |
| 5861 | ret = copy_items(trans, inode, dst_path, path, |
| 5862 | ins_start_slot, ins_nr, |
| 5863 | inode_only, logged_isize); |
| 5864 | if (ret < 0) |
| 5865 | return ret; |
| 5866 | ins_nr = 0; |
| 5867 | |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5868 | btrfs_release_path(path); |
Filipe Manana | 5557a06 | 2022-08-17 12:22:47 +0100 | [diff] [blame] | 5869 | ret = add_conflicting_inode(trans, root, path, |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 5870 | other_ino, |
| 5871 | other_parent, ctx); |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 5872 | if (ret) |
| 5873 | return ret; |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 5874 | goto next_key; |
| 5875 | } |
Filipe Manana | d994788 | 2022-02-17 12:12:02 +0000 | [diff] [blame] | 5876 | } else if (min_key->type == BTRFS_XATTR_ITEM_KEY) { |
| 5877 | /* Skip xattrs, logged later with btrfs_log_all_xattrs() */ |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 5878 | if (ins_nr == 0) |
| 5879 | goto next_slot; |
| 5880 | ret = copy_items(trans, inode, dst_path, path, |
| 5881 | ins_start_slot, |
| 5882 | ins_nr, inode_only, logged_isize); |
| 5883 | if (ret < 0) |
| 5884 | return ret; |
| 5885 | ins_nr = 0; |
| 5886 | goto next_slot; |
| 5887 | } |
| 5888 | |
| 5889 | if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { |
| 5890 | ins_nr++; |
| 5891 | goto next_slot; |
| 5892 | } else if (!ins_nr) { |
| 5893 | ins_start_slot = path->slots[0]; |
| 5894 | ins_nr = 1; |
| 5895 | goto next_slot; |
| 5896 | } |
| 5897 | |
| 5898 | ret = copy_items(trans, inode, dst_path, path, ins_start_slot, |
| 5899 | ins_nr, inode_only, logged_isize); |
| 5900 | if (ret < 0) |
| 5901 | return ret; |
| 5902 | ins_nr = 1; |
| 5903 | ins_start_slot = path->slots[0]; |
| 5904 | next_slot: |
| 5905 | path->slots[0]++; |
| 5906 | if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) { |
| 5907 | btrfs_item_key_to_cpu(path->nodes[0], min_key, |
| 5908 | path->slots[0]); |
| 5909 | goto again; |
| 5910 | } |
| 5911 | if (ins_nr) { |
| 5912 | ret = copy_items(trans, inode, dst_path, path, |
| 5913 | ins_start_slot, ins_nr, inode_only, |
| 5914 | logged_isize); |
| 5915 | if (ret < 0) |
| 5916 | return ret; |
| 5917 | ins_nr = 0; |
| 5918 | } |
| 5919 | btrfs_release_path(path); |
| 5920 | next_key: |
| 5921 | if (min_key->offset < (u64)-1) { |
| 5922 | min_key->offset++; |
| 5923 | } else if (min_key->type < max_key->type) { |
| 5924 | min_key->type++; |
| 5925 | min_key->offset = 0; |
| 5926 | } else { |
| 5927 | break; |
| 5928 | } |
Filipe Manana | 96acb37 | 2022-02-17 12:12:05 +0000 | [diff] [blame] | 5929 | |
| 5930 | /* |
| 5931 | * We may process many leaves full of items for our inode, so |
| 5932 | * avoid monopolizing a cpu for too long by rescheduling while |
| 5933 | * not holding locks on any tree. |
| 5934 | */ |
| 5935 | cond_resched(); |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 5936 | } |
Filipe Manana | d994788 | 2022-02-17 12:12:02 +0000 | [diff] [blame] | 5937 | if (ins_nr) { |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 5938 | ret = copy_items(trans, inode, dst_path, path, ins_start_slot, |
| 5939 | ins_nr, inode_only, logged_isize); |
Filipe Manana | d994788 | 2022-02-17 12:12:02 +0000 | [diff] [blame] | 5940 | if (ret) |
| 5941 | return ret; |
| 5942 | } |
| 5943 | |
| 5944 | if (inode_only == LOG_INODE_ALL && S_ISREG(inode->vfs_inode.i_mode)) { |
| 5945 | /* |
| 5946 | * Release the path because otherwise we might attempt to double |
| 5947 | * lock the same leaf with btrfs_log_prealloc_extents() below. |
| 5948 | */ |
| 5949 | btrfs_release_path(path); |
| 5950 | ret = btrfs_log_prealloc_extents(trans, inode, dst_path); |
| 5951 | } |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 5952 | |
| 5953 | return ret; |
| 5954 | } |
| 5955 | |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 5956 | static int insert_delayed_items_batch(struct btrfs_trans_handle *trans, |
| 5957 | struct btrfs_root *log, |
| 5958 | struct btrfs_path *path, |
| 5959 | const struct btrfs_item_batch *batch, |
| 5960 | const struct btrfs_delayed_item *first_item) |
| 5961 | { |
| 5962 | const struct btrfs_delayed_item *curr = first_item; |
| 5963 | int ret; |
| 5964 | |
| 5965 | ret = btrfs_insert_empty_items(trans, log, path, batch); |
| 5966 | if (ret) |
| 5967 | return ret; |
| 5968 | |
| 5969 | for (int i = 0; i < batch->nr; i++) { |
| 5970 | char *data_ptr; |
| 5971 | |
| 5972 | data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char); |
| 5973 | write_extent_buffer(path->nodes[0], &curr->data, |
| 5974 | (unsigned long)data_ptr, curr->data_len); |
| 5975 | curr = list_next_entry(curr, log_list); |
| 5976 | path->slots[0]++; |
| 5977 | } |
| 5978 | |
| 5979 | btrfs_release_path(path); |
| 5980 | |
| 5981 | return 0; |
| 5982 | } |
| 5983 | |
| 5984 | static int log_delayed_insertion_items(struct btrfs_trans_handle *trans, |
| 5985 | struct btrfs_inode *inode, |
| 5986 | struct btrfs_path *path, |
| 5987 | const struct list_head *delayed_ins_list, |
| 5988 | struct btrfs_log_ctx *ctx) |
| 5989 | { |
| 5990 | /* 195 (4095 bytes of keys and sizes) fits in a single 4K page. */ |
| 5991 | const int max_batch_size = 195; |
| 5992 | const int leaf_data_size = BTRFS_LEAF_DATA_SIZE(trans->fs_info); |
| 5993 | const u64 ino = btrfs_ino(inode); |
| 5994 | struct btrfs_root *log = inode->root->log_root; |
| 5995 | struct btrfs_item_batch batch = { |
| 5996 | .nr = 0, |
| 5997 | .total_data_size = 0, |
| 5998 | }; |
| 5999 | const struct btrfs_delayed_item *first = NULL; |
| 6000 | const struct btrfs_delayed_item *curr; |
| 6001 | char *ins_data; |
| 6002 | struct btrfs_key *ins_keys; |
| 6003 | u32 *ins_sizes; |
| 6004 | u64 curr_batch_size = 0; |
| 6005 | int batch_idx = 0; |
| 6006 | int ret; |
| 6007 | |
| 6008 | /* We are adding dir index items to the log tree. */ |
| 6009 | lockdep_assert_held(&inode->log_mutex); |
| 6010 | |
| 6011 | /* |
| 6012 | * We collect delayed items before copying index keys from the subvolume |
| 6013 | * to the log tree. However just after we collected them, they may have |
| 6014 | * been flushed (all of them or just some of them), and therefore we |
| 6015 | * could have copied them from the subvolume tree to the log tree. |
| 6016 | * So find the first delayed item that was not yet logged (they are |
| 6017 | * sorted by index number). |
| 6018 | */ |
| 6019 | list_for_each_entry(curr, delayed_ins_list, log_list) { |
| 6020 | if (curr->index > inode->last_dir_index_offset) { |
| 6021 | first = curr; |
| 6022 | break; |
| 6023 | } |
| 6024 | } |
| 6025 | |
| 6026 | /* Empty list or all delayed items were already logged. */ |
| 6027 | if (!first) |
| 6028 | return 0; |
| 6029 | |
| 6030 | ins_data = kmalloc(max_batch_size * sizeof(u32) + |
| 6031 | max_batch_size * sizeof(struct btrfs_key), GFP_NOFS); |
| 6032 | if (!ins_data) |
| 6033 | return -ENOMEM; |
| 6034 | ins_sizes = (u32 *)ins_data; |
| 6035 | batch.data_sizes = ins_sizes; |
| 6036 | ins_keys = (struct btrfs_key *)(ins_data + max_batch_size * sizeof(u32)); |
| 6037 | batch.keys = ins_keys; |
| 6038 | |
| 6039 | curr = first; |
| 6040 | while (!list_entry_is_head(curr, delayed_ins_list, log_list)) { |
| 6041 | const u32 curr_size = curr->data_len + sizeof(struct btrfs_item); |
| 6042 | |
| 6043 | if (curr_batch_size + curr_size > leaf_data_size || |
| 6044 | batch.nr == max_batch_size) { |
| 6045 | ret = insert_delayed_items_batch(trans, log, path, |
| 6046 | &batch, first); |
| 6047 | if (ret) |
| 6048 | goto out; |
| 6049 | batch_idx = 0; |
| 6050 | batch.nr = 0; |
| 6051 | batch.total_data_size = 0; |
| 6052 | curr_batch_size = 0; |
| 6053 | first = curr; |
| 6054 | } |
| 6055 | |
| 6056 | ins_sizes[batch_idx] = curr->data_len; |
| 6057 | ins_keys[batch_idx].objectid = ino; |
| 6058 | ins_keys[batch_idx].type = BTRFS_DIR_INDEX_KEY; |
| 6059 | ins_keys[batch_idx].offset = curr->index; |
| 6060 | curr_batch_size += curr_size; |
| 6061 | batch.total_data_size += curr->data_len; |
| 6062 | batch.nr++; |
| 6063 | batch_idx++; |
| 6064 | curr = list_next_entry(curr, log_list); |
| 6065 | } |
| 6066 | |
| 6067 | ASSERT(batch.nr >= 1); |
| 6068 | ret = insert_delayed_items_batch(trans, log, path, &batch, first); |
| 6069 | |
| 6070 | curr = list_last_entry(delayed_ins_list, struct btrfs_delayed_item, |
| 6071 | log_list); |
| 6072 | inode->last_dir_index_offset = curr->index; |
| 6073 | out: |
| 6074 | kfree(ins_data); |
| 6075 | |
| 6076 | return ret; |
| 6077 | } |
| 6078 | |
| 6079 | static int log_delayed_deletions_full(struct btrfs_trans_handle *trans, |
| 6080 | struct btrfs_inode *inode, |
| 6081 | struct btrfs_path *path, |
| 6082 | const struct list_head *delayed_del_list, |
| 6083 | struct btrfs_log_ctx *ctx) |
| 6084 | { |
| 6085 | const u64 ino = btrfs_ino(inode); |
| 6086 | const struct btrfs_delayed_item *curr; |
| 6087 | |
| 6088 | curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item, |
| 6089 | log_list); |
| 6090 | |
| 6091 | while (!list_entry_is_head(curr, delayed_del_list, log_list)) { |
| 6092 | u64 first_dir_index = curr->index; |
| 6093 | u64 last_dir_index; |
| 6094 | const struct btrfs_delayed_item *next; |
| 6095 | int ret; |
| 6096 | |
| 6097 | /* |
| 6098 | * Find a range of consecutive dir index items to delete. Like |
| 6099 | * this we log a single dir range item spanning several contiguous |
| 6100 | * dir items instead of logging one range item per dir index item. |
| 6101 | */ |
| 6102 | next = list_next_entry(curr, log_list); |
| 6103 | while (!list_entry_is_head(next, delayed_del_list, log_list)) { |
| 6104 | if (next->index != curr->index + 1) |
| 6105 | break; |
| 6106 | curr = next; |
| 6107 | next = list_next_entry(next, log_list); |
| 6108 | } |
| 6109 | |
| 6110 | last_dir_index = curr->index; |
| 6111 | ASSERT(last_dir_index >= first_dir_index); |
| 6112 | |
| 6113 | ret = insert_dir_log_key(trans, inode->root->log_root, path, |
| 6114 | ino, first_dir_index, last_dir_index); |
| 6115 | if (ret) |
| 6116 | return ret; |
| 6117 | curr = list_next_entry(curr, log_list); |
| 6118 | } |
| 6119 | |
| 6120 | return 0; |
| 6121 | } |
| 6122 | |
| 6123 | static int batch_delete_dir_index_items(struct btrfs_trans_handle *trans, |
| 6124 | struct btrfs_inode *inode, |
| 6125 | struct btrfs_path *path, |
| 6126 | struct btrfs_log_ctx *ctx, |
| 6127 | const struct list_head *delayed_del_list, |
| 6128 | const struct btrfs_delayed_item *first, |
| 6129 | const struct btrfs_delayed_item **last_ret) |
| 6130 | { |
| 6131 | const struct btrfs_delayed_item *next; |
| 6132 | struct extent_buffer *leaf = path->nodes[0]; |
| 6133 | const int last_slot = btrfs_header_nritems(leaf) - 1; |
| 6134 | int slot = path->slots[0] + 1; |
| 6135 | const u64 ino = btrfs_ino(inode); |
| 6136 | |
| 6137 | next = list_next_entry(first, log_list); |
| 6138 | |
| 6139 | while (slot < last_slot && |
| 6140 | !list_entry_is_head(next, delayed_del_list, log_list)) { |
| 6141 | struct btrfs_key key; |
| 6142 | |
| 6143 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 6144 | if (key.objectid != ino || |
| 6145 | key.type != BTRFS_DIR_INDEX_KEY || |
| 6146 | key.offset != next->index) |
| 6147 | break; |
| 6148 | |
| 6149 | slot++; |
| 6150 | *last_ret = next; |
| 6151 | next = list_next_entry(next, log_list); |
| 6152 | } |
| 6153 | |
| 6154 | return btrfs_del_items(trans, inode->root->log_root, path, |
| 6155 | path->slots[0], slot - path->slots[0]); |
| 6156 | } |
| 6157 | |
| 6158 | static int log_delayed_deletions_incremental(struct btrfs_trans_handle *trans, |
| 6159 | struct btrfs_inode *inode, |
| 6160 | struct btrfs_path *path, |
| 6161 | const struct list_head *delayed_del_list, |
| 6162 | struct btrfs_log_ctx *ctx) |
| 6163 | { |
| 6164 | struct btrfs_root *log = inode->root->log_root; |
| 6165 | const struct btrfs_delayed_item *curr; |
| 6166 | u64 last_range_start; |
| 6167 | u64 last_range_end = 0; |
| 6168 | struct btrfs_key key; |
| 6169 | |
| 6170 | key.objectid = btrfs_ino(inode); |
| 6171 | key.type = BTRFS_DIR_INDEX_KEY; |
| 6172 | curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item, |
| 6173 | log_list); |
| 6174 | |
| 6175 | while (!list_entry_is_head(curr, delayed_del_list, log_list)) { |
| 6176 | const struct btrfs_delayed_item *last = curr; |
| 6177 | u64 first_dir_index = curr->index; |
| 6178 | u64 last_dir_index; |
| 6179 | bool deleted_items = false; |
| 6180 | int ret; |
| 6181 | |
| 6182 | key.offset = curr->index; |
| 6183 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); |
| 6184 | if (ret < 0) { |
| 6185 | return ret; |
| 6186 | } else if (ret == 0) { |
| 6187 | ret = batch_delete_dir_index_items(trans, inode, path, ctx, |
| 6188 | delayed_del_list, curr, |
| 6189 | &last); |
| 6190 | if (ret) |
| 6191 | return ret; |
| 6192 | deleted_items = true; |
| 6193 | } |
| 6194 | |
| 6195 | btrfs_release_path(path); |
| 6196 | |
| 6197 | /* |
| 6198 | * If we deleted items from the leaf, it means we have a range |
| 6199 | * item logging their range, so no need to add one or update an |
| 6200 | * existing one. Otherwise we have to log a dir range item. |
| 6201 | */ |
| 6202 | if (deleted_items) |
| 6203 | goto next_batch; |
| 6204 | |
| 6205 | last_dir_index = last->index; |
| 6206 | ASSERT(last_dir_index >= first_dir_index); |
| 6207 | /* |
| 6208 | * If this range starts right after where the previous one ends, |
| 6209 | * then we want to reuse the previous range item and change its |
| 6210 | * end offset to the end of this range. This is just to minimize |
| 6211 | * leaf space usage, by avoiding adding a new range item. |
| 6212 | */ |
| 6213 | if (last_range_end != 0 && first_dir_index == last_range_end + 1) |
| 6214 | first_dir_index = last_range_start; |
| 6215 | |
| 6216 | ret = insert_dir_log_key(trans, log, path, key.objectid, |
| 6217 | first_dir_index, last_dir_index); |
| 6218 | if (ret) |
| 6219 | return ret; |
| 6220 | |
| 6221 | last_range_start = first_dir_index; |
| 6222 | last_range_end = last_dir_index; |
| 6223 | next_batch: |
| 6224 | curr = list_next_entry(last, log_list); |
| 6225 | } |
| 6226 | |
| 6227 | return 0; |
| 6228 | } |
| 6229 | |
| 6230 | static int log_delayed_deletion_items(struct btrfs_trans_handle *trans, |
| 6231 | struct btrfs_inode *inode, |
| 6232 | struct btrfs_path *path, |
| 6233 | const struct list_head *delayed_del_list, |
| 6234 | struct btrfs_log_ctx *ctx) |
| 6235 | { |
| 6236 | /* |
| 6237 | * We are deleting dir index items from the log tree or adding range |
| 6238 | * items to it. |
| 6239 | */ |
| 6240 | lockdep_assert_held(&inode->log_mutex); |
| 6241 | |
| 6242 | if (list_empty(delayed_del_list)) |
| 6243 | return 0; |
| 6244 | |
| 6245 | if (ctx->logged_before) |
| 6246 | return log_delayed_deletions_incremental(trans, inode, path, |
| 6247 | delayed_del_list, ctx); |
| 6248 | |
| 6249 | return log_delayed_deletions_full(trans, inode, path, delayed_del_list, |
| 6250 | ctx); |
| 6251 | } |
| 6252 | |
| 6253 | /* |
| 6254 | * Similar logic as for log_new_dir_dentries(), but it iterates over the delayed |
| 6255 | * items instead of the subvolume tree. |
| 6256 | */ |
| 6257 | static int log_new_delayed_dentries(struct btrfs_trans_handle *trans, |
| 6258 | struct btrfs_inode *inode, |
| 6259 | const struct list_head *delayed_ins_list, |
| 6260 | struct btrfs_log_ctx *ctx) |
| 6261 | { |
| 6262 | const bool orig_log_new_dentries = ctx->log_new_dentries; |
| 6263 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 6264 | struct btrfs_delayed_item *item; |
| 6265 | int ret = 0; |
| 6266 | |
| 6267 | /* |
| 6268 | * No need for the log mutex, plus to avoid potential deadlocks or |
| 6269 | * lockdep annotations due to nesting of delayed inode mutexes and log |
| 6270 | * mutexes. |
| 6271 | */ |
| 6272 | lockdep_assert_not_held(&inode->log_mutex); |
| 6273 | |
| 6274 | ASSERT(!ctx->logging_new_delayed_dentries); |
| 6275 | ctx->logging_new_delayed_dentries = true; |
| 6276 | |
| 6277 | list_for_each_entry(item, delayed_ins_list, log_list) { |
| 6278 | struct btrfs_dir_item *dir_item; |
| 6279 | struct inode *di_inode; |
| 6280 | struct btrfs_key key; |
| 6281 | int log_mode = LOG_INODE_EXISTS; |
| 6282 | |
| 6283 | dir_item = (struct btrfs_dir_item *)item->data; |
| 6284 | btrfs_disk_key_to_cpu(&key, &dir_item->location); |
| 6285 | |
| 6286 | if (key.type == BTRFS_ROOT_ITEM_KEY) |
| 6287 | continue; |
| 6288 | |
| 6289 | di_inode = btrfs_iget(fs_info->sb, key.objectid, inode->root); |
| 6290 | if (IS_ERR(di_inode)) { |
| 6291 | ret = PTR_ERR(di_inode); |
| 6292 | break; |
| 6293 | } |
| 6294 | |
| 6295 | if (!need_log_inode(trans, BTRFS_I(di_inode))) { |
David Sterba | e55cf7c | 2022-10-28 03:53:04 +0200 | [diff] [blame] | 6296 | btrfs_add_delayed_iput(BTRFS_I(di_inode)); |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 6297 | continue; |
| 6298 | } |
| 6299 | |
Omar Sandoval | 94a48ae | 2022-10-20 12:58:28 -0400 | [diff] [blame] | 6300 | if (btrfs_stack_dir_ftype(dir_item) == BTRFS_FT_DIR) |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 6301 | log_mode = LOG_INODE_ALL; |
| 6302 | |
| 6303 | ctx->log_new_dentries = false; |
| 6304 | ret = btrfs_log_inode(trans, BTRFS_I(di_inode), log_mode, ctx); |
| 6305 | |
| 6306 | if (!ret && ctx->log_new_dentries) |
| 6307 | ret = log_new_dir_dentries(trans, BTRFS_I(di_inode), ctx); |
| 6308 | |
David Sterba | e55cf7c | 2022-10-28 03:53:04 +0200 | [diff] [blame] | 6309 | btrfs_add_delayed_iput(BTRFS_I(di_inode)); |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 6310 | |
| 6311 | if (ret) |
| 6312 | break; |
| 6313 | } |
| 6314 | |
| 6315 | ctx->log_new_dentries = orig_log_new_dentries; |
| 6316 | ctx->logging_new_delayed_dentries = false; |
| 6317 | |
| 6318 | return ret; |
| 6319 | } |
| 6320 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6321 | /* log a single inode in the tree log. |
| 6322 | * At least one parent directory for this inode must exist in the tree |
| 6323 | * or be logged already. |
| 6324 | * |
| 6325 | * Any items from this inode changed by the current transaction are copied |
| 6326 | * to the log tree. An extra reference is taken on any extents in this |
| 6327 | * file, allowing us to avoid a whole pile of corner cases around logging |
| 6328 | * blocks that have been removed from the tree. |
| 6329 | * |
| 6330 | * See LOG_INODE_ALL and related defines for a description of what inode_only |
| 6331 | * does. |
| 6332 | * |
| 6333 | * This handles both files and directories. |
| 6334 | */ |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6335 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 6336 | struct btrfs_inode *inode, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 6337 | int inode_only, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 6338 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6339 | { |
| 6340 | struct btrfs_path *path; |
| 6341 | struct btrfs_path *dst_path; |
| 6342 | struct btrfs_key min_key; |
| 6343 | struct btrfs_key max_key; |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 6344 | struct btrfs_root *log = inode->root->log_root; |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6345 | int ret; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6346 | bool fast_search = false; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 6347 | u64 ino = btrfs_ino(inode); |
| 6348 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 6349 | u64 logged_isize = 0; |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 6350 | bool need_log_inode_item = true; |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 6351 | bool xattrs_logged = false; |
Filipe Manana | 2ac691d | 2021-07-20 16:03:43 +0100 | [diff] [blame] | 6352 | bool inode_item_dropped = true; |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 6353 | bool full_dir_logging = false; |
| 6354 | LIST_HEAD(delayed_ins_list); |
| 6355 | LIST_HEAD(delayed_del_list); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6356 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6357 | path = btrfs_alloc_path(); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 6358 | if (!path) |
| 6359 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6360 | dst_path = btrfs_alloc_path(); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 6361 | if (!dst_path) { |
| 6362 | btrfs_free_path(path); |
| 6363 | return -ENOMEM; |
| 6364 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6365 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6366 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6367 | min_key.type = BTRFS_INODE_ITEM_KEY; |
| 6368 | min_key.offset = 0; |
| 6369 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6370 | max_key.objectid = ino; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6371 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6372 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6373 | /* today the code can only do partial logging of directories */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 6374 | if (S_ISDIR(inode->vfs_inode.i_mode) || |
Miao Xie | 5269b67 | 2012-11-01 07:35:23 +0000 | [diff] [blame] | 6375 | (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 6376 | &inode->runtime_flags) && |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 6377 | inode_only >= LOG_INODE_EXISTS)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6378 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
| 6379 | else |
| 6380 | max_key.type = (u8)-1; |
| 6381 | max_key.offset = (u64)-1; |
| 6382 | |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 6383 | if (S_ISDIR(inode->vfs_inode.i_mode) && inode_only == LOG_INODE_ALL) |
| 6384 | full_dir_logging = true; |
| 6385 | |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 6386 | /* |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 6387 | * If we are logging a directory while we are logging dentries of the |
| 6388 | * delayed items of some other inode, then we need to flush the delayed |
| 6389 | * items of this directory and not log the delayed items directly. This |
| 6390 | * is to prevent more than one level of recursion into btrfs_log_inode() |
| 6391 | * by having something like this: |
| 6392 | * |
| 6393 | * $ mkdir -p a/b/c/d/e/f/g/h/... |
| 6394 | * $ xfs_io -c "fsync" a |
| 6395 | * |
| 6396 | * Where all directories in the path did not exist before and are |
| 6397 | * created in the current transaction. |
| 6398 | * So in such a case we directly log the delayed items of the main |
| 6399 | * directory ("a") without flushing them first, while for each of its |
| 6400 | * subdirectories we flush their delayed items before logging them. |
| 6401 | * This prevents a potential unbounded recursion like this: |
| 6402 | * |
| 6403 | * btrfs_log_inode() |
| 6404 | * log_new_delayed_dentries() |
| 6405 | * btrfs_log_inode() |
| 6406 | * log_new_delayed_dentries() |
| 6407 | * btrfs_log_inode() |
| 6408 | * log_new_delayed_dentries() |
| 6409 | * (...) |
| 6410 | * |
| 6411 | * We have thresholds for the maximum number of delayed items to have in |
| 6412 | * memory, and once they are hit, the items are flushed asynchronously. |
| 6413 | * However the limit is quite high, so lets prevent deep levels of |
| 6414 | * recursion to happen by limiting the maximum depth to be 1. |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 6415 | */ |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 6416 | if (full_dir_logging && ctx->logging_new_delayed_dentries) { |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6417 | ret = btrfs_commit_inode_delayed_items(trans, inode); |
| 6418 | if (ret) |
Filipe Manana | f6df27d | 2021-08-31 15:30:40 +0100 | [diff] [blame] | 6419 | goto out; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6420 | } |
| 6421 | |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 6422 | mutex_lock(&inode->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6423 | |
Filipe Manana | 5e33a2b | 2016-02-25 23:19:38 +0000 | [diff] [blame] | 6424 | /* |
Filipe Manana | d0e64a9 | 2022-04-21 10:56:39 +0100 | [diff] [blame] | 6425 | * For symlinks, we must always log their content, which is stored in an |
| 6426 | * inline extent, otherwise we could end up with an empty symlink after |
| 6427 | * log replay, which is invalid on linux (symlink(2) returns -ENOENT if |
| 6428 | * one attempts to create an empty symlink). |
| 6429 | * We don't need to worry about flushing delalloc, because when we create |
| 6430 | * the inline extent when the symlink is created (we never have delalloc |
| 6431 | * for symlinks). |
| 6432 | */ |
| 6433 | if (S_ISLNK(inode->vfs_inode.i_mode)) |
| 6434 | inode_only = LOG_INODE_ALL; |
| 6435 | |
| 6436 | /* |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 6437 | * Before logging the inode item, cache the value returned by |
| 6438 | * inode_logged(), because after that we have the need to figure out if |
| 6439 | * the inode was previously logged in this transaction. |
| 6440 | */ |
| 6441 | ret = inode_logged(trans, inode, path); |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6442 | if (ret < 0) |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 6443 | goto out_unlock; |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 6444 | ctx->logged_before = (ret == 1); |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6445 | ret = 0; |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 6446 | |
| 6447 | /* |
Filipe Manana | 64d6b28 | 2021-01-27 10:34:59 +0000 | [diff] [blame] | 6448 | * This is for cases where logging a directory could result in losing a |
| 6449 | * a file after replaying the log. For example, if we move a file from a |
| 6450 | * directory A to a directory B, then fsync directory A, we have no way |
| 6451 | * to known the file was moved from A to B, so logging just A would |
| 6452 | * result in losing the file after a log replay. |
| 6453 | */ |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 6454 | if (full_dir_logging && inode->last_unlink_trans >= trans->transid) { |
Josef Bacik | f31f09f | 2022-06-13 15:09:48 -0400 | [diff] [blame] | 6455 | ret = BTRFS_LOG_FORCE_COMMIT; |
Filipe Manana | 64d6b28 | 2021-01-27 10:34:59 +0000 | [diff] [blame] | 6456 | goto out_unlock; |
| 6457 | } |
| 6458 | |
| 6459 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6460 | * a brute force approach to making sure we get the most uptodate |
| 6461 | * copies of everything. |
| 6462 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 6463 | if (S_ISDIR(inode->vfs_inode.i_mode)) { |
Filipe Manana | ab12313 | 2021-01-27 10:34:56 +0000 | [diff] [blame] | 6464 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags); |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 6465 | if (ctx->logged_before) |
| 6466 | ret = drop_inode_items(trans, log, path, inode, |
Filipe Manana | 04fc7d51 | 2022-08-17 12:22:34 +0100 | [diff] [blame] | 6467 | BTRFS_XATTR_ITEM_KEY); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6468 | } else { |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 6469 | if (inode_only == LOG_INODE_EXISTS && ctx->logged_before) { |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 6470 | /* |
| 6471 | * Make sure the new inode item we write to the log has |
| 6472 | * the same isize as the current one (if it exists). |
| 6473 | * This is necessary to prevent data loss after log |
| 6474 | * replay, and also to prevent doing a wrong expanding |
| 6475 | * truncate - for e.g. create file, write 4K into offset |
| 6476 | * 0, fsync, write 4K into offset 4096, add hard link, |
| 6477 | * fsync some other file (to sync log), power fail - if |
| 6478 | * we use the inode's current i_size, after log replay |
| 6479 | * we get a 8Kb file, with the last 4Kb extent as a hole |
| 6480 | * (zeroes), as if an expanding truncate happened, |
| 6481 | * instead of getting a file of 4Kb only. |
| 6482 | */ |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6483 | ret = logged_inode_size(log, inode, path, &logged_isize); |
| 6484 | if (ret) |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 6485 | goto out_unlock; |
| 6486 | } |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 6487 | if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 6488 | &inode->runtime_flags)) { |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 6489 | if (inode_only == LOG_INODE_EXISTS) { |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 6490 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 6491 | if (ctx->logged_before) |
| 6492 | ret = drop_inode_items(trans, log, path, |
| 6493 | inode, max_key.type); |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 6494 | } else { |
| 6495 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 6496 | &inode->runtime_flags); |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 6497 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 6498 | &inode->runtime_flags); |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 6499 | if (ctx->logged_before) |
Filipe Manana | 4934a81 | 2021-08-31 15:30:37 +0100 | [diff] [blame] | 6500 | ret = truncate_inode_items(trans, log, |
| 6501 | inode, 0, 0); |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 6502 | } |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 6503 | } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 6504 | &inode->runtime_flags) || |
Josef Bacik | 6cfab85 | 2013-11-12 16:25:58 -0500 | [diff] [blame] | 6505 | inode_only == LOG_INODE_EXISTS) { |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 6506 | if (inode_only == LOG_INODE_ALL) |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 6507 | fast_search = true; |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 6508 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 6509 | if (ctx->logged_before) |
| 6510 | ret = drop_inode_items(trans, log, path, inode, |
| 6511 | max_key.type); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6512 | } else { |
Liu Bo | 183f37f | 2012-11-01 06:38:47 +0000 | [diff] [blame] | 6513 | if (inode_only == LOG_INODE_ALL) |
| 6514 | fast_search = true; |
Filipe Manana | 2ac691d | 2021-07-20 16:03:43 +0100 | [diff] [blame] | 6515 | inode_item_dropped = false; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 6516 | goto log_extents; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6517 | } |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 6518 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6519 | } |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6520 | if (ret) |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6521 | goto out_unlock; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6522 | |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 6523 | /* |
| 6524 | * If we are logging a directory in full mode, collect the delayed items |
| 6525 | * before iterating the subvolume tree, so that we don't miss any new |
| 6526 | * dir index items in case they get flushed while or right after we are |
| 6527 | * iterating the subvolume tree. |
| 6528 | */ |
| 6529 | if (full_dir_logging && !ctx->logging_new_delayed_dentries) |
| 6530 | btrfs_log_get_delayed_items(inode, &delayed_ins_list, |
| 6531 | &delayed_del_list); |
| 6532 | |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6533 | ret = copy_inode_items_to_log(trans, inode, &min_key, &max_key, |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 6534 | path, dst_path, logged_isize, |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 6535 | inode_only, ctx, |
Filipe Manana | 7af5974 | 2020-04-07 11:37:44 +0100 | [diff] [blame] | 6536 | &need_log_inode_item); |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6537 | if (ret) |
Filipe Manana | da44700 | 2020-03-09 12:41:07 +0000 | [diff] [blame] | 6538 | goto out_unlock; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6539 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 6540 | btrfs_release_path(path); |
| 6541 | btrfs_release_path(dst_path); |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6542 | ret = btrfs_log_all_xattrs(trans, inode, path, dst_path); |
| 6543 | if (ret) |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 6544 | goto out_unlock; |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 6545 | xattrs_logged = true; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 6546 | if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) { |
| 6547 | btrfs_release_path(path); |
| 6548 | btrfs_release_path(dst_path); |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6549 | ret = btrfs_log_holes(trans, inode, path); |
| 6550 | if (ret) |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 6551 | goto out_unlock; |
| 6552 | } |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 6553 | log_extents: |
Josef Bacik | f3b15cc | 2013-07-22 12:54:30 -0400 | [diff] [blame] | 6554 | btrfs_release_path(path); |
| 6555 | btrfs_release_path(dst_path); |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 6556 | if (need_log_inode_item) { |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6557 | ret = log_inode_item(trans, log, dst_path, inode, inode_item_dropped); |
| 6558 | if (ret) |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 6559 | goto out_unlock; |
Filipe Manana | b590b83 | 2021-05-28 11:37:32 +0100 | [diff] [blame] | 6560 | /* |
| 6561 | * If we are doing a fast fsync and the inode was logged before |
| 6562 | * in this transaction, we don't need to log the xattrs because |
| 6563 | * they were logged before. If xattrs were added, changed or |
| 6564 | * deleted since the last time we logged the inode, then we have |
| 6565 | * already logged them because the inode had the runtime flag |
| 6566 | * BTRFS_INODE_COPY_EVERYTHING set. |
| 6567 | */ |
| 6568 | if (!xattrs_logged && inode->logged_trans < trans->transid) { |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6569 | ret = btrfs_log_all_xattrs(trans, inode, path, dst_path); |
| 6570 | if (ret) |
Filipe Manana | b590b83 | 2021-05-28 11:37:32 +0100 | [diff] [blame] | 6571 | goto out_unlock; |
| 6572 | btrfs_release_path(path); |
| 6573 | } |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 6574 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6575 | if (fast_search) { |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 6576 | ret = btrfs_log_changed_extents(trans, inode, dst_path, ctx); |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6577 | if (ret) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6578 | goto out_unlock; |
Josef Bacik | d006a04 | 2013-11-12 20:54:09 -0500 | [diff] [blame] | 6579 | } else if (inode_only == LOG_INODE_ALL) { |
Liu Bo | 06d3d22 | 2012-08-27 10:52:19 -0600 | [diff] [blame] | 6580 | struct extent_map *em, *n; |
| 6581 | |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 6582 | write_lock(&em_tree->lock); |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 6583 | list_for_each_entry_safe(em, n, &em_tree->modified_extents, list) |
| 6584 | list_del_init(&em->list); |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 6585 | write_unlock(&em_tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6586 | } |
| 6587 | |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 6588 | if (full_dir_logging) { |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 6589 | ret = log_directory_changes(trans, inode, path, dst_path, ctx); |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6590 | if (ret) |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6591 | goto out_unlock; |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 6592 | ret = log_delayed_insertion_items(trans, inode, path, |
| 6593 | &delayed_ins_list, ctx); |
| 6594 | if (ret) |
| 6595 | goto out_unlock; |
| 6596 | ret = log_delayed_deletion_items(trans, inode, path, |
| 6597 | &delayed_del_list, ctx); |
| 6598 | if (ret) |
| 6599 | goto out_unlock; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6600 | } |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 6601 | |
Filipe Manana | 130341b | 2021-08-31 15:30:34 +0100 | [diff] [blame] | 6602 | spin_lock(&inode->lock); |
| 6603 | inode->logged_trans = trans->transid; |
Filipe Manana | d1d832a | 2019-06-07 11:25:24 +0100 | [diff] [blame] | 6604 | /* |
Filipe Manana | 130341b | 2021-08-31 15:30:34 +0100 | [diff] [blame] | 6605 | * Don't update last_log_commit if we logged that an inode exists. |
| 6606 | * We do this for three reasons: |
| 6607 | * |
| 6608 | * 1) We might have had buffered writes to this inode that were |
| 6609 | * flushed and had their ordered extents completed in this |
| 6610 | * transaction, but we did not previously log the inode with |
| 6611 | * LOG_INODE_ALL. Later the inode was evicted and after that |
| 6612 | * it was loaded again and this LOG_INODE_EXISTS log operation |
| 6613 | * happened. We must make sure that if an explicit fsync against |
| 6614 | * the inode is performed later, it logs the new extents, an |
| 6615 | * updated inode item, etc, and syncs the log. The same logic |
| 6616 | * applies to direct IO writes instead of buffered writes. |
| 6617 | * |
| 6618 | * 2) When we log the inode with LOG_INODE_EXISTS, its inode item |
| 6619 | * is logged with an i_size of 0 or whatever value was logged |
| 6620 | * before. If later the i_size of the inode is increased by a |
| 6621 | * truncate operation, the log is synced through an fsync of |
| 6622 | * some other inode and then finally an explicit fsync against |
| 6623 | * this inode is made, we must make sure this fsync logs the |
| 6624 | * inode with the new i_size, the hole between old i_size and |
| 6625 | * the new i_size, and syncs the log. |
| 6626 | * |
| 6627 | * 3) If we are logging that an ancestor inode exists as part of |
| 6628 | * logging a new name from a link or rename operation, don't update |
| 6629 | * its last_log_commit - otherwise if an explicit fsync is made |
| 6630 | * against an ancestor, the fsync considers the inode in the log |
| 6631 | * and doesn't sync the log, resulting in the ancestor missing after |
| 6632 | * a power failure unless the log was synced as part of an fsync |
| 6633 | * against any other unrelated inode. |
Filipe Manana | d1d832a | 2019-06-07 11:25:24 +0100 | [diff] [blame] | 6634 | */ |
Filipe Manana | 130341b | 2021-08-31 15:30:34 +0100 | [diff] [blame] | 6635 | if (inode_only != LOG_INODE_EXISTS) |
| 6636 | inode->last_log_commit = inode->last_sub_trans; |
| 6637 | spin_unlock(&inode->lock); |
Filipe Manana | 23e3337 | 2022-02-17 12:12:06 +0000 | [diff] [blame] | 6638 | |
| 6639 | /* |
| 6640 | * Reset the last_reflink_trans so that the next fsync does not need to |
| 6641 | * go through the slower path when logging extents and their checksums. |
| 6642 | */ |
| 6643 | if (inode_only == LOG_INODE_ALL) |
| 6644 | inode->last_reflink_trans = 0; |
| 6645 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6646 | out_unlock: |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 6647 | mutex_unlock(&inode->log_mutex); |
Filipe Manana | f6df27d | 2021-08-31 15:30:40 +0100 | [diff] [blame] | 6648 | out: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6649 | btrfs_free_path(path); |
| 6650 | btrfs_free_path(dst_path); |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 6651 | |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 6652 | if (ret) |
| 6653 | free_conflicting_inodes(ctx); |
| 6654 | else |
| 6655 | ret = log_conflicting_inodes(trans, inode->root, ctx); |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 6656 | |
Filipe Manana | 30b80f3 | 2022-08-22 11:51:44 +0100 | [diff] [blame] | 6657 | if (full_dir_logging && !ctx->logging_new_delayed_dentries) { |
| 6658 | if (!ret) |
| 6659 | ret = log_new_delayed_dentries(trans, inode, |
| 6660 | &delayed_ins_list, ctx); |
| 6661 | |
| 6662 | btrfs_log_put_delayed_items(inode, &delayed_ins_list, |
| 6663 | &delayed_del_list); |
| 6664 | } |
| 6665 | |
Filipe Manana | 65faced | 2022-01-20 11:00:11 +0000 | [diff] [blame] | 6666 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6667 | } |
| 6668 | |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 6669 | static int btrfs_log_all_parents(struct btrfs_trans_handle *trans, |
Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 6670 | struct btrfs_inode *inode, |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 6671 | struct btrfs_log_ctx *ctx) |
| 6672 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 6673 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 6674 | int ret; |
| 6675 | struct btrfs_path *path; |
| 6676 | struct btrfs_key key; |
Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 6677 | struct btrfs_root *root = inode->root; |
| 6678 | const u64 ino = btrfs_ino(inode); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 6679 | |
| 6680 | path = btrfs_alloc_path(); |
| 6681 | if (!path) |
| 6682 | return -ENOMEM; |
| 6683 | path->skip_locking = 1; |
| 6684 | path->search_commit_root = 1; |
| 6685 | |
| 6686 | key.objectid = ino; |
| 6687 | key.type = BTRFS_INODE_REF_KEY; |
| 6688 | key.offset = 0; |
| 6689 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6690 | if (ret < 0) |
| 6691 | goto out; |
| 6692 | |
| 6693 | while (true) { |
| 6694 | struct extent_buffer *leaf = path->nodes[0]; |
| 6695 | int slot = path->slots[0]; |
| 6696 | u32 cur_offset = 0; |
| 6697 | u32 item_size; |
| 6698 | unsigned long ptr; |
| 6699 | |
| 6700 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6701 | ret = btrfs_next_leaf(root, path); |
| 6702 | if (ret < 0) |
| 6703 | goto out; |
| 6704 | else if (ret > 0) |
| 6705 | break; |
| 6706 | continue; |
| 6707 | } |
| 6708 | |
| 6709 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 6710 | /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */ |
| 6711 | if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY) |
| 6712 | break; |
| 6713 | |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 6714 | item_size = btrfs_item_size(leaf, slot); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 6715 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 6716 | while (cur_offset < item_size) { |
| 6717 | struct btrfs_key inode_key; |
| 6718 | struct inode *dir_inode; |
| 6719 | |
| 6720 | inode_key.type = BTRFS_INODE_ITEM_KEY; |
| 6721 | inode_key.offset = 0; |
| 6722 | |
| 6723 | if (key.type == BTRFS_INODE_EXTREF_KEY) { |
| 6724 | struct btrfs_inode_extref *extref; |
| 6725 | |
| 6726 | extref = (struct btrfs_inode_extref *) |
| 6727 | (ptr + cur_offset); |
| 6728 | inode_key.objectid = btrfs_inode_extref_parent( |
| 6729 | leaf, extref); |
| 6730 | cur_offset += sizeof(*extref); |
| 6731 | cur_offset += btrfs_inode_extref_name_len(leaf, |
| 6732 | extref); |
| 6733 | } else { |
| 6734 | inode_key.objectid = key.offset; |
| 6735 | cur_offset = item_size; |
| 6736 | } |
| 6737 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 6738 | dir_inode = btrfs_iget(fs_info->sb, inode_key.objectid, |
| 6739 | root); |
Filipe Manana | 0f375ee | 2018-10-09 15:05:29 +0100 | [diff] [blame] | 6740 | /* |
| 6741 | * If the parent inode was deleted, return an error to |
| 6742 | * fallback to a transaction commit. This is to prevent |
| 6743 | * getting an inode that was moved from one parent A to |
| 6744 | * a parent B, got its former parent A deleted and then |
| 6745 | * it got fsync'ed, from existing at both parents after |
| 6746 | * a log replay (and the old parent still existing). |
| 6747 | * Example: |
| 6748 | * |
| 6749 | * mkdir /mnt/A |
| 6750 | * mkdir /mnt/B |
| 6751 | * touch /mnt/B/bar |
| 6752 | * sync |
| 6753 | * mv /mnt/B/bar /mnt/A/bar |
| 6754 | * mv -T /mnt/A /mnt/B |
| 6755 | * fsync /mnt/B/bar |
| 6756 | * <power fail> |
| 6757 | * |
| 6758 | * If we ignore the old parent B which got deleted, |
| 6759 | * after a log replay we would have file bar linked |
| 6760 | * at both parents and the old parent B would still |
| 6761 | * exist. |
| 6762 | */ |
| 6763 | if (IS_ERR(dir_inode)) { |
| 6764 | ret = PTR_ERR(dir_inode); |
| 6765 | goto out; |
| 6766 | } |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 6767 | |
Filipe Manana | 3e6a86a | 2021-01-27 10:34:57 +0000 | [diff] [blame] | 6768 | if (!need_log_inode(trans, BTRFS_I(dir_inode))) { |
David Sterba | e55cf7c | 2022-10-28 03:53:04 +0200 | [diff] [blame] | 6769 | btrfs_add_delayed_iput(BTRFS_I(dir_inode)); |
Filipe Manana | 3e6a86a | 2021-01-27 10:34:57 +0000 | [diff] [blame] | 6770 | continue; |
| 6771 | } |
| 6772 | |
Filipe Manana | 289cffc | 2021-08-31 15:30:32 +0100 | [diff] [blame] | 6773 | ctx->log_new_dentries = false; |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 6774 | ret = btrfs_log_inode(trans, BTRFS_I(dir_inode), |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 6775 | LOG_INODE_ALL, ctx); |
Filipe Manana | 289cffc | 2021-08-31 15:30:32 +0100 | [diff] [blame] | 6776 | if (!ret && ctx->log_new_dentries) |
Filipe Manana | 8786a6d | 2022-08-17 12:22:35 +0100 | [diff] [blame] | 6777 | ret = log_new_dir_dentries(trans, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6778 | BTRFS_I(dir_inode), ctx); |
David Sterba | e55cf7c | 2022-10-28 03:53:04 +0200 | [diff] [blame] | 6779 | btrfs_add_delayed_iput(BTRFS_I(dir_inode)); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 6780 | if (ret) |
| 6781 | goto out; |
| 6782 | } |
| 6783 | path->slots[0]++; |
| 6784 | } |
| 6785 | ret = 0; |
| 6786 | out: |
| 6787 | btrfs_free_path(path); |
| 6788 | return ret; |
| 6789 | } |
| 6790 | |
Filipe Manana | b8aa330 | 2019-04-17 11:31:06 +0100 | [diff] [blame] | 6791 | static int log_new_ancestors(struct btrfs_trans_handle *trans, |
| 6792 | struct btrfs_root *root, |
| 6793 | struct btrfs_path *path, |
| 6794 | struct btrfs_log_ctx *ctx) |
| 6795 | { |
| 6796 | struct btrfs_key found_key; |
| 6797 | |
| 6798 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]); |
| 6799 | |
| 6800 | while (true) { |
| 6801 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | b8aa330 | 2019-04-17 11:31:06 +0100 | [diff] [blame] | 6802 | struct extent_buffer *leaf = path->nodes[0]; |
| 6803 | int slot = path->slots[0]; |
| 6804 | struct btrfs_key search_key; |
| 6805 | struct inode *inode; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 6806 | u64 ino; |
Filipe Manana | b8aa330 | 2019-04-17 11:31:06 +0100 | [diff] [blame] | 6807 | int ret = 0; |
| 6808 | |
| 6809 | btrfs_release_path(path); |
| 6810 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 6811 | ino = found_key.offset; |
| 6812 | |
Filipe Manana | b8aa330 | 2019-04-17 11:31:06 +0100 | [diff] [blame] | 6813 | search_key.objectid = found_key.offset; |
| 6814 | search_key.type = BTRFS_INODE_ITEM_KEY; |
| 6815 | search_key.offset = 0; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 6816 | inode = btrfs_iget(fs_info->sb, ino, root); |
Filipe Manana | b8aa330 | 2019-04-17 11:31:06 +0100 | [diff] [blame] | 6817 | if (IS_ERR(inode)) |
| 6818 | return PTR_ERR(inode); |
| 6819 | |
Filipe Manana | ab12313 | 2021-01-27 10:34:56 +0000 | [diff] [blame] | 6820 | if (BTRFS_I(inode)->generation >= trans->transid && |
| 6821 | need_log_inode(trans, BTRFS_I(inode))) |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 6822 | ret = btrfs_log_inode(trans, BTRFS_I(inode), |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 6823 | LOG_INODE_EXISTS, ctx); |
David Sterba | e55cf7c | 2022-10-28 03:53:04 +0200 | [diff] [blame] | 6824 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
Filipe Manana | b8aa330 | 2019-04-17 11:31:06 +0100 | [diff] [blame] | 6825 | if (ret) |
| 6826 | return ret; |
| 6827 | |
| 6828 | if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID) |
| 6829 | break; |
| 6830 | |
| 6831 | search_key.type = BTRFS_INODE_REF_KEY; |
| 6832 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
| 6833 | if (ret < 0) |
| 6834 | return ret; |
| 6835 | |
| 6836 | leaf = path->nodes[0]; |
| 6837 | slot = path->slots[0]; |
| 6838 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6839 | ret = btrfs_next_leaf(root, path); |
| 6840 | if (ret < 0) |
| 6841 | return ret; |
| 6842 | else if (ret > 0) |
| 6843 | return -ENOENT; |
| 6844 | leaf = path->nodes[0]; |
| 6845 | slot = path->slots[0]; |
| 6846 | } |
| 6847 | |
| 6848 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 6849 | if (found_key.objectid != search_key.objectid || |
| 6850 | found_key.type != BTRFS_INODE_REF_KEY) |
| 6851 | return -ENOENT; |
| 6852 | } |
| 6853 | return 0; |
| 6854 | } |
| 6855 | |
| 6856 | static int log_new_ancestors_fast(struct btrfs_trans_handle *trans, |
| 6857 | struct btrfs_inode *inode, |
| 6858 | struct dentry *parent, |
| 6859 | struct btrfs_log_ctx *ctx) |
| 6860 | { |
| 6861 | struct btrfs_root *root = inode->root; |
Filipe Manana | b8aa330 | 2019-04-17 11:31:06 +0100 | [diff] [blame] | 6862 | struct dentry *old_parent = NULL; |
| 6863 | struct super_block *sb = inode->vfs_inode.i_sb; |
| 6864 | int ret = 0; |
| 6865 | |
| 6866 | while (true) { |
| 6867 | if (!parent || d_really_is_negative(parent) || |
| 6868 | sb != parent->d_sb) |
| 6869 | break; |
| 6870 | |
| 6871 | inode = BTRFS_I(d_inode(parent)); |
| 6872 | if (root != inode->root) |
| 6873 | break; |
| 6874 | |
Filipe Manana | ab12313 | 2021-01-27 10:34:56 +0000 | [diff] [blame] | 6875 | if (inode->generation >= trans->transid && |
| 6876 | need_log_inode(trans, inode)) { |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 6877 | ret = btrfs_log_inode(trans, inode, |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 6878 | LOG_INODE_EXISTS, ctx); |
Filipe Manana | b8aa330 | 2019-04-17 11:31:06 +0100 | [diff] [blame] | 6879 | if (ret) |
| 6880 | break; |
| 6881 | } |
| 6882 | if (IS_ROOT(parent)) |
| 6883 | break; |
| 6884 | |
| 6885 | parent = dget_parent(parent); |
| 6886 | dput(old_parent); |
| 6887 | old_parent = parent; |
| 6888 | } |
| 6889 | dput(old_parent); |
| 6890 | |
| 6891 | return ret; |
| 6892 | } |
| 6893 | |
| 6894 | static int log_all_new_ancestors(struct btrfs_trans_handle *trans, |
| 6895 | struct btrfs_inode *inode, |
| 6896 | struct dentry *parent, |
| 6897 | struct btrfs_log_ctx *ctx) |
| 6898 | { |
| 6899 | struct btrfs_root *root = inode->root; |
| 6900 | const u64 ino = btrfs_ino(inode); |
| 6901 | struct btrfs_path *path; |
| 6902 | struct btrfs_key search_key; |
| 6903 | int ret; |
| 6904 | |
| 6905 | /* |
| 6906 | * For a single hard link case, go through a fast path that does not |
| 6907 | * need to iterate the fs/subvolume tree. |
| 6908 | */ |
| 6909 | if (inode->vfs_inode.i_nlink < 2) |
| 6910 | return log_new_ancestors_fast(trans, inode, parent, ctx); |
| 6911 | |
| 6912 | path = btrfs_alloc_path(); |
| 6913 | if (!path) |
| 6914 | return -ENOMEM; |
| 6915 | |
| 6916 | search_key.objectid = ino; |
| 6917 | search_key.type = BTRFS_INODE_REF_KEY; |
| 6918 | search_key.offset = 0; |
| 6919 | again: |
| 6920 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
| 6921 | if (ret < 0) |
| 6922 | goto out; |
| 6923 | if (ret == 0) |
| 6924 | path->slots[0]++; |
| 6925 | |
| 6926 | while (true) { |
| 6927 | struct extent_buffer *leaf = path->nodes[0]; |
| 6928 | int slot = path->slots[0]; |
| 6929 | struct btrfs_key found_key; |
| 6930 | |
| 6931 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6932 | ret = btrfs_next_leaf(root, path); |
| 6933 | if (ret < 0) |
| 6934 | goto out; |
| 6935 | else if (ret > 0) |
| 6936 | break; |
| 6937 | continue; |
| 6938 | } |
| 6939 | |
| 6940 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 6941 | if (found_key.objectid != ino || |
| 6942 | found_key.type > BTRFS_INODE_EXTREF_KEY) |
| 6943 | break; |
| 6944 | |
| 6945 | /* |
| 6946 | * Don't deal with extended references because they are rare |
| 6947 | * cases and too complex to deal with (we would need to keep |
| 6948 | * track of which subitem we are processing for each item in |
| 6949 | * this loop, etc). So just return some error to fallback to |
| 6950 | * a transaction commit. |
| 6951 | */ |
| 6952 | if (found_key.type == BTRFS_INODE_EXTREF_KEY) { |
| 6953 | ret = -EMLINK; |
| 6954 | goto out; |
| 6955 | } |
| 6956 | |
| 6957 | /* |
| 6958 | * Logging ancestors needs to do more searches on the fs/subvol |
| 6959 | * tree, so it releases the path as needed to avoid deadlocks. |
| 6960 | * Keep track of the last inode ref key and resume from that key |
| 6961 | * after logging all new ancestors for the current hard link. |
| 6962 | */ |
| 6963 | memcpy(&search_key, &found_key, sizeof(search_key)); |
| 6964 | |
| 6965 | ret = log_new_ancestors(trans, root, path, ctx); |
| 6966 | if (ret) |
| 6967 | goto out; |
| 6968 | btrfs_release_path(path); |
| 6969 | goto again; |
| 6970 | } |
| 6971 | ret = 0; |
| 6972 | out: |
| 6973 | btrfs_free_path(path); |
| 6974 | return ret; |
| 6975 | } |
| 6976 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6977 | /* |
| 6978 | * helper function around btrfs_log_inode to make sure newly created |
| 6979 | * parent directories also end up in the log. A minimal inode and backref |
| 6980 | * only logging is done of any parent directories that are older than |
| 6981 | * the last committed transaction |
| 6982 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6983 | static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 6984 | struct btrfs_inode *inode, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 6985 | struct dentry *parent, |
Edmund Nadolski | 41a1ead | 2017-11-20 13:24:47 -0700 | [diff] [blame] | 6986 | int inode_only, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 6987 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6988 | { |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 6989 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6990 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6991 | int ret = 0; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 6992 | bool log_dentries = false; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6993 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6994 | if (btrfs_test_opt(fs_info, NOTREELOG)) { |
Josef Bacik | f31f09f | 2022-06-13 15:09:48 -0400 | [diff] [blame] | 6995 | ret = BTRFS_LOG_FORCE_COMMIT; |
Sage Weil | 3a5e140 | 2009-04-02 16:49:40 -0400 | [diff] [blame] | 6996 | goto end_no_trans; |
| 6997 | } |
| 6998 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 6999 | if (btrfs_root_refs(&root->root_item) == 0) { |
Josef Bacik | f31f09f | 2022-06-13 15:09:48 -0400 | [diff] [blame] | 7000 | ret = BTRFS_LOG_FORCE_COMMIT; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7001 | goto end_no_trans; |
| 7002 | } |
| 7003 | |
Filipe Manana | f2d72f4 | 2018-10-08 11:12:55 +0100 | [diff] [blame] | 7004 | /* |
| 7005 | * Skip already logged inodes or inodes corresponding to tmpfiles |
| 7006 | * (since logging them is pointless, a link count of 0 means they |
| 7007 | * will never be accessible). |
| 7008 | */ |
Filipe Manana | 626e9f4 | 2021-04-27 11:27:20 +0100 | [diff] [blame] | 7009 | if ((btrfs_inode_in_log(inode, trans->transid) && |
| 7010 | list_empty(&ctx->ordered_extents)) || |
Filipe Manana | f2d72f4 | 2018-10-08 11:12:55 +0100 | [diff] [blame] | 7011 | inode->vfs_inode.i_nlink == 0) { |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7012 | ret = BTRFS_NO_LOG_SYNC; |
| 7013 | goto end_no_trans; |
| 7014 | } |
| 7015 | |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 7016 | ret = start_log_trans(trans, root, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7017 | if (ret) |
Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 7018 | goto end_no_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7019 | |
Filipe Manana | 90d0451 | 2021-09-16 11:32:10 +0100 | [diff] [blame] | 7020 | ret = btrfs_log_inode(trans, inode, inode_only, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7021 | if (ret) |
| 7022 | goto end_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7023 | |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 7024 | /* |
| 7025 | * for regular files, if its inode is already on disk, we don't |
| 7026 | * have to worry about the parents at all. This is because |
| 7027 | * we can use the last_unlink_trans field to record renames |
| 7028 | * and other fun in this file. |
| 7029 | */ |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 7030 | if (S_ISREG(inode->vfs_inode.i_mode) && |
Filipe Manana | 47d3db4 | 2020-11-25 12:19:26 +0000 | [diff] [blame] | 7031 | inode->generation < trans->transid && |
| 7032 | inode->last_unlink_trans < trans->transid) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7033 | ret = 0; |
| 7034 | goto end_trans; |
| 7035 | } |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 7036 | |
Filipe Manana | 289cffc | 2021-08-31 15:30:32 +0100 | [diff] [blame] | 7037 | if (S_ISDIR(inode->vfs_inode.i_mode) && ctx->log_new_dentries) |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 7038 | log_dentries = true; |
| 7039 | |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 7040 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7041 | * On unlink we must make sure all our current and old parent directory |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 7042 | * inodes are fully logged. This is to prevent leaving dangling |
| 7043 | * directory index entries in directories that were our parents but are |
| 7044 | * not anymore. Not doing this results in old parent directory being |
| 7045 | * impossible to delete after log replay (rmdir will always fail with |
| 7046 | * error -ENOTEMPTY). |
| 7047 | * |
| 7048 | * Example 1: |
| 7049 | * |
| 7050 | * mkdir testdir |
| 7051 | * touch testdir/foo |
| 7052 | * ln testdir/foo testdir/bar |
| 7053 | * sync |
| 7054 | * unlink testdir/bar |
| 7055 | * xfs_io -c fsync testdir/foo |
| 7056 | * <power failure> |
| 7057 | * mount fs, triggers log replay |
| 7058 | * |
| 7059 | * If we don't log the parent directory (testdir), after log replay the |
| 7060 | * directory still has an entry pointing to the file inode using the bar |
| 7061 | * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and |
| 7062 | * the file inode has a link count of 1. |
| 7063 | * |
| 7064 | * Example 2: |
| 7065 | * |
| 7066 | * mkdir testdir |
| 7067 | * touch foo |
| 7068 | * ln foo testdir/foo2 |
| 7069 | * ln foo testdir/foo3 |
| 7070 | * sync |
| 7071 | * unlink testdir/foo3 |
| 7072 | * xfs_io -c fsync foo |
| 7073 | * <power failure> |
| 7074 | * mount fs, triggers log replay |
| 7075 | * |
| 7076 | * Similar as the first example, after log replay the parent directory |
| 7077 | * testdir still has an entry pointing to the inode file with name foo3 |
| 7078 | * but the file inode does not have a matching BTRFS_INODE_REF_KEY item |
| 7079 | * and has a link count of 2. |
| 7080 | */ |
Filipe Manana | 47d3db4 | 2020-11-25 12:19:26 +0000 | [diff] [blame] | 7081 | if (inode->last_unlink_trans >= trans->transid) { |
Filipe Manana | b8aa330 | 2019-04-17 11:31:06 +0100 | [diff] [blame] | 7082 | ret = btrfs_log_all_parents(trans, inode, ctx); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 7083 | if (ret) |
| 7084 | goto end_trans; |
| 7085 | } |
| 7086 | |
Filipe Manana | b8aa330 | 2019-04-17 11:31:06 +0100 | [diff] [blame] | 7087 | ret = log_all_new_ancestors(trans, inode, parent, ctx); |
| 7088 | if (ret) |
Filipe Manana | 41bd606 | 2018-11-28 14:54:28 +0000 | [diff] [blame] | 7089 | goto end_trans; |
Filipe Manana | 41bd606 | 2018-11-28 14:54:28 +0000 | [diff] [blame] | 7090 | |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 7091 | if (log_dentries) |
Filipe Manana | 8786a6d | 2022-08-17 12:22:35 +0100 | [diff] [blame] | 7092 | ret = log_new_dir_dentries(trans, inode, ctx); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 7093 | else |
| 7094 | ret = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7095 | end_trans: |
| 7096 | if (ret < 0) { |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 7097 | btrfs_set_log_full_commit(trans); |
Josef Bacik | f31f09f | 2022-06-13 15:09:48 -0400 | [diff] [blame] | 7098 | ret = BTRFS_LOG_FORCE_COMMIT; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7099 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 7100 | |
| 7101 | if (ret) |
| 7102 | btrfs_remove_log_ctx(root, ctx); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7103 | btrfs_end_log_trans(root); |
| 7104 | end_no_trans: |
| 7105 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7106 | } |
| 7107 | |
| 7108 | /* |
| 7109 | * it is not safe to log dentry if the chunk root has added new |
| 7110 | * chunks. This returns 0 if the dentry was logged, and 1 otherwise. |
| 7111 | * If this returns 1, you must commit the transaction to safely get your |
| 7112 | * data on disk. |
| 7113 | */ |
| 7114 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e5b84f7a | 2018-02-27 17:37:18 +0200 | [diff] [blame] | 7115 | struct dentry *dentry, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 7116 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7117 | { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 7118 | struct dentry *parent = dget_parent(dentry); |
| 7119 | int ret; |
| 7120 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 7121 | ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent, |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 7122 | LOG_INODE_ALL, ctx); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 7123 | dput(parent); |
| 7124 | |
| 7125 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7126 | } |
| 7127 | |
| 7128 | /* |
| 7129 | * should be called during mount to recover any replay any log trees |
| 7130 | * from the FS |
| 7131 | */ |
| 7132 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) |
| 7133 | { |
| 7134 | int ret; |
| 7135 | struct btrfs_path *path; |
| 7136 | struct btrfs_trans_handle *trans; |
| 7137 | struct btrfs_key key; |
| 7138 | struct btrfs_key found_key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7139 | struct btrfs_root *log; |
| 7140 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; |
| 7141 | struct walk_control wc = { |
| 7142 | .process_func = process_one_buffer, |
David Sterba | 430a662 | 2019-08-01 14:50:35 +0200 | [diff] [blame] | 7143 | .stage = LOG_WALK_PIN_ONLY, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7144 | }; |
| 7145 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7146 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7147 | if (!path) |
| 7148 | return -ENOMEM; |
| 7149 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 7150 | set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7151 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7152 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7153 | if (IS_ERR(trans)) { |
| 7154 | ret = PTR_ERR(trans); |
| 7155 | goto error; |
| 7156 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7157 | |
| 7158 | wc.trans = trans; |
| 7159 | wc.pin = 1; |
| 7160 | |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7161 | ret = walk_log_tree(trans, log_root_tree, &wc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7162 | if (ret) { |
Josef Bacik | ba51e2a | 2021-10-05 16:35:23 -0400 | [diff] [blame] | 7163 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7164 | goto error; |
| 7165 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7166 | |
| 7167 | again: |
| 7168 | key.objectid = BTRFS_TREE_LOG_OBJECTID; |
| 7169 | key.offset = (u64)-1; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 7170 | key.type = BTRFS_ROOT_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7171 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7172 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7173 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7174 | |
| 7175 | if (ret < 0) { |
Josef Bacik | ba51e2a | 2021-10-05 16:35:23 -0400 | [diff] [blame] | 7176 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7177 | goto error; |
| 7178 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7179 | if (ret > 0) { |
| 7180 | if (path->slots[0] == 0) |
| 7181 | break; |
| 7182 | path->slots[0]--; |
| 7183 | } |
| 7184 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 7185 | path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7186 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7187 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 7188 | break; |
| 7189 | |
Josef Bacik | 62a2c73 | 2020-01-24 09:32:21 -0500 | [diff] [blame] | 7190 | log = btrfs_read_tree_root(log_root_tree, &found_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7191 | if (IS_ERR(log)) { |
| 7192 | ret = PTR_ERR(log); |
Josef Bacik | ba51e2a | 2021-10-05 16:35:23 -0400 | [diff] [blame] | 7193 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7194 | goto error; |
| 7195 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7196 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 7197 | wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset, |
| 7198 | true); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7199 | if (IS_ERR(wc.replay_dest)) { |
| 7200 | ret = PTR_ERR(wc.replay_dest); |
Josef Bacik | 9bc574d | 2019-12-06 09:37:17 -0500 | [diff] [blame] | 7201 | |
| 7202 | /* |
| 7203 | * We didn't find the subvol, likely because it was |
| 7204 | * deleted. This is ok, simply skip this log and go to |
| 7205 | * the next one. |
| 7206 | * |
| 7207 | * We need to exclude the root because we can't have |
| 7208 | * other log replays overwriting this log as we'll read |
| 7209 | * it back in a few more times. This will keep our |
| 7210 | * block from being modified, and we'll just bail for |
| 7211 | * each subsequent pass. |
| 7212 | */ |
| 7213 | if (ret == -ENOENT) |
Nikolay Borisov | 9fce570 | 2020-01-20 16:09:13 +0200 | [diff] [blame] | 7214 | ret = btrfs_pin_extent_for_log_replay(trans, |
Josef Bacik | 9bc574d | 2019-12-06 09:37:17 -0500 | [diff] [blame] | 7215 | log->node->start, |
| 7216 | log->node->len); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 7217 | btrfs_put_root(log); |
Josef Bacik | 9bc574d | 2019-12-06 09:37:17 -0500 | [diff] [blame] | 7218 | |
| 7219 | if (!ret) |
| 7220 | goto next; |
Josef Bacik | ba51e2a | 2021-10-05 16:35:23 -0400 | [diff] [blame] | 7221 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7222 | goto error; |
| 7223 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7224 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 7225 | wc.replay_dest->log_root = log; |
Josef Bacik | 2002ae1 | 2021-03-12 15:25:05 -0500 | [diff] [blame] | 7226 | ret = btrfs_record_root_in_trans(trans, wc.replay_dest); |
| 7227 | if (ret) |
| 7228 | /* The loop needs to continue due to the root refs */ |
Josef Bacik | ba51e2a | 2021-10-05 16:35:23 -0400 | [diff] [blame] | 7229 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 2002ae1 | 2021-03-12 15:25:05 -0500 | [diff] [blame] | 7230 | else |
| 7231 | ret = walk_log_tree(trans, log, &wc); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7232 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 7233 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7234 | ret = fixup_inode_link_counts(trans, wc.replay_dest, |
| 7235 | path); |
Josef Bacik | ba51e2a | 2021-10-05 16:35:23 -0400 | [diff] [blame] | 7236 | if (ret) |
| 7237 | btrfs_abort_transaction(trans, ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7238 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7239 | |
Liu Bo | 900c998 | 2018-01-25 11:02:56 -0700 | [diff] [blame] | 7240 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
| 7241 | struct btrfs_root *root = wc.replay_dest; |
| 7242 | |
| 7243 | btrfs_release_path(path); |
| 7244 | |
| 7245 | /* |
| 7246 | * We have just replayed everything, and the highest |
| 7247 | * objectid of fs roots probably has changed in case |
| 7248 | * some inode_item's got replayed. |
| 7249 | * |
| 7250 | * root->objectid_mutex is not acquired as log replay |
| 7251 | * could only happen during mount. |
| 7252 | */ |
Nikolay Borisov | 453e487 | 2020-12-07 17:32:32 +0200 | [diff] [blame] | 7253 | ret = btrfs_init_root_free_objectid(root); |
Josef Bacik | ba51e2a | 2021-10-05 16:35:23 -0400 | [diff] [blame] | 7254 | if (ret) |
| 7255 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 900c998 | 2018-01-25 11:02:56 -0700 | [diff] [blame] | 7256 | } |
| 7257 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 7258 | wc.replay_dest->log_root = NULL; |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 7259 | btrfs_put_root(wc.replay_dest); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 7260 | btrfs_put_root(log); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7261 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 7262 | if (ret) |
| 7263 | goto error; |
Josef Bacik | 9bc574d | 2019-12-06 09:37:17 -0500 | [diff] [blame] | 7264 | next: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7265 | if (found_key.offset == 0) |
| 7266 | break; |
Josef Bacik | 9bc574d | 2019-12-06 09:37:17 -0500 | [diff] [blame] | 7267 | key.offset = found_key.offset - 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7268 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7269 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7270 | |
| 7271 | /* step one is to pin it all, step two is to replay just inodes */ |
| 7272 | if (wc.pin) { |
| 7273 | wc.pin = 0; |
| 7274 | wc.process_func = replay_one_buffer; |
| 7275 | wc.stage = LOG_WALK_REPLAY_INODES; |
| 7276 | goto again; |
| 7277 | } |
| 7278 | /* step three is to replay everything */ |
| 7279 | if (wc.stage < LOG_WALK_REPLAY_ALL) { |
| 7280 | wc.stage++; |
| 7281 | goto again; |
| 7282 | } |
| 7283 | |
| 7284 | btrfs_free_path(path); |
| 7285 | |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 7286 | /* step 4: commit the transaction, which also unpins the blocks */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 7287 | ret = btrfs_commit_transaction(trans); |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 7288 | if (ret) |
| 7289 | return ret; |
| 7290 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7291 | log_root_tree->log_root = NULL; |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 7292 | clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 7293 | btrfs_put_root(log_root_tree); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7294 | |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 7295 | return 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7296 | error: |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 7297 | if (wc.trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 7298 | btrfs_end_transaction(wc.trans); |
David Sterba | 1aeb6b5 | 2020-07-07 18:38:05 +0200 | [diff] [blame] | 7299 | clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7300 | btrfs_free_path(path); |
| 7301 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7302 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7303 | |
| 7304 | /* |
| 7305 | * there are some corner cases where we want to force a full |
| 7306 | * commit instead of allowing a directory to be logged. |
| 7307 | * |
| 7308 | * They revolve around files there were unlinked from the directory, and |
| 7309 | * this function updates the parent directory so that a full commit is |
| 7310 | * properly done if it is fsync'd later after the unlinks are done. |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 7311 | * |
| 7312 | * Must be called before the unlink operations (updates to the subvolume tree, |
| 7313 | * inodes, etc) are done. |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7314 | */ |
| 7315 | void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 7316 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7317 | int for_rename) |
| 7318 | { |
| 7319 | /* |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 7320 | * when we're logging a file, if it hasn't been renamed |
| 7321 | * or unlinked, and its inode is fully committed on disk, |
| 7322 | * we don't have to worry about walking up the directory chain |
| 7323 | * to log its parents. |
| 7324 | * |
| 7325 | * So, we use the last_unlink_trans field to put this transid |
| 7326 | * into the file. When the file is logged we check it and |
| 7327 | * don't log the parents if the file is fully on disk. |
| 7328 | */ |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 7329 | mutex_lock(&inode->log_mutex); |
| 7330 | inode->last_unlink_trans = trans->transid; |
| 7331 | mutex_unlock(&inode->log_mutex); |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 7332 | |
| 7333 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7334 | * if this directory was already logged any new |
| 7335 | * names for this file/dir will get recorded |
| 7336 | */ |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 7337 | if (dir->logged_trans == trans->transid) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7338 | return; |
| 7339 | |
| 7340 | /* |
| 7341 | * if the inode we're about to unlink was logged, |
| 7342 | * the log will be properly updated for any new names |
| 7343 | */ |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 7344 | if (inode->logged_trans == trans->transid) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7345 | return; |
| 7346 | |
| 7347 | /* |
| 7348 | * when renaming files across directories, if the directory |
| 7349 | * there we're unlinking from gets fsync'd later on, there's |
| 7350 | * no way to find the destination directory later and fsync it |
| 7351 | * properly. So, we have to be conservative and force commits |
| 7352 | * so the new name gets discovered. |
| 7353 | */ |
| 7354 | if (for_rename) |
| 7355 | goto record; |
| 7356 | |
| 7357 | /* we can safely do the unlink without any special recording */ |
| 7358 | return; |
| 7359 | |
| 7360 | record: |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 7361 | mutex_lock(&dir->log_mutex); |
| 7362 | dir->last_unlink_trans = trans->transid; |
| 7363 | mutex_unlock(&dir->log_mutex); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7364 | } |
| 7365 | |
| 7366 | /* |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 7367 | * Make sure that if someone attempts to fsync the parent directory of a deleted |
| 7368 | * snapshot, it ends up triggering a transaction commit. This is to guarantee |
| 7369 | * that after replaying the log tree of the parent directory's root we will not |
| 7370 | * see the snapshot anymore and at log replay time we will not see any log tree |
| 7371 | * corresponding to the deleted snapshot's root, which could lead to replaying |
| 7372 | * it after replaying the log tree of the parent directory (which would replay |
| 7373 | * the snapshot delete operation). |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 7374 | * |
| 7375 | * Must be called before the actual snapshot destroy operation (updates to the |
| 7376 | * parent root and tree of tree roots trees, etc) are done. |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 7377 | */ |
| 7378 | void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4366355 | 2017-01-18 00:31:29 +0200 | [diff] [blame] | 7379 | struct btrfs_inode *dir) |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 7380 | { |
Nikolay Borisov | 4366355 | 2017-01-18 00:31:29 +0200 | [diff] [blame] | 7381 | mutex_lock(&dir->log_mutex); |
| 7382 | dir->last_unlink_trans = trans->transid; |
| 7383 | mutex_unlock(&dir->log_mutex); |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 7384 | } |
| 7385 | |
David Sterba | 43dd529 | 2022-10-27 14:21:42 +0200 | [diff] [blame] | 7386 | /* |
Filipe Manana | d5f5bd5 | 2022-01-20 11:00:07 +0000 | [diff] [blame] | 7387 | * Update the log after adding a new name for an inode. |
| 7388 | * |
| 7389 | * @trans: Transaction handle. |
| 7390 | * @old_dentry: The dentry associated with the old name and the old |
| 7391 | * parent directory. |
| 7392 | * @old_dir: The inode of the previous parent directory for the case |
| 7393 | * of a rename. For a link operation, it must be NULL. |
Filipe Manana | 88d2bee | 2022-01-20 11:00:08 +0000 | [diff] [blame] | 7394 | * @old_dir_index: The index number associated with the old name, meaningful |
| 7395 | * only for rename operations (when @old_dir is not NULL). |
| 7396 | * Ignored for link operations. |
Filipe Manana | d5f5bd5 | 2022-01-20 11:00:07 +0000 | [diff] [blame] | 7397 | * @parent: The dentry associated with the directory under which the |
| 7398 | * new name is located. |
| 7399 | * |
| 7400 | * Call this after adding a new name for an inode, as a result of a link or |
| 7401 | * rename operation, and it will properly update the log to reflect the new name. |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7402 | */ |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 7403 | void btrfs_log_new_name(struct btrfs_trans_handle *trans, |
Filipe Manana | d5f5bd5 | 2022-01-20 11:00:07 +0000 | [diff] [blame] | 7404 | struct dentry *old_dentry, struct btrfs_inode *old_dir, |
Filipe Manana | 88d2bee | 2022-01-20 11:00:08 +0000 | [diff] [blame] | 7405 | u64 old_dir_index, struct dentry *parent) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7406 | { |
Filipe Manana | d5f5bd5 | 2022-01-20 11:00:07 +0000 | [diff] [blame] | 7407 | struct btrfs_inode *inode = BTRFS_I(d_inode(old_dentry)); |
Filipe Manana | 259c4b9 | 2022-01-20 11:00:09 +0000 | [diff] [blame] | 7408 | struct btrfs_root *root = inode->root; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 7409 | struct btrfs_log_ctx ctx; |
Filipe Manana | 259c4b9 | 2022-01-20 11:00:09 +0000 | [diff] [blame] | 7410 | bool log_pinned = false; |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 7411 | int ret; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7412 | |
| 7413 | /* |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 7414 | * this will force the logging code to walk the dentry chain |
| 7415 | * up for the file |
| 7416 | */ |
Filipe Manana | 9a6509c | 2018-02-28 15:55:40 +0000 | [diff] [blame] | 7417 | if (!S_ISDIR(inode->vfs_inode.i_mode)) |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 7418 | inode->last_unlink_trans = trans->transid; |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 7419 | |
| 7420 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7421 | * if this inode hasn't been logged and directory we're renaming it |
| 7422 | * from hasn't been logged, we don't need to log it |
| 7423 | */ |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 7424 | ret = inode_logged(trans, inode, NULL); |
| 7425 | if (ret < 0) { |
| 7426 | goto out; |
| 7427 | } else if (ret == 0) { |
| 7428 | if (!old_dir) |
| 7429 | return; |
| 7430 | /* |
| 7431 | * If the inode was not logged and we are doing a rename (old_dir is not |
| 7432 | * NULL), check if old_dir was logged - if it was not we can return and |
| 7433 | * do nothing. |
| 7434 | */ |
| 7435 | ret = inode_logged(trans, old_dir, NULL); |
| 7436 | if (ret < 0) |
| 7437 | goto out; |
| 7438 | else if (ret == 0) |
| 7439 | return; |
| 7440 | } |
| 7441 | ret = 0; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7442 | |
Filipe Manana | 54a40fc | 2021-05-12 16:27:16 +0100 | [diff] [blame] | 7443 | /* |
| 7444 | * If we are doing a rename (old_dir is not NULL) from a directory that |
Filipe Manana | 88d2bee | 2022-01-20 11:00:08 +0000 | [diff] [blame] | 7445 | * was previously logged, make sure that on log replay we get the old |
| 7446 | * dir entry deleted. This is needed because we will also log the new |
| 7447 | * name of the renamed inode, so we need to make sure that after log |
| 7448 | * replay we don't end up with both the new and old dir entries existing. |
Filipe Manana | 54a40fc | 2021-05-12 16:27:16 +0100 | [diff] [blame] | 7449 | */ |
Filipe Manana | 88d2bee | 2022-01-20 11:00:08 +0000 | [diff] [blame] | 7450 | if (old_dir && old_dir->logged_trans == trans->transid) { |
| 7451 | struct btrfs_root *log = old_dir->root->log_root; |
| 7452 | struct btrfs_path *path; |
Sweet Tea Dorminy | ab3c5c1 | 2022-10-20 12:58:26 -0400 | [diff] [blame] | 7453 | struct fscrypt_name fname; |
Filipe Manana | 88d2bee | 2022-01-20 11:00:08 +0000 | [diff] [blame] | 7454 | |
| 7455 | ASSERT(old_dir_index >= BTRFS_DIR_START_INDEX); |
| 7456 | |
Sweet Tea Dorminy | ab3c5c1 | 2022-10-20 12:58:26 -0400 | [diff] [blame] | 7457 | ret = fscrypt_setup_filename(&old_dir->vfs_inode, |
| 7458 | &old_dentry->d_name, 0, &fname); |
| 7459 | if (ret) |
| 7460 | goto out; |
Filipe Manana | 259c4b9 | 2022-01-20 11:00:09 +0000 | [diff] [blame] | 7461 | /* |
| 7462 | * We have two inodes to update in the log, the old directory and |
| 7463 | * the inode that got renamed, so we must pin the log to prevent |
| 7464 | * anyone from syncing the log until we have updated both inodes |
| 7465 | * in the log. |
| 7466 | */ |
Filipe Manana | 723df2b | 2022-07-17 22:05:05 +0100 | [diff] [blame] | 7467 | ret = join_running_log_trans(root); |
| 7468 | /* |
| 7469 | * At least one of the inodes was logged before, so this should |
| 7470 | * not fail, but if it does, it's not serious, just bail out and |
| 7471 | * mark the log for a full commit. |
| 7472 | */ |
Filipe Manana | fee4c19 | 2022-12-20 11:13:33 +0000 | [diff] [blame] | 7473 | if (WARN_ON_ONCE(ret < 0)) { |
| 7474 | fscrypt_free_filename(&fname); |
Filipe Manana | 723df2b | 2022-07-17 22:05:05 +0100 | [diff] [blame] | 7475 | goto out; |
Filipe Manana | fee4c19 | 2022-12-20 11:13:33 +0000 | [diff] [blame] | 7476 | } |
| 7477 | |
Filipe Manana | 259c4b9 | 2022-01-20 11:00:09 +0000 | [diff] [blame] | 7478 | log_pinned = true; |
Filipe Manana | 259c4b9 | 2022-01-20 11:00:09 +0000 | [diff] [blame] | 7479 | |
Filipe Manana | 88d2bee | 2022-01-20 11:00:08 +0000 | [diff] [blame] | 7480 | path = btrfs_alloc_path(); |
| 7481 | if (!path) { |
Filipe Manana | 259c4b9 | 2022-01-20 11:00:09 +0000 | [diff] [blame] | 7482 | ret = -ENOMEM; |
Sweet Tea Dorminy | ab3c5c1 | 2022-10-20 12:58:26 -0400 | [diff] [blame] | 7483 | fscrypt_free_filename(&fname); |
Filipe Manana | 259c4b9 | 2022-01-20 11:00:09 +0000 | [diff] [blame] | 7484 | goto out; |
Filipe Manana | 88d2bee | 2022-01-20 11:00:08 +0000 | [diff] [blame] | 7485 | } |
| 7486 | |
| 7487 | /* |
| 7488 | * Other concurrent task might be logging the old directory, |
| 7489 | * as it can be triggered when logging other inode that had or |
Filipe Manana | 750ee45 | 2022-05-03 11:57:02 +0100 | [diff] [blame] | 7490 | * still has a dentry in the old directory. We lock the old |
| 7491 | * directory's log_mutex to ensure the deletion of the old |
| 7492 | * name is persisted, because during directory logging we |
| 7493 | * delete all BTRFS_DIR_LOG_INDEX_KEY keys and the deletion of |
| 7494 | * the old name's dir index item is in the delayed items, so |
| 7495 | * it could be missed by an in progress directory logging. |
Filipe Manana | 88d2bee | 2022-01-20 11:00:08 +0000 | [diff] [blame] | 7496 | */ |
| 7497 | mutex_lock(&old_dir->log_mutex); |
| 7498 | ret = del_logged_dentry(trans, log, path, btrfs_ino(old_dir), |
Sweet Tea Dorminy | 6db7531 | 2022-10-20 12:58:27 -0400 | [diff] [blame] | 7499 | &fname.disk_name, old_dir_index); |
Filipe Manana | 88d2bee | 2022-01-20 11:00:08 +0000 | [diff] [blame] | 7500 | if (ret > 0) { |
| 7501 | /* |
| 7502 | * The dentry does not exist in the log, so record its |
| 7503 | * deletion. |
| 7504 | */ |
| 7505 | btrfs_release_path(path); |
| 7506 | ret = insert_dir_log_key(trans, log, path, |
| 7507 | btrfs_ino(old_dir), |
| 7508 | old_dir_index, old_dir_index); |
| 7509 | } |
| 7510 | mutex_unlock(&old_dir->log_mutex); |
| 7511 | |
| 7512 | btrfs_free_path(path); |
Sweet Tea Dorminy | ab3c5c1 | 2022-10-20 12:58:26 -0400 | [diff] [blame] | 7513 | fscrypt_free_filename(&fname); |
Filipe Manana | 259c4b9 | 2022-01-20 11:00:09 +0000 | [diff] [blame] | 7514 | if (ret < 0) |
| 7515 | goto out; |
Filipe Manana | 88d2bee | 2022-01-20 11:00:08 +0000 | [diff] [blame] | 7516 | } |
Filipe Manana | 54a40fc | 2021-05-12 16:27:16 +0100 | [diff] [blame] | 7517 | |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 7518 | btrfs_init_log_ctx(&ctx, &inode->vfs_inode); |
| 7519 | ctx.logging_new_name = true; |
| 7520 | /* |
| 7521 | * We don't care about the return value. If we fail to log the new name |
| 7522 | * then we know the next attempt to sync the log will fallback to a full |
| 7523 | * transaction commit (due to a call to btrfs_set_log_full_commit()), so |
| 7524 | * we don't need to worry about getting a log committed that has an |
| 7525 | * inconsistent state after a rename operation. |
| 7526 | */ |
Filipe Manana | 4877817 | 2020-08-11 12:43:58 +0100 | [diff] [blame] | 7527 | btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx); |
Filipe Manana | e09d94c | 2022-08-17 12:22:46 +0100 | [diff] [blame] | 7528 | ASSERT(list_empty(&ctx.conflict_inodes)); |
Filipe Manana | 259c4b9 | 2022-01-20 11:00:09 +0000 | [diff] [blame] | 7529 | out: |
Filipe Manana | 0f8ce49 | 2022-01-20 11:00:10 +0000 | [diff] [blame] | 7530 | /* |
| 7531 | * If an error happened mark the log for a full commit because it's not |
| 7532 | * consistent and up to date or we couldn't find out if one of the |
| 7533 | * inodes was logged before in this transaction. Do it before unpinning |
| 7534 | * the log, to avoid any races with someone else trying to commit it. |
| 7535 | */ |
| 7536 | if (ret < 0) |
| 7537 | btrfs_set_log_full_commit(trans); |
| 7538 | if (log_pinned) |
Filipe Manana | 259c4b9 | 2022-01-20 11:00:09 +0000 | [diff] [blame] | 7539 | btrfs_end_log_trans(root); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7540 | } |
| 7541 | |