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> |
Nikolay Borisov | 9678c54 | 2018-01-08 11:45:05 +0200 | [diff] [blame] | 11 | #include "ctree.h" |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 12 | #include "tree-log.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 13 | #include "disk-io.h" |
| 14 | #include "locking.h" |
| 15 | #include "print-tree.h" |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 16 | #include "backref.h" |
Anand Jain | ebb8765 | 2016-03-10 17:26:59 +0800 | [diff] [blame] | 17 | #include "compression.h" |
Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 18 | #include "qgroup.h" |
Liu Bo | 900c998 | 2018-01-25 11:02:56 -0700 | [diff] [blame] | 19 | #include "inode-map.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 20 | |
| 21 | /* magic values for the inode_only field in btrfs_log_inode: |
| 22 | * |
| 23 | * LOG_INODE_ALL means to log everything |
| 24 | * LOG_INODE_EXISTS means to log just enough to recreate the inode |
| 25 | * during log replay |
| 26 | */ |
| 27 | #define LOG_INODE_ALL 0 |
| 28 | #define LOG_INODE_EXISTS 1 |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 29 | #define LOG_OTHER_INODE 2 |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 30 | |
| 31 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 32 | * directory trouble cases |
| 33 | * |
| 34 | * 1) on rename or unlink, if the inode being unlinked isn't in the fsync |
| 35 | * log, we must force a full commit before doing an fsync of the directory |
| 36 | * where the unlink was done. |
| 37 | * ---> record transid of last unlink/rename per directory |
| 38 | * |
| 39 | * mkdir foo/some_dir |
| 40 | * normal commit |
| 41 | * rename foo/some_dir foo2/some_dir |
| 42 | * mkdir foo/some_dir |
| 43 | * fsync foo/some_dir/some_file |
| 44 | * |
| 45 | * The fsync above will unlink the original some_dir without recording |
| 46 | * it in its new location (foo2). After a crash, some_dir will be gone |
| 47 | * unless the fsync of some_file forces a full commit |
| 48 | * |
| 49 | * 2) we must log any new names for any file or dir that is in the fsync |
| 50 | * log. ---> check inode while renaming/linking. |
| 51 | * |
| 52 | * 2a) we must log any new names for any file or dir during rename |
| 53 | * when the directory they are being removed from was logged. |
| 54 | * ---> check inode and old parent dir during rename |
| 55 | * |
| 56 | * 2a is actually the more important variant. With the extra logging |
| 57 | * a crash might unlink the old name without recreating the new one |
| 58 | * |
| 59 | * 3) after a crash, we must go through any directories with a link count |
| 60 | * of zero and redo the rm -rf |
| 61 | * |
| 62 | * mkdir f1/foo |
| 63 | * normal commit |
| 64 | * rm -rf f1/foo |
| 65 | * fsync(f1) |
| 66 | * |
| 67 | * The directory f1 was fully removed from the FS, but fsync was never |
| 68 | * called on f1, only its parent dir. After a crash the rm -rf must |
| 69 | * be replayed. This must be able to recurse down the entire |
| 70 | * directory tree. The inode link count fixup code takes care of the |
| 71 | * ugly details. |
| 72 | */ |
| 73 | |
| 74 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 75 | * stages for the tree walking. The first |
| 76 | * stage (0) is to only pin down the blocks we find |
| 77 | * the second stage (1) is to make sure that all the inodes |
| 78 | * we find in the log are created in the subvolume. |
| 79 | * |
| 80 | * The last stage is to deal with directories and links and extents |
| 81 | * and all the other fun semantics |
| 82 | */ |
| 83 | #define LOG_WALK_PIN_ONLY 0 |
| 84 | #define LOG_WALK_REPLAY_INODES 1 |
Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 85 | #define LOG_WALK_REPLAY_DIR_INDEX 2 |
| 86 | #define LOG_WALK_REPLAY_ALL 3 |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 87 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 88 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 89 | struct btrfs_root *root, struct btrfs_inode *inode, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 90 | int inode_only, |
| 91 | const loff_t start, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 92 | const loff_t end, |
| 93 | struct btrfs_log_ctx *ctx); |
Yan Zheng | ec051c0 | 2009-01-05 15:43:42 -0500 | [diff] [blame] | 94 | static int link_to_fixup_dir(struct btrfs_trans_handle *trans, |
| 95 | struct btrfs_root *root, |
| 96 | struct btrfs_path *path, u64 objectid); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 97 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, |
| 98 | struct btrfs_root *root, |
| 99 | struct btrfs_root *log, |
| 100 | struct btrfs_path *path, |
| 101 | u64 dirid, int del_all); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * tree logging is a special write ahead log used to make sure that |
| 105 | * fsyncs and O_SYNCs can happen without doing full tree commits. |
| 106 | * |
| 107 | * Full tree commits are expensive because they require commonly |
| 108 | * modified blocks to be recowed, creating many dirty pages in the |
| 109 | * extent tree an 4x-6x higher write load than ext3. |
| 110 | * |
| 111 | * Instead of doing a tree commit on every fsync, we use the |
| 112 | * key ranges and transaction ids to find items for a given file or directory |
| 113 | * that have changed in this transaction. Those items are copied into |
| 114 | * a special tree (one per subvolume root), that tree is written to disk |
| 115 | * and then the fsync is considered complete. |
| 116 | * |
| 117 | * After a crash, items are copied out of the log-tree back into the |
| 118 | * subvolume tree. Any file data extents found are recorded in the extent |
| 119 | * allocation tree, and the log-tree freed. |
| 120 | * |
| 121 | * The log tree is read three times, once to pin down all the extents it is |
| 122 | * using in ram and once, once to create all the inodes logged in the tree |
| 123 | * and once to do all the other items. |
| 124 | */ |
| 125 | |
| 126 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 127 | * start a sub transaction and setup the log tree |
| 128 | * this increments the log tree writer count to make the people |
| 129 | * syncing the tree wait for us to finish |
| 130 | */ |
| 131 | static int start_log_trans(struct btrfs_trans_handle *trans, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 132 | struct btrfs_root *root, |
| 133 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 134 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 135 | struct btrfs_fs_info *fs_info = root->fs_info; |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 136 | int ret = 0; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 137 | |
| 138 | mutex_lock(&root->log_mutex); |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 139 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 140 | if (root->log_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 141 | if (btrfs_need_log_full_commit(fs_info, trans)) { |
Miao Xie | 50471a3 | 2014-02-20 18:08:57 +0800 | [diff] [blame] | 142 | ret = -EAGAIN; |
| 143 | goto out; |
| 144 | } |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 145 | |
Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 146 | if (!root->log_start_pid) { |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 147 | clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 148 | root->log_start_pid = current->pid; |
Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 149 | } else if (root->log_start_pid != current->pid) { |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 150 | set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 151 | } |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 152 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 153 | mutex_lock(&fs_info->tree_log_mutex); |
| 154 | if (!fs_info->log_root_tree) |
| 155 | ret = btrfs_init_log_root_tree(trans, fs_info); |
| 156 | mutex_unlock(&fs_info->tree_log_mutex); |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 157 | if (ret) |
| 158 | goto out; |
Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 159 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 160 | ret = btrfs_add_log_tree(trans, root); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 161 | if (ret) |
Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 162 | goto out; |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 163 | |
| 164 | clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
| 165 | root->log_start_pid = current->pid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 166 | } |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 167 | |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 168 | atomic_inc(&root->log_batch); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 169 | atomic_inc(&root->log_writers); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 170 | if (ctx) { |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 171 | int index = root->log_transid % 2; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 172 | list_add_tail(&ctx->list, &root->log_ctxs[index]); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 173 | ctx->log_transid = root->log_transid; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 174 | } |
Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 175 | |
Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 176 | out: |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 177 | mutex_unlock(&root->log_mutex); |
Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 178 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
| 182 | * returns 0 if there was a log transaction running and we were able |
| 183 | * to join, or returns -ENOENT if there were not transactions |
| 184 | * in progress |
| 185 | */ |
| 186 | static int join_running_log_trans(struct btrfs_root *root) |
| 187 | { |
| 188 | int ret = -ENOENT; |
| 189 | |
| 190 | smp_mb(); |
| 191 | if (!root->log_root) |
| 192 | return -ENOENT; |
| 193 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 194 | mutex_lock(&root->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 195 | if (root->log_root) { |
| 196 | ret = 0; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 197 | atomic_inc(&root->log_writers); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 198 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 199 | mutex_unlock(&root->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 204 | * This either makes the current running log transaction wait |
| 205 | * until you call btrfs_end_log_trans() or it makes any future |
| 206 | * log transactions wait until you call btrfs_end_log_trans() |
| 207 | */ |
| 208 | int btrfs_pin_log_trans(struct btrfs_root *root) |
| 209 | { |
| 210 | int ret = -ENOENT; |
| 211 | |
| 212 | mutex_lock(&root->log_mutex); |
| 213 | atomic_inc(&root->log_writers); |
| 214 | mutex_unlock(&root->log_mutex); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 219 | * indicate we're done making changes to the log tree |
| 220 | * and wake up anyone waiting to do a sync |
| 221 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 222 | void btrfs_end_log_trans(struct btrfs_root *root) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 223 | { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 224 | if (atomic_dec_and_test(&root->log_writers)) { |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 225 | /* atomic_dec_and_test implies a barrier */ |
| 226 | cond_wake_up_nomb(&root->log_writer_wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 227 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | /* |
| 232 | * the walk control struct is used to pass state down the chain when |
| 233 | * processing the log tree. The stage field tells us which part |
| 234 | * of the log tree processing we are currently doing. The others |
| 235 | * are state fields used for that specific part |
| 236 | */ |
| 237 | struct walk_control { |
| 238 | /* should we free the extent on disk when done? This is used |
| 239 | * at transaction commit time while freeing a log tree |
| 240 | */ |
| 241 | int free; |
| 242 | |
| 243 | /* should we write out the extent buffer? This is used |
| 244 | * while flushing the log tree to disk during a sync |
| 245 | */ |
| 246 | int write; |
| 247 | |
| 248 | /* should we wait for the extent buffer io to finish? Also used |
| 249 | * while flushing the log tree to disk for a sync |
| 250 | */ |
| 251 | int wait; |
| 252 | |
| 253 | /* pin only walk, we record which extents on disk belong to the |
| 254 | * log trees |
| 255 | */ |
| 256 | int pin; |
| 257 | |
| 258 | /* what stage of the replay code we're currently in */ |
| 259 | int stage; |
| 260 | |
Filipe Manana | 55f21e1 | 2018-10-08 11:12:55 +0100 | [diff] [blame] | 261 | /* |
| 262 | * Ignore any items from the inode currently being processed. Needs |
| 263 | * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in |
| 264 | * the LOG_WALK_REPLAY_INODES stage. |
| 265 | */ |
| 266 | bool ignore_cur_inode; |
| 267 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 268 | /* the root we are currently replaying */ |
| 269 | struct btrfs_root *replay_dest; |
| 270 | |
| 271 | /* the trans handle for the current replay */ |
| 272 | struct btrfs_trans_handle *trans; |
| 273 | |
| 274 | /* the function that gets used to process blocks we find in the |
| 275 | * tree. Note the extent_buffer might not be up to date when it is |
| 276 | * passed in, and it must be checked or read if you need the data |
| 277 | * inside it |
| 278 | */ |
| 279 | int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 280 | struct walk_control *wc, u64 gen, int level); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | /* |
| 284 | * process_func used to pin down extents, write them or wait on them |
| 285 | */ |
| 286 | static int process_one_buffer(struct btrfs_root *log, |
| 287 | struct extent_buffer *eb, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 288 | struct walk_control *wc, u64 gen, int level) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 289 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 290 | struct btrfs_fs_info *fs_info = log->fs_info; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 291 | int ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 292 | |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 293 | /* |
| 294 | * If this fs is mixed then we need to be able to process the leaves to |
| 295 | * pin down any logged extents, so we have to read the block. |
| 296 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 297 | if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 298 | ret = btrfs_read_buffer(eb, gen, level, NULL); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 299 | if (ret) |
| 300 | return ret; |
| 301 | } |
| 302 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 303 | if (wc->pin) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 304 | ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start, |
| 305 | eb->len); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 306 | |
| 307 | if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) { |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 308 | if (wc->pin && btrfs_header_level(eb) == 0) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 309 | ret = btrfs_exclude_logged_extents(fs_info, eb); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 310 | if (wc->write) |
| 311 | btrfs_write_tree_block(eb); |
| 312 | if (wc->wait) |
| 313 | btrfs_wait_tree_block_writeback(eb); |
| 314 | } |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 315 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Item overwrite used by replay and tree logging. eb, slot and key all refer |
| 320 | * to the src data we are copying out. |
| 321 | * |
| 322 | * root is the tree we are copying into, and path is a scratch |
| 323 | * path for use in this function (it should be released on entry and |
| 324 | * will be released on exit). |
| 325 | * |
| 326 | * If the key is already in the destination tree the existing item is |
| 327 | * overwritten. If the existing item isn't big enough, it is extended. |
| 328 | * If it is too large, it is truncated. |
| 329 | * |
| 330 | * If the key isn't in the destination yet, a new item is inserted. |
| 331 | */ |
| 332 | static noinline int overwrite_item(struct btrfs_trans_handle *trans, |
| 333 | struct btrfs_root *root, |
| 334 | struct btrfs_path *path, |
| 335 | struct extent_buffer *eb, int slot, |
| 336 | struct btrfs_key *key) |
| 337 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 338 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 339 | int ret; |
| 340 | u32 item_size; |
| 341 | u64 saved_i_size = 0; |
| 342 | int save_old_i_size = 0; |
| 343 | unsigned long src_ptr; |
| 344 | unsigned long dst_ptr; |
| 345 | int overwrite_root = 0; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 346 | bool inode_item = key->type == BTRFS_INODE_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 347 | |
| 348 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 349 | overwrite_root = 1; |
| 350 | |
| 351 | item_size = btrfs_item_size_nr(eb, slot); |
| 352 | src_ptr = btrfs_item_ptr_offset(eb, slot); |
| 353 | |
| 354 | /* look for the key in the destination tree */ |
| 355 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 356 | if (ret < 0) |
| 357 | return ret; |
| 358 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 359 | if (ret == 0) { |
| 360 | char *src_copy; |
| 361 | char *dst_copy; |
| 362 | u32 dst_size = btrfs_item_size_nr(path->nodes[0], |
| 363 | path->slots[0]); |
| 364 | if (dst_size != item_size) |
| 365 | goto insert; |
| 366 | |
| 367 | if (item_size == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 368 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | dst_copy = kmalloc(item_size, GFP_NOFS); |
| 372 | src_copy = kmalloc(item_size, GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 373 | if (!dst_copy || !src_copy) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 374 | btrfs_release_path(path); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 375 | kfree(dst_copy); |
| 376 | kfree(src_copy); |
| 377 | return -ENOMEM; |
| 378 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 379 | |
| 380 | read_extent_buffer(eb, src_copy, src_ptr, item_size); |
| 381 | |
| 382 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
| 383 | read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, |
| 384 | item_size); |
| 385 | ret = memcmp(dst_copy, src_copy, item_size); |
| 386 | |
| 387 | kfree(dst_copy); |
| 388 | kfree(src_copy); |
| 389 | /* |
| 390 | * they have the same contents, just return, this saves |
| 391 | * us from cowing blocks in the destination tree and doing |
| 392 | * extra writes that may not have been done by a previous |
| 393 | * sync |
| 394 | */ |
| 395 | if (ret == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 396 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 400 | /* |
| 401 | * We need to load the old nbytes into the inode so when we |
| 402 | * replay the extents we've logged we get the right nbytes. |
| 403 | */ |
| 404 | if (inode_item) { |
| 405 | struct btrfs_inode_item *item; |
| 406 | u64 nbytes; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 407 | u32 mode; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 408 | |
| 409 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 410 | struct btrfs_inode_item); |
| 411 | nbytes = btrfs_inode_nbytes(path->nodes[0], item); |
| 412 | item = btrfs_item_ptr(eb, slot, |
| 413 | struct btrfs_inode_item); |
| 414 | btrfs_set_inode_nbytes(eb, item, nbytes); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 415 | |
| 416 | /* |
| 417 | * If this is a directory we need to reset the i_size to |
| 418 | * 0 so that we can set it up properly when replaying |
| 419 | * the rest of the items in this log. |
| 420 | */ |
| 421 | mode = btrfs_inode_mode(eb, item); |
| 422 | if (S_ISDIR(mode)) |
| 423 | btrfs_set_inode_size(eb, item, 0); |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 424 | } |
| 425 | } else if (inode_item) { |
| 426 | struct btrfs_inode_item *item; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 427 | u32 mode; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * New inode, set nbytes to 0 so that the nbytes comes out |
| 431 | * properly when we replay the extents. |
| 432 | */ |
| 433 | item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); |
| 434 | btrfs_set_inode_nbytes(eb, item, 0); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * If this is a directory we need to reset the i_size to 0 so |
| 438 | * that we can set it up properly when replaying the rest of |
| 439 | * the items in this log. |
| 440 | */ |
| 441 | mode = btrfs_inode_mode(eb, item); |
| 442 | if (S_ISDIR(mode)) |
| 443 | btrfs_set_inode_size(eb, item, 0); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 444 | } |
| 445 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 446 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 447 | /* try to insert the key into the destination tree */ |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 448 | path->skip_release_on_error = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 449 | ret = btrfs_insert_empty_item(trans, root, path, |
| 450 | key, item_size); |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 451 | path->skip_release_on_error = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 452 | |
| 453 | /* make sure any existing item is the correct size */ |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 454 | if (ret == -EEXIST || ret == -EOVERFLOW) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 455 | u32 found_size; |
| 456 | found_size = btrfs_item_size_nr(path->nodes[0], |
| 457 | path->slots[0]); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 458 | if (found_size > item_size) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 459 | btrfs_truncate_item(fs_info, path, item_size, 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 460 | else if (found_size < item_size) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 461 | btrfs_extend_item(fs_info, path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 462 | item_size - found_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 463 | } else if (ret) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 464 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 465 | } |
| 466 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], |
| 467 | path->slots[0]); |
| 468 | |
| 469 | /* don't overwrite an existing inode if the generation number |
| 470 | * was logged as zero. This is done when the tree logging code |
| 471 | * is just logging an inode to make sure it exists after recovery. |
| 472 | * |
| 473 | * Also, don't overwrite i_size on directories during replay. |
| 474 | * log replay inserts and removes directory items based on the |
| 475 | * state of the tree found in the subvolume, and i_size is modified |
| 476 | * as it goes |
| 477 | */ |
| 478 | if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) { |
| 479 | struct btrfs_inode_item *src_item; |
| 480 | struct btrfs_inode_item *dst_item; |
| 481 | |
| 482 | src_item = (struct btrfs_inode_item *)src_ptr; |
| 483 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 484 | |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 485 | if (btrfs_inode_generation(eb, src_item) == 0) { |
| 486 | struct extent_buffer *dst_eb = path->nodes[0]; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 487 | const u64 ino_size = btrfs_inode_size(eb, src_item); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 488 | |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 489 | /* |
| 490 | * For regular files an ino_size == 0 is used only when |
| 491 | * logging that an inode exists, as part of a directory |
| 492 | * fsync, and the inode wasn't fsynced before. In this |
| 493 | * case don't set the size of the inode in the fs/subvol |
| 494 | * tree, otherwise we would be throwing valid data away. |
| 495 | */ |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 496 | if (S_ISREG(btrfs_inode_mode(eb, src_item)) && |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 497 | S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) && |
| 498 | ino_size != 0) { |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 499 | struct btrfs_map_token token; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 500 | |
| 501 | btrfs_init_map_token(&token); |
| 502 | btrfs_set_token_inode_size(dst_eb, dst_item, |
| 503 | ino_size, &token); |
| 504 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 505 | goto no_copy; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 506 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 507 | |
| 508 | if (overwrite_root && |
| 509 | S_ISDIR(btrfs_inode_mode(eb, src_item)) && |
| 510 | S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { |
| 511 | save_old_i_size = 1; |
| 512 | saved_i_size = btrfs_inode_size(path->nodes[0], |
| 513 | dst_item); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | copy_extent_buffer(path->nodes[0], eb, dst_ptr, |
| 518 | src_ptr, item_size); |
| 519 | |
| 520 | if (save_old_i_size) { |
| 521 | struct btrfs_inode_item *dst_item; |
| 522 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 523 | btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); |
| 524 | } |
| 525 | |
| 526 | /* make sure the generation is filled in */ |
| 527 | if (key->type == BTRFS_INODE_ITEM_KEY) { |
| 528 | struct btrfs_inode_item *dst_item; |
| 529 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 530 | if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { |
| 531 | btrfs_set_inode_generation(path->nodes[0], dst_item, |
| 532 | trans->transid); |
| 533 | } |
| 534 | } |
| 535 | no_copy: |
| 536 | btrfs_mark_buffer_dirty(path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 537 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * simple helper to read an inode off the disk from a given root |
| 543 | * This can only be called for subvolume roots and not for the log |
| 544 | */ |
| 545 | static noinline struct inode *read_one_inode(struct btrfs_root *root, |
| 546 | u64 objectid) |
| 547 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 548 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 549 | struct inode *inode; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 550 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 551 | key.objectid = objectid; |
| 552 | key.type = BTRFS_INODE_ITEM_KEY; |
| 553 | key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 554 | inode = btrfs_iget(root->fs_info->sb, &key, root, NULL); |
Al Viro | 2e19f1f | 2018-07-29 23:04:45 +0100 | [diff] [blame] | 555 | if (IS_ERR(inode)) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 556 | inode = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 557 | return inode; |
| 558 | } |
| 559 | |
| 560 | /* replays a single extent in 'eb' at 'slot' with 'key' into the |
| 561 | * subvolume 'root'. path is released on entry and should be released |
| 562 | * on exit. |
| 563 | * |
| 564 | * extents in the log tree have not been allocated out of the extent |
| 565 | * tree yet. So, this completes the allocation, taking a reference |
| 566 | * as required if the extent already exists or creating a new extent |
| 567 | * if it isn't in the extent allocation tree yet. |
| 568 | * |
| 569 | * The extent is inserted into the file, dropping any existing extents |
| 570 | * from the file that overlap the new one. |
| 571 | */ |
| 572 | static noinline int replay_one_extent(struct btrfs_trans_handle *trans, |
| 573 | struct btrfs_root *root, |
| 574 | struct btrfs_path *path, |
| 575 | struct extent_buffer *eb, int slot, |
| 576 | struct btrfs_key *key) |
| 577 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 578 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 579 | int found_type; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 580 | u64 extent_end; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 581 | u64 start = key->offset; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 582 | u64 nbytes = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 583 | struct btrfs_file_extent_item *item; |
| 584 | struct inode *inode = NULL; |
| 585 | unsigned long size; |
| 586 | int ret = 0; |
| 587 | |
| 588 | item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 589 | found_type = btrfs_file_extent_type(eb, item); |
| 590 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 591 | if (found_type == BTRFS_FILE_EXTENT_REG || |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 592 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 593 | nbytes = btrfs_file_extent_num_bytes(eb, item); |
| 594 | extent_end = start + nbytes; |
| 595 | |
| 596 | /* |
| 597 | * We don't add to the inodes nbytes if we are prealloc or a |
| 598 | * hole. |
| 599 | */ |
| 600 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 601 | nbytes = 0; |
| 602 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 603 | size = btrfs_file_extent_ram_bytes(eb, item); |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 604 | nbytes = btrfs_file_extent_ram_bytes(eb, item); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 605 | extent_end = ALIGN(start + size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 606 | fs_info->sectorsize); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 607 | } else { |
| 608 | ret = 0; |
| 609 | goto out; |
| 610 | } |
| 611 | |
| 612 | inode = read_one_inode(root, key->objectid); |
| 613 | if (!inode) { |
| 614 | ret = -EIO; |
| 615 | goto out; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * first check to see if we already have this extent in the |
| 620 | * file. This must be done before the btrfs_drop_extents run |
| 621 | * so we don't try to drop this extent. |
| 622 | */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 623 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 624 | btrfs_ino(BTRFS_I(inode)), start, 0); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 625 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 626 | if (ret == 0 && |
| 627 | (found_type == BTRFS_FILE_EXTENT_REG || |
| 628 | found_type == BTRFS_FILE_EXTENT_PREALLOC)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 629 | struct btrfs_file_extent_item cmp1; |
| 630 | struct btrfs_file_extent_item cmp2; |
| 631 | struct btrfs_file_extent_item *existing; |
| 632 | struct extent_buffer *leaf; |
| 633 | |
| 634 | leaf = path->nodes[0]; |
| 635 | existing = btrfs_item_ptr(leaf, path->slots[0], |
| 636 | struct btrfs_file_extent_item); |
| 637 | |
| 638 | read_extent_buffer(eb, &cmp1, (unsigned long)item, |
| 639 | sizeof(cmp1)); |
| 640 | read_extent_buffer(leaf, &cmp2, (unsigned long)existing, |
| 641 | sizeof(cmp2)); |
| 642 | |
| 643 | /* |
| 644 | * we already have a pointer to this exact extent, |
| 645 | * we don't have to do anything |
| 646 | */ |
| 647 | if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 648 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 649 | goto out; |
| 650 | } |
| 651 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 652 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 653 | |
| 654 | /* drop any overlapping extents */ |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 655 | ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 656 | if (ret) |
| 657 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 658 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 659 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 660 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 661 | u64 offset; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 662 | unsigned long dest_offset; |
| 663 | struct btrfs_key ins; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 664 | |
Filipe Manana | 3168021c | 2017-02-01 14:58:02 +0000 | [diff] [blame] | 665 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0 && |
| 666 | btrfs_fs_incompat(fs_info, NO_HOLES)) |
| 667 | goto update_inode; |
| 668 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 669 | ret = btrfs_insert_empty_item(trans, root, path, key, |
| 670 | sizeof(*item)); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 671 | if (ret) |
| 672 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 673 | dest_offset = btrfs_item_ptr_offset(path->nodes[0], |
| 674 | path->slots[0]); |
| 675 | copy_extent_buffer(path->nodes[0], eb, dest_offset, |
| 676 | (unsigned long)item, sizeof(*item)); |
| 677 | |
| 678 | ins.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 679 | ins.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
| 680 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 681 | offset = key->offset - btrfs_file_extent_offset(eb, item); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 682 | |
Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 683 | /* |
| 684 | * Manually record dirty extent, as here we did a shallow |
| 685 | * file extent item copy and skip normal backref update, |
| 686 | * but modifying extent tree all by ourselves. |
| 687 | * So need to manually record dirty extent for qgroup, |
| 688 | * as the owner of the file extent changed from log tree |
| 689 | * (doesn't affect qgroup) to fs/file tree(affects qgroup) |
| 690 | */ |
Lu Fengqi | a95f3aa | 2018-07-18 16:28:03 +0800 | [diff] [blame] | 691 | ret = btrfs_qgroup_trace_extent(trans, |
Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 692 | btrfs_file_extent_disk_bytenr(eb, item), |
| 693 | btrfs_file_extent_disk_num_bytes(eb, item), |
| 694 | GFP_NOFS); |
| 695 | if (ret < 0) |
| 696 | goto out; |
| 697 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 698 | if (ins.objectid > 0) { |
| 699 | u64 csum_start; |
| 700 | u64 csum_end; |
| 701 | LIST_HEAD(ordered_sums); |
| 702 | /* |
| 703 | * is this extent already allocated in the extent |
| 704 | * allocation tree? If so, just add a reference |
| 705 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 706 | ret = btrfs_lookup_data_extent(fs_info, ins.objectid, |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 707 | ins.offset); |
| 708 | if (ret == 0) { |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 709 | ret = btrfs_inc_extent_ref(trans, root, |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 710 | ins.objectid, ins.offset, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 711 | 0, root->root_key.objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 712 | key->objectid, offset); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 713 | if (ret) |
| 714 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 715 | } else { |
| 716 | /* |
| 717 | * insert the extent pointer in the extent |
| 718 | * allocation tree |
| 719 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 720 | ret = btrfs_alloc_logged_file_extent(trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 721 | root->root_key.objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 722 | key->objectid, offset, &ins); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 723 | if (ret) |
| 724 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 725 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 726 | btrfs_release_path(path); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 727 | |
| 728 | if (btrfs_file_extent_compression(eb, item)) { |
| 729 | csum_start = ins.objectid; |
| 730 | csum_end = csum_start + ins.offset; |
| 731 | } else { |
| 732 | csum_start = ins.objectid + |
| 733 | btrfs_file_extent_offset(eb, item); |
| 734 | csum_end = csum_start + |
| 735 | btrfs_file_extent_num_bytes(eb, item); |
| 736 | } |
| 737 | |
| 738 | ret = btrfs_lookup_csums_range(root->log_root, |
| 739 | csum_start, csum_end - 1, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 740 | &ordered_sums, 0); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 741 | if (ret) |
| 742 | goto out; |
Filipe Manana | b84b839 | 2015-08-19 11:09:40 +0100 | [diff] [blame] | 743 | /* |
| 744 | * Now delete all existing cums in the csum root that |
| 745 | * cover our range. We do this because we can have an |
| 746 | * extent that is completely referenced by one file |
| 747 | * extent item and partially referenced by another |
| 748 | * file extent item (like after using the clone or |
| 749 | * extent_same ioctls). In this case if we end up doing |
| 750 | * the replay of the one that partially references the |
| 751 | * extent first, and we do not do the csum deletion |
| 752 | * below, we can get 2 csum items in the csum tree that |
| 753 | * overlap each other. For example, imagine our log has |
| 754 | * the two following file extent items: |
| 755 | * |
| 756 | * key (257 EXTENT_DATA 409600) |
| 757 | * extent data disk byte 12845056 nr 102400 |
| 758 | * extent data offset 20480 nr 20480 ram 102400 |
| 759 | * |
| 760 | * key (257 EXTENT_DATA 819200) |
| 761 | * extent data disk byte 12845056 nr 102400 |
| 762 | * extent data offset 0 nr 102400 ram 102400 |
| 763 | * |
| 764 | * Where the second one fully references the 100K extent |
| 765 | * that starts at disk byte 12845056, and the log tree |
| 766 | * has a single csum item that covers the entire range |
| 767 | * of the extent: |
| 768 | * |
| 769 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 |
| 770 | * |
| 771 | * After the first file extent item is replayed, the |
| 772 | * csum tree gets the following csum item: |
| 773 | * |
| 774 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 |
| 775 | * |
| 776 | * Which covers the 20K sub-range starting at offset 20K |
| 777 | * of our extent. Now when we replay the second file |
| 778 | * extent item, if we do not delete existing csum items |
| 779 | * that cover any of its blocks, we end up getting two |
| 780 | * csum items in our csum tree that overlap each other: |
| 781 | * |
| 782 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 |
| 783 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 |
| 784 | * |
| 785 | * Which is a problem, because after this anyone trying |
| 786 | * to lookup up for the checksum of any block of our |
| 787 | * extent starting at an offset of 40K or higher, will |
| 788 | * end up looking at the second csum item only, which |
| 789 | * does not contain the checksum for any block starting |
| 790 | * at offset 40K or higher of our extent. |
| 791 | */ |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 792 | while (!list_empty(&ordered_sums)) { |
| 793 | struct btrfs_ordered_sum *sums; |
| 794 | sums = list_entry(ordered_sums.next, |
| 795 | struct btrfs_ordered_sum, |
| 796 | list); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 797 | if (!ret) |
Filipe Manana | 0151049 | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 798 | ret = btrfs_del_csums(trans, |
| 799 | fs_info->csum_root, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 800 | sums->bytenr, |
| 801 | sums->len); |
Filipe Manana | b84b839 | 2015-08-19 11:09:40 +0100 | [diff] [blame] | 802 | if (!ret) |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 803 | ret = btrfs_csum_file_blocks(trans, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 804 | fs_info->csum_root, sums); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 805 | list_del(&sums->list); |
| 806 | kfree(sums); |
| 807 | } |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 808 | if (ret) |
| 809 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 810 | } else { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 811 | btrfs_release_path(path); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 812 | } |
| 813 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 814 | /* inline extents are easy, we just overwrite them */ |
| 815 | ret = overwrite_item(trans, root, path, eb, slot, key); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 816 | if (ret) |
| 817 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 818 | } |
| 819 | |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 820 | inode_add_bytes(inode, nbytes); |
Filipe Manana | 3168021c | 2017-02-01 14:58:02 +0000 | [diff] [blame] | 821 | update_inode: |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 822 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 823 | out: |
| 824 | if (inode) |
| 825 | iput(inode); |
| 826 | return ret; |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * when cleaning up conflicts between the directory names in the |
| 831 | * subvolume, directory names in the log and directory names in the |
| 832 | * inode back references, we may have to unlink inodes from directories. |
| 833 | * |
| 834 | * This is a helper function to do the unlink of a specific directory |
| 835 | * item |
| 836 | */ |
| 837 | static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, |
| 838 | struct btrfs_root *root, |
| 839 | struct btrfs_path *path, |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 840 | struct btrfs_inode *dir, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 841 | struct btrfs_dir_item *di) |
| 842 | { |
| 843 | struct inode *inode; |
| 844 | char *name; |
| 845 | int name_len; |
| 846 | struct extent_buffer *leaf; |
| 847 | struct btrfs_key location; |
| 848 | int ret; |
| 849 | |
| 850 | leaf = path->nodes[0]; |
| 851 | |
| 852 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
| 853 | name_len = btrfs_dir_name_len(leaf, di); |
| 854 | name = kmalloc(name_len, GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 855 | if (!name) |
| 856 | return -ENOMEM; |
| 857 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 858 | read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 859 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 860 | |
| 861 | inode = read_one_inode(root, location.objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 862 | if (!inode) { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 863 | ret = -EIO; |
| 864 | goto out; |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 865 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 866 | |
Yan Zheng | ec051c0 | 2009-01-05 15:43:42 -0500 | [diff] [blame] | 867 | ret = link_to_fixup_dir(trans, root, path, location.objectid); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 868 | if (ret) |
| 869 | goto out; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 870 | |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 871 | ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name, |
| 872 | name_len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 873 | if (ret) |
| 874 | goto out; |
Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 875 | else |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 876 | ret = btrfs_run_delayed_items(trans); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 877 | out: |
| 878 | kfree(name); |
| 879 | iput(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 880 | return ret; |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * helper function to see if a given name and sequence number found |
| 885 | * in an inode back reference are already in a directory and correctly |
| 886 | * point to this inode |
| 887 | */ |
| 888 | static noinline int inode_in_dir(struct btrfs_root *root, |
| 889 | struct btrfs_path *path, |
| 890 | u64 dirid, u64 objectid, u64 index, |
| 891 | const char *name, int name_len) |
| 892 | { |
| 893 | struct btrfs_dir_item *di; |
| 894 | struct btrfs_key location; |
| 895 | int match = 0; |
| 896 | |
| 897 | di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, |
| 898 | index, name, name_len, 0); |
| 899 | if (di && !IS_ERR(di)) { |
| 900 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
| 901 | if (location.objectid != objectid) |
| 902 | goto out; |
| 903 | } else |
| 904 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 905 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 906 | |
| 907 | di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0); |
| 908 | if (di && !IS_ERR(di)) { |
| 909 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
| 910 | if (location.objectid != objectid) |
| 911 | goto out; |
| 912 | } else |
| 913 | goto out; |
| 914 | match = 1; |
| 915 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 916 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 917 | return match; |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * helper function to check a log tree for a named back reference in |
| 922 | * an inode. This is used to decide if a back reference that is |
| 923 | * found in the subvolume conflicts with what we find in the log. |
| 924 | * |
| 925 | * inode backreferences may have multiple refs in a single item, |
| 926 | * during replay we process one reference at a time, and we don't |
| 927 | * want to delete valid links to a file from the subvolume if that |
| 928 | * link is also in the log. |
| 929 | */ |
| 930 | static noinline int backref_in_log(struct btrfs_root *log, |
| 931 | struct btrfs_key *key, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 932 | u64 ref_objectid, |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 933 | const char *name, int namelen) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 934 | { |
| 935 | struct btrfs_path *path; |
| 936 | struct btrfs_inode_ref *ref; |
| 937 | unsigned long ptr; |
| 938 | unsigned long ptr_end; |
| 939 | unsigned long name_ptr; |
| 940 | int found_name_len; |
| 941 | int item_size; |
| 942 | int ret; |
| 943 | int match = 0; |
| 944 | |
| 945 | path = btrfs_alloc_path(); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 946 | if (!path) |
| 947 | return -ENOMEM; |
| 948 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 949 | ret = btrfs_search_slot(NULL, log, key, path, 0, 0); |
| 950 | if (ret != 0) |
| 951 | goto out; |
| 952 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 953 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 954 | |
| 955 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 956 | if (btrfs_find_name_in_ext_backref(path->nodes[0], |
| 957 | path->slots[0], |
| 958 | ref_objectid, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 959 | name, namelen, NULL)) |
| 960 | match = 1; |
| 961 | |
| 962 | goto out; |
| 963 | } |
| 964 | |
| 965 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 966 | ptr_end = ptr + item_size; |
| 967 | while (ptr < ptr_end) { |
| 968 | ref = (struct btrfs_inode_ref *)ptr; |
| 969 | found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref); |
| 970 | if (found_name_len == namelen) { |
| 971 | name_ptr = (unsigned long)(ref + 1); |
| 972 | ret = memcmp_extent_buffer(path->nodes[0], name, |
| 973 | name_ptr, namelen); |
| 974 | if (ret == 0) { |
| 975 | match = 1; |
| 976 | goto out; |
| 977 | } |
| 978 | } |
| 979 | ptr = (unsigned long)(ref + 1) + found_name_len; |
| 980 | } |
| 981 | out: |
| 982 | btrfs_free_path(path); |
| 983 | return match; |
| 984 | } |
| 985 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 986 | static inline int __add_inode_ref(struct btrfs_trans_handle *trans, |
| 987 | struct btrfs_root *root, |
| 988 | struct btrfs_path *path, |
| 989 | struct btrfs_root *log_root, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 990 | struct btrfs_inode *dir, |
| 991 | struct btrfs_inode *inode, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 992 | u64 inode_objectid, u64 parent_objectid, |
| 993 | u64 ref_index, char *name, int namelen, |
| 994 | int *search_done) |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 995 | { |
| 996 | int ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 997 | char *victim_name; |
| 998 | int victim_name_len; |
| 999 | struct extent_buffer *leaf; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1000 | struct btrfs_dir_item *di; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1001 | struct btrfs_key search_key; |
| 1002 | struct btrfs_inode_extref *extref; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1003 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1004 | again: |
| 1005 | /* Search old style refs */ |
| 1006 | search_key.objectid = inode_objectid; |
| 1007 | search_key.type = BTRFS_INODE_REF_KEY; |
| 1008 | search_key.offset = parent_objectid; |
| 1009 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1010 | if (ret == 0) { |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1011 | struct btrfs_inode_ref *victim_ref; |
| 1012 | unsigned long ptr; |
| 1013 | unsigned long ptr_end; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1014 | |
| 1015 | leaf = path->nodes[0]; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1016 | |
| 1017 | /* are we trying to overwrite a back ref for the root directory |
| 1018 | * if so, just jump out, we're done |
| 1019 | */ |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1020 | if (search_key.objectid == search_key.offset) |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1021 | return 1; |
| 1022 | |
| 1023 | /* check all the names in this back reference to see |
| 1024 | * if they are in the log. if so, we allow them to stay |
| 1025 | * otherwise they must be unlinked as a conflict |
| 1026 | */ |
| 1027 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1028 | ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1029 | while (ptr < ptr_end) { |
| 1030 | victim_ref = (struct btrfs_inode_ref *)ptr; |
| 1031 | victim_name_len = btrfs_inode_ref_name_len(leaf, |
| 1032 | victim_ref); |
| 1033 | victim_name = kmalloc(victim_name_len, GFP_NOFS); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1034 | if (!victim_name) |
| 1035 | return -ENOMEM; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1036 | |
| 1037 | read_extent_buffer(leaf, victim_name, |
| 1038 | (unsigned long)(victim_ref + 1), |
| 1039 | victim_name_len); |
| 1040 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1041 | if (!backref_in_log(log_root, &search_key, |
| 1042 | parent_objectid, |
| 1043 | victim_name, |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1044 | victim_name_len)) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1045 | inc_nlink(&inode->vfs_inode); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1046 | btrfs_release_path(path); |
| 1047 | |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1048 | ret = btrfs_unlink_inode(trans, root, dir, inode, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1049 | victim_name, victim_name_len); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1050 | kfree(victim_name); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1051 | if (ret) |
| 1052 | return ret; |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 1053 | ret = btrfs_run_delayed_items(trans); |
Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 1054 | if (ret) |
| 1055 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1056 | *search_done = 1; |
| 1057 | goto again; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1058 | } |
| 1059 | kfree(victim_name); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1060 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1061 | ptr = (unsigned long)(victim_ref + 1) + victim_name_len; |
| 1062 | } |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1063 | |
| 1064 | /* |
| 1065 | * NOTE: we have searched root tree and checked the |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 1066 | * corresponding ref, it does not need to check again. |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1067 | */ |
| 1068 | *search_done = 1; |
| 1069 | } |
| 1070 | btrfs_release_path(path); |
| 1071 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1072 | /* Same search but for extended refs */ |
| 1073 | extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen, |
| 1074 | inode_objectid, parent_objectid, 0, |
| 1075 | 0); |
| 1076 | if (!IS_ERR_OR_NULL(extref)) { |
| 1077 | u32 item_size; |
| 1078 | u32 cur_offset = 0; |
| 1079 | unsigned long base; |
| 1080 | struct inode *victim_parent; |
| 1081 | |
| 1082 | leaf = path->nodes[0]; |
| 1083 | |
| 1084 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1085 | base = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1086 | |
| 1087 | while (cur_offset < item_size) { |
Quentin Casasnovas | dd9ef13 | 2015-03-03 16:31:38 +0100 | [diff] [blame] | 1088 | extref = (struct btrfs_inode_extref *)(base + cur_offset); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1089 | |
| 1090 | victim_name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 1091 | |
| 1092 | if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid) |
| 1093 | goto next; |
| 1094 | |
| 1095 | victim_name = kmalloc(victim_name_len, GFP_NOFS); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1096 | if (!victim_name) |
| 1097 | return -ENOMEM; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1098 | read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name, |
| 1099 | victim_name_len); |
| 1100 | |
| 1101 | search_key.objectid = inode_objectid; |
| 1102 | search_key.type = BTRFS_INODE_EXTREF_KEY; |
| 1103 | search_key.offset = btrfs_extref_hash(parent_objectid, |
| 1104 | victim_name, |
| 1105 | victim_name_len); |
| 1106 | ret = 0; |
| 1107 | if (!backref_in_log(log_root, &search_key, |
| 1108 | parent_objectid, victim_name, |
| 1109 | victim_name_len)) { |
| 1110 | ret = -ENOENT; |
| 1111 | victim_parent = read_one_inode(root, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1112 | parent_objectid); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1113 | if (victim_parent) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1114 | inc_nlink(&inode->vfs_inode); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1115 | btrfs_release_path(path); |
| 1116 | |
| 1117 | ret = btrfs_unlink_inode(trans, root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1118 | BTRFS_I(victim_parent), |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1119 | inode, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1120 | victim_name, |
| 1121 | victim_name_len); |
Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 1122 | if (!ret) |
| 1123 | ret = btrfs_run_delayed_items( |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 1124 | trans); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1125 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1126 | iput(victim_parent); |
| 1127 | kfree(victim_name); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1128 | if (ret) |
| 1129 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1130 | *search_done = 1; |
| 1131 | goto again; |
| 1132 | } |
| 1133 | kfree(victim_name); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1134 | next: |
| 1135 | cur_offset += victim_name_len + sizeof(*extref); |
| 1136 | } |
| 1137 | *search_done = 1; |
| 1138 | } |
| 1139 | btrfs_release_path(path); |
| 1140 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1141 | /* look for a conflicting sequence number */ |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1142 | di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir), |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1143 | ref_index, name, namelen, 0); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1144 | if (di && !IS_ERR(di)) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1145 | ret = drop_one_dir_item(trans, root, path, dir, di); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1146 | if (ret) |
| 1147 | return ret; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1148 | } |
| 1149 | btrfs_release_path(path); |
| 1150 | |
| 1151 | /* look for a conflicing name */ |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1152 | di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1153 | name, namelen, 0); |
| 1154 | if (di && !IS_ERR(di)) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1155 | ret = drop_one_dir_item(trans, root, path, dir, di); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1156 | if (ret) |
| 1157 | return ret; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1158 | } |
| 1159 | btrfs_release_path(path); |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1163 | |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1164 | static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
| 1165 | u32 *namelen, char **name, u64 *index, |
| 1166 | u64 *parent_objectid) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1167 | { |
| 1168 | struct btrfs_inode_extref *extref; |
| 1169 | |
| 1170 | extref = (struct btrfs_inode_extref *)ref_ptr; |
| 1171 | |
| 1172 | *namelen = btrfs_inode_extref_name_len(eb, extref); |
| 1173 | *name = kmalloc(*namelen, GFP_NOFS); |
| 1174 | if (*name == NULL) |
| 1175 | return -ENOMEM; |
| 1176 | |
| 1177 | read_extent_buffer(eb, *name, (unsigned long)&extref->name, |
| 1178 | *namelen); |
| 1179 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1180 | if (index) |
| 1181 | *index = btrfs_inode_extref_index(eb, extref); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1182 | if (parent_objectid) |
| 1183 | *parent_objectid = btrfs_inode_extref_parent(eb, extref); |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1188 | static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
| 1189 | u32 *namelen, char **name, u64 *index) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1190 | { |
| 1191 | struct btrfs_inode_ref *ref; |
| 1192 | |
| 1193 | ref = (struct btrfs_inode_ref *)ref_ptr; |
| 1194 | |
| 1195 | *namelen = btrfs_inode_ref_name_len(eb, ref); |
| 1196 | *name = kmalloc(*namelen, GFP_NOFS); |
| 1197 | if (*name == NULL) |
| 1198 | return -ENOMEM; |
| 1199 | |
| 1200 | read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen); |
| 1201 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1202 | if (index) |
| 1203 | *index = btrfs_inode_ref_index(eb, ref); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1204 | |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1208 | /* |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1209 | * Take an inode reference item from the log tree and iterate all names from the |
| 1210 | * inode reference item in the subvolume tree with the same key (if it exists). |
| 1211 | * For any name that is not in the inode reference item from the log tree, do a |
| 1212 | * proper unlink of that name (that is, remove its entry from the inode |
| 1213 | * reference item and both dir index keys). |
| 1214 | */ |
| 1215 | static int unlink_old_inode_refs(struct btrfs_trans_handle *trans, |
| 1216 | struct btrfs_root *root, |
| 1217 | struct btrfs_path *path, |
| 1218 | struct btrfs_inode *inode, |
| 1219 | struct extent_buffer *log_eb, |
| 1220 | int log_slot, |
| 1221 | struct btrfs_key *key) |
| 1222 | { |
| 1223 | int ret; |
| 1224 | unsigned long ref_ptr; |
| 1225 | unsigned long ref_end; |
| 1226 | struct extent_buffer *eb; |
| 1227 | |
| 1228 | again: |
| 1229 | btrfs_release_path(path); |
| 1230 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 1231 | if (ret > 0) { |
| 1232 | ret = 0; |
| 1233 | goto out; |
| 1234 | } |
| 1235 | if (ret < 0) |
| 1236 | goto out; |
| 1237 | |
| 1238 | eb = path->nodes[0]; |
| 1239 | ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]); |
| 1240 | ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]); |
| 1241 | while (ref_ptr < ref_end) { |
| 1242 | char *name = NULL; |
| 1243 | int namelen; |
| 1244 | u64 parent_id; |
| 1245 | |
| 1246 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
| 1247 | ret = extref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1248 | NULL, &parent_id); |
| 1249 | } else { |
| 1250 | parent_id = key->offset; |
| 1251 | ret = ref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1252 | NULL); |
| 1253 | } |
| 1254 | if (ret) |
| 1255 | goto out; |
| 1256 | |
| 1257 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
| 1258 | ret = btrfs_find_name_in_ext_backref(log_eb, log_slot, |
| 1259 | parent_id, name, |
| 1260 | namelen, NULL); |
| 1261 | else |
| 1262 | ret = btrfs_find_name_in_backref(log_eb, log_slot, name, |
| 1263 | namelen, NULL); |
| 1264 | |
| 1265 | if (!ret) { |
| 1266 | struct inode *dir; |
| 1267 | |
| 1268 | btrfs_release_path(path); |
| 1269 | dir = read_one_inode(root, parent_id); |
| 1270 | if (!dir) { |
| 1271 | ret = -ENOENT; |
| 1272 | kfree(name); |
| 1273 | goto out; |
| 1274 | } |
| 1275 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 1276 | inode, name, namelen); |
| 1277 | kfree(name); |
| 1278 | iput(dir); |
| 1279 | if (ret) |
| 1280 | goto out; |
| 1281 | goto again; |
| 1282 | } |
| 1283 | |
| 1284 | kfree(name); |
| 1285 | ref_ptr += namelen; |
| 1286 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
| 1287 | ref_ptr += sizeof(struct btrfs_inode_extref); |
| 1288 | else |
| 1289 | ref_ptr += sizeof(struct btrfs_inode_ref); |
| 1290 | } |
| 1291 | ret = 0; |
| 1292 | out: |
| 1293 | btrfs_release_path(path); |
| 1294 | return ret; |
| 1295 | } |
| 1296 | |
Filipe Manana | 0d83639 | 2018-07-20 10:59:06 +0100 | [diff] [blame] | 1297 | static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir, |
| 1298 | const u8 ref_type, const char *name, |
| 1299 | const int namelen) |
| 1300 | { |
| 1301 | struct btrfs_key key; |
| 1302 | struct btrfs_path *path; |
| 1303 | const u64 parent_id = btrfs_ino(BTRFS_I(dir)); |
| 1304 | int ret; |
| 1305 | |
| 1306 | path = btrfs_alloc_path(); |
| 1307 | if (!path) |
| 1308 | return -ENOMEM; |
| 1309 | |
| 1310 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
| 1311 | key.type = ref_type; |
| 1312 | if (key.type == BTRFS_INODE_REF_KEY) |
| 1313 | key.offset = parent_id; |
| 1314 | else |
| 1315 | key.offset = btrfs_extref_hash(parent_id, name, namelen); |
| 1316 | |
| 1317 | ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0); |
| 1318 | if (ret < 0) |
| 1319 | goto out; |
| 1320 | if (ret > 0) { |
| 1321 | ret = 0; |
| 1322 | goto out; |
| 1323 | } |
| 1324 | if (key.type == BTRFS_INODE_EXTREF_KEY) |
| 1325 | ret = btrfs_find_name_in_ext_backref(path->nodes[0], |
| 1326 | path->slots[0], parent_id, |
| 1327 | name, namelen, NULL); |
| 1328 | else |
| 1329 | ret = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], |
| 1330 | name, namelen, NULL); |
| 1331 | |
| 1332 | out: |
| 1333 | btrfs_free_path(path); |
| 1334 | return ret; |
| 1335 | } |
| 1336 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1337 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1338 | * replay one inode back reference item found in the log tree. |
| 1339 | * eb, slot and key refer to the buffer and key found in the log tree. |
| 1340 | * root is the destination we are replaying into, and path is for temp |
| 1341 | * use by this function. (it should be released on return). |
| 1342 | */ |
| 1343 | static noinline int add_inode_ref(struct btrfs_trans_handle *trans, |
| 1344 | struct btrfs_root *root, |
| 1345 | struct btrfs_root *log, |
| 1346 | struct btrfs_path *path, |
| 1347 | struct extent_buffer *eb, int slot, |
| 1348 | struct btrfs_key *key) |
| 1349 | { |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1350 | struct inode *dir = NULL; |
| 1351 | struct inode *inode = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1352 | unsigned long ref_ptr; |
| 1353 | unsigned long ref_end; |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1354 | char *name = NULL; |
liubo | 34f3e4f | 2011-08-06 08:35:23 +0000 | [diff] [blame] | 1355 | int namelen; |
| 1356 | int ret; |
liubo | c622ae6 | 2011-03-26 08:01:12 -0400 | [diff] [blame] | 1357 | int search_done = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1358 | int log_ref_ver = 0; |
| 1359 | u64 parent_objectid; |
| 1360 | u64 inode_objectid; |
Chris Mason | f46dbe3 | 2012-10-09 11:17:20 -0400 | [diff] [blame] | 1361 | u64 ref_index = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1362 | int ref_struct_size; |
| 1363 | |
| 1364 | ref_ptr = btrfs_item_ptr_offset(eb, slot); |
| 1365 | ref_end = ref_ptr + btrfs_item_size_nr(eb, slot); |
| 1366 | |
| 1367 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
| 1368 | struct btrfs_inode_extref *r; |
| 1369 | |
| 1370 | ref_struct_size = sizeof(struct btrfs_inode_extref); |
| 1371 | log_ref_ver = 1; |
| 1372 | r = (struct btrfs_inode_extref *)ref_ptr; |
| 1373 | parent_objectid = btrfs_inode_extref_parent(eb, r); |
| 1374 | } else { |
| 1375 | ref_struct_size = sizeof(struct btrfs_inode_ref); |
| 1376 | parent_objectid = key->offset; |
| 1377 | } |
| 1378 | inode_objectid = key->objectid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1379 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1380 | /* |
| 1381 | * it is possible that we didn't log all the parent directories |
| 1382 | * for a given inode. If we don't find the dir, just don't |
| 1383 | * copy the back ref in. The link count fixup code will take |
| 1384 | * care of the rest |
| 1385 | */ |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1386 | dir = read_one_inode(root, parent_objectid); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1387 | if (!dir) { |
| 1388 | ret = -ENOENT; |
| 1389 | goto out; |
| 1390 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1391 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1392 | inode = read_one_inode(root, inode_objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1393 | if (!inode) { |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1394 | ret = -EIO; |
| 1395 | goto out; |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1396 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1397 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1398 | while (ref_ptr < ref_end) { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1399 | if (log_ref_ver) { |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1400 | ret = extref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1401 | &ref_index, &parent_objectid); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1402 | /* |
| 1403 | * parent object can change from one array |
| 1404 | * item to another. |
| 1405 | */ |
| 1406 | if (!dir) |
| 1407 | dir = read_one_inode(root, parent_objectid); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1408 | if (!dir) { |
| 1409 | ret = -ENOENT; |
| 1410 | goto out; |
| 1411 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1412 | } else { |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1413 | ret = ref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1414 | &ref_index); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1415 | } |
| 1416 | if (ret) |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1417 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1418 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1419 | /* if we already have a perfect match, we're done */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 1420 | if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)), |
| 1421 | btrfs_ino(BTRFS_I(inode)), ref_index, |
| 1422 | name, namelen)) { |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1423 | /* |
| 1424 | * look for a conflicting back reference in the |
| 1425 | * metadata. if we find one we have to unlink that name |
| 1426 | * of the file before we add our new link. Later on, we |
| 1427 | * overwrite any existing back reference, and we don't |
| 1428 | * want to create dangling pointers in the directory. |
| 1429 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1430 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1431 | if (!search_done) { |
| 1432 | ret = __add_inode_ref(trans, root, path, log, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1433 | BTRFS_I(dir), |
David Sterba | d75eefd | 2017-02-10 20:20:19 +0100 | [diff] [blame] | 1434 | BTRFS_I(inode), |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1435 | inode_objectid, |
| 1436 | parent_objectid, |
| 1437 | ref_index, name, namelen, |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1438 | &search_done); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1439 | if (ret) { |
| 1440 | if (ret == 1) |
| 1441 | ret = 0; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1442 | goto out; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1443 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1444 | } |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1445 | |
Filipe Manana | 0d83639 | 2018-07-20 10:59:06 +0100 | [diff] [blame] | 1446 | /* |
| 1447 | * If a reference item already exists for this inode |
| 1448 | * with the same parent and name, but different index, |
| 1449 | * drop it and the corresponding directory index entries |
| 1450 | * from the parent before adding the new reference item |
| 1451 | * and dir index entries, otherwise we would fail with |
| 1452 | * -EEXIST returned from btrfs_add_link() below. |
| 1453 | */ |
| 1454 | ret = btrfs_inode_ref_exists(inode, dir, key->type, |
| 1455 | name, namelen); |
| 1456 | if (ret > 0) { |
| 1457 | ret = btrfs_unlink_inode(trans, root, |
| 1458 | BTRFS_I(dir), |
| 1459 | BTRFS_I(inode), |
| 1460 | name, namelen); |
| 1461 | /* |
| 1462 | * If we dropped the link count to 0, bump it so |
| 1463 | * that later the iput() on the inode will not |
| 1464 | * free it. We will fixup the link count later. |
| 1465 | */ |
| 1466 | if (!ret && inode->i_nlink == 0) |
| 1467 | inc_nlink(inode); |
| 1468 | } |
| 1469 | if (ret < 0) |
| 1470 | goto out; |
| 1471 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1472 | /* insert our name */ |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 1473 | ret = btrfs_add_link(trans, BTRFS_I(dir), |
| 1474 | BTRFS_I(inode), |
| 1475 | name, namelen, 0, ref_index); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1476 | if (ret) |
| 1477 | goto out; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1478 | |
| 1479 | btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1480 | } |
liubo | c622ae6 | 2011-03-26 08:01:12 -0400 | [diff] [blame] | 1481 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1482 | ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1483 | kfree(name); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1484 | name = NULL; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1485 | if (log_ref_ver) { |
| 1486 | iput(dir); |
| 1487 | dir = NULL; |
| 1488 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1489 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1490 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1491 | /* |
| 1492 | * Before we overwrite the inode reference item in the subvolume tree |
| 1493 | * with the item from the log tree, we must unlink all names from the |
| 1494 | * parent directory that are in the subvolume's tree inode reference |
| 1495 | * item, otherwise we end up with an inconsistent subvolume tree where |
| 1496 | * dir index entries exist for a name but there is no inode reference |
| 1497 | * item with the same name. |
| 1498 | */ |
| 1499 | ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot, |
| 1500 | key); |
| 1501 | if (ret) |
| 1502 | goto out; |
| 1503 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1504 | /* finally write the back reference in the inode */ |
| 1505 | ret = overwrite_item(trans, root, path, eb, slot, key); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1506 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1507 | btrfs_release_path(path); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1508 | kfree(name); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1509 | iput(dir); |
| 1510 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1511 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1512 | } |
| 1513 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1514 | static int insert_orphan_item(struct btrfs_trans_handle *trans, |
David Sterba | 9c4f61f | 2015-01-02 19:12:57 +0100 | [diff] [blame] | 1515 | struct btrfs_root *root, u64 ino) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1516 | { |
| 1517 | int ret; |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 1518 | |
David Sterba | 9c4f61f | 2015-01-02 19:12:57 +0100 | [diff] [blame] | 1519 | ret = btrfs_insert_orphan_item(trans, root, ino); |
| 1520 | if (ret == -EEXIST) |
| 1521 | ret = 0; |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 1522 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1523 | return ret; |
| 1524 | } |
| 1525 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1526 | static int count_inode_extrefs(struct btrfs_root *root, |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1527 | struct btrfs_inode *inode, struct btrfs_path *path) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1528 | { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1529 | int ret = 0; |
| 1530 | int name_len; |
| 1531 | unsigned int nlink = 0; |
| 1532 | u32 item_size; |
| 1533 | u32 cur_offset = 0; |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1534 | u64 inode_objectid = btrfs_ino(inode); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1535 | u64 offset = 0; |
| 1536 | unsigned long ptr; |
| 1537 | struct btrfs_inode_extref *extref; |
| 1538 | struct extent_buffer *leaf; |
| 1539 | |
| 1540 | while (1) { |
| 1541 | ret = btrfs_find_one_extref(root, inode_objectid, offset, path, |
| 1542 | &extref, &offset); |
| 1543 | if (ret) |
| 1544 | break; |
| 1545 | |
| 1546 | leaf = path->nodes[0]; |
| 1547 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1548 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 1549 | cur_offset = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1550 | |
| 1551 | while (cur_offset < item_size) { |
| 1552 | extref = (struct btrfs_inode_extref *) (ptr + cur_offset); |
| 1553 | name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 1554 | |
| 1555 | nlink++; |
| 1556 | |
| 1557 | cur_offset += name_len + sizeof(*extref); |
| 1558 | } |
| 1559 | |
| 1560 | offset++; |
| 1561 | btrfs_release_path(path); |
| 1562 | } |
| 1563 | btrfs_release_path(path); |
| 1564 | |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 1565 | if (ret < 0 && ret != -ENOENT) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1566 | return ret; |
| 1567 | return nlink; |
| 1568 | } |
| 1569 | |
| 1570 | static int count_inode_refs(struct btrfs_root *root, |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1571 | struct btrfs_inode *inode, struct btrfs_path *path) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1572 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1573 | int ret; |
| 1574 | struct btrfs_key key; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1575 | unsigned int nlink = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1576 | unsigned long ptr; |
| 1577 | unsigned long ptr_end; |
| 1578 | int name_len; |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1579 | u64 ino = btrfs_ino(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1580 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1581 | key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1582 | key.type = BTRFS_INODE_REF_KEY; |
| 1583 | key.offset = (u64)-1; |
| 1584 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1585 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1586 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1587 | if (ret < 0) |
| 1588 | break; |
| 1589 | if (ret > 0) { |
| 1590 | if (path->slots[0] == 0) |
| 1591 | break; |
| 1592 | path->slots[0]--; |
| 1593 | } |
Filipe David Borba Manana | e93ae26 | 2013-10-14 22:49:11 +0100 | [diff] [blame] | 1594 | process_slot: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1595 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1596 | path->slots[0]); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1597 | if (key.objectid != ino || |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1598 | key.type != BTRFS_INODE_REF_KEY) |
| 1599 | break; |
| 1600 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
| 1601 | ptr_end = ptr + btrfs_item_size_nr(path->nodes[0], |
| 1602 | path->slots[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1603 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1604 | struct btrfs_inode_ref *ref; |
| 1605 | |
| 1606 | ref = (struct btrfs_inode_ref *)ptr; |
| 1607 | name_len = btrfs_inode_ref_name_len(path->nodes[0], |
| 1608 | ref); |
| 1609 | ptr = (unsigned long)(ref + 1) + name_len; |
| 1610 | nlink++; |
| 1611 | } |
| 1612 | |
| 1613 | if (key.offset == 0) |
| 1614 | break; |
Filipe David Borba Manana | e93ae26 | 2013-10-14 22:49:11 +0100 | [diff] [blame] | 1615 | if (path->slots[0] > 0) { |
| 1616 | path->slots[0]--; |
| 1617 | goto process_slot; |
| 1618 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1619 | key.offset--; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1620 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1621 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1622 | btrfs_release_path(path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1623 | |
| 1624 | return nlink; |
| 1625 | } |
| 1626 | |
| 1627 | /* |
| 1628 | * There are a few corners where the link count of the file can't |
| 1629 | * be properly maintained during replay. So, instead of adding |
| 1630 | * lots of complexity to the log code, we just scan the backrefs |
| 1631 | * for any file that has been through replay. |
| 1632 | * |
| 1633 | * The scan will update the link count on the inode to reflect the |
| 1634 | * number of back refs found. If it goes down to zero, the iput |
| 1635 | * will free the inode. |
| 1636 | */ |
| 1637 | static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, |
| 1638 | struct btrfs_root *root, |
| 1639 | struct inode *inode) |
| 1640 | { |
| 1641 | struct btrfs_path *path; |
| 1642 | int ret; |
| 1643 | u64 nlink = 0; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1644 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1645 | |
| 1646 | path = btrfs_alloc_path(); |
| 1647 | if (!path) |
| 1648 | return -ENOMEM; |
| 1649 | |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1650 | ret = count_inode_refs(root, BTRFS_I(inode), path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1651 | if (ret < 0) |
| 1652 | goto out; |
| 1653 | |
| 1654 | nlink = ret; |
| 1655 | |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1656 | ret = count_inode_extrefs(root, BTRFS_I(inode), path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1657 | if (ret < 0) |
| 1658 | goto out; |
| 1659 | |
| 1660 | nlink += ret; |
| 1661 | |
| 1662 | ret = 0; |
| 1663 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1664 | if (nlink != inode->i_nlink) { |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 1665 | set_nlink(inode, nlink); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1666 | btrfs_update_inode(trans, root, inode); |
| 1667 | } |
Chris Mason | 8d5bf1c | 2008-09-11 15:51:21 -0400 | [diff] [blame] | 1668 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1669 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1670 | if (inode->i_nlink == 0) { |
| 1671 | if (S_ISDIR(inode->i_mode)) { |
| 1672 | ret = replay_dir_deletes(trans, root, NULL, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1673 | ino, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1674 | if (ret) |
| 1675 | goto out; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1676 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1677 | ret = insert_orphan_item(trans, root, ino); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1678 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1679 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1680 | out: |
| 1681 | btrfs_free_path(path); |
| 1682 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, |
| 1686 | struct btrfs_root *root, |
| 1687 | struct btrfs_path *path) |
| 1688 | { |
| 1689 | int ret; |
| 1690 | struct btrfs_key key; |
| 1691 | struct inode *inode; |
| 1692 | |
| 1693 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; |
| 1694 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
| 1695 | key.offset = (u64)-1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1696 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1697 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1698 | if (ret < 0) |
| 1699 | break; |
| 1700 | |
| 1701 | if (ret == 1) { |
| 1702 | if (path->slots[0] == 0) |
| 1703 | break; |
| 1704 | path->slots[0]--; |
| 1705 | } |
| 1706 | |
| 1707 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 1708 | if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || |
| 1709 | key.type != BTRFS_ORPHAN_ITEM_KEY) |
| 1710 | break; |
| 1711 | |
| 1712 | ret = btrfs_del_item(trans, root, path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1713 | if (ret) |
| 1714 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1715 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1716 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1717 | inode = read_one_inode(root, key.offset); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1718 | if (!inode) |
| 1719 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1720 | |
| 1721 | ret = fixup_inode_link_count(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1722 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1723 | if (ret) |
| 1724 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1725 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1726 | /* |
| 1727 | * fixup on a directory may create new entries, |
| 1728 | * make sure we always look for the highset possible |
| 1729 | * offset |
| 1730 | */ |
| 1731 | key.offset = (u64)-1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1732 | } |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1733 | ret = 0; |
| 1734 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1735 | btrfs_release_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1736 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | |
| 1740 | /* |
| 1741 | * record a given inode in the fixup dir so we can check its link |
| 1742 | * count when replay is done. The link count is incremented here |
| 1743 | * so the inode won't go away until we check it |
| 1744 | */ |
| 1745 | static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, |
| 1746 | struct btrfs_root *root, |
| 1747 | struct btrfs_path *path, |
| 1748 | u64 objectid) |
| 1749 | { |
| 1750 | struct btrfs_key key; |
| 1751 | int ret = 0; |
| 1752 | struct inode *inode; |
| 1753 | |
| 1754 | inode = read_one_inode(root, objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1755 | if (!inode) |
| 1756 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1757 | |
| 1758 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1759 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1760 | key.offset = objectid; |
| 1761 | |
| 1762 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
| 1763 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1764 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1765 | if (ret == 0) { |
Josef Bacik | 9bf7a48 | 2013-03-01 13:35:47 -0500 | [diff] [blame] | 1766 | if (!inode->i_nlink) |
| 1767 | set_nlink(inode, 1); |
| 1768 | else |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 1769 | inc_nlink(inode); |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 1770 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1771 | } else if (ret == -EEXIST) { |
| 1772 | ret = 0; |
| 1773 | } else { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1774 | BUG(); /* Logic Error */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1775 | } |
| 1776 | iput(inode); |
| 1777 | |
| 1778 | return ret; |
| 1779 | } |
| 1780 | |
| 1781 | /* |
| 1782 | * when replaying the log for a directory, we only insert names |
| 1783 | * for inodes that actually exist. This means an fsync on a directory |
| 1784 | * does not implicitly fsync all the new files in it |
| 1785 | */ |
| 1786 | static noinline int insert_one_name(struct btrfs_trans_handle *trans, |
| 1787 | struct btrfs_root *root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1788 | u64 dirid, u64 index, |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 1789 | char *name, int name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1790 | struct btrfs_key *location) |
| 1791 | { |
| 1792 | struct inode *inode; |
| 1793 | struct inode *dir; |
| 1794 | int ret; |
| 1795 | |
| 1796 | inode = read_one_inode(root, location->objectid); |
| 1797 | if (!inode) |
| 1798 | return -ENOENT; |
| 1799 | |
| 1800 | dir = read_one_inode(root, dirid); |
| 1801 | if (!dir) { |
| 1802 | iput(inode); |
| 1803 | return -EIO; |
| 1804 | } |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1805 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 1806 | ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name, |
| 1807 | name_len, 1, index); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1808 | |
| 1809 | /* FIXME, put inode into FIXUP list */ |
| 1810 | |
| 1811 | iput(inode); |
| 1812 | iput(dir); |
| 1813 | return ret; |
| 1814 | } |
| 1815 | |
| 1816 | /* |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1817 | * Return true if an inode reference exists in the log for the given name, |
| 1818 | * inode and parent inode. |
| 1819 | */ |
| 1820 | static bool name_in_log_ref(struct btrfs_root *log_root, |
| 1821 | const char *name, const int name_len, |
| 1822 | const u64 dirid, const u64 ino) |
| 1823 | { |
| 1824 | struct btrfs_key search_key; |
| 1825 | |
| 1826 | search_key.objectid = ino; |
| 1827 | search_key.type = BTRFS_INODE_REF_KEY; |
| 1828 | search_key.offset = dirid; |
| 1829 | if (backref_in_log(log_root, &search_key, dirid, name, name_len)) |
| 1830 | return true; |
| 1831 | |
| 1832 | search_key.type = BTRFS_INODE_EXTREF_KEY; |
| 1833 | search_key.offset = btrfs_extref_hash(dirid, name, name_len); |
| 1834 | if (backref_in_log(log_root, &search_key, dirid, name, name_len)) |
| 1835 | return true; |
| 1836 | |
| 1837 | return false; |
| 1838 | } |
| 1839 | |
| 1840 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1841 | * take a single entry in a log directory item and replay it into |
| 1842 | * the subvolume. |
| 1843 | * |
| 1844 | * if a conflicting item exists in the subdirectory already, |
| 1845 | * the inode it points to is unlinked and put into the link count |
| 1846 | * fix up tree. |
| 1847 | * |
| 1848 | * If a name from the log points to a file or directory that does |
| 1849 | * not exist in the FS, it is skipped. fsyncs on directories |
| 1850 | * do not force down inodes inside that directory, just changes to the |
| 1851 | * names or unlinks in a directory. |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1852 | * |
| 1853 | * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a |
| 1854 | * non-existing inode) and 1 if the name was replayed. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1855 | */ |
| 1856 | static noinline int replay_one_name(struct btrfs_trans_handle *trans, |
| 1857 | struct btrfs_root *root, |
| 1858 | struct btrfs_path *path, |
| 1859 | struct extent_buffer *eb, |
| 1860 | struct btrfs_dir_item *di, |
| 1861 | struct btrfs_key *key) |
| 1862 | { |
| 1863 | char *name; |
| 1864 | int name_len; |
| 1865 | struct btrfs_dir_item *dst_di; |
| 1866 | struct btrfs_key found_key; |
| 1867 | struct btrfs_key log_key; |
| 1868 | struct inode *dir; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1869 | u8 log_type; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1870 | int exists; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1871 | int ret = 0; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1872 | bool update_size = (key->type == BTRFS_DIR_INDEX_KEY); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1873 | bool name_added = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1874 | |
| 1875 | dir = read_one_inode(root, key->objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1876 | if (!dir) |
| 1877 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1878 | |
| 1879 | name_len = btrfs_dir_name_len(eb, di); |
| 1880 | name = kmalloc(name_len, GFP_NOFS); |
Filipe David Borba Manana | 2bac325 | 2013-08-04 19:58:57 +0100 | [diff] [blame] | 1881 | if (!name) { |
| 1882 | ret = -ENOMEM; |
| 1883 | goto out; |
| 1884 | } |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 1885 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1886 | log_type = btrfs_dir_type(eb, di); |
| 1887 | read_extent_buffer(eb, name, (unsigned long)(di + 1), |
| 1888 | name_len); |
| 1889 | |
| 1890 | btrfs_dir_item_key_to_cpu(eb, di, &log_key); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1891 | exists = btrfs_lookup_inode(trans, root, path, &log_key, 0); |
| 1892 | if (exists == 0) |
| 1893 | exists = 1; |
| 1894 | else |
| 1895 | exists = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1896 | btrfs_release_path(path); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1897 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1898 | if (key->type == BTRFS_DIR_ITEM_KEY) { |
| 1899 | dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, |
| 1900 | name, name_len, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1901 | } else if (key->type == BTRFS_DIR_INDEX_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1902 | dst_di = btrfs_lookup_dir_index_item(trans, root, path, |
| 1903 | key->objectid, |
| 1904 | key->offset, name, |
| 1905 | name_len, 1); |
| 1906 | } else { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1907 | /* Corruption */ |
| 1908 | ret = -EINVAL; |
| 1909 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1910 | } |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 1911 | if (IS_ERR_OR_NULL(dst_di)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1912 | /* we need a sequence number to insert, so we only |
| 1913 | * do inserts for the BTRFS_DIR_INDEX_KEY types |
| 1914 | */ |
| 1915 | if (key->type != BTRFS_DIR_INDEX_KEY) |
| 1916 | goto out; |
| 1917 | goto insert; |
| 1918 | } |
| 1919 | |
| 1920 | btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); |
| 1921 | /* the existing item matches the logged item */ |
| 1922 | if (found_key.objectid == log_key.objectid && |
| 1923 | found_key.type == log_key.type && |
| 1924 | found_key.offset == log_key.offset && |
| 1925 | btrfs_dir_type(path->nodes[0], dst_di) == log_type) { |
Filipe Manana | a2cc11d | 2014-09-08 22:53:18 +0100 | [diff] [blame] | 1926 | update_size = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1927 | goto out; |
| 1928 | } |
| 1929 | |
| 1930 | /* |
| 1931 | * don't drop the conflicting directory entry if the inode |
| 1932 | * for the new entry doesn't exist |
| 1933 | */ |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1934 | if (!exists) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1935 | goto out; |
| 1936 | |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 1937 | ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1938 | if (ret) |
| 1939 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1940 | |
| 1941 | if (key->type == BTRFS_DIR_INDEX_KEY) |
| 1942 | goto insert; |
| 1943 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1944 | btrfs_release_path(path); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1945 | if (!ret && update_size) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 1946 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1947 | ret = btrfs_update_inode(trans, root, dir); |
| 1948 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1949 | kfree(name); |
| 1950 | iput(dir); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1951 | if (!ret && name_added) |
| 1952 | ret = 1; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1953 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1954 | |
| 1955 | insert: |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1956 | if (name_in_log_ref(root->log_root, name, name_len, |
| 1957 | key->objectid, log_key.objectid)) { |
| 1958 | /* The dentry will be added later. */ |
| 1959 | ret = 0; |
| 1960 | update_size = false; |
| 1961 | goto out; |
| 1962 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1963 | btrfs_release_path(path); |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 1964 | ret = insert_one_name(trans, root, key->objectid, key->offset, |
| 1965 | name, name_len, &log_key); |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1966 | if (ret && ret != -ENOENT && ret != -EEXIST) |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1967 | goto out; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1968 | if (!ret) |
| 1969 | name_added = true; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1970 | update_size = false; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1971 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1972 | goto out; |
| 1973 | } |
| 1974 | |
| 1975 | /* |
| 1976 | * find all the names in a directory item and reconcile them into |
| 1977 | * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than |
| 1978 | * one name in a directory item, but the same code gets used for |
| 1979 | * both directory index types |
| 1980 | */ |
| 1981 | static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, |
| 1982 | struct btrfs_root *root, |
| 1983 | struct btrfs_path *path, |
| 1984 | struct extent_buffer *eb, int slot, |
| 1985 | struct btrfs_key *key) |
| 1986 | { |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1987 | int ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1988 | u32 item_size = btrfs_item_size_nr(eb, slot); |
| 1989 | struct btrfs_dir_item *di; |
| 1990 | int name_len; |
| 1991 | unsigned long ptr; |
| 1992 | unsigned long ptr_end; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1993 | struct btrfs_path *fixup_path = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1994 | |
| 1995 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 1996 | ptr_end = ptr + item_size; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1997 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1998 | di = (struct btrfs_dir_item *)ptr; |
| 1999 | name_len = btrfs_dir_name_len(eb, di); |
| 2000 | ret = replay_one_name(trans, root, path, eb, di, key); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 2001 | if (ret < 0) |
| 2002 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2003 | ptr = (unsigned long)(di + 1); |
| 2004 | ptr += name_len; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 2005 | |
| 2006 | /* |
| 2007 | * If this entry refers to a non-directory (directories can not |
| 2008 | * have a link count > 1) and it was added in the transaction |
| 2009 | * that was not committed, make sure we fixup the link count of |
| 2010 | * the inode it the entry points to. Otherwise something like |
| 2011 | * the following would result in a directory pointing to an |
| 2012 | * inode with a wrong link that does not account for this dir |
| 2013 | * entry: |
| 2014 | * |
| 2015 | * mkdir testdir |
| 2016 | * touch testdir/foo |
| 2017 | * touch testdir/bar |
| 2018 | * sync |
| 2019 | * |
| 2020 | * ln testdir/bar testdir/bar_link |
| 2021 | * ln testdir/foo testdir/foo_link |
| 2022 | * xfs_io -c "fsync" testdir/bar |
| 2023 | * |
| 2024 | * <power failure> |
| 2025 | * |
| 2026 | * mount fs, log replay happens |
| 2027 | * |
| 2028 | * File foo would remain with a link count of 1 when it has two |
| 2029 | * entries pointing to it in the directory testdir. This would |
| 2030 | * make it impossible to ever delete the parent directory has |
| 2031 | * it would result in stale dentries that can never be deleted. |
| 2032 | */ |
| 2033 | if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) { |
| 2034 | struct btrfs_key di_key; |
| 2035 | |
| 2036 | if (!fixup_path) { |
| 2037 | fixup_path = btrfs_alloc_path(); |
| 2038 | if (!fixup_path) { |
| 2039 | ret = -ENOMEM; |
| 2040 | break; |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 2045 | ret = link_to_fixup_dir(trans, root, fixup_path, |
| 2046 | di_key.objectid); |
| 2047 | if (ret) |
| 2048 | break; |
| 2049 | } |
| 2050 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2051 | } |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 2052 | btrfs_free_path(fixup_path); |
| 2053 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | /* |
| 2057 | * directory replay has two parts. There are the standard directory |
| 2058 | * items in the log copied from the subvolume, and range items |
| 2059 | * created in the log while the subvolume was logged. |
| 2060 | * |
| 2061 | * The range items tell us which parts of the key space the log |
| 2062 | * is authoritative for. During replay, if a key in the subvolume |
| 2063 | * directory is in a logged range item, but not actually in the log |
| 2064 | * that means it was deleted from the directory before the fsync |
| 2065 | * and should be removed. |
| 2066 | */ |
| 2067 | static noinline int find_dir_range(struct btrfs_root *root, |
| 2068 | struct btrfs_path *path, |
| 2069 | u64 dirid, int key_type, |
| 2070 | u64 *start_ret, u64 *end_ret) |
| 2071 | { |
| 2072 | struct btrfs_key key; |
| 2073 | u64 found_end; |
| 2074 | struct btrfs_dir_log_item *item; |
| 2075 | int ret; |
| 2076 | int nritems; |
| 2077 | |
| 2078 | if (*start_ret == (u64)-1) |
| 2079 | return 1; |
| 2080 | |
| 2081 | key.objectid = dirid; |
| 2082 | key.type = key_type; |
| 2083 | key.offset = *start_ret; |
| 2084 | |
| 2085 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2086 | if (ret < 0) |
| 2087 | goto out; |
| 2088 | if (ret > 0) { |
| 2089 | if (path->slots[0] == 0) |
| 2090 | goto out; |
| 2091 | path->slots[0]--; |
| 2092 | } |
| 2093 | if (ret != 0) |
| 2094 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 2095 | |
| 2096 | if (key.type != key_type || key.objectid != dirid) { |
| 2097 | ret = 1; |
| 2098 | goto next; |
| 2099 | } |
| 2100 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2101 | struct btrfs_dir_log_item); |
| 2102 | found_end = btrfs_dir_log_end(path->nodes[0], item); |
| 2103 | |
| 2104 | if (*start_ret >= key.offset && *start_ret <= found_end) { |
| 2105 | ret = 0; |
| 2106 | *start_ret = key.offset; |
| 2107 | *end_ret = found_end; |
| 2108 | goto out; |
| 2109 | } |
| 2110 | ret = 1; |
| 2111 | next: |
| 2112 | /* check the next slot in the tree to see if it is a valid item */ |
| 2113 | nritems = btrfs_header_nritems(path->nodes[0]); |
Robbie Ko | 2a7bf53 | 2016-10-07 17:30:47 +0800 | [diff] [blame] | 2114 | path->slots[0]++; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2115 | if (path->slots[0] >= nritems) { |
| 2116 | ret = btrfs_next_leaf(root, path); |
| 2117 | if (ret) |
| 2118 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 2122 | |
| 2123 | if (key.type != key_type || key.objectid != dirid) { |
| 2124 | ret = 1; |
| 2125 | goto out; |
| 2126 | } |
| 2127 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2128 | struct btrfs_dir_log_item); |
| 2129 | found_end = btrfs_dir_log_end(path->nodes[0], item); |
| 2130 | *start_ret = key.offset; |
| 2131 | *end_ret = found_end; |
| 2132 | ret = 0; |
| 2133 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2134 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2135 | return ret; |
| 2136 | } |
| 2137 | |
| 2138 | /* |
| 2139 | * this looks for a given directory item in the log. If the directory |
| 2140 | * item is not in the log, the item is removed and the inode it points |
| 2141 | * to is unlinked |
| 2142 | */ |
| 2143 | static noinline int check_item_in_log(struct btrfs_trans_handle *trans, |
| 2144 | struct btrfs_root *root, |
| 2145 | struct btrfs_root *log, |
| 2146 | struct btrfs_path *path, |
| 2147 | struct btrfs_path *log_path, |
| 2148 | struct inode *dir, |
| 2149 | struct btrfs_key *dir_key) |
| 2150 | { |
| 2151 | int ret; |
| 2152 | struct extent_buffer *eb; |
| 2153 | int slot; |
| 2154 | u32 item_size; |
| 2155 | struct btrfs_dir_item *di; |
| 2156 | struct btrfs_dir_item *log_di; |
| 2157 | int name_len; |
| 2158 | unsigned long ptr; |
| 2159 | unsigned long ptr_end; |
| 2160 | char *name; |
| 2161 | struct inode *inode; |
| 2162 | struct btrfs_key location; |
| 2163 | |
| 2164 | again: |
| 2165 | eb = path->nodes[0]; |
| 2166 | slot = path->slots[0]; |
| 2167 | item_size = btrfs_item_size_nr(eb, slot); |
| 2168 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 2169 | ptr_end = ptr + item_size; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2170 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2171 | di = (struct btrfs_dir_item *)ptr; |
| 2172 | name_len = btrfs_dir_name_len(eb, di); |
| 2173 | name = kmalloc(name_len, GFP_NOFS); |
| 2174 | if (!name) { |
| 2175 | ret = -ENOMEM; |
| 2176 | goto out; |
| 2177 | } |
| 2178 | read_extent_buffer(eb, name, (unsigned long)(di + 1), |
| 2179 | name_len); |
| 2180 | log_di = NULL; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2181 | if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2182 | log_di = btrfs_lookup_dir_item(trans, log, log_path, |
| 2183 | dir_key->objectid, |
| 2184 | name, name_len, 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2185 | } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2186 | log_di = btrfs_lookup_dir_index_item(trans, log, |
| 2187 | log_path, |
| 2188 | dir_key->objectid, |
| 2189 | dir_key->offset, |
| 2190 | name, name_len, 0); |
| 2191 | } |
Al Viro | 8d9e220 | 2018-07-29 23:04:46 +0100 | [diff] [blame] | 2192 | if (!log_di || log_di == ERR_PTR(-ENOENT)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2193 | btrfs_dir_item_key_to_cpu(eb, di, &location); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2194 | btrfs_release_path(path); |
| 2195 | btrfs_release_path(log_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2196 | inode = read_one_inode(root, location.objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 2197 | if (!inode) { |
| 2198 | kfree(name); |
| 2199 | return -EIO; |
| 2200 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2201 | |
| 2202 | ret = link_to_fixup_dir(trans, root, |
| 2203 | path, location.objectid); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2204 | if (ret) { |
| 2205 | kfree(name); |
| 2206 | iput(inode); |
| 2207 | goto out; |
| 2208 | } |
| 2209 | |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 2210 | inc_nlink(inode); |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 2211 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 2212 | BTRFS_I(inode), name, name_len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2213 | if (!ret) |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 2214 | ret = btrfs_run_delayed_items(trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2215 | kfree(name); |
| 2216 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2217 | if (ret) |
| 2218 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2219 | |
| 2220 | /* there might still be more names under this key |
| 2221 | * check and repeat if required |
| 2222 | */ |
| 2223 | ret = btrfs_search_slot(NULL, root, dir_key, path, |
| 2224 | 0, 0); |
| 2225 | if (ret == 0) |
| 2226 | goto again; |
| 2227 | ret = 0; |
| 2228 | goto out; |
Filipe David Borba Manana | 269d040 | 2013-10-28 17:39:21 +0000 | [diff] [blame] | 2229 | } else if (IS_ERR(log_di)) { |
| 2230 | kfree(name); |
| 2231 | return PTR_ERR(log_di); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2232 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2233 | btrfs_release_path(log_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2234 | kfree(name); |
| 2235 | |
| 2236 | ptr = (unsigned long)(di + 1); |
| 2237 | ptr += name_len; |
| 2238 | } |
| 2239 | ret = 0; |
| 2240 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2241 | btrfs_release_path(path); |
| 2242 | btrfs_release_path(log_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2243 | return ret; |
| 2244 | } |
| 2245 | |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2246 | static int replay_xattr_deletes(struct btrfs_trans_handle *trans, |
| 2247 | struct btrfs_root *root, |
| 2248 | struct btrfs_root *log, |
| 2249 | struct btrfs_path *path, |
| 2250 | const u64 ino) |
| 2251 | { |
| 2252 | struct btrfs_key search_key; |
| 2253 | struct btrfs_path *log_path; |
| 2254 | int i; |
| 2255 | int nritems; |
| 2256 | int ret; |
| 2257 | |
| 2258 | log_path = btrfs_alloc_path(); |
| 2259 | if (!log_path) |
| 2260 | return -ENOMEM; |
| 2261 | |
| 2262 | search_key.objectid = ino; |
| 2263 | search_key.type = BTRFS_XATTR_ITEM_KEY; |
| 2264 | search_key.offset = 0; |
| 2265 | again: |
| 2266 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
| 2267 | if (ret < 0) |
| 2268 | goto out; |
| 2269 | process_leaf: |
| 2270 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2271 | for (i = path->slots[0]; i < nritems; i++) { |
| 2272 | struct btrfs_key key; |
| 2273 | struct btrfs_dir_item *di; |
| 2274 | struct btrfs_dir_item *log_di; |
| 2275 | u32 total_size; |
| 2276 | u32 cur; |
| 2277 | |
| 2278 | btrfs_item_key_to_cpu(path->nodes[0], &key, i); |
| 2279 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) { |
| 2280 | ret = 0; |
| 2281 | goto out; |
| 2282 | } |
| 2283 | |
| 2284 | di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item); |
| 2285 | total_size = btrfs_item_size_nr(path->nodes[0], i); |
| 2286 | cur = 0; |
| 2287 | while (cur < total_size) { |
| 2288 | u16 name_len = btrfs_dir_name_len(path->nodes[0], di); |
| 2289 | u16 data_len = btrfs_dir_data_len(path->nodes[0], di); |
| 2290 | u32 this_len = sizeof(*di) + name_len + data_len; |
| 2291 | char *name; |
| 2292 | |
| 2293 | name = kmalloc(name_len, GFP_NOFS); |
| 2294 | if (!name) { |
| 2295 | ret = -ENOMEM; |
| 2296 | goto out; |
| 2297 | } |
| 2298 | read_extent_buffer(path->nodes[0], name, |
| 2299 | (unsigned long)(di + 1), name_len); |
| 2300 | |
| 2301 | log_di = btrfs_lookup_xattr(NULL, log, log_path, ino, |
| 2302 | name, name_len, 0); |
| 2303 | btrfs_release_path(log_path); |
| 2304 | if (!log_di) { |
| 2305 | /* Doesn't exist in log tree, so delete it. */ |
| 2306 | btrfs_release_path(path); |
| 2307 | di = btrfs_lookup_xattr(trans, root, path, ino, |
| 2308 | name, name_len, -1); |
| 2309 | kfree(name); |
| 2310 | if (IS_ERR(di)) { |
| 2311 | ret = PTR_ERR(di); |
| 2312 | goto out; |
| 2313 | } |
| 2314 | ASSERT(di); |
| 2315 | ret = btrfs_delete_one_dir_name(trans, root, |
| 2316 | path, di); |
| 2317 | if (ret) |
| 2318 | goto out; |
| 2319 | btrfs_release_path(path); |
| 2320 | search_key = key; |
| 2321 | goto again; |
| 2322 | } |
| 2323 | kfree(name); |
| 2324 | if (IS_ERR(log_di)) { |
| 2325 | ret = PTR_ERR(log_di); |
| 2326 | goto out; |
| 2327 | } |
| 2328 | cur += this_len; |
| 2329 | di = (struct btrfs_dir_item *)((char *)di + this_len); |
| 2330 | } |
| 2331 | } |
| 2332 | ret = btrfs_next_leaf(root, path); |
| 2333 | if (ret > 0) |
| 2334 | ret = 0; |
| 2335 | else if (ret == 0) |
| 2336 | goto process_leaf; |
| 2337 | out: |
| 2338 | btrfs_free_path(log_path); |
| 2339 | btrfs_release_path(path); |
| 2340 | return ret; |
| 2341 | } |
| 2342 | |
| 2343 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2344 | /* |
| 2345 | * deletion replay happens before we copy any new directory items |
| 2346 | * out of the log or out of backreferences from inodes. It |
| 2347 | * scans the log to find ranges of keys that log is authoritative for, |
| 2348 | * and then scans the directory to find items in those ranges that are |
| 2349 | * not present in the log. |
| 2350 | * |
| 2351 | * Anything we don't find in the log is unlinked and removed from the |
| 2352 | * directory. |
| 2353 | */ |
| 2354 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, |
| 2355 | struct btrfs_root *root, |
| 2356 | struct btrfs_root *log, |
| 2357 | struct btrfs_path *path, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2358 | u64 dirid, int del_all) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2359 | { |
| 2360 | u64 range_start; |
| 2361 | u64 range_end; |
| 2362 | int key_type = BTRFS_DIR_LOG_ITEM_KEY; |
| 2363 | int ret = 0; |
| 2364 | struct btrfs_key dir_key; |
| 2365 | struct btrfs_key found_key; |
| 2366 | struct btrfs_path *log_path; |
| 2367 | struct inode *dir; |
| 2368 | |
| 2369 | dir_key.objectid = dirid; |
| 2370 | dir_key.type = BTRFS_DIR_ITEM_KEY; |
| 2371 | log_path = btrfs_alloc_path(); |
| 2372 | if (!log_path) |
| 2373 | return -ENOMEM; |
| 2374 | |
| 2375 | dir = read_one_inode(root, dirid); |
| 2376 | /* it isn't an error if the inode isn't there, that can happen |
| 2377 | * because we replay the deletes before we copy in the inode item |
| 2378 | * from the log |
| 2379 | */ |
| 2380 | if (!dir) { |
| 2381 | btrfs_free_path(log_path); |
| 2382 | return 0; |
| 2383 | } |
| 2384 | again: |
| 2385 | range_start = 0; |
| 2386 | range_end = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2387 | while (1) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2388 | if (del_all) |
| 2389 | range_end = (u64)-1; |
| 2390 | else { |
| 2391 | ret = find_dir_range(log, path, dirid, key_type, |
| 2392 | &range_start, &range_end); |
| 2393 | if (ret != 0) |
| 2394 | break; |
| 2395 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2396 | |
| 2397 | dir_key.offset = range_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2398 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2399 | int nritems; |
| 2400 | ret = btrfs_search_slot(NULL, root, &dir_key, path, |
| 2401 | 0, 0); |
| 2402 | if (ret < 0) |
| 2403 | goto out; |
| 2404 | |
| 2405 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2406 | if (path->slots[0] >= nritems) { |
| 2407 | ret = btrfs_next_leaf(root, path); |
Liu Bo | b98def7 | 2018-04-03 01:59:48 +0800 | [diff] [blame] | 2408 | if (ret == 1) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2409 | break; |
Liu Bo | b98def7 | 2018-04-03 01:59:48 +0800 | [diff] [blame] | 2410 | else if (ret < 0) |
| 2411 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2412 | } |
| 2413 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2414 | path->slots[0]); |
| 2415 | if (found_key.objectid != dirid || |
| 2416 | found_key.type != dir_key.type) |
| 2417 | goto next_type; |
| 2418 | |
| 2419 | if (found_key.offset > range_end) |
| 2420 | break; |
| 2421 | |
| 2422 | ret = check_item_in_log(trans, root, log, path, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2423 | log_path, dir, |
| 2424 | &found_key); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2425 | if (ret) |
| 2426 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2427 | if (found_key.offset == (u64)-1) |
| 2428 | break; |
| 2429 | dir_key.offset = found_key.offset + 1; |
| 2430 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2431 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2432 | if (range_end == (u64)-1) |
| 2433 | break; |
| 2434 | range_start = range_end + 1; |
| 2435 | } |
| 2436 | |
| 2437 | next_type: |
| 2438 | ret = 0; |
| 2439 | if (key_type == BTRFS_DIR_LOG_ITEM_KEY) { |
| 2440 | key_type = BTRFS_DIR_LOG_INDEX_KEY; |
| 2441 | dir_key.type = BTRFS_DIR_INDEX_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2442 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2443 | goto again; |
| 2444 | } |
| 2445 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2446 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2447 | btrfs_free_path(log_path); |
| 2448 | iput(dir); |
| 2449 | return ret; |
| 2450 | } |
| 2451 | |
| 2452 | /* |
| 2453 | * the process_func used to replay items from the log tree. This |
| 2454 | * gets called in two different stages. The first stage just looks |
| 2455 | * for inodes and makes sure they are all copied into the subvolume. |
| 2456 | * |
| 2457 | * The second stage copies all the other item types from the log into |
| 2458 | * the subvolume. The two stage approach is slower, but gets rid of |
| 2459 | * lots of complexity around inodes referencing other inodes that exist |
| 2460 | * only in the log (references come from either directory items or inode |
| 2461 | * back refs). |
| 2462 | */ |
| 2463 | 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] | 2464 | struct walk_control *wc, u64 gen, int level) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2465 | { |
| 2466 | int nritems; |
| 2467 | struct btrfs_path *path; |
| 2468 | struct btrfs_root *root = wc->replay_dest; |
| 2469 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2470 | int i; |
| 2471 | int ret; |
| 2472 | |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2473 | ret = btrfs_read_buffer(eb, gen, level, NULL); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2474 | if (ret) |
| 2475 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2476 | |
| 2477 | level = btrfs_header_level(eb); |
| 2478 | |
| 2479 | if (level != 0) |
| 2480 | return 0; |
| 2481 | |
| 2482 | path = btrfs_alloc_path(); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2483 | if (!path) |
| 2484 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2485 | |
| 2486 | nritems = btrfs_header_nritems(eb); |
| 2487 | for (i = 0; i < nritems; i++) { |
| 2488 | btrfs_item_key_to_cpu(eb, &key, i); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2489 | |
| 2490 | /* inode keys are done during the first stage */ |
| 2491 | if (key.type == BTRFS_INODE_ITEM_KEY && |
| 2492 | wc->stage == LOG_WALK_REPLAY_INODES) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2493 | struct btrfs_inode_item *inode_item; |
| 2494 | u32 mode; |
| 2495 | |
| 2496 | inode_item = btrfs_item_ptr(eb, i, |
| 2497 | struct btrfs_inode_item); |
Filipe Manana | 55f21e1 | 2018-10-08 11:12:55 +0100 | [diff] [blame] | 2498 | /* |
| 2499 | * If we have a tmpfile (O_TMPFILE) that got fsync'ed |
| 2500 | * and never got linked before the fsync, skip it, as |
| 2501 | * replaying it is pointless since it would be deleted |
| 2502 | * later. We skip logging tmpfiles, but it's always |
| 2503 | * possible we are replaying a log created with a kernel |
| 2504 | * that used to log tmpfiles. |
| 2505 | */ |
| 2506 | if (btrfs_inode_nlink(eb, inode_item) == 0) { |
| 2507 | wc->ignore_cur_inode = true; |
| 2508 | continue; |
| 2509 | } else { |
| 2510 | wc->ignore_cur_inode = false; |
| 2511 | } |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2512 | ret = replay_xattr_deletes(wc->trans, root, log, |
| 2513 | path, key.objectid); |
| 2514 | if (ret) |
| 2515 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2516 | mode = btrfs_inode_mode(eb, inode_item); |
| 2517 | if (S_ISDIR(mode)) { |
| 2518 | ret = replay_dir_deletes(wc->trans, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2519 | root, log, path, key.objectid, 0); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2520 | if (ret) |
| 2521 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2522 | } |
| 2523 | ret = overwrite_item(wc->trans, root, path, |
| 2524 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2525 | if (ret) |
| 2526 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2527 | |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2528 | /* |
| 2529 | * Before replaying extents, truncate the inode to its |
| 2530 | * size. We need to do it now and not after log replay |
| 2531 | * because before an fsync we can have prealloc extents |
| 2532 | * added beyond the inode's i_size. If we did it after, |
| 2533 | * through orphan cleanup for example, we would drop |
| 2534 | * those prealloc extents just after replaying them. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2535 | */ |
| 2536 | if (S_ISREG(mode)) { |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2537 | struct inode *inode; |
| 2538 | u64 from; |
| 2539 | |
| 2540 | inode = read_one_inode(root, key.objectid); |
| 2541 | if (!inode) { |
| 2542 | ret = -EIO; |
| 2543 | break; |
| 2544 | } |
| 2545 | from = ALIGN(i_size_read(inode), |
| 2546 | root->fs_info->sectorsize); |
| 2547 | ret = btrfs_drop_extents(wc->trans, root, inode, |
| 2548 | from, (u64)-1, 1); |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2549 | if (!ret) { |
Filipe Manana | 55f21e1 | 2018-10-08 11:12:55 +0100 | [diff] [blame] | 2550 | /* Update the inode's nbytes. */ |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2551 | ret = btrfs_update_inode(wc->trans, |
| 2552 | root, inode); |
| 2553 | } |
| 2554 | iput(inode); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2555 | if (ret) |
| 2556 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2557 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2558 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2559 | ret = link_to_fixup_dir(wc->trans, root, |
| 2560 | path, key.objectid); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2561 | if (ret) |
| 2562 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2563 | } |
Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2564 | |
Filipe Manana | 55f21e1 | 2018-10-08 11:12:55 +0100 | [diff] [blame] | 2565 | if (wc->ignore_cur_inode) |
| 2566 | continue; |
| 2567 | |
Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2568 | if (key.type == BTRFS_DIR_INDEX_KEY && |
| 2569 | wc->stage == LOG_WALK_REPLAY_DIR_INDEX) { |
| 2570 | ret = replay_one_dir_item(wc->trans, root, path, |
| 2571 | eb, i, &key); |
| 2572 | if (ret) |
| 2573 | break; |
| 2574 | } |
| 2575 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2576 | if (wc->stage < LOG_WALK_REPLAY_ALL) |
| 2577 | continue; |
| 2578 | |
| 2579 | /* these keys are simply copied */ |
| 2580 | if (key.type == BTRFS_XATTR_ITEM_KEY) { |
| 2581 | ret = overwrite_item(wc->trans, root, path, |
| 2582 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2583 | if (ret) |
| 2584 | break; |
Liu Bo | 2da1c66 | 2013-05-26 13:50:29 +0000 | [diff] [blame] | 2585 | } else if (key.type == BTRFS_INODE_REF_KEY || |
| 2586 | key.type == BTRFS_INODE_EXTREF_KEY) { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 2587 | ret = add_inode_ref(wc->trans, root, log, path, |
| 2588 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2589 | if (ret && ret != -ENOENT) |
| 2590 | break; |
| 2591 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2592 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 2593 | ret = replay_one_extent(wc->trans, root, path, |
| 2594 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2595 | if (ret) |
| 2596 | break; |
Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2597 | } else if (key.type == BTRFS_DIR_ITEM_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2598 | ret = replay_one_dir_item(wc->trans, root, path, |
| 2599 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2600 | if (ret) |
| 2601 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2602 | } |
| 2603 | } |
| 2604 | btrfs_free_path(path); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2605 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2606 | } |
| 2607 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2608 | static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2609 | struct btrfs_root *root, |
| 2610 | struct btrfs_path *path, int *level, |
| 2611 | struct walk_control *wc) |
| 2612 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2613 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2614 | u64 root_owner; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2615 | u64 bytenr; |
| 2616 | u64 ptr_gen; |
| 2617 | struct extent_buffer *next; |
| 2618 | struct extent_buffer *cur; |
| 2619 | struct extent_buffer *parent; |
| 2620 | u32 blocksize; |
| 2621 | int ret = 0; |
| 2622 | |
| 2623 | WARN_ON(*level < 0); |
| 2624 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 2625 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2626 | while (*level > 0) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2627 | struct btrfs_key first_key; |
| 2628 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2629 | WARN_ON(*level < 0); |
| 2630 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 2631 | cur = path->nodes[*level]; |
| 2632 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2633 | WARN_ON(btrfs_header_level(cur) != *level); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2634 | |
| 2635 | if (path->slots[*level] >= |
| 2636 | btrfs_header_nritems(cur)) |
| 2637 | break; |
| 2638 | |
| 2639 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 2640 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2641 | btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2642 | blocksize = fs_info->nodesize; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2643 | |
| 2644 | parent = path->nodes[*level]; |
| 2645 | root_owner = btrfs_header_owner(parent); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2646 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2647 | next = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 2648 | if (IS_ERR(next)) |
| 2649 | return PTR_ERR(next); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2650 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2651 | if (*level == 1) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2652 | ret = wc->process_func(root, next, wc, ptr_gen, |
| 2653 | *level - 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2654 | if (ret) { |
| 2655 | free_extent_buffer(next); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2656 | return ret; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2657 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2658 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2659 | path->slots[*level]++; |
| 2660 | if (wc->free) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2661 | ret = btrfs_read_buffer(next, ptr_gen, |
| 2662 | *level - 1, &first_key); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2663 | if (ret) { |
| 2664 | free_extent_buffer(next); |
| 2665 | return ret; |
| 2666 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2667 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2668 | if (trans) { |
| 2669 | btrfs_tree_lock(next); |
| 2670 | btrfs_set_lock_blocking(next); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2671 | clean_tree_block(fs_info, next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2672 | btrfs_wait_tree_block_writeback(next); |
| 2673 | btrfs_tree_unlock(next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2674 | } else { |
| 2675 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) |
| 2676 | clear_extent_buffer_dirty(next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2677 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2678 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2679 | WARN_ON(root_owner != |
| 2680 | BTRFS_TREE_LOG_OBJECTID); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2681 | ret = btrfs_free_and_pin_reserved_extent( |
| 2682 | fs_info, bytenr, |
| 2683 | blocksize); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2684 | if (ret) { |
| 2685 | free_extent_buffer(next); |
| 2686 | return ret; |
| 2687 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2688 | } |
| 2689 | free_extent_buffer(next); |
| 2690 | continue; |
| 2691 | } |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2692 | ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2693 | if (ret) { |
| 2694 | free_extent_buffer(next); |
| 2695 | return ret; |
| 2696 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2697 | |
| 2698 | WARN_ON(*level <= 0); |
| 2699 | if (path->nodes[*level-1]) |
| 2700 | free_extent_buffer(path->nodes[*level-1]); |
| 2701 | path->nodes[*level-1] = next; |
| 2702 | *level = btrfs_header_level(next); |
| 2703 | path->slots[*level] = 0; |
| 2704 | cond_resched(); |
| 2705 | } |
| 2706 | WARN_ON(*level < 0); |
| 2707 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 2708 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2709 | path->slots[*level] = btrfs_header_nritems(path->nodes[*level]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2710 | |
| 2711 | cond_resched(); |
| 2712 | return 0; |
| 2713 | } |
| 2714 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2715 | static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2716 | struct btrfs_root *root, |
| 2717 | struct btrfs_path *path, int *level, |
| 2718 | struct walk_control *wc) |
| 2719 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2720 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2721 | u64 root_owner; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2722 | int i; |
| 2723 | int slot; |
| 2724 | int ret; |
| 2725 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2726 | for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2727 | slot = path->slots[i]; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2728 | if (slot + 1 < btrfs_header_nritems(path->nodes[i])) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2729 | path->slots[i]++; |
| 2730 | *level = i; |
| 2731 | WARN_ON(*level == 0); |
| 2732 | return 0; |
| 2733 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2734 | struct extent_buffer *parent; |
| 2735 | if (path->nodes[*level] == root->node) |
| 2736 | parent = path->nodes[*level]; |
| 2737 | else |
| 2738 | parent = path->nodes[*level + 1]; |
| 2739 | |
| 2740 | root_owner = btrfs_header_owner(parent); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2741 | ret = wc->process_func(root, path->nodes[*level], wc, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2742 | btrfs_header_generation(path->nodes[*level]), |
| 2743 | *level); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2744 | if (ret) |
| 2745 | return ret; |
| 2746 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2747 | if (wc->free) { |
| 2748 | struct extent_buffer *next; |
| 2749 | |
| 2750 | next = path->nodes[*level]; |
| 2751 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2752 | if (trans) { |
| 2753 | btrfs_tree_lock(next); |
| 2754 | btrfs_set_lock_blocking(next); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2755 | clean_tree_block(fs_info, next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2756 | btrfs_wait_tree_block_writeback(next); |
| 2757 | btrfs_tree_unlock(next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2758 | } else { |
| 2759 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) |
| 2760 | clear_extent_buffer_dirty(next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2761 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2762 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2763 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2764 | ret = btrfs_free_and_pin_reserved_extent( |
| 2765 | fs_info, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2766 | path->nodes[*level]->start, |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2767 | path->nodes[*level]->len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2768 | if (ret) |
| 2769 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2770 | } |
| 2771 | free_extent_buffer(path->nodes[*level]); |
| 2772 | path->nodes[*level] = NULL; |
| 2773 | *level = i + 1; |
| 2774 | } |
| 2775 | } |
| 2776 | return 1; |
| 2777 | } |
| 2778 | |
| 2779 | /* |
| 2780 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 2781 | * the tree freeing any blocks that have a ref count of zero after being |
| 2782 | * decremented. |
| 2783 | */ |
| 2784 | static int walk_log_tree(struct btrfs_trans_handle *trans, |
| 2785 | struct btrfs_root *log, struct walk_control *wc) |
| 2786 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2787 | struct btrfs_fs_info *fs_info = log->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2788 | int ret = 0; |
| 2789 | int wret; |
| 2790 | int level; |
| 2791 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2792 | int orig_level; |
| 2793 | |
| 2794 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 2795 | if (!path) |
| 2796 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2797 | |
| 2798 | level = btrfs_header_level(log->node); |
| 2799 | orig_level = level; |
| 2800 | path->nodes[level] = log->node; |
| 2801 | extent_buffer_get(log->node); |
| 2802 | path->slots[level] = 0; |
| 2803 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2804 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2805 | wret = walk_down_log_tree(trans, log, path, &level, wc); |
| 2806 | if (wret > 0) |
| 2807 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2808 | if (wret < 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2809 | ret = wret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2810 | goto out; |
| 2811 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2812 | |
| 2813 | wret = walk_up_log_tree(trans, log, path, &level, wc); |
| 2814 | if (wret > 0) |
| 2815 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2816 | if (wret < 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2817 | ret = wret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2818 | goto out; |
| 2819 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | /* was the root node processed? if not, catch it here */ |
| 2823 | if (path->nodes[orig_level]) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2824 | ret = wc->process_func(log, path->nodes[orig_level], wc, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2825 | btrfs_header_generation(path->nodes[orig_level]), |
| 2826 | orig_level); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2827 | if (ret) |
| 2828 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2829 | if (wc->free) { |
| 2830 | struct extent_buffer *next; |
| 2831 | |
| 2832 | next = path->nodes[orig_level]; |
| 2833 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2834 | if (trans) { |
| 2835 | btrfs_tree_lock(next); |
| 2836 | btrfs_set_lock_blocking(next); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2837 | clean_tree_block(fs_info, next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2838 | btrfs_wait_tree_block_writeback(next); |
| 2839 | btrfs_tree_unlock(next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2840 | } else { |
| 2841 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) |
| 2842 | clear_extent_buffer_dirty(next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2843 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2844 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2845 | WARN_ON(log->root_key.objectid != |
| 2846 | BTRFS_TREE_LOG_OBJECTID); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2847 | ret = btrfs_free_and_pin_reserved_extent(fs_info, |
| 2848 | next->start, next->len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2849 | if (ret) |
| 2850 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2851 | } |
| 2852 | } |
| 2853 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2854 | out: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2855 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2856 | return ret; |
| 2857 | } |
| 2858 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2859 | /* |
| 2860 | * helper function to update the item for a given subvolumes log root |
| 2861 | * in the tree of log roots |
| 2862 | */ |
| 2863 | static int update_log_root(struct btrfs_trans_handle *trans, |
Josef Bacik | f7313de | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 2864 | struct btrfs_root *log, |
| 2865 | struct btrfs_root_item *root_item) |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2866 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2867 | struct btrfs_fs_info *fs_info = log->fs_info; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2868 | int ret; |
| 2869 | |
| 2870 | if (log->log_transid == 1) { |
| 2871 | /* insert root item on the first sync */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2872 | ret = btrfs_insert_root(trans, fs_info->log_root_tree, |
Josef Bacik | f7313de | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 2873 | &log->root_key, root_item); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2874 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2875 | ret = btrfs_update_root(trans, fs_info->log_root_tree, |
Josef Bacik | f7313de | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 2876 | &log->root_key, root_item); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2877 | } |
| 2878 | return ret; |
| 2879 | } |
| 2880 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2881 | static void wait_log_commit(struct btrfs_root *root, int transid) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2882 | { |
| 2883 | DEFINE_WAIT(wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2884 | int index = transid % 2; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2885 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2886 | /* |
| 2887 | * we only allow two pending log transactions at a time, |
| 2888 | * so we know that if ours is more than 2 older than the |
| 2889 | * current transaction, we're done |
| 2890 | */ |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2891 | for (;;) { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2892 | prepare_to_wait(&root->log_commit_wait[index], |
| 2893 | &wait, TASK_UNINTERRUPTIBLE); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2894 | |
| 2895 | if (!(root->log_transid_committed < transid && |
| 2896 | atomic_read(&root->log_commit[index]))) |
| 2897 | break; |
| 2898 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2899 | mutex_unlock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2900 | schedule(); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2901 | mutex_lock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2902 | } |
| 2903 | finish_wait(&root->log_commit_wait[index], &wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2904 | } |
| 2905 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2906 | static void wait_for_writer(struct btrfs_root *root) |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2907 | { |
| 2908 | DEFINE_WAIT(wait); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2909 | |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2910 | for (;;) { |
| 2911 | prepare_to_wait(&root->log_writer_wait, &wait, |
| 2912 | TASK_UNINTERRUPTIBLE); |
| 2913 | if (!atomic_read(&root->log_writers)) |
| 2914 | break; |
| 2915 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2916 | mutex_unlock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2917 | schedule(); |
Filipe Manana | 575849e | 2015-02-11 11:12:39 +0000 | [diff] [blame] | 2918 | mutex_lock(&root->log_mutex); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2919 | } |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2920 | finish_wait(&root->log_writer_wait, &wait); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2921 | } |
| 2922 | |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2923 | static inline void btrfs_remove_log_ctx(struct btrfs_root *root, |
| 2924 | struct btrfs_log_ctx *ctx) |
| 2925 | { |
| 2926 | if (!ctx) |
| 2927 | return; |
| 2928 | |
| 2929 | mutex_lock(&root->log_mutex); |
| 2930 | list_del_init(&ctx->list); |
| 2931 | mutex_unlock(&root->log_mutex); |
| 2932 | } |
| 2933 | |
| 2934 | /* |
| 2935 | * Invoked in log mutex context, or be sure there is no other task which |
| 2936 | * can access the list. |
| 2937 | */ |
| 2938 | static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root, |
| 2939 | int index, int error) |
| 2940 | { |
| 2941 | struct btrfs_log_ctx *ctx; |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2942 | struct btrfs_log_ctx *safe; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2943 | |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2944 | list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) { |
| 2945 | list_del_init(&ctx->list); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2946 | ctx->log_ret = error; |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2947 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2948 | |
| 2949 | INIT_LIST_HEAD(&root->log_ctxs[index]); |
| 2950 | } |
| 2951 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2952 | /* |
| 2953 | * btrfs_sync_log does sends a given tree log down to the disk and |
| 2954 | * updates the super blocks to record it. When this call is done, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2955 | * you know that any inodes previously logged are safely on disk only |
| 2956 | * if it returns 0. |
| 2957 | * |
| 2958 | * Any other return value means you need to call btrfs_commit_transaction. |
| 2959 | * Some of the edge cases for fsyncing directories that have had unlinks |
| 2960 | * or renames done in the past mean that sometimes the only safe |
| 2961 | * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN, |
| 2962 | * that has happened. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2963 | */ |
| 2964 | int btrfs_sync_log(struct btrfs_trans_handle *trans, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2965 | struct btrfs_root *root, struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2966 | { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2967 | int index1; |
| 2968 | int index2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2969 | int mark; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2970 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2971 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2972 | struct btrfs_root *log = root->log_root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2973 | struct btrfs_root *log_root_tree = fs_info->log_root_tree; |
Josef Bacik | f7313de | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 2974 | struct btrfs_root_item new_root_item; |
Miao Xie | bb14a59 | 2014-02-20 18:08:56 +0800 | [diff] [blame] | 2975 | int log_transid = 0; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2976 | struct btrfs_log_ctx root_log_ctx; |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2977 | struct blk_plug plug; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2978 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2979 | mutex_lock(&root->log_mutex); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2980 | log_transid = ctx->log_transid; |
| 2981 | if (root->log_transid_committed >= log_transid) { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2982 | mutex_unlock(&root->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2983 | return ctx->log_ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2984 | } |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2985 | |
| 2986 | index1 = log_transid % 2; |
| 2987 | if (atomic_read(&root->log_commit[index1])) { |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2988 | wait_log_commit(root, log_transid); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2989 | mutex_unlock(&root->log_mutex); |
| 2990 | return ctx->log_ret; |
| 2991 | } |
| 2992 | ASSERT(log_transid == root->log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2993 | atomic_set(&root->log_commit[index1], 1); |
| 2994 | |
| 2995 | /* wait for previous tree log sync to complete */ |
| 2996 | if (atomic_read(&root->log_commit[(index1 + 1) % 2])) |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2997 | wait_log_commit(root, log_transid - 1); |
Miao Xie | 48cab2e | 2014-02-20 18:08:52 +0800 | [diff] [blame] | 2998 | |
Yan, Zheng | 86df7eb | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2999 | while (1) { |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 3000 | int batch = atomic_read(&root->log_batch); |
Chris Mason | cd354ad | 2011-10-20 15:45:37 -0400 | [diff] [blame] | 3001 | /* when we're on an ssd, just kick the log commit out */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3002 | if (!btrfs_test_opt(fs_info, SSD) && |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3003 | test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) { |
Yan, Zheng | 86df7eb | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 3004 | mutex_unlock(&root->log_mutex); |
| 3005 | schedule_timeout_uninterruptible(1); |
| 3006 | mutex_lock(&root->log_mutex); |
| 3007 | } |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3008 | wait_for_writer(root); |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 3009 | if (batch == atomic_read(&root->log_batch)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3010 | break; |
| 3011 | } |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3012 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3013 | /* bail out if we need to do a full commit */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3014 | if (btrfs_need_log_full_commit(fs_info, trans)) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3015 | ret = -EAGAIN; |
| 3016 | mutex_unlock(&root->log_mutex); |
| 3017 | goto out; |
| 3018 | } |
| 3019 | |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 3020 | if (log_transid % 2 == 0) |
| 3021 | mark = EXTENT_DIRTY; |
| 3022 | else |
| 3023 | mark = EXTENT_NEW; |
| 3024 | |
Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 3025 | /* we start IO on all the marked extents here, but we don't actually |
| 3026 | * wait for them until later. |
| 3027 | */ |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3028 | blk_start_plug(&plug); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3029 | ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3030 | if (ret) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3031 | blk_finish_plug(&plug); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3032 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3033 | btrfs_set_log_full_commit(fs_info, trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3034 | mutex_unlock(&root->log_mutex); |
| 3035 | goto out; |
| 3036 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3037 | |
Josef Bacik | f7313de | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 3038 | /* |
| 3039 | * We _must_ update under the root->log_mutex in order to make sure we |
| 3040 | * have a consistent view of the log root we are trying to commit at |
| 3041 | * this moment. |
| 3042 | * |
| 3043 | * We _must_ copy this into a local copy, because we are not holding the |
| 3044 | * log_root_tree->log_mutex yet. This is important because when we |
| 3045 | * commit the log_root_tree we must have a consistent view of the |
| 3046 | * log_root_tree when we update the super block to point at the |
| 3047 | * log_root_tree bytenr. If we update the log_root_tree here we'll race |
| 3048 | * with the commit and possibly point at the new block which we may not |
| 3049 | * have written out. |
| 3050 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3051 | btrfs_set_root_node(&log->root_item, log->node); |
Josef Bacik | f7313de | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 3052 | memcpy(&new_root_item, &log->root_item, sizeof(new_root_item)); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3053 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3054 | root->log_transid++; |
| 3055 | log->log_transid = root->log_transid; |
Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 3056 | root->log_start_pid = 0; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3057 | /* |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 3058 | * IO has been started, blocks of the log tree have WRITTEN flag set |
| 3059 | * in their headers. new modifications of the log will be written to |
| 3060 | * 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] | 3061 | */ |
| 3062 | mutex_unlock(&root->log_mutex); |
| 3063 | |
Filipe Manana | 28a2359 | 2016-08-23 21:13:51 +0100 | [diff] [blame] | 3064 | btrfs_init_log_ctx(&root_log_ctx, NULL); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3065 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3066 | mutex_lock(&log_root_tree->log_mutex); |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 3067 | atomic_inc(&log_root_tree->log_batch); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3068 | atomic_inc(&log_root_tree->log_writers); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3069 | |
| 3070 | index2 = log_root_tree->log_transid % 2; |
| 3071 | list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]); |
| 3072 | root_log_ctx.log_transid = log_root_tree->log_transid; |
| 3073 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3074 | mutex_unlock(&log_root_tree->log_mutex); |
| 3075 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3076 | mutex_lock(&log_root_tree->log_mutex); |
Josef Bacik | f7313de | 2019-09-30 16:27:25 -0400 | [diff] [blame] | 3077 | |
| 3078 | /* |
| 3079 | * Now we are safe to update the log_root_tree because we're under the |
| 3080 | * log_mutex, and we're a current writer so we're holding the commit |
| 3081 | * open until we drop the log_mutex. |
| 3082 | */ |
| 3083 | ret = update_log_root(trans, log, &new_root_item); |
| 3084 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3085 | if (atomic_dec_and_test(&log_root_tree->log_writers)) { |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3086 | /* atomic_dec_and_test implies a barrier */ |
| 3087 | cond_wake_up_nomb(&log_root_tree->log_writer_wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3088 | } |
| 3089 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3090 | if (ret) { |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3091 | if (!list_empty(&root_log_ctx.list)) |
| 3092 | list_del_init(&root_log_ctx.list); |
| 3093 | |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3094 | blk_finish_plug(&plug); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3095 | btrfs_set_log_full_commit(fs_info, trans); |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 3096 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3097 | if (ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3098 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3099 | mutex_unlock(&log_root_tree->log_mutex); |
| 3100 | goto out; |
| 3101 | } |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3102 | btrfs_wait_tree_log_extents(log, mark); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3103 | mutex_unlock(&log_root_tree->log_mutex); |
| 3104 | ret = -EAGAIN; |
| 3105 | goto out; |
| 3106 | } |
| 3107 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3108 | if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) { |
Forrest Liu | 3da5ab5 | 2015-01-30 19:42:12 +0800 | [diff] [blame] | 3109 | blk_finish_plug(&plug); |
Chris Mason | cbd60aa | 2016-09-06 05:37:40 -0700 | [diff] [blame] | 3110 | list_del_init(&root_log_ctx.list); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3111 | mutex_unlock(&log_root_tree->log_mutex); |
| 3112 | ret = root_log_ctx.log_ret; |
| 3113 | goto out; |
| 3114 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3115 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3116 | index2 = root_log_ctx.log_transid % 2; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3117 | if (atomic_read(&log_root_tree->log_commit[index2])) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3118 | blk_finish_plug(&plug); |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3119 | ret = btrfs_wait_tree_log_extents(log, mark); |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3120 | wait_log_commit(log_root_tree, |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3121 | root_log_ctx.log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3122 | mutex_unlock(&log_root_tree->log_mutex); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3123 | if (!ret) |
| 3124 | ret = root_log_ctx.log_ret; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3125 | goto out; |
| 3126 | } |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3127 | ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3128 | atomic_set(&log_root_tree->log_commit[index2], 1); |
| 3129 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3130 | if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) { |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3131 | wait_log_commit(log_root_tree, |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3132 | root_log_ctx.log_transid - 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3133 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3134 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3135 | wait_for_writer(log_root_tree); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3136 | |
| 3137 | /* |
| 3138 | * now that we've moved on to the tree of log tree roots, |
| 3139 | * check the full commit flag again |
| 3140 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3141 | if (btrfs_need_log_full_commit(fs_info, trans)) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3142 | blk_finish_plug(&plug); |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3143 | btrfs_wait_tree_log_extents(log, mark); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3144 | mutex_unlock(&log_root_tree->log_mutex); |
| 3145 | ret = -EAGAIN; |
| 3146 | goto out_wake_log_root; |
| 3147 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3148 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3149 | ret = btrfs_write_marked_extents(fs_info, |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3150 | &log_root_tree->dirty_log_pages, |
| 3151 | EXTENT_DIRTY | EXTENT_NEW); |
| 3152 | blk_finish_plug(&plug); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3153 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3154 | btrfs_set_log_full_commit(fs_info, trans); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3155 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3156 | mutex_unlock(&log_root_tree->log_mutex); |
| 3157 | goto out_wake_log_root; |
| 3158 | } |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3159 | ret = btrfs_wait_tree_log_extents(log, mark); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3160 | if (!ret) |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3161 | ret = btrfs_wait_tree_log_extents(log_root_tree, |
| 3162 | EXTENT_NEW | EXTENT_DIRTY); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3163 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3164 | btrfs_set_log_full_commit(fs_info, trans); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3165 | mutex_unlock(&log_root_tree->log_mutex); |
| 3166 | goto out_wake_log_root; |
| 3167 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3168 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3169 | btrfs_set_super_log_root(fs_info->super_for_commit, |
| 3170 | log_root_tree->node->start); |
| 3171 | btrfs_set_super_log_root_level(fs_info->super_for_commit, |
| 3172 | btrfs_header_level(log_root_tree->node)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3173 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3174 | log_root_tree->log_transid++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3175 | mutex_unlock(&log_root_tree->log_mutex); |
| 3176 | |
| 3177 | /* |
| 3178 | * nobody else is going to jump in and write the the ctree |
| 3179 | * super here because the log_commit atomic below is protecting |
| 3180 | * us. We must be called with a transaction handle pinning |
| 3181 | * the running transaction open, so a full commit can't hop |
| 3182 | * in and cause problems either. |
| 3183 | */ |
David Sterba | eece6a9 | 2017-02-10 19:04:32 +0100 | [diff] [blame] | 3184 | ret = write_all_supers(fs_info, 1); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3185 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3186 | btrfs_set_log_full_commit(fs_info, trans); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3187 | btrfs_abort_transaction(trans, ret); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3188 | goto out_wake_log_root; |
| 3189 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3190 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 3191 | mutex_lock(&root->log_mutex); |
| 3192 | if (root->last_log_commit < log_transid) |
| 3193 | root->last_log_commit = log_transid; |
| 3194 | mutex_unlock(&root->log_mutex); |
| 3195 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3196 | out_wake_log_root: |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 3197 | mutex_lock(&log_root_tree->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3198 | btrfs_remove_all_log_ctxs(log_root_tree, index2, ret); |
| 3199 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3200 | log_root_tree->log_transid_committed++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3201 | atomic_set(&log_root_tree->log_commit[index2], 0); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3202 | mutex_unlock(&log_root_tree->log_mutex); |
| 3203 | |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3204 | /* |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3205 | * The barrier before waitqueue_active (in cond_wake_up) is needed so |
| 3206 | * all the updates above are seen by the woken threads. It might not be |
| 3207 | * necessary, but proving that seems to be hard. |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3208 | */ |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3209 | cond_wake_up(&log_root_tree->log_commit_wait[index2]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3210 | out: |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3211 | mutex_lock(&root->log_mutex); |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 3212 | btrfs_remove_all_log_ctxs(root, index1, ret); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3213 | root->log_transid_committed++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3214 | atomic_set(&root->log_commit[index1], 0); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3215 | mutex_unlock(&root->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3216 | |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3217 | /* |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3218 | * The barrier before waitqueue_active (in cond_wake_up) is needed so |
| 3219 | * all the updates above are seen by the woken threads. It might not be |
| 3220 | * necessary, but proving that seems to be hard. |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3221 | */ |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3222 | cond_wake_up(&root->log_commit_wait[index1]); |
Chris Mason | b31eabd | 2011-01-31 16:48:24 -0500 | [diff] [blame] | 3223 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3224 | } |
| 3225 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3226 | static void free_log_tree(struct btrfs_trans_handle *trans, |
| 3227 | struct btrfs_root *log) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3228 | { |
| 3229 | int ret; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3230 | u64 start; |
| 3231 | u64 end; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3232 | struct walk_control wc = { |
| 3233 | .free = 1, |
| 3234 | .process_func = process_one_buffer |
| 3235 | }; |
| 3236 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 3237 | ret = walk_log_tree(trans, log, &wc); |
Jeff Mahoney | cdecd48 | 2018-09-06 16:59:33 -0400 | [diff] [blame] | 3238 | if (ret) { |
| 3239 | if (trans) |
| 3240 | btrfs_abort_transaction(trans, ret); |
| 3241 | else |
| 3242 | btrfs_handle_fs_error(log->fs_info, ret, NULL); |
| 3243 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3244 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3245 | while (1) { |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3246 | ret = find_first_extent_bit(&log->dirty_log_pages, |
Liu Bo | 55237a5 | 2018-01-25 11:02:52 -0700 | [diff] [blame] | 3247 | 0, &start, &end, |
| 3248 | EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 3249 | NULL); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3250 | if (ret) |
| 3251 | break; |
| 3252 | |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 3253 | clear_extent_bits(&log->dirty_log_pages, start, end, |
Liu Bo | 55237a5 | 2018-01-25 11:02:52 -0700 | [diff] [blame] | 3254 | EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3255 | } |
| 3256 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3257 | free_extent_buffer(log->node); |
| 3258 | kfree(log); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | /* |
| 3262 | * free all the extents used by the tree log. This should be called |
| 3263 | * at commit time of the full transaction |
| 3264 | */ |
| 3265 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) |
| 3266 | { |
| 3267 | if (root->log_root) { |
| 3268 | free_log_tree(trans, root->log_root); |
| 3269 | root->log_root = NULL; |
| 3270 | } |
| 3271 | return 0; |
| 3272 | } |
| 3273 | |
| 3274 | int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, |
| 3275 | struct btrfs_fs_info *fs_info) |
| 3276 | { |
| 3277 | if (fs_info->log_root_tree) { |
| 3278 | free_log_tree(trans, fs_info->log_root_tree); |
| 3279 | fs_info->log_root_tree = NULL; |
| 3280 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3281 | return 0; |
| 3282 | } |
| 3283 | |
| 3284 | /* |
Filipe Manana | fffedf5 | 2019-06-19 13:05:39 +0100 | [diff] [blame] | 3285 | * Check if an inode was logged in the current transaction. We can't always rely |
| 3286 | * on an inode's logged_trans value, because it's an in-memory only field and |
| 3287 | * therefore not persisted. This means that its value is lost if the inode gets |
| 3288 | * evicted and loaded again from disk (in which case it has a value of 0, and |
| 3289 | * certainly it is smaller then any possible transaction ID), when that happens |
| 3290 | * the full_sync flag is set in the inode's runtime flags, so on that case we |
| 3291 | * assume eviction happened and ignore the logged_trans value, assuming the |
| 3292 | * worst case, that the inode was logged before in the current transaction. |
| 3293 | */ |
| 3294 | static bool inode_logged(struct btrfs_trans_handle *trans, |
| 3295 | struct btrfs_inode *inode) |
| 3296 | { |
| 3297 | if (inode->logged_trans == trans->transid) |
| 3298 | return true; |
| 3299 | |
| 3300 | if (inode->last_trans == trans->transid && |
| 3301 | test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) && |
| 3302 | !test_bit(BTRFS_FS_LOG_RECOVERING, &trans->fs_info->flags)) |
| 3303 | return true; |
| 3304 | |
| 3305 | return false; |
| 3306 | } |
| 3307 | |
| 3308 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3309 | * If both a file and directory are logged, and unlinks or renames are |
| 3310 | * mixed in, we have a few interesting corners: |
| 3311 | * |
| 3312 | * create file X in dir Y |
| 3313 | * link file X to X.link in dir Y |
| 3314 | * fsync file X |
| 3315 | * unlink file X but leave X.link |
| 3316 | * fsync dir Y |
| 3317 | * |
| 3318 | * After a crash we would expect only X.link to exist. But file X |
| 3319 | * didn't get fsync'd again so the log has back refs for X and X.link. |
| 3320 | * |
| 3321 | * We solve this by removing directory entries and inode backrefs from the |
| 3322 | * log when a file that was logged in the current transaction is |
| 3323 | * unlinked. Any later fsync will include the updated log entries, and |
| 3324 | * we'll be able to reconstruct the proper directory items from backrefs. |
| 3325 | * |
| 3326 | * This optimizations allows us to avoid relogging the entire inode |
| 3327 | * or the entire directory. |
| 3328 | */ |
| 3329 | int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, |
| 3330 | struct btrfs_root *root, |
| 3331 | const char *name, int name_len, |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3332 | struct btrfs_inode *dir, u64 index) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3333 | { |
| 3334 | struct btrfs_root *log; |
| 3335 | struct btrfs_dir_item *di; |
| 3336 | struct btrfs_path *path; |
| 3337 | int ret; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3338 | int err = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3339 | int bytes_del = 0; |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3340 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3341 | |
Filipe Manana | fffedf5 | 2019-06-19 13:05:39 +0100 | [diff] [blame] | 3342 | if (!inode_logged(trans, dir)) |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 3343 | return 0; |
| 3344 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3345 | ret = join_running_log_trans(root); |
| 3346 | if (ret) |
| 3347 | return 0; |
| 3348 | |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3349 | mutex_lock(&dir->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3350 | |
| 3351 | log = root->log_root; |
| 3352 | path = btrfs_alloc_path(); |
Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3353 | if (!path) { |
| 3354 | err = -ENOMEM; |
| 3355 | goto out_unlock; |
| 3356 | } |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 3357 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3358 | di = btrfs_lookup_dir_item(trans, log, path, dir_ino, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3359 | name, name_len, -1); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3360 | if (IS_ERR(di)) { |
| 3361 | err = PTR_ERR(di); |
| 3362 | goto fail; |
| 3363 | } |
| 3364 | if (di) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3365 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
| 3366 | bytes_del += name_len; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3367 | if (ret) { |
| 3368 | err = ret; |
| 3369 | goto fail; |
| 3370 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3371 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3372 | btrfs_release_path(path); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3373 | di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3374 | index, name, name_len, -1); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3375 | if (IS_ERR(di)) { |
| 3376 | err = PTR_ERR(di); |
| 3377 | goto fail; |
| 3378 | } |
| 3379 | if (di) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3380 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
| 3381 | bytes_del += name_len; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3382 | if (ret) { |
| 3383 | err = ret; |
| 3384 | goto fail; |
| 3385 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | /* update the directory size in the log to reflect the names |
| 3389 | * we have removed |
| 3390 | */ |
| 3391 | if (bytes_del) { |
| 3392 | struct btrfs_key key; |
| 3393 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3394 | key.objectid = dir_ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3395 | key.offset = 0; |
| 3396 | key.type = BTRFS_INODE_ITEM_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3397 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3398 | |
| 3399 | ret = btrfs_search_slot(trans, log, &key, path, 0, 1); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3400 | if (ret < 0) { |
| 3401 | err = ret; |
| 3402 | goto fail; |
| 3403 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3404 | if (ret == 0) { |
| 3405 | struct btrfs_inode_item *item; |
| 3406 | u64 i_size; |
| 3407 | |
| 3408 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3409 | struct btrfs_inode_item); |
| 3410 | i_size = btrfs_inode_size(path->nodes[0], item); |
| 3411 | if (i_size > bytes_del) |
| 3412 | i_size -= bytes_del; |
| 3413 | else |
| 3414 | i_size = 0; |
| 3415 | btrfs_set_inode_size(path->nodes[0], item, i_size); |
| 3416 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 3417 | } else |
| 3418 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3419 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3420 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3421 | fail: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3422 | btrfs_free_path(path); |
Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3423 | out_unlock: |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3424 | mutex_unlock(&dir->log_mutex); |
Josef Bacik | 2d03728 | 2020-08-10 17:31:16 -0400 | [diff] [blame] | 3425 | if (err == -ENOSPC) { |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 3426 | btrfs_set_log_full_commit(root->fs_info, trans); |
Josef Bacik | 2d03728 | 2020-08-10 17:31:16 -0400 | [diff] [blame] | 3427 | err = 0; |
| 3428 | } else if (err < 0 && err != -ENOENT) { |
| 3429 | /* ENOENT can be returned if the entry hasn't been fsynced yet */ |
| 3430 | btrfs_abort_transaction(trans, err); |
| 3431 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3432 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3433 | btrfs_end_log_trans(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3434 | |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 3435 | return err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /* see comments for btrfs_del_dir_entries_in_log */ |
| 3439 | int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, |
| 3440 | struct btrfs_root *root, |
| 3441 | const char *name, int name_len, |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3442 | struct btrfs_inode *inode, u64 dirid) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3443 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3444 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3445 | struct btrfs_root *log; |
| 3446 | u64 index; |
| 3447 | int ret; |
| 3448 | |
Filipe Manana | fffedf5 | 2019-06-19 13:05:39 +0100 | [diff] [blame] | 3449 | if (!inode_logged(trans, inode)) |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 3450 | return 0; |
| 3451 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3452 | ret = join_running_log_trans(root); |
| 3453 | if (ret) |
| 3454 | return 0; |
| 3455 | log = root->log_root; |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3456 | mutex_lock(&inode->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3457 | |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3458 | ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode), |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3459 | dirid, &index); |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3460 | mutex_unlock(&inode->log_mutex); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3461 | if (ret == -ENOSPC) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3462 | btrfs_set_log_full_commit(fs_info, trans); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3463 | ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3464 | } else if (ret < 0 && ret != -ENOENT) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3465 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3466 | btrfs_end_log_trans(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3467 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3468 | return ret; |
| 3469 | } |
| 3470 | |
| 3471 | /* |
| 3472 | * creates a range item in the log for 'dirid'. first_offset and |
| 3473 | * last_offset tell us which parts of the key space the log should |
| 3474 | * be considered authoritative for. |
| 3475 | */ |
| 3476 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, |
| 3477 | struct btrfs_root *log, |
| 3478 | struct btrfs_path *path, |
| 3479 | int key_type, u64 dirid, |
| 3480 | u64 first_offset, u64 last_offset) |
| 3481 | { |
| 3482 | int ret; |
| 3483 | struct btrfs_key key; |
| 3484 | struct btrfs_dir_log_item *item; |
| 3485 | |
| 3486 | key.objectid = dirid; |
| 3487 | key.offset = first_offset; |
| 3488 | if (key_type == BTRFS_DIR_ITEM_KEY) |
| 3489 | key.type = BTRFS_DIR_LOG_ITEM_KEY; |
| 3490 | else |
| 3491 | key.type = BTRFS_DIR_LOG_INDEX_KEY; |
| 3492 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3493 | if (ret) |
| 3494 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3495 | |
| 3496 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3497 | struct btrfs_dir_log_item); |
| 3498 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); |
| 3499 | btrfs_mark_buffer_dirty(path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3500 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3501 | return 0; |
| 3502 | } |
| 3503 | |
| 3504 | /* |
| 3505 | * log all the items included in the current transaction for a given |
| 3506 | * directory. This also creates the range items in the log tree required |
| 3507 | * to replay anything deleted before the fsync |
| 3508 | */ |
| 3509 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 684a577 | 2017-01-18 00:31:41 +0200 | [diff] [blame] | 3510 | struct btrfs_root *root, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3511 | struct btrfs_path *path, |
| 3512 | struct btrfs_path *dst_path, int key_type, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3513 | struct btrfs_log_ctx *ctx, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3514 | u64 min_offset, u64 *last_offset_ret) |
| 3515 | { |
| 3516 | struct btrfs_key min_key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3517 | struct btrfs_root *log = root->log_root; |
| 3518 | struct extent_buffer *src; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3519 | int err = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3520 | int ret; |
| 3521 | int i; |
| 3522 | int nritems; |
| 3523 | u64 first_offset = min_offset; |
| 3524 | u64 last_offset = (u64)-1; |
Nikolay Borisov | 684a577 | 2017-01-18 00:31:41 +0200 | [diff] [blame] | 3525 | u64 ino = btrfs_ino(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3526 | |
| 3527 | log = root->log_root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3528 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3529 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3530 | min_key.type = key_type; |
| 3531 | min_key.offset = min_offset; |
| 3532 | |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3533 | ret = btrfs_search_forward(root, &min_key, path, trans->transid); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3534 | |
| 3535 | /* |
| 3536 | * we didn't find anything from this transaction, see if there |
| 3537 | * is anything at all |
| 3538 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3539 | if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) { |
| 3540 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3541 | min_key.type = key_type; |
| 3542 | min_key.offset = (u64)-1; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3543 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3544 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
| 3545 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3546 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3547 | return ret; |
| 3548 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3549 | ret = btrfs_previous_item(root, path, ino, key_type); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3550 | |
| 3551 | /* if ret == 0 there are items for this type, |
| 3552 | * create a range to tell us the last key of this type. |
| 3553 | * otherwise, there are no items in this directory after |
| 3554 | * *min_offset, and we create a range to indicate that. |
| 3555 | */ |
| 3556 | if (ret == 0) { |
| 3557 | struct btrfs_key tmp; |
| 3558 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, |
| 3559 | path->slots[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3560 | if (key_type == tmp.type) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3561 | first_offset = max(min_offset, tmp.offset) + 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3562 | } |
| 3563 | goto done; |
| 3564 | } |
| 3565 | |
| 3566 | /* go backward to find any previous key */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3567 | ret = btrfs_previous_item(root, path, ino, key_type); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3568 | if (ret == 0) { |
| 3569 | struct btrfs_key tmp; |
| 3570 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
| 3571 | if (key_type == tmp.type) { |
| 3572 | first_offset = tmp.offset; |
| 3573 | ret = overwrite_item(trans, log, dst_path, |
| 3574 | path->nodes[0], path->slots[0], |
| 3575 | &tmp); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3576 | if (ret) { |
| 3577 | err = ret; |
| 3578 | goto done; |
| 3579 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3580 | } |
| 3581 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3582 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3583 | |
Josef Bacik | b57220c | 2019-03-06 17:13:04 -0500 | [diff] [blame] | 3584 | /* |
| 3585 | * Find the first key from this transaction again. See the note for |
| 3586 | * log_new_dir_dentries, if we're logging a directory recursively we |
| 3587 | * won't be holding its i_mutex, which means we can modify the directory |
| 3588 | * while we're logging it. If we remove an entry between our first |
| 3589 | * search and this search we'll not find the key again and can just |
| 3590 | * bail. |
| 3591 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3592 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
Josef Bacik | b57220c | 2019-03-06 17:13:04 -0500 | [diff] [blame] | 3593 | if (ret != 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3594 | goto done; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3595 | |
| 3596 | /* |
| 3597 | * we have a block from this transaction, log every item in it |
| 3598 | * from our directory |
| 3599 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3600 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3601 | struct btrfs_key tmp; |
| 3602 | src = path->nodes[0]; |
| 3603 | nritems = btrfs_header_nritems(src); |
| 3604 | for (i = path->slots[0]; i < nritems; i++) { |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3605 | struct btrfs_dir_item *di; |
| 3606 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3607 | btrfs_item_key_to_cpu(src, &min_key, i); |
| 3608 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3609 | if (min_key.objectid != ino || min_key.type != key_type) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3610 | goto done; |
| 3611 | ret = overwrite_item(trans, log, dst_path, src, i, |
| 3612 | &min_key); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3613 | if (ret) { |
| 3614 | err = ret; |
| 3615 | goto done; |
| 3616 | } |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3617 | |
| 3618 | /* |
| 3619 | * We must make sure that when we log a directory entry, |
| 3620 | * the corresponding inode, after log replay, has a |
| 3621 | * matching link count. For example: |
| 3622 | * |
| 3623 | * touch foo |
| 3624 | * mkdir mydir |
| 3625 | * sync |
| 3626 | * ln foo mydir/bar |
| 3627 | * xfs_io -c "fsync" mydir |
| 3628 | * <crash> |
| 3629 | * <mount fs and log replay> |
| 3630 | * |
| 3631 | * Would result in a fsync log that when replayed, our |
| 3632 | * file inode would have a link count of 1, but we get |
| 3633 | * two directory entries pointing to the same inode. |
| 3634 | * After removing one of the names, it would not be |
| 3635 | * possible to remove the other name, which resulted |
| 3636 | * always in stale file handle errors, and would not |
| 3637 | * be possible to rmdir the parent directory, since |
| 3638 | * its i_size could never decrement to the value |
| 3639 | * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors. |
| 3640 | */ |
| 3641 | di = btrfs_item_ptr(src, i, struct btrfs_dir_item); |
| 3642 | btrfs_dir_item_key_to_cpu(src, di, &tmp); |
| 3643 | if (ctx && |
| 3644 | (btrfs_dir_transid(src, di) == trans->transid || |
| 3645 | btrfs_dir_type(src, di) == BTRFS_FT_DIR) && |
| 3646 | tmp.type != BTRFS_ROOT_ITEM_KEY) |
| 3647 | ctx->log_new_dentries = true; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3648 | } |
| 3649 | path->slots[0] = nritems; |
| 3650 | |
| 3651 | /* |
| 3652 | * look ahead to the next item and see if it is also |
| 3653 | * from this directory and from this transaction |
| 3654 | */ |
| 3655 | ret = btrfs_next_leaf(root, path); |
Liu Bo | 80c0b42 | 2018-04-03 01:59:47 +0800 | [diff] [blame] | 3656 | if (ret) { |
| 3657 | if (ret == 1) |
| 3658 | last_offset = (u64)-1; |
| 3659 | else |
| 3660 | err = ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3661 | goto done; |
| 3662 | } |
| 3663 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3664 | if (tmp.objectid != ino || tmp.type != key_type) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3665 | last_offset = (u64)-1; |
| 3666 | goto done; |
| 3667 | } |
| 3668 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { |
| 3669 | ret = overwrite_item(trans, log, dst_path, |
| 3670 | path->nodes[0], path->slots[0], |
| 3671 | &tmp); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3672 | if (ret) |
| 3673 | err = ret; |
| 3674 | else |
| 3675 | last_offset = tmp.offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3676 | goto done; |
| 3677 | } |
| 3678 | } |
| 3679 | done: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3680 | btrfs_release_path(path); |
| 3681 | btrfs_release_path(dst_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3682 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3683 | if (err == 0) { |
| 3684 | *last_offset_ret = last_offset; |
| 3685 | /* |
| 3686 | * insert the log range keys to indicate where the log |
| 3687 | * is valid |
| 3688 | */ |
| 3689 | ret = insert_dir_log_key(trans, log, path, key_type, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3690 | ino, first_offset, last_offset); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3691 | if (ret) |
| 3692 | err = ret; |
| 3693 | } |
| 3694 | return err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | /* |
| 3698 | * logging directories is very similar to logging inodes, We find all the items |
| 3699 | * from the current transaction and write them to the log. |
| 3700 | * |
| 3701 | * The recovery code scans the directory in the subvolume, and if it finds a |
| 3702 | * key in the range logged that is not present in the log tree, then it means |
| 3703 | * that dir entry was unlinked during the transaction. |
| 3704 | * |
| 3705 | * In order for that scan to work, we must include one key smaller than |
| 3706 | * the smallest logged by this transaction and one key larger than the largest |
| 3707 | * key logged by this transaction. |
| 3708 | */ |
| 3709 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, |
Nikolay Borisov | dbf39ea | 2017-01-18 00:31:42 +0200 | [diff] [blame] | 3710 | struct btrfs_root *root, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3711 | struct btrfs_path *path, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3712 | struct btrfs_path *dst_path, |
| 3713 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3714 | { |
| 3715 | u64 min_key; |
| 3716 | u64 max_key; |
| 3717 | int ret; |
| 3718 | int key_type = BTRFS_DIR_ITEM_KEY; |
| 3719 | |
| 3720 | again: |
| 3721 | min_key = 0; |
| 3722 | max_key = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3723 | while (1) { |
Nikolay Borisov | dbf39ea | 2017-01-18 00:31:42 +0200 | [diff] [blame] | 3724 | ret = log_dir_items(trans, root, inode, path, dst_path, key_type, |
| 3725 | ctx, min_key, &max_key); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3726 | if (ret) |
| 3727 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3728 | if (max_key == (u64)-1) |
| 3729 | break; |
| 3730 | min_key = max_key + 1; |
| 3731 | } |
| 3732 | |
| 3733 | if (key_type == BTRFS_DIR_ITEM_KEY) { |
| 3734 | key_type = BTRFS_DIR_INDEX_KEY; |
| 3735 | goto again; |
| 3736 | } |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
| 3740 | /* |
| 3741 | * a helper function to drop items from the log before we relog an |
| 3742 | * inode. max_key_type indicates the highest item type to remove. |
| 3743 | * This cannot be run for file data extents because it does not |
| 3744 | * free the extents they point to. |
| 3745 | */ |
| 3746 | static int drop_objectid_items(struct btrfs_trans_handle *trans, |
| 3747 | struct btrfs_root *log, |
| 3748 | struct btrfs_path *path, |
| 3749 | u64 objectid, int max_key_type) |
| 3750 | { |
| 3751 | int ret; |
| 3752 | struct btrfs_key key; |
| 3753 | struct btrfs_key found_key; |
Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 3754 | int start_slot; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3755 | |
| 3756 | key.objectid = objectid; |
| 3757 | key.type = max_key_type; |
| 3758 | key.offset = (u64)-1; |
| 3759 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3760 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3761 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3762 | BUG_ON(ret == 0); /* Logic error */ |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3763 | if (ret < 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3764 | break; |
| 3765 | |
| 3766 | if (path->slots[0] == 0) |
| 3767 | break; |
| 3768 | |
| 3769 | path->slots[0]--; |
| 3770 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 3771 | path->slots[0]); |
| 3772 | |
| 3773 | if (found_key.objectid != objectid) |
| 3774 | break; |
| 3775 | |
Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 3776 | found_key.offset = 0; |
| 3777 | found_key.type = 0; |
| 3778 | ret = btrfs_bin_search(path->nodes[0], &found_key, 0, |
| 3779 | &start_slot); |
| 3780 | |
| 3781 | ret = btrfs_del_items(trans, log, path, start_slot, |
| 3782 | path->slots[0] - start_slot + 1); |
| 3783 | /* |
| 3784 | * If start slot isn't 0 then we don't need to re-search, we've |
| 3785 | * found the last guy with the objectid in this tree. |
| 3786 | */ |
| 3787 | if (ret || start_slot != 0) |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 3788 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3789 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3790 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3791 | btrfs_release_path(path); |
Josef Bacik | 5bdbeb2 | 2012-05-29 16:59:49 -0400 | [diff] [blame] | 3792 | if (ret > 0) |
| 3793 | ret = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3794 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3795 | } |
| 3796 | |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3797 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3798 | struct extent_buffer *leaf, |
| 3799 | struct btrfs_inode_item *item, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3800 | struct inode *inode, int log_inode_only, |
| 3801 | u64 logged_isize) |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3802 | { |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3803 | struct btrfs_map_token token; |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3804 | |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3805 | btrfs_init_map_token(&token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3806 | |
| 3807 | if (log_inode_only) { |
| 3808 | /* set the generation to zero so the recover code |
| 3809 | * can tell the difference between an logging |
| 3810 | * just to say 'this inode exists' and a logging |
| 3811 | * to say 'update this inode with these values' |
| 3812 | */ |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3813 | btrfs_set_token_inode_generation(leaf, item, 0, &token); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3814 | btrfs_set_token_inode_size(leaf, item, logged_isize, &token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3815 | } else { |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3816 | btrfs_set_token_inode_generation(leaf, item, |
| 3817 | BTRFS_I(inode)->generation, |
| 3818 | &token); |
| 3819 | btrfs_set_token_inode_size(leaf, item, inode->i_size, &token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3820 | } |
| 3821 | |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3822 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3823 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3824 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3825 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
| 3826 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3827 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3828 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3829 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3830 | inode->i_atime.tv_nsec, &token); |
| 3831 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3832 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3833 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3834 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3835 | inode->i_mtime.tv_nsec, &token); |
| 3836 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3837 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3838 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3839 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3840 | inode->i_ctime.tv_nsec, &token); |
| 3841 | |
| 3842 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3843 | &token); |
| 3844 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3845 | btrfs_set_token_inode_sequence(leaf, item, |
| 3846 | inode_peek_iversion(inode), &token); |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3847 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3848 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3849 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3850 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3851 | } |
| 3852 | |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3853 | static int log_inode_item(struct btrfs_trans_handle *trans, |
| 3854 | struct btrfs_root *log, struct btrfs_path *path, |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3855 | struct btrfs_inode *inode) |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3856 | { |
| 3857 | struct btrfs_inode_item *inode_item; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3858 | int ret; |
| 3859 | |
Filipe David Borba Manana | efd0c40 | 2013-10-07 21:20:44 +0100 | [diff] [blame] | 3860 | ret = btrfs_insert_empty_item(trans, log, path, |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3861 | &inode->location, sizeof(*inode_item)); |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3862 | if (ret && ret != -EEXIST) |
| 3863 | return ret; |
| 3864 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3865 | struct btrfs_inode_item); |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3866 | fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode, |
| 3867 | 0, 0); |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3868 | btrfs_release_path(path); |
| 3869 | return 0; |
| 3870 | } |
| 3871 | |
Filipe Manana | 0151049 | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 3872 | static int log_csums(struct btrfs_trans_handle *trans, |
| 3873 | struct btrfs_root *log_root, |
| 3874 | struct btrfs_ordered_sum *sums) |
| 3875 | { |
| 3876 | int ret; |
| 3877 | |
| 3878 | /* |
| 3879 | * Due to extent cloning, we might have logged a csum item that covers a |
| 3880 | * subrange of a cloned extent, and later we can end up logging a csum |
| 3881 | * item for a larger subrange of the same extent or the entire range. |
| 3882 | * This would leave csum items in the log tree that cover the same range |
| 3883 | * and break the searches for checksums in the log tree, resulting in |
| 3884 | * some checksums missing in the fs/subvolume tree. So just delete (or |
| 3885 | * trim and adjust) any existing csum items in the log for this range. |
| 3886 | */ |
| 3887 | ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len); |
| 3888 | if (ret) |
| 3889 | return ret; |
| 3890 | |
| 3891 | return btrfs_csum_file_blocks(trans, log_root, sums); |
| 3892 | } |
| 3893 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3894 | static noinline int copy_items(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3895 | struct btrfs_inode *inode, |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3896 | struct btrfs_path *dst_path, |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 3897 | struct btrfs_path *src_path, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3898 | int start_slot, int nr, int inode_only, |
| 3899 | u64 logged_isize) |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3900 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 3901 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3902 | unsigned long src_offset; |
| 3903 | unsigned long dst_offset; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3904 | struct btrfs_root *log = inode->root->log_root; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3905 | struct btrfs_file_extent_item *extent; |
| 3906 | struct btrfs_inode_item *inode_item; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3907 | struct extent_buffer *src = src_path->nodes[0]; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3908 | int ret; |
| 3909 | struct btrfs_key *ins_keys; |
| 3910 | u32 *ins_sizes; |
| 3911 | char *ins_data; |
| 3912 | int i; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3913 | struct list_head ordered_sums; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3914 | int skip_csum = inode->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3915 | |
| 3916 | INIT_LIST_HEAD(&ordered_sums); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3917 | |
| 3918 | ins_data = kmalloc(nr * sizeof(struct btrfs_key) + |
| 3919 | nr * sizeof(u32), GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 3920 | if (!ins_data) |
| 3921 | return -ENOMEM; |
| 3922 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3923 | ins_sizes = (u32 *)ins_data; |
| 3924 | ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); |
| 3925 | |
| 3926 | for (i = 0; i < nr; i++) { |
| 3927 | ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot); |
| 3928 | btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot); |
| 3929 | } |
| 3930 | ret = btrfs_insert_empty_items(trans, log, dst_path, |
| 3931 | ins_keys, ins_sizes, nr); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3932 | if (ret) { |
| 3933 | kfree(ins_data); |
| 3934 | return ret; |
| 3935 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3936 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3937 | for (i = 0; i < nr; i++, dst_path->slots[0]++) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3938 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], |
| 3939 | dst_path->slots[0]); |
| 3940 | |
| 3941 | src_offset = btrfs_item_ptr_offset(src, start_slot + i); |
| 3942 | |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3943 | if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3944 | inode_item = btrfs_item_ptr(dst_path->nodes[0], |
| 3945 | dst_path->slots[0], |
| 3946 | struct btrfs_inode_item); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3947 | fill_inode_item(trans, dst_path->nodes[0], inode_item, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3948 | &inode->vfs_inode, |
| 3949 | inode_only == LOG_INODE_EXISTS, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3950 | logged_isize); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3951 | } else { |
| 3952 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, |
| 3953 | src_offset, ins_sizes[i]); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3954 | } |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3955 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3956 | /* take a reference on file data extents so that truncates |
| 3957 | * or deletes of this inode don't have to relog the inode |
| 3958 | * again |
| 3959 | */ |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3960 | if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY && |
Liu Bo | d279440 | 2012-08-29 01:07:56 -0600 | [diff] [blame] | 3961 | !skip_csum) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3962 | int found_type; |
| 3963 | extent = btrfs_item_ptr(src, start_slot + i, |
| 3964 | struct btrfs_file_extent_item); |
| 3965 | |
liubo | 8e531cd | 2011-05-06 10:36:09 +0800 | [diff] [blame] | 3966 | if (btrfs_file_extent_generation(src, extent) < trans->transid) |
| 3967 | continue; |
| 3968 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3969 | found_type = btrfs_file_extent_type(src, extent); |
Josef Bacik | 6f1fed7 | 2012-09-26 11:07:06 -0400 | [diff] [blame] | 3970 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3971 | u64 ds, dl, cs, cl; |
| 3972 | ds = btrfs_file_extent_disk_bytenr(src, |
| 3973 | extent); |
| 3974 | /* ds == 0 is a hole */ |
| 3975 | if (ds == 0) |
| 3976 | continue; |
| 3977 | |
| 3978 | dl = btrfs_file_extent_disk_num_bytes(src, |
| 3979 | extent); |
| 3980 | cs = btrfs_file_extent_offset(src, extent); |
| 3981 | cl = btrfs_file_extent_num_bytes(src, |
Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 3982 | extent); |
Chris Mason | 580afd7 | 2008-12-08 19:15:39 -0500 | [diff] [blame] | 3983 | if (btrfs_file_extent_compression(src, |
| 3984 | extent)) { |
| 3985 | cs = 0; |
| 3986 | cl = dl; |
| 3987 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3988 | |
| 3989 | ret = btrfs_lookup_csums_range( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3990 | fs_info->csum_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3991 | ds + cs, ds + cs + cl - 1, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3992 | &ordered_sums, 0); |
Filipe Manana | 183af2d | 2020-07-29 10:17:50 +0100 | [diff] [blame] | 3993 | if (ret) |
| 3994 | break; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3995 | } |
| 3996 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3997 | } |
| 3998 | |
| 3999 | btrfs_mark_buffer_dirty(dst_path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4000 | btrfs_release_path(dst_path); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4001 | kfree(ins_data); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 4002 | |
| 4003 | /* |
| 4004 | * we have to do this after the loop above to avoid changing the |
| 4005 | * log tree while trying to change the log tree. |
| 4006 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4007 | while (!list_empty(&ordered_sums)) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 4008 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, |
| 4009 | struct btrfs_ordered_sum, |
| 4010 | list); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4011 | if (!ret) |
Filipe Manana | 0151049 | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 4012 | ret = log_csums(trans, log, sums); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 4013 | list_del(&sums->list); |
| 4014 | kfree(sums); |
| 4015 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4016 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4017 | return ret; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4018 | } |
| 4019 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4020 | static int extent_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 4021 | { |
| 4022 | struct extent_map *em1, *em2; |
| 4023 | |
| 4024 | em1 = list_entry(a, struct extent_map, list); |
| 4025 | em2 = list_entry(b, struct extent_map, list); |
| 4026 | |
| 4027 | if (em1->start < em2->start) |
| 4028 | return -1; |
| 4029 | else if (em1->start > em2->start) |
| 4030 | return 1; |
| 4031 | return 0; |
| 4032 | } |
| 4033 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4034 | static int log_extent_csums(struct btrfs_trans_handle *trans, |
| 4035 | struct btrfs_inode *inode, |
Nikolay Borisov | a9ecb65 | 2018-06-20 17:26:42 +0300 | [diff] [blame] | 4036 | struct btrfs_root *log_root, |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4037 | const struct extent_map *em) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4038 | { |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4039 | u64 csum_offset; |
| 4040 | u64 csum_len; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4041 | LIST_HEAD(ordered_sums); |
| 4042 | int ret = 0; |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4043 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4044 | if (inode->flags & BTRFS_INODE_NODATASUM || |
| 4045 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4046 | em->block_start == EXTENT_MAP_HOLE) |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4047 | return 0; |
| 4048 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4049 | /* 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] | 4050 | if (em->compress_type) { |
| 4051 | csum_offset = 0; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4052 | csum_len = max(em->block_len, em->orig_block_len); |
Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4053 | } else { |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4054 | csum_offset = em->mod_start - em->start; |
| 4055 | csum_len = em->mod_len; |
Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4056 | } |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4057 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4058 | /* block start is already adjusted for the file extent offset. */ |
Nikolay Borisov | a9ecb65 | 2018-06-20 17:26:42 +0300 | [diff] [blame] | 4059 | ret = btrfs_lookup_csums_range(trans->fs_info->csum_root, |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4060 | em->block_start + csum_offset, |
| 4061 | em->block_start + csum_offset + |
| 4062 | csum_len - 1, &ordered_sums, 0); |
| 4063 | if (ret) |
| 4064 | return ret; |
| 4065 | |
| 4066 | while (!list_empty(&ordered_sums)) { |
| 4067 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, |
| 4068 | struct btrfs_ordered_sum, |
| 4069 | list); |
| 4070 | if (!ret) |
Filipe Manana | 0151049 | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 4071 | ret = log_csums(trans, log_root, sums); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4072 | list_del(&sums->list); |
| 4073 | kfree(sums); |
| 4074 | } |
| 4075 | |
| 4076 | return ret; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4077 | } |
| 4078 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4079 | static int log_one_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4080 | struct btrfs_inode *inode, struct btrfs_root *root, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4081 | const struct extent_map *em, |
| 4082 | struct btrfs_path *path, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4083 | struct btrfs_log_ctx *ctx) |
| 4084 | { |
| 4085 | struct btrfs_root *log = root->log_root; |
| 4086 | struct btrfs_file_extent_item *fi; |
| 4087 | struct extent_buffer *leaf; |
| 4088 | struct btrfs_map_token token; |
| 4089 | struct btrfs_key key; |
| 4090 | u64 extent_offset = em->start - em->orig_start; |
| 4091 | u64 block_len; |
| 4092 | int ret; |
| 4093 | int extent_inserted = 0; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4094 | |
Nikolay Borisov | a9ecb65 | 2018-06-20 17:26:42 +0300 | [diff] [blame] | 4095 | ret = log_extent_csums(trans, inode, log, em); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4096 | if (ret) |
| 4097 | return ret; |
| 4098 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4099 | btrfs_init_map_token(&token); |
| 4100 | |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4101 | ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4102 | em->start + em->len, NULL, 0, 1, |
| 4103 | sizeof(*fi), &extent_inserted); |
| 4104 | if (ret) |
| 4105 | return ret; |
| 4106 | |
| 4107 | if (!extent_inserted) { |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4108 | key.objectid = btrfs_ino(inode); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4109 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4110 | key.offset = em->start; |
| 4111 | |
| 4112 | ret = btrfs_insert_empty_item(trans, log, path, &key, |
| 4113 | sizeof(*fi)); |
| 4114 | if (ret) |
| 4115 | return ret; |
| 4116 | } |
| 4117 | leaf = path->nodes[0]; |
| 4118 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4119 | struct btrfs_file_extent_item); |
| 4120 | |
Josef Bacik | 50d9aa9 | 2014-11-21 14:52:38 -0500 | [diff] [blame] | 4121 | btrfs_set_token_file_extent_generation(leaf, fi, trans->transid, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4122 | &token); |
| 4123 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 4124 | btrfs_set_token_file_extent_type(leaf, fi, |
| 4125 | BTRFS_FILE_EXTENT_PREALLOC, |
| 4126 | &token); |
| 4127 | else |
| 4128 | btrfs_set_token_file_extent_type(leaf, fi, |
| 4129 | BTRFS_FILE_EXTENT_REG, |
| 4130 | &token); |
| 4131 | |
| 4132 | block_len = max(em->block_len, em->orig_block_len); |
| 4133 | if (em->compress_type != BTRFS_COMPRESS_NONE) { |
| 4134 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, |
| 4135 | em->block_start, |
| 4136 | &token); |
| 4137 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len, |
| 4138 | &token); |
| 4139 | } else if (em->block_start < EXTENT_MAP_LAST_BYTE) { |
| 4140 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, |
| 4141 | em->block_start - |
| 4142 | extent_offset, &token); |
| 4143 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len, |
| 4144 | &token); |
| 4145 | } else { |
| 4146 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token); |
| 4147 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0, |
| 4148 | &token); |
| 4149 | } |
| 4150 | |
| 4151 | btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token); |
| 4152 | btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token); |
| 4153 | btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token); |
| 4154 | btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type, |
| 4155 | &token); |
| 4156 | btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token); |
| 4157 | btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token); |
| 4158 | btrfs_mark_buffer_dirty(leaf); |
| 4159 | |
| 4160 | btrfs_release_path(path); |
| 4161 | |
| 4162 | return ret; |
| 4163 | } |
| 4164 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4165 | /* |
| 4166 | * Log all prealloc extents beyond the inode's i_size to make sure we do not |
| 4167 | * lose them after doing a fast fsync and replaying the log. We scan the |
| 4168 | * subvolume's root instead of iterating the inode's extent map tree because |
| 4169 | * otherwise we can log incorrect extent items based on extent map conversion. |
| 4170 | * That can happen due to the fact that extent maps are merged when they |
| 4171 | * are not in the extent map tree's list of modified extents. |
| 4172 | */ |
| 4173 | static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans, |
| 4174 | struct btrfs_inode *inode, |
| 4175 | struct btrfs_path *path) |
| 4176 | { |
| 4177 | struct btrfs_root *root = inode->root; |
| 4178 | struct btrfs_key key; |
| 4179 | const u64 i_size = i_size_read(&inode->vfs_inode); |
| 4180 | const u64 ino = btrfs_ino(inode); |
| 4181 | struct btrfs_path *dst_path = NULL; |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4182 | bool dropped_extents = false; |
Filipe Manana | 0004345 | 2020-04-23 16:30:53 +0100 | [diff] [blame] | 4183 | u64 truncate_offset = i_size; |
| 4184 | struct extent_buffer *leaf; |
| 4185 | int slot; |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4186 | int ins_nr = 0; |
| 4187 | int start_slot; |
| 4188 | int ret; |
| 4189 | |
| 4190 | if (!(inode->flags & BTRFS_INODE_PREALLOC)) |
| 4191 | return 0; |
| 4192 | |
| 4193 | key.objectid = ino; |
| 4194 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4195 | key.offset = i_size; |
| 4196 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4197 | if (ret < 0) |
| 4198 | goto out; |
| 4199 | |
Filipe Manana | 0004345 | 2020-04-23 16:30:53 +0100 | [diff] [blame] | 4200 | /* |
| 4201 | * We must check if there is a prealloc extent that starts before the |
| 4202 | * i_size and crosses the i_size boundary. This is to ensure later we |
| 4203 | * truncate down to the end of that extent and not to the i_size, as |
| 4204 | * otherwise we end up losing part of the prealloc extent after a log |
| 4205 | * replay and with an implicit hole if there is another prealloc extent |
| 4206 | * that starts at an offset beyond i_size. |
| 4207 | */ |
| 4208 | ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY); |
| 4209 | if (ret < 0) |
| 4210 | goto out; |
| 4211 | |
| 4212 | if (ret == 0) { |
| 4213 | struct btrfs_file_extent_item *ei; |
| 4214 | |
| 4215 | leaf = path->nodes[0]; |
| 4216 | slot = path->slots[0]; |
| 4217 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 4218 | |
| 4219 | if (btrfs_file_extent_type(leaf, ei) == |
| 4220 | BTRFS_FILE_EXTENT_PREALLOC) { |
| 4221 | u64 extent_end; |
| 4222 | |
| 4223 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 4224 | extent_end = key.offset + |
| 4225 | btrfs_file_extent_num_bytes(leaf, ei); |
| 4226 | |
| 4227 | if (extent_end > i_size) |
| 4228 | truncate_offset = extent_end; |
| 4229 | } |
| 4230 | } else { |
| 4231 | ret = 0; |
| 4232 | } |
| 4233 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4234 | while (true) { |
Filipe Manana | 0004345 | 2020-04-23 16:30:53 +0100 | [diff] [blame] | 4235 | leaf = path->nodes[0]; |
| 4236 | slot = path->slots[0]; |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4237 | |
| 4238 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4239 | if (ins_nr > 0) { |
| 4240 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4241 | start_slot, ins_nr, 1, 0); |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4242 | if (ret < 0) |
| 4243 | goto out; |
| 4244 | ins_nr = 0; |
| 4245 | } |
| 4246 | ret = btrfs_next_leaf(root, path); |
| 4247 | if (ret < 0) |
| 4248 | goto out; |
| 4249 | if (ret > 0) { |
| 4250 | ret = 0; |
| 4251 | break; |
| 4252 | } |
| 4253 | continue; |
| 4254 | } |
| 4255 | |
| 4256 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 4257 | if (key.objectid > ino) |
| 4258 | break; |
| 4259 | if (WARN_ON_ONCE(key.objectid < ino) || |
| 4260 | key.type < BTRFS_EXTENT_DATA_KEY || |
| 4261 | key.offset < i_size) { |
| 4262 | path->slots[0]++; |
| 4263 | continue; |
| 4264 | } |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4265 | if (!dropped_extents) { |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4266 | /* |
| 4267 | * Avoid logging extent items logged in past fsync calls |
| 4268 | * and leading to duplicate keys in the log tree. |
| 4269 | */ |
| 4270 | do { |
| 4271 | ret = btrfs_truncate_inode_items(trans, |
| 4272 | root->log_root, |
| 4273 | &inode->vfs_inode, |
Filipe Manana | 0004345 | 2020-04-23 16:30:53 +0100 | [diff] [blame] | 4274 | truncate_offset, |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4275 | BTRFS_EXTENT_DATA_KEY); |
| 4276 | } while (ret == -EAGAIN); |
| 4277 | if (ret) |
| 4278 | goto out; |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4279 | dropped_extents = true; |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4280 | } |
| 4281 | if (ins_nr == 0) |
| 4282 | start_slot = slot; |
| 4283 | ins_nr++; |
| 4284 | path->slots[0]++; |
| 4285 | if (!dst_path) { |
| 4286 | dst_path = btrfs_alloc_path(); |
| 4287 | if (!dst_path) { |
| 4288 | ret = -ENOMEM; |
| 4289 | goto out; |
| 4290 | } |
| 4291 | } |
| 4292 | } |
| 4293 | if (ins_nr > 0) { |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4294 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4295 | start_slot, ins_nr, 1, 0); |
| 4296 | if (ret > 0) |
| 4297 | ret = 0; |
| 4298 | } |
| 4299 | out: |
| 4300 | btrfs_release_path(path); |
| 4301 | btrfs_free_path(dst_path); |
| 4302 | return ret; |
| 4303 | } |
| 4304 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4305 | static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, |
| 4306 | struct btrfs_root *root, |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4307 | struct btrfs_inode *inode, |
Miao Xie | 827463c | 2014-01-14 20:31:51 +0800 | [diff] [blame] | 4308 | struct btrfs_path *path, |
Filipe Manana | de0ee0e | 2016-01-21 10:17:54 +0000 | [diff] [blame] | 4309 | struct btrfs_log_ctx *ctx, |
| 4310 | const u64 start, |
| 4311 | const u64 end) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4312 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4313 | struct extent_map *em, *n; |
| 4314 | struct list_head extents; |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4315 | struct extent_map_tree *tree = &inode->extent_tree; |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4316 | u64 logged_start, logged_end; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4317 | u64 test_gen; |
| 4318 | int ret = 0; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4319 | int num = 0; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4320 | |
| 4321 | INIT_LIST_HEAD(&extents); |
| 4322 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4323 | write_lock(&tree->lock); |
| 4324 | test_gen = root->fs_info->last_trans_committed; |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4325 | logged_start = start; |
| 4326 | logged_end = end; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4327 | |
| 4328 | list_for_each_entry_safe(em, n, &tree->modified_extents, list) { |
Filipe Manana | fa625a4 | 2018-10-29 09:42:06 +0000 | [diff] [blame] | 4329 | /* |
| 4330 | * Skip extents outside our logging range. It's important to do |
| 4331 | * it for correctness because if we don't ignore them, we may |
| 4332 | * log them before their ordered extent completes, and therefore |
| 4333 | * we could log them without logging their respective checksums |
| 4334 | * (the checksum items are added to the csum tree at the very |
| 4335 | * end of btrfs_finish_ordered_io()). Also leave such extents |
| 4336 | * outside of our range in the list, since we may have another |
| 4337 | * ranged fsync in the near future that needs them. If an extent |
| 4338 | * outside our range corresponds to a hole, log it to avoid |
| 4339 | * leaving gaps between extents (fsck will complain when we are |
| 4340 | * not using the NO_HOLES feature). |
| 4341 | */ |
| 4342 | if ((em->start > end || em->start + em->len <= start) && |
| 4343 | em->block_start != EXTENT_MAP_HOLE) |
| 4344 | continue; |
| 4345 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4346 | list_del_init(&em->list); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4347 | /* |
| 4348 | * Just an arbitrary number, this can be really CPU intensive |
| 4349 | * once we start getting a lot of extents, and really once we |
| 4350 | * have a bunch of extents we just want to commit since it will |
| 4351 | * be faster. |
| 4352 | */ |
| 4353 | if (++num > 32768) { |
| 4354 | list_del_init(&tree->modified_extents); |
| 4355 | ret = -EFBIG; |
| 4356 | goto process; |
| 4357 | } |
| 4358 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4359 | if (em->generation <= test_gen) |
| 4360 | continue; |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4361 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4362 | /* We log prealloc extents beyond eof later. */ |
| 4363 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && |
| 4364 | em->start >= i_size_read(&inode->vfs_inode)) |
| 4365 | continue; |
| 4366 | |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4367 | if (em->start < logged_start) |
| 4368 | logged_start = em->start; |
| 4369 | if ((em->start + em->len - 1) > logged_end) |
| 4370 | logged_end = em->start + em->len - 1; |
| 4371 | |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4372 | /* Need a ref to keep it from getting evicted from cache */ |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 4373 | refcount_inc(&em->refs); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4374 | set_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4375 | list_add_tail(&em->list, &extents); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4376 | num++; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4377 | } |
| 4378 | |
| 4379 | list_sort(NULL, &extents, extent_cmp); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4380 | process: |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4381 | while (!list_empty(&extents)) { |
| 4382 | em = list_entry(extents.next, struct extent_map, list); |
| 4383 | |
| 4384 | list_del_init(&em->list); |
| 4385 | |
| 4386 | /* |
| 4387 | * If we had an error we just need to delete everybody from our |
| 4388 | * private list. |
| 4389 | */ |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4390 | if (ret) { |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 4391 | clear_em_logging(tree, em); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4392 | free_extent_map(em); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4393 | continue; |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | write_unlock(&tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4397 | |
Josef Bacik | a2120a4 | 2018-05-23 11:58:35 -0400 | [diff] [blame] | 4398 | ret = log_one_extent(trans, inode, root, em, path, ctx); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4399 | write_lock(&tree->lock); |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 4400 | clear_em_logging(tree, em); |
| 4401 | free_extent_map(em); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4402 | } |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4403 | WARN_ON(!list_empty(&extents)); |
| 4404 | write_unlock(&tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4405 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4406 | btrfs_release_path(path); |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4407 | if (!ret) |
| 4408 | ret = btrfs_log_prealloc_extents(trans, inode, path); |
| 4409 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4410 | return ret; |
| 4411 | } |
| 4412 | |
Nikolay Borisov | 481b01c | 2017-01-18 00:31:34 +0200 | [diff] [blame] | 4413 | 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] | 4414 | struct btrfs_path *path, u64 *size_ret) |
| 4415 | { |
| 4416 | struct btrfs_key key; |
| 4417 | int ret; |
| 4418 | |
Nikolay Borisov | 481b01c | 2017-01-18 00:31:34 +0200 | [diff] [blame] | 4419 | key.objectid = btrfs_ino(inode); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4420 | key.type = BTRFS_INODE_ITEM_KEY; |
| 4421 | key.offset = 0; |
| 4422 | |
| 4423 | ret = btrfs_search_slot(NULL, log, &key, path, 0, 0); |
| 4424 | if (ret < 0) { |
| 4425 | return ret; |
| 4426 | } else if (ret > 0) { |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 4427 | *size_ret = 0; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4428 | } else { |
| 4429 | struct btrfs_inode_item *item; |
| 4430 | |
| 4431 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4432 | struct btrfs_inode_item); |
| 4433 | *size_ret = btrfs_inode_size(path->nodes[0], item); |
Filipe Manana | 22dcb30 | 2019-03-04 14:06:12 +0000 | [diff] [blame] | 4434 | /* |
| 4435 | * If the in-memory inode's i_size is smaller then the inode |
| 4436 | * size stored in the btree, return the inode's i_size, so |
| 4437 | * that we get a correct inode size after replaying the log |
| 4438 | * when before a power failure we had a shrinking truncate |
| 4439 | * followed by addition of a new name (rename / new hard link). |
| 4440 | * Otherwise return the inode size from the btree, to avoid |
| 4441 | * data loss when replaying a log due to previously doing a |
| 4442 | * write that expands the inode's size and logging a new name |
| 4443 | * immediately after. |
| 4444 | */ |
| 4445 | if (*size_ret > inode->vfs_inode.i_size) |
| 4446 | *size_ret = inode->vfs_inode.i_size; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
| 4449 | btrfs_release_path(path); |
| 4450 | return 0; |
| 4451 | } |
| 4452 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4453 | /* |
| 4454 | * At the moment we always log all xattrs. This is to figure out at log replay |
| 4455 | * time which xattrs must have their deletion replayed. If a xattr is missing |
| 4456 | * in the log tree and exists in the fs/subvol tree, we delete it. This is |
| 4457 | * because if a xattr is deleted, the inode is fsynced and a power failure |
| 4458 | * happens, causing the log to be replayed the next time the fs is mounted, |
| 4459 | * we want the xattr to not exist anymore (same behaviour as other filesystems |
| 4460 | * with a journal, ext3/4, xfs, f2fs, etc). |
| 4461 | */ |
| 4462 | static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans, |
| 4463 | struct btrfs_root *root, |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4464 | struct btrfs_inode *inode, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4465 | struct btrfs_path *path, |
| 4466 | struct btrfs_path *dst_path) |
| 4467 | { |
| 4468 | int ret; |
| 4469 | struct btrfs_key key; |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4470 | const u64 ino = btrfs_ino(inode); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4471 | int ins_nr = 0; |
| 4472 | int start_slot = 0; |
| 4473 | |
| 4474 | key.objectid = ino; |
| 4475 | key.type = BTRFS_XATTR_ITEM_KEY; |
| 4476 | key.offset = 0; |
| 4477 | |
| 4478 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4479 | if (ret < 0) |
| 4480 | return ret; |
| 4481 | |
| 4482 | while (true) { |
| 4483 | int slot = path->slots[0]; |
| 4484 | struct extent_buffer *leaf = path->nodes[0]; |
| 4485 | int nritems = btrfs_header_nritems(leaf); |
| 4486 | |
| 4487 | if (slot >= nritems) { |
| 4488 | if (ins_nr > 0) { |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4489 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4490 | start_slot, ins_nr, 1, 0); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4491 | if (ret < 0) |
| 4492 | return ret; |
| 4493 | ins_nr = 0; |
| 4494 | } |
| 4495 | ret = btrfs_next_leaf(root, path); |
| 4496 | if (ret < 0) |
| 4497 | return ret; |
| 4498 | else if (ret > 0) |
| 4499 | break; |
| 4500 | continue; |
| 4501 | } |
| 4502 | |
| 4503 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 4504 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) |
| 4505 | break; |
| 4506 | |
| 4507 | if (ins_nr == 0) |
| 4508 | start_slot = slot; |
| 4509 | ins_nr++; |
| 4510 | path->slots[0]++; |
| 4511 | cond_resched(); |
| 4512 | } |
| 4513 | if (ins_nr > 0) { |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4514 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4515 | start_slot, ins_nr, 1, 0); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4516 | if (ret < 0) |
| 4517 | return ret; |
| 4518 | } |
| 4519 | |
| 4520 | return 0; |
| 4521 | } |
| 4522 | |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4523 | /* |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4524 | * When using the NO_HOLES feature if we punched a hole that causes the |
| 4525 | * deletion of entire leafs or all the extent items of the first leaf (the one |
| 4526 | * that contains the inode item and references) we may end up not processing |
| 4527 | * any extents, because there are no leafs with a generation matching the |
| 4528 | * current transaction that have extent items for our inode. So we need to find |
| 4529 | * if any holes exist and then log them. We also need to log holes after any |
| 4530 | * truncate operation that changes the inode's size. |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4531 | */ |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4532 | static int btrfs_log_holes(struct btrfs_trans_handle *trans, |
| 4533 | struct btrfs_root *root, |
| 4534 | struct btrfs_inode *inode, |
| 4535 | struct btrfs_path *path) |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4536 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4537 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4538 | struct btrfs_key key; |
Nikolay Borisov | a0308dd | 2017-01-18 00:31:38 +0200 | [diff] [blame] | 4539 | const u64 ino = btrfs_ino(inode); |
| 4540 | const u64 i_size = i_size_read(&inode->vfs_inode); |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4541 | u64 prev_extent_end = 0; |
| 4542 | int ret; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4543 | |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4544 | if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0) |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4545 | return 0; |
| 4546 | |
| 4547 | key.objectid = ino; |
| 4548 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4549 | key.offset = 0; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4550 | |
| 4551 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4552 | if (ret < 0) |
| 4553 | return ret; |
| 4554 | |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4555 | while (true) { |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4556 | struct btrfs_file_extent_item *extent; |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4557 | struct extent_buffer *leaf = path->nodes[0]; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4558 | u64 len; |
| 4559 | |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4560 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 4561 | ret = btrfs_next_leaf(root, path); |
| 4562 | if (ret < 0) |
| 4563 | return ret; |
| 4564 | if (ret > 0) { |
| 4565 | ret = 0; |
| 4566 | break; |
| 4567 | } |
| 4568 | leaf = path->nodes[0]; |
| 4569 | } |
| 4570 | |
| 4571 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4572 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) |
| 4573 | break; |
| 4574 | |
| 4575 | /* We have a hole, log it. */ |
| 4576 | if (prev_extent_end < key.offset) { |
| 4577 | const u64 hole_len = key.offset - prev_extent_end; |
| 4578 | |
| 4579 | /* |
| 4580 | * Release the path to avoid deadlocks with other code |
| 4581 | * paths that search the root while holding locks on |
| 4582 | * leafs from the log root. |
| 4583 | */ |
| 4584 | btrfs_release_path(path); |
| 4585 | ret = btrfs_insert_file_extent(trans, root->log_root, |
| 4586 | ino, prev_extent_end, 0, |
| 4587 | 0, hole_len, 0, hole_len, |
| 4588 | 0, 0, 0); |
| 4589 | if (ret < 0) |
| 4590 | return ret; |
| 4591 | |
| 4592 | /* |
| 4593 | * Search for the same key again in the root. Since it's |
| 4594 | * an extent item and we are holding the inode lock, the |
| 4595 | * key must still exist. If it doesn't just emit warning |
| 4596 | * and return an error to fall back to a transaction |
| 4597 | * commit. |
| 4598 | */ |
| 4599 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4600 | if (ret < 0) |
| 4601 | return ret; |
| 4602 | if (WARN_ON(ret > 0)) |
| 4603 | return -ENOENT; |
| 4604 | leaf = path->nodes[0]; |
| 4605 | } |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4606 | |
| 4607 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 4608 | struct btrfs_file_extent_item); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4609 | if (btrfs_file_extent_type(leaf, extent) == |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4610 | BTRFS_FILE_EXTENT_INLINE) { |
| 4611 | len = btrfs_file_extent_ram_bytes(leaf, extent); |
| 4612 | prev_extent_end = ALIGN(key.offset + len, |
| 4613 | fs_info->sectorsize); |
| 4614 | } else { |
| 4615 | len = btrfs_file_extent_num_bytes(leaf, extent); |
| 4616 | prev_extent_end = key.offset + len; |
| 4617 | } |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4618 | |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4619 | path->slots[0]++; |
| 4620 | cond_resched(); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4621 | } |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4622 | |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4623 | if (prev_extent_end < i_size) { |
| 4624 | u64 hole_len; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4625 | |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4626 | btrfs_release_path(path); |
| 4627 | hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize); |
| 4628 | ret = btrfs_insert_file_extent(trans, root->log_root, |
| 4629 | ino, prev_extent_end, 0, 0, |
| 4630 | hole_len, 0, hole_len, |
| 4631 | 0, 0, 0); |
| 4632 | if (ret < 0) |
| 4633 | return ret; |
| 4634 | } |
| 4635 | |
| 4636 | return 0; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4637 | } |
| 4638 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4639 | /* |
| 4640 | * When we are logging a new inode X, check if it doesn't have a reference that |
| 4641 | * matches the reference from some other inode Y created in a past transaction |
| 4642 | * and that was renamed in the current transaction. If we don't do this, then at |
| 4643 | * log replay time we can lose inode Y (and all its files if it's a directory): |
| 4644 | * |
| 4645 | * mkdir /mnt/x |
| 4646 | * echo "hello world" > /mnt/x/foobar |
| 4647 | * sync |
| 4648 | * mv /mnt/x /mnt/y |
| 4649 | * mkdir /mnt/x # or touch /mnt/x |
| 4650 | * xfs_io -c fsync /mnt/x |
| 4651 | * <power fail> |
| 4652 | * mount fs, trigger log replay |
| 4653 | * |
| 4654 | * After the log replay procedure, we would lose the first directory and all its |
| 4655 | * files (file foobar). |
| 4656 | * For the case where inode Y is not a directory we simply end up losing it: |
| 4657 | * |
| 4658 | * echo "123" > /mnt/foo |
| 4659 | * sync |
| 4660 | * mv /mnt/foo /mnt/bar |
| 4661 | * echo "abc" > /mnt/foo |
| 4662 | * xfs_io -c fsync /mnt/foo |
| 4663 | * <power fail> |
| 4664 | * |
| 4665 | * We also need this for cases where a snapshot entry is replaced by some other |
| 4666 | * entry (file or directory) otherwise we end up with an unreplayable log due to |
| 4667 | * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as |
| 4668 | * if it were a regular entry: |
| 4669 | * |
| 4670 | * mkdir /mnt/x |
| 4671 | * btrfs subvolume snapshot /mnt /mnt/x/snap |
| 4672 | * btrfs subvolume delete /mnt/x/snap |
| 4673 | * rmdir /mnt/x |
| 4674 | * mkdir /mnt/x |
| 4675 | * fsync /mnt/x or fsync some new file inside it |
| 4676 | * <power fail> |
| 4677 | * |
| 4678 | * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in |
| 4679 | * the same transaction. |
| 4680 | */ |
| 4681 | static int btrfs_check_ref_name_override(struct extent_buffer *eb, |
| 4682 | const int slot, |
| 4683 | const struct btrfs_key *key, |
Nikolay Borisov | 4791c8f | 2017-01-18 00:31:35 +0200 | [diff] [blame] | 4684 | struct btrfs_inode *inode, |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4685 | u64 *other_ino) |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4686 | { |
| 4687 | int ret; |
| 4688 | struct btrfs_path *search_path; |
| 4689 | char *name = NULL; |
| 4690 | u32 name_len = 0; |
| 4691 | u32 item_size = btrfs_item_size_nr(eb, slot); |
| 4692 | u32 cur_offset = 0; |
| 4693 | unsigned long ptr = btrfs_item_ptr_offset(eb, slot); |
| 4694 | |
| 4695 | search_path = btrfs_alloc_path(); |
| 4696 | if (!search_path) |
| 4697 | return -ENOMEM; |
| 4698 | search_path->search_commit_root = 1; |
| 4699 | search_path->skip_locking = 1; |
| 4700 | |
| 4701 | while (cur_offset < item_size) { |
| 4702 | u64 parent; |
| 4703 | u32 this_name_len; |
| 4704 | u32 this_len; |
| 4705 | unsigned long name_ptr; |
| 4706 | struct btrfs_dir_item *di; |
| 4707 | |
| 4708 | if (key->type == BTRFS_INODE_REF_KEY) { |
| 4709 | struct btrfs_inode_ref *iref; |
| 4710 | |
| 4711 | iref = (struct btrfs_inode_ref *)(ptr + cur_offset); |
| 4712 | parent = key->offset; |
| 4713 | this_name_len = btrfs_inode_ref_name_len(eb, iref); |
| 4714 | name_ptr = (unsigned long)(iref + 1); |
| 4715 | this_len = sizeof(*iref) + this_name_len; |
| 4716 | } else { |
| 4717 | struct btrfs_inode_extref *extref; |
| 4718 | |
| 4719 | extref = (struct btrfs_inode_extref *)(ptr + |
| 4720 | cur_offset); |
| 4721 | parent = btrfs_inode_extref_parent(eb, extref); |
| 4722 | this_name_len = btrfs_inode_extref_name_len(eb, extref); |
| 4723 | name_ptr = (unsigned long)&extref->name; |
| 4724 | this_len = sizeof(*extref) + this_name_len; |
| 4725 | } |
| 4726 | |
| 4727 | if (this_name_len > name_len) { |
| 4728 | char *new_name; |
| 4729 | |
| 4730 | new_name = krealloc(name, this_name_len, GFP_NOFS); |
| 4731 | if (!new_name) { |
| 4732 | ret = -ENOMEM; |
| 4733 | goto out; |
| 4734 | } |
| 4735 | name_len = this_name_len; |
| 4736 | name = new_name; |
| 4737 | } |
| 4738 | |
| 4739 | read_extent_buffer(eb, name, name_ptr, this_name_len); |
Nikolay Borisov | 4791c8f | 2017-01-18 00:31:35 +0200 | [diff] [blame] | 4740 | di = btrfs_lookup_dir_item(NULL, inode->root, search_path, |
| 4741 | parent, name, this_name_len, 0); |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4742 | if (di && !IS_ERR(di)) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4743 | struct btrfs_key di_key; |
| 4744 | |
| 4745 | btrfs_dir_item_key_to_cpu(search_path->nodes[0], |
| 4746 | di, &di_key); |
| 4747 | if (di_key.type == BTRFS_INODE_ITEM_KEY) { |
| 4748 | ret = 1; |
| 4749 | *other_ino = di_key.objectid; |
| 4750 | } else { |
| 4751 | ret = -EAGAIN; |
| 4752 | } |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4753 | goto out; |
| 4754 | } else if (IS_ERR(di)) { |
| 4755 | ret = PTR_ERR(di); |
| 4756 | goto out; |
| 4757 | } |
| 4758 | btrfs_release_path(search_path); |
| 4759 | |
| 4760 | cur_offset += this_len; |
| 4761 | } |
| 4762 | ret = 0; |
| 4763 | out: |
| 4764 | btrfs_free_path(search_path); |
| 4765 | kfree(name); |
| 4766 | return ret; |
| 4767 | } |
| 4768 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4769 | /* log a single inode in the tree log. |
| 4770 | * At least one parent directory for this inode must exist in the tree |
| 4771 | * or be logged already. |
| 4772 | * |
| 4773 | * Any items from this inode changed by the current transaction are copied |
| 4774 | * to the log tree. An extra reference is taken on any extents in this |
| 4775 | * file, allowing us to avoid a whole pile of corner cases around logging |
| 4776 | * blocks that have been removed from the tree. |
| 4777 | * |
| 4778 | * See LOG_INODE_ALL and related defines for a description of what inode_only |
| 4779 | * does. |
| 4780 | * |
| 4781 | * This handles both files and directories. |
| 4782 | */ |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4783 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4784 | struct btrfs_root *root, struct btrfs_inode *inode, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 4785 | int inode_only, |
| 4786 | const loff_t start, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4787 | const loff_t end, |
| 4788 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4789 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4790 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4791 | struct btrfs_path *path; |
| 4792 | struct btrfs_path *dst_path; |
| 4793 | struct btrfs_key min_key; |
| 4794 | struct btrfs_key max_key; |
| 4795 | struct btrfs_root *log = root->log_root; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4796 | int err = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4797 | int ret; |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 4798 | int nritems; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4799 | int ins_start_slot = 0; |
| 4800 | int ins_nr; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4801 | bool fast_search = false; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4802 | u64 ino = btrfs_ino(inode); |
| 4803 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4804 | u64 logged_isize = 0; |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 4805 | bool need_log_inode_item = true; |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 4806 | bool xattrs_logged = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4807 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4808 | path = btrfs_alloc_path(); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 4809 | if (!path) |
| 4810 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4811 | dst_path = btrfs_alloc_path(); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 4812 | if (!dst_path) { |
| 4813 | btrfs_free_path(path); |
| 4814 | return -ENOMEM; |
| 4815 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4816 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4817 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4818 | min_key.type = BTRFS_INODE_ITEM_KEY; |
| 4819 | min_key.offset = 0; |
| 4820 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4821 | max_key.objectid = ino; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4822 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4823 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4824 | /* today the code can only do partial logging of directories */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4825 | if (S_ISDIR(inode->vfs_inode.i_mode) || |
Miao Xie | 5269b67 | 2012-11-01 07:35:23 +0000 | [diff] [blame] | 4826 | (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4827 | &inode->runtime_flags) && |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4828 | inode_only >= LOG_INODE_EXISTS)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4829 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
| 4830 | else |
| 4831 | max_key.type = (u8)-1; |
| 4832 | max_key.offset = (u64)-1; |
| 4833 | |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4834 | /* |
| 4835 | * Only run delayed items if we are a dir or a new file. |
| 4836 | * Otherwise commit the delayed inode only, which is needed in |
| 4837 | * order for the log replay code to mark inodes for link count |
| 4838 | * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items). |
| 4839 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4840 | if (S_ISDIR(inode->vfs_inode.i_mode) || |
| 4841 | inode->generation > fs_info->last_trans_committed) |
| 4842 | ret = btrfs_commit_inode_delayed_items(trans, inode); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4843 | else |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4844 | ret = btrfs_commit_inode_delayed_inode(inode); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4845 | |
| 4846 | if (ret) { |
| 4847 | btrfs_free_path(path); |
| 4848 | btrfs_free_path(dst_path); |
| 4849 | return ret; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4850 | } |
| 4851 | |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4852 | if (inode_only == LOG_OTHER_INODE) { |
| 4853 | inode_only = LOG_INODE_EXISTS; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4854 | mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING); |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4855 | } else { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4856 | mutex_lock(&inode->log_mutex); |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4857 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4858 | |
Filipe Manana | 5e33a2b | 2016-02-25 23:19:38 +0000 | [diff] [blame] | 4859 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4860 | * a brute force approach to making sure we get the most uptodate |
| 4861 | * copies of everything. |
| 4862 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4863 | if (S_ISDIR(inode->vfs_inode.i_mode)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4864 | int max_key_type = BTRFS_DIR_LOG_INDEX_KEY; |
| 4865 | |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4866 | if (inode_only == LOG_INODE_EXISTS) |
| 4867 | max_key_type = BTRFS_XATTR_ITEM_KEY; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4868 | ret = drop_objectid_items(trans, log, path, ino, max_key_type); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4869 | } else { |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4870 | if (inode_only == LOG_INODE_EXISTS) { |
| 4871 | /* |
| 4872 | * Make sure the new inode item we write to the log has |
| 4873 | * the same isize as the current one (if it exists). |
| 4874 | * This is necessary to prevent data loss after log |
| 4875 | * replay, and also to prevent doing a wrong expanding |
| 4876 | * truncate - for e.g. create file, write 4K into offset |
| 4877 | * 0, fsync, write 4K into offset 4096, add hard link, |
| 4878 | * fsync some other file (to sync log), power fail - if |
| 4879 | * we use the inode's current i_size, after log replay |
| 4880 | * we get a 8Kb file, with the last 4Kb extent as a hole |
| 4881 | * (zeroes), as if an expanding truncate happened, |
| 4882 | * instead of getting a file of 4Kb only. |
| 4883 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4884 | err = logged_inode_size(log, inode, path, &logged_isize); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4885 | if (err) |
| 4886 | goto out_unlock; |
| 4887 | } |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4888 | if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4889 | &inode->runtime_flags)) { |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4890 | if (inode_only == LOG_INODE_EXISTS) { |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4891 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4892 | ret = drop_objectid_items(trans, log, path, ino, |
| 4893 | max_key.type); |
| 4894 | } else { |
| 4895 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4896 | &inode->runtime_flags); |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4897 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4898 | &inode->runtime_flags); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4899 | while(1) { |
| 4900 | ret = btrfs_truncate_inode_items(trans, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4901 | log, &inode->vfs_inode, 0, 0); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4902 | if (ret != -EAGAIN) |
| 4903 | break; |
| 4904 | } |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4905 | } |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4906 | } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4907 | &inode->runtime_flags) || |
Josef Bacik | 6cfab85 | 2013-11-12 16:25:58 -0500 | [diff] [blame] | 4908 | inode_only == LOG_INODE_EXISTS) { |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4909 | if (inode_only == LOG_INODE_ALL) |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4910 | fast_search = true; |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4911 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4912 | ret = drop_objectid_items(trans, log, path, ino, |
| 4913 | max_key.type); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4914 | } else { |
Liu Bo | 183f37f | 2012-11-01 06:38:47 +0000 | [diff] [blame] | 4915 | if (inode_only == LOG_INODE_ALL) |
| 4916 | fast_search = true; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4917 | goto log_extents; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4918 | } |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4919 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4920 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4921 | if (ret) { |
| 4922 | err = ret; |
| 4923 | goto out_unlock; |
| 4924 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4925 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4926 | while (1) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4927 | ins_nr = 0; |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 4928 | ret = btrfs_search_forward(root, &min_key, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4929 | path, trans->transid); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 4930 | if (ret < 0) { |
| 4931 | err = ret; |
| 4932 | goto out_unlock; |
| 4933 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4934 | if (ret != 0) |
| 4935 | break; |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 4936 | again: |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4937 | /* note, ins_nr might be > 0 here, cleanup outside the loop */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4938 | if (min_key.objectid != ino) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4939 | break; |
| 4940 | if (min_key.type > max_key.type) |
| 4941 | break; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4942 | |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 4943 | if (min_key.type == BTRFS_INODE_ITEM_KEY) |
| 4944 | need_log_inode_item = false; |
| 4945 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4946 | if ((min_key.type == BTRFS_INODE_REF_KEY || |
| 4947 | min_key.type == BTRFS_INODE_EXTREF_KEY) && |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4948 | inode->generation == trans->transid) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4949 | u64 other_ino = 0; |
| 4950 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4951 | ret = btrfs_check_ref_name_override(path->nodes[0], |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4952 | path->slots[0], &min_key, inode, |
| 4953 | &other_ino); |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4954 | if (ret < 0) { |
| 4955 | err = ret; |
| 4956 | goto out_unlock; |
Filipe Manana | 28a2359 | 2016-08-23 21:13:51 +0100 | [diff] [blame] | 4957 | } else if (ret > 0 && ctx && |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4958 | other_ino != btrfs_ino(BTRFS_I(ctx->inode))) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4959 | struct btrfs_key inode_key; |
| 4960 | struct inode *other_inode; |
| 4961 | |
| 4962 | if (ins_nr > 0) { |
| 4963 | ins_nr++; |
| 4964 | } else { |
| 4965 | ins_nr = 1; |
| 4966 | ins_start_slot = path->slots[0]; |
| 4967 | } |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4968 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 4969 | ins_start_slot, |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4970 | ins_nr, inode_only, |
| 4971 | logged_isize); |
| 4972 | if (ret < 0) { |
| 4973 | err = ret; |
| 4974 | goto out_unlock; |
| 4975 | } |
| 4976 | ins_nr = 0; |
| 4977 | btrfs_release_path(path); |
| 4978 | inode_key.objectid = other_ino; |
| 4979 | inode_key.type = BTRFS_INODE_ITEM_KEY; |
| 4980 | inode_key.offset = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4981 | other_inode = btrfs_iget(fs_info->sb, |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4982 | &inode_key, root, |
| 4983 | NULL); |
| 4984 | /* |
| 4985 | * If the other inode that had a conflicting dir |
| 4986 | * entry was deleted in the current transaction, |
| 4987 | * we don't need to do more work nor fallback to |
| 4988 | * a transaction commit. |
| 4989 | */ |
Al Viro | 8d9e220 | 2018-07-29 23:04:46 +0100 | [diff] [blame] | 4990 | if (other_inode == ERR_PTR(-ENOENT)) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4991 | goto next_key; |
| 4992 | } else if (IS_ERR(other_inode)) { |
| 4993 | err = PTR_ERR(other_inode); |
| 4994 | goto out_unlock; |
| 4995 | } |
| 4996 | /* |
| 4997 | * We are safe logging the other inode without |
| 4998 | * acquiring its i_mutex as long as we log with |
| 4999 | * the LOG_INODE_EXISTS mode. We're safe against |
| 5000 | * concurrent renames of the other inode as well |
| 5001 | * because during a rename we pin the log and |
| 5002 | * update the log with the new name before we |
| 5003 | * unpin it. |
| 5004 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5005 | err = btrfs_log_inode(trans, root, |
| 5006 | BTRFS_I(other_inode), |
| 5007 | LOG_OTHER_INODE, 0, LLONG_MAX, |
| 5008 | ctx); |
Filipe Manana | 7cbd49c | 2019-09-10 15:26:49 +0100 | [diff] [blame] | 5009 | btrfs_add_delayed_iput(other_inode); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5010 | if (err) |
| 5011 | goto out_unlock; |
| 5012 | else |
| 5013 | goto next_key; |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5014 | } |
| 5015 | } |
| 5016 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5017 | /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */ |
| 5018 | if (min_key.type == BTRFS_XATTR_ITEM_KEY) { |
| 5019 | if (ins_nr == 0) |
| 5020 | goto next_slot; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5021 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5022 | ins_start_slot, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5023 | ins_nr, inode_only, logged_isize); |
| 5024 | if (ret < 0) { |
| 5025 | err = ret; |
| 5026 | goto out_unlock; |
| 5027 | } |
| 5028 | ins_nr = 0; |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5029 | goto next_slot; |
| 5030 | } |
| 5031 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5032 | if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { |
| 5033 | ins_nr++; |
| 5034 | goto next_slot; |
| 5035 | } else if (!ins_nr) { |
| 5036 | ins_start_slot = path->slots[0]; |
| 5037 | ins_nr = 1; |
| 5038 | goto next_slot; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5039 | } |
| 5040 | |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5041 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5042 | ins_start_slot, ins_nr, inode_only, |
| 5043 | logged_isize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5044 | if (ret < 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5045 | err = ret; |
| 5046 | goto out_unlock; |
Rasmus Villemoes | a71db86 | 2014-06-20 21:51:43 +0200 | [diff] [blame] | 5047 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5048 | ins_nr = 1; |
| 5049 | ins_start_slot = path->slots[0]; |
| 5050 | next_slot: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5051 | |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 5052 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 5053 | path->slots[0]++; |
| 5054 | if (path->slots[0] < nritems) { |
| 5055 | btrfs_item_key_to_cpu(path->nodes[0], &min_key, |
| 5056 | path->slots[0]); |
| 5057 | goto again; |
| 5058 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5059 | if (ins_nr) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5060 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5061 | ins_start_slot, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5062 | ins_nr, inode_only, logged_isize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5063 | if (ret < 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5064 | err = ret; |
| 5065 | goto out_unlock; |
| 5066 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5067 | ins_nr = 0; |
| 5068 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5069 | btrfs_release_path(path); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5070 | next_key: |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5071 | if (min_key.offset < (u64)-1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5072 | min_key.offset++; |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5073 | } else if (min_key.type < max_key.type) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5074 | min_key.type++; |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5075 | min_key.offset = 0; |
| 5076 | } else { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5077 | break; |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5078 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5079 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5080 | if (ins_nr) { |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5081 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5082 | ins_start_slot, ins_nr, inode_only, |
| 5083 | logged_isize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5084 | if (ret < 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5085 | err = ret; |
| 5086 | goto out_unlock; |
| 5087 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5088 | ins_nr = 0; |
| 5089 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5090 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5091 | btrfs_release_path(path); |
| 5092 | btrfs_release_path(dst_path); |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5093 | err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5094 | if (err) |
| 5095 | goto out_unlock; |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 5096 | xattrs_logged = true; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5097 | if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) { |
| 5098 | btrfs_release_path(path); |
| 5099 | btrfs_release_path(dst_path); |
Filipe Manana | 587292a | 2019-11-19 12:07:33 +0000 | [diff] [blame] | 5100 | err = btrfs_log_holes(trans, root, inode, path); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5101 | if (err) |
| 5102 | goto out_unlock; |
| 5103 | } |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 5104 | log_extents: |
Josef Bacik | f3b15cc | 2013-07-22 12:54:30 -0400 | [diff] [blame] | 5105 | btrfs_release_path(path); |
| 5106 | btrfs_release_path(dst_path); |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 5107 | if (need_log_inode_item) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5108 | err = log_inode_item(trans, log, dst_path, inode); |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 5109 | if (!err && !xattrs_logged) { |
| 5110 | err = btrfs_log_all_xattrs(trans, root, inode, path, |
| 5111 | dst_path); |
| 5112 | btrfs_release_path(path); |
| 5113 | } |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 5114 | if (err) |
| 5115 | goto out_unlock; |
| 5116 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5117 | if (fast_search) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5118 | ret = btrfs_log_changed_extents(trans, root, inode, dst_path, |
Josef Bacik | a2120a4 | 2018-05-23 11:58:35 -0400 | [diff] [blame] | 5119 | ctx, start, end); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5120 | if (ret) { |
| 5121 | err = ret; |
| 5122 | goto out_unlock; |
| 5123 | } |
Josef Bacik | d006a04 | 2013-11-12 20:54:09 -0500 | [diff] [blame] | 5124 | } else if (inode_only == LOG_INODE_ALL) { |
Liu Bo | 06d3d22 | 2012-08-27 10:52:19 -0600 | [diff] [blame] | 5125 | struct extent_map *em, *n; |
| 5126 | |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5127 | write_lock(&em_tree->lock); |
| 5128 | /* |
| 5129 | * We can't just remove every em if we're called for a ranged |
| 5130 | * fsync - that is, one that doesn't cover the whole possible |
| 5131 | * file range (0 to LLONG_MAX). This is because we can have |
| 5132 | * em's that fall outside the range we're logging and therefore |
| 5133 | * their ordered operations haven't completed yet |
| 5134 | * (btrfs_finish_ordered_io() not invoked yet). This means we |
| 5135 | * didn't get their respective file extent item in the fs/subvol |
| 5136 | * tree yet, and need to let the next fast fsync (one which |
| 5137 | * consults the list of modified extent maps) find the em so |
| 5138 | * that it logs a matching file extent item and waits for the |
| 5139 | * respective ordered operation to complete (if it's still |
| 5140 | * running). |
| 5141 | * |
| 5142 | * Removing every em outside the range we're logging would make |
| 5143 | * the next fast fsync not log their matching file extent items, |
| 5144 | * therefore making us lose data after a log replay. |
| 5145 | */ |
| 5146 | list_for_each_entry_safe(em, n, &em_tree->modified_extents, |
| 5147 | list) { |
| 5148 | const u64 mod_end = em->mod_start + em->mod_len - 1; |
| 5149 | |
| 5150 | if (em->mod_start >= start && mod_end <= end) |
| 5151 | list_del_init(&em->list); |
| 5152 | } |
| 5153 | write_unlock(&em_tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5154 | } |
| 5155 | |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5156 | if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) { |
| 5157 | ret = log_directory_changes(trans, root, inode, path, dst_path, |
| 5158 | ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5159 | if (ret) { |
| 5160 | err = ret; |
| 5161 | goto out_unlock; |
| 5162 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5163 | } |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5164 | |
Filipe Manana | 110850f | 2019-06-07 11:25:24 +0100 | [diff] [blame] | 5165 | /* |
| 5166 | * Don't update last_log_commit if we logged that an inode exists after |
| 5167 | * it was loaded to memory (full_sync bit set). |
| 5168 | * This is to prevent data loss when we do a write to the inode, then |
| 5169 | * the inode gets evicted after all delalloc was flushed, then we log |
| 5170 | * it exists (due to a rename for example) and then fsync it. This last |
| 5171 | * fsync would do nothing (not logging the extents previously written). |
| 5172 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5173 | spin_lock(&inode->lock); |
| 5174 | inode->logged_trans = trans->transid; |
Filipe Manana | 110850f | 2019-06-07 11:25:24 +0100 | [diff] [blame] | 5175 | if (inode_only != LOG_INODE_EXISTS || |
| 5176 | !test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags)) |
| 5177 | inode->last_log_commit = inode->last_sub_trans; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5178 | spin_unlock(&inode->lock); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5179 | out_unlock: |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5180 | mutex_unlock(&inode->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5181 | |
| 5182 | btrfs_free_path(path); |
| 5183 | btrfs_free_path(dst_path); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5184 | return err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5185 | } |
| 5186 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5187 | /* |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5188 | * Check if we must fallback to a transaction commit when logging an inode. |
| 5189 | * This must be called after logging the inode and is used only in the context |
| 5190 | * when fsyncing an inode requires the need to log some other inode - in which |
| 5191 | * case we can't lock the i_mutex of each other inode we need to log as that |
| 5192 | * can lead to deadlocks with concurrent fsync against other inodes (as we can |
| 5193 | * log inodes up or down in the hierarchy) or rename operations for example. So |
| 5194 | * we take the log_mutex of the inode after we have logged it and then check for |
| 5195 | * its last_unlink_trans value - this is safe because any task setting |
| 5196 | * last_unlink_trans must take the log_mutex and it must do this before it does |
| 5197 | * the actual unlink operation, so if we do this check before a concurrent task |
| 5198 | * sets last_unlink_trans it means we've logged a consistent version/state of |
| 5199 | * all the inode items, otherwise we are not sure and must do a transaction |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5200 | * commit (the concurrent task might have only updated last_unlink_trans before |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5201 | * we logged the inode or it might have also done the unlink). |
| 5202 | */ |
| 5203 | static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans, |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5204 | struct btrfs_inode *inode) |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5205 | { |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5206 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5207 | bool ret = false; |
| 5208 | |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5209 | mutex_lock(&inode->log_mutex); |
| 5210 | if (inode->last_unlink_trans > fs_info->last_trans_committed) { |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5211 | /* |
| 5212 | * Make sure any commits to the log are forced to be full |
| 5213 | * commits. |
| 5214 | */ |
| 5215 | btrfs_set_log_full_commit(fs_info, trans); |
| 5216 | ret = true; |
| 5217 | } |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5218 | mutex_unlock(&inode->log_mutex); |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5219 | |
| 5220 | return ret; |
| 5221 | } |
| 5222 | |
| 5223 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5224 | * follow the dentry parent pointers up the chain and see if any |
| 5225 | * of the directories in it require a full commit before they can |
| 5226 | * be logged. Returns zero if nothing special needs to be done or 1 if |
| 5227 | * a full commit is required. |
| 5228 | */ |
| 5229 | static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans, |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5230 | struct btrfs_inode *inode, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5231 | struct dentry *parent, |
| 5232 | struct super_block *sb, |
| 5233 | u64 last_committed) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5234 | { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5235 | int ret = 0; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5236 | struct dentry *old_parent = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5237 | |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5238 | /* |
| 5239 | * for regular files, if its inode is already on disk, we don't |
| 5240 | * have to worry about the parents at all. This is because |
| 5241 | * we can use the last_unlink_trans field to record renames |
| 5242 | * and other fun in this file. |
| 5243 | */ |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5244 | if (S_ISREG(inode->vfs_inode.i_mode) && |
| 5245 | inode->generation <= last_committed && |
| 5246 | inode->last_unlink_trans <= last_committed) |
| 5247 | goto out; |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5248 | |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5249 | if (!S_ISDIR(inode->vfs_inode.i_mode)) { |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5250 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5251 | goto out; |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5252 | inode = BTRFS_I(d_inode(parent)); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5253 | } |
| 5254 | |
| 5255 | while (1) { |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5256 | if (btrfs_must_commit_transaction(trans, inode)) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5257 | ret = 1; |
| 5258 | break; |
| 5259 | } |
| 5260 | |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5261 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5262 | break; |
| 5263 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5264 | if (IS_ROOT(parent)) { |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5265 | inode = BTRFS_I(d_inode(parent)); |
| 5266 | if (btrfs_must_commit_transaction(trans, inode)) |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5267 | ret = 1; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5268 | break; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5269 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5270 | |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5271 | parent = dget_parent(parent); |
| 5272 | dput(old_parent); |
| 5273 | old_parent = parent; |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5274 | inode = BTRFS_I(d_inode(parent)); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5275 | |
| 5276 | } |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5277 | dput(old_parent); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5278 | out: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5279 | return ret; |
| 5280 | } |
| 5281 | |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5282 | struct btrfs_dir_list { |
| 5283 | u64 ino; |
| 5284 | struct list_head list; |
| 5285 | }; |
| 5286 | |
| 5287 | /* |
| 5288 | * Log the inodes of the new dentries of a directory. See log_dir_items() for |
| 5289 | * details about the why it is needed. |
| 5290 | * This is a recursive operation - if an existing dentry corresponds to a |
| 5291 | * directory, that directory's new entries are logged too (same behaviour as |
| 5292 | * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes |
| 5293 | * the dentries point to we do not lock their i_mutex, otherwise lockdep |
| 5294 | * complains about the following circular lock dependency / possible deadlock: |
| 5295 | * |
| 5296 | * CPU0 CPU1 |
| 5297 | * ---- ---- |
| 5298 | * lock(&type->i_mutex_dir_key#3/2); |
| 5299 | * lock(sb_internal#2); |
| 5300 | * lock(&type->i_mutex_dir_key#3/2); |
| 5301 | * lock(&sb->s_type->i_mutex_key#14); |
| 5302 | * |
| 5303 | * Where sb_internal is the lock (a counter that works as a lock) acquired by |
| 5304 | * sb_start_intwrite() in btrfs_start_transaction(). |
| 5305 | * Not locking i_mutex of the inodes is still safe because: |
| 5306 | * |
| 5307 | * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible |
| 5308 | * that while logging the inode new references (names) are added or removed |
| 5309 | * from the inode, leaving the logged inode item with a link count that does |
| 5310 | * not match the number of logged inode reference items. This is fine because |
| 5311 | * at log replay time we compute the real number of links and correct the |
| 5312 | * link count in the inode item (see replay_one_buffer() and |
| 5313 | * link_to_fixup_dir()); |
| 5314 | * |
| 5315 | * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that |
| 5316 | * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and |
| 5317 | * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item |
| 5318 | * has a size that doesn't match the sum of the lengths of all the logged |
| 5319 | * names. This does not result in a problem because if a dir_item key is |
| 5320 | * logged but its matching dir_index key is not logged, at log replay time we |
| 5321 | * don't use it to replay the respective name (see replay_one_name()). On the |
| 5322 | * other hand if only the dir_index key ends up being logged, the respective |
| 5323 | * name is added to the fs/subvol tree with both the dir_item and dir_index |
| 5324 | * keys created (see replay_one_name()). |
| 5325 | * The directory's inode item with a wrong i_size is not a problem as well, |
| 5326 | * since we don't use it at log replay time to set the i_size in the inode |
| 5327 | * item of the fs/subvol tree (see overwrite_item()). |
| 5328 | */ |
| 5329 | static int log_new_dir_dentries(struct btrfs_trans_handle *trans, |
| 5330 | struct btrfs_root *root, |
Nikolay Borisov | 51cc0d3 | 2017-01-18 00:31:43 +0200 | [diff] [blame] | 5331 | struct btrfs_inode *start_inode, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5332 | struct btrfs_log_ctx *ctx) |
| 5333 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5334 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5335 | struct btrfs_root *log = root->log_root; |
| 5336 | struct btrfs_path *path; |
| 5337 | LIST_HEAD(dir_list); |
| 5338 | struct btrfs_dir_list *dir_elem; |
| 5339 | int ret = 0; |
| 5340 | |
| 5341 | path = btrfs_alloc_path(); |
| 5342 | if (!path) |
| 5343 | return -ENOMEM; |
| 5344 | |
| 5345 | dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS); |
| 5346 | if (!dir_elem) { |
| 5347 | btrfs_free_path(path); |
| 5348 | return -ENOMEM; |
| 5349 | } |
Nikolay Borisov | 51cc0d3 | 2017-01-18 00:31:43 +0200 | [diff] [blame] | 5350 | dir_elem->ino = btrfs_ino(start_inode); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5351 | list_add_tail(&dir_elem->list, &dir_list); |
| 5352 | |
| 5353 | while (!list_empty(&dir_list)) { |
| 5354 | struct extent_buffer *leaf; |
| 5355 | struct btrfs_key min_key; |
| 5356 | int nritems; |
| 5357 | int i; |
| 5358 | |
| 5359 | dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, |
| 5360 | list); |
| 5361 | if (ret) |
| 5362 | goto next_dir_inode; |
| 5363 | |
| 5364 | min_key.objectid = dir_elem->ino; |
| 5365 | min_key.type = BTRFS_DIR_ITEM_KEY; |
| 5366 | min_key.offset = 0; |
| 5367 | again: |
| 5368 | btrfs_release_path(path); |
| 5369 | ret = btrfs_search_forward(log, &min_key, path, trans->transid); |
| 5370 | if (ret < 0) { |
| 5371 | goto next_dir_inode; |
| 5372 | } else if (ret > 0) { |
| 5373 | ret = 0; |
| 5374 | goto next_dir_inode; |
| 5375 | } |
| 5376 | |
| 5377 | process_leaf: |
| 5378 | leaf = path->nodes[0]; |
| 5379 | nritems = btrfs_header_nritems(leaf); |
| 5380 | for (i = path->slots[0]; i < nritems; i++) { |
| 5381 | struct btrfs_dir_item *di; |
| 5382 | struct btrfs_key di_key; |
| 5383 | struct inode *di_inode; |
| 5384 | struct btrfs_dir_list *new_dir_elem; |
| 5385 | int log_mode = LOG_INODE_EXISTS; |
| 5386 | int type; |
| 5387 | |
| 5388 | btrfs_item_key_to_cpu(leaf, &min_key, i); |
| 5389 | if (min_key.objectid != dir_elem->ino || |
| 5390 | min_key.type != BTRFS_DIR_ITEM_KEY) |
| 5391 | goto next_dir_inode; |
| 5392 | |
| 5393 | di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item); |
| 5394 | type = btrfs_dir_type(leaf, di); |
| 5395 | if (btrfs_dir_transid(leaf, di) < trans->transid && |
| 5396 | type != BTRFS_FT_DIR) |
| 5397 | continue; |
| 5398 | btrfs_dir_item_key_to_cpu(leaf, di, &di_key); |
| 5399 | if (di_key.type == BTRFS_ROOT_ITEM_KEY) |
| 5400 | continue; |
| 5401 | |
Robbie Ko | ec125cf | 2016-10-28 10:48:26 +0800 | [diff] [blame] | 5402 | btrfs_release_path(path); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5403 | di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5404 | if (IS_ERR(di_inode)) { |
| 5405 | ret = PTR_ERR(di_inode); |
| 5406 | goto next_dir_inode; |
| 5407 | } |
| 5408 | |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 5409 | if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) { |
Filipe Manana | 7cbd49c | 2019-09-10 15:26:49 +0100 | [diff] [blame] | 5410 | btrfs_add_delayed_iput(di_inode); |
Robbie Ko | ec125cf | 2016-10-28 10:48:26 +0800 | [diff] [blame] | 5411 | break; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | ctx->log_new_dentries = false; |
Filipe Manana | 3f9749f | 2016-04-25 04:45:02 +0100 | [diff] [blame] | 5415 | if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK) |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5416 | log_mode = LOG_INODE_ALL; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5417 | ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode), |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5418 | log_mode, 0, LLONG_MAX, ctx); |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5419 | if (!ret && |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5420 | btrfs_must_commit_transaction(trans, BTRFS_I(di_inode))) |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5421 | ret = 1; |
Filipe Manana | 7cbd49c | 2019-09-10 15:26:49 +0100 | [diff] [blame] | 5422 | btrfs_add_delayed_iput(di_inode); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5423 | if (ret) |
| 5424 | goto next_dir_inode; |
| 5425 | if (ctx->log_new_dentries) { |
| 5426 | new_dir_elem = kmalloc(sizeof(*new_dir_elem), |
| 5427 | GFP_NOFS); |
| 5428 | if (!new_dir_elem) { |
| 5429 | ret = -ENOMEM; |
| 5430 | goto next_dir_inode; |
| 5431 | } |
| 5432 | new_dir_elem->ino = di_key.objectid; |
| 5433 | list_add_tail(&new_dir_elem->list, &dir_list); |
| 5434 | } |
| 5435 | break; |
| 5436 | } |
| 5437 | if (i == nritems) { |
| 5438 | ret = btrfs_next_leaf(log, path); |
| 5439 | if (ret < 0) { |
| 5440 | goto next_dir_inode; |
| 5441 | } else if (ret > 0) { |
| 5442 | ret = 0; |
| 5443 | goto next_dir_inode; |
| 5444 | } |
| 5445 | goto process_leaf; |
| 5446 | } |
| 5447 | if (min_key.offset < (u64)-1) { |
| 5448 | min_key.offset++; |
| 5449 | goto again; |
| 5450 | } |
| 5451 | next_dir_inode: |
| 5452 | list_del(&dir_elem->list); |
| 5453 | kfree(dir_elem); |
| 5454 | } |
| 5455 | |
| 5456 | btrfs_free_path(path); |
| 5457 | return ret; |
| 5458 | } |
| 5459 | |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5460 | static int btrfs_log_all_parents(struct btrfs_trans_handle *trans, |
Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 5461 | struct btrfs_inode *inode, |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5462 | struct btrfs_log_ctx *ctx) |
| 5463 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 5464 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5465 | int ret; |
| 5466 | struct btrfs_path *path; |
| 5467 | struct btrfs_key key; |
Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 5468 | struct btrfs_root *root = inode->root; |
| 5469 | const u64 ino = btrfs_ino(inode); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5470 | |
| 5471 | path = btrfs_alloc_path(); |
| 5472 | if (!path) |
| 5473 | return -ENOMEM; |
| 5474 | path->skip_locking = 1; |
| 5475 | path->search_commit_root = 1; |
| 5476 | |
| 5477 | key.objectid = ino; |
| 5478 | key.type = BTRFS_INODE_REF_KEY; |
| 5479 | key.offset = 0; |
| 5480 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5481 | if (ret < 0) |
| 5482 | goto out; |
| 5483 | |
| 5484 | while (true) { |
| 5485 | struct extent_buffer *leaf = path->nodes[0]; |
| 5486 | int slot = path->slots[0]; |
| 5487 | u32 cur_offset = 0; |
| 5488 | u32 item_size; |
| 5489 | unsigned long ptr; |
| 5490 | |
| 5491 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5492 | ret = btrfs_next_leaf(root, path); |
| 5493 | if (ret < 0) |
| 5494 | goto out; |
| 5495 | else if (ret > 0) |
| 5496 | break; |
| 5497 | continue; |
| 5498 | } |
| 5499 | |
| 5500 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5501 | /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */ |
| 5502 | if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY) |
| 5503 | break; |
| 5504 | |
| 5505 | item_size = btrfs_item_size_nr(leaf, slot); |
| 5506 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 5507 | while (cur_offset < item_size) { |
| 5508 | struct btrfs_key inode_key; |
| 5509 | struct inode *dir_inode; |
| 5510 | |
| 5511 | inode_key.type = BTRFS_INODE_ITEM_KEY; |
| 5512 | inode_key.offset = 0; |
| 5513 | |
| 5514 | if (key.type == BTRFS_INODE_EXTREF_KEY) { |
| 5515 | struct btrfs_inode_extref *extref; |
| 5516 | |
| 5517 | extref = (struct btrfs_inode_extref *) |
| 5518 | (ptr + cur_offset); |
| 5519 | inode_key.objectid = btrfs_inode_extref_parent( |
| 5520 | leaf, extref); |
| 5521 | cur_offset += sizeof(*extref); |
| 5522 | cur_offset += btrfs_inode_extref_name_len(leaf, |
| 5523 | extref); |
| 5524 | } else { |
| 5525 | inode_key.objectid = key.offset; |
| 5526 | cur_offset = item_size; |
| 5527 | } |
| 5528 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5529 | dir_inode = btrfs_iget(fs_info->sb, &inode_key, |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5530 | root, NULL); |
Filipe Manana | d2c6df3 | 2018-10-09 15:05:29 +0100 | [diff] [blame] | 5531 | /* |
| 5532 | * If the parent inode was deleted, return an error to |
| 5533 | * fallback to a transaction commit. This is to prevent |
| 5534 | * getting an inode that was moved from one parent A to |
| 5535 | * a parent B, got its former parent A deleted and then |
| 5536 | * it got fsync'ed, from existing at both parents after |
| 5537 | * a log replay (and the old parent still existing). |
| 5538 | * Example: |
| 5539 | * |
| 5540 | * mkdir /mnt/A |
| 5541 | * mkdir /mnt/B |
| 5542 | * touch /mnt/B/bar |
| 5543 | * sync |
| 5544 | * mv /mnt/B/bar /mnt/A/bar |
| 5545 | * mv -T /mnt/A /mnt/B |
| 5546 | * fsync /mnt/B/bar |
| 5547 | * <power fail> |
| 5548 | * |
| 5549 | * If we ignore the old parent B which got deleted, |
| 5550 | * after a log replay we would have file bar linked |
| 5551 | * at both parents and the old parent B would still |
| 5552 | * exist. |
| 5553 | */ |
| 5554 | if (IS_ERR(dir_inode)) { |
| 5555 | ret = PTR_ERR(dir_inode); |
| 5556 | goto out; |
| 5557 | } |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5558 | |
Filipe Manana | 657ed1a | 2016-04-06 17:11:56 +0100 | [diff] [blame] | 5559 | if (ctx) |
| 5560 | ctx->log_new_dentries = false; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5561 | ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode), |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5562 | LOG_INODE_ALL, 0, LLONG_MAX, ctx); |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5563 | if (!ret && |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5564 | btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode))) |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5565 | ret = 1; |
Filipe Manana | 657ed1a | 2016-04-06 17:11:56 +0100 | [diff] [blame] | 5566 | if (!ret && ctx && ctx->log_new_dentries) |
| 5567 | ret = log_new_dir_dentries(trans, root, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5568 | BTRFS_I(dir_inode), ctx); |
Filipe Manana | 7cbd49c | 2019-09-10 15:26:49 +0100 | [diff] [blame] | 5569 | btrfs_add_delayed_iput(dir_inode); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5570 | if (ret) |
| 5571 | goto out; |
| 5572 | } |
| 5573 | path->slots[0]++; |
| 5574 | } |
| 5575 | ret = 0; |
| 5576 | out: |
| 5577 | btrfs_free_path(path); |
| 5578 | return ret; |
| 5579 | } |
| 5580 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5581 | /* |
| 5582 | * helper function around btrfs_log_inode to make sure newly created |
| 5583 | * parent directories also end up in the log. A minimal inode and backref |
| 5584 | * only logging is done of any parent directories that are older than |
| 5585 | * the last committed transaction |
| 5586 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5587 | static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5588 | struct btrfs_inode *inode, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5589 | struct dentry *parent, |
| 5590 | const loff_t start, |
| 5591 | const loff_t end, |
Edmund Nadolski | 41a1ead | 2017-11-20 13:24:47 -0700 | [diff] [blame] | 5592 | int inode_only, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5593 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5594 | { |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5595 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5596 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5597 | struct super_block *sb; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5598 | struct dentry *old_parent = NULL; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5599 | int ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5600 | u64 last_committed = fs_info->last_trans_committed; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5601 | bool log_dentries = false; |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5602 | struct btrfs_inode *orig_inode = inode; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5603 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5604 | sb = inode->vfs_inode.i_sb; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5605 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5606 | if (btrfs_test_opt(fs_info, NOTREELOG)) { |
Sage Weil | 3a5e140 | 2009-04-02 16:49:40 -0400 | [diff] [blame] | 5607 | ret = 1; |
| 5608 | goto end_no_trans; |
| 5609 | } |
| 5610 | |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 5611 | /* |
| 5612 | * The prev transaction commit doesn't complete, we need do |
| 5613 | * full commit by ourselves. |
| 5614 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5615 | if (fs_info->last_trans_log_full_commit > |
| 5616 | fs_info->last_trans_committed) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5617 | ret = 1; |
| 5618 | goto end_no_trans; |
| 5619 | } |
| 5620 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5621 | if (btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5622 | ret = 1; |
| 5623 | goto end_no_trans; |
| 5624 | } |
| 5625 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5626 | ret = check_parent_dirs_for_sync(trans, inode, parent, sb, |
| 5627 | last_committed); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5628 | if (ret) |
| 5629 | goto end_no_trans; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5630 | |
Filipe Manana | 55f21e1 | 2018-10-08 11:12:55 +0100 | [diff] [blame] | 5631 | /* |
| 5632 | * Skip already logged inodes or inodes corresponding to tmpfiles |
| 5633 | * (since logging them is pointless, a link count of 0 means they |
| 5634 | * will never be accessible). |
| 5635 | */ |
| 5636 | if (btrfs_inode_in_log(inode, trans->transid) || |
| 5637 | inode->vfs_inode.i_nlink == 0) { |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5638 | ret = BTRFS_NO_LOG_SYNC; |
| 5639 | goto end_no_trans; |
| 5640 | } |
| 5641 | |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5642 | ret = start_log_trans(trans, root, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5643 | if (ret) |
Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 5644 | goto end_no_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5645 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5646 | ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5647 | if (ret) |
| 5648 | goto end_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5649 | |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5650 | /* |
| 5651 | * for regular files, if its inode is already on disk, we don't |
| 5652 | * have to worry about the parents at all. This is because |
| 5653 | * we can use the last_unlink_trans field to record renames |
| 5654 | * and other fun in this file. |
| 5655 | */ |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5656 | if (S_ISREG(inode->vfs_inode.i_mode) && |
| 5657 | inode->generation <= last_committed && |
| 5658 | inode->last_unlink_trans <= last_committed) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5659 | ret = 0; |
| 5660 | goto end_trans; |
| 5661 | } |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5662 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5663 | if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries) |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5664 | log_dentries = true; |
| 5665 | |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5666 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5667 | * 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] | 5668 | * inodes are fully logged. This is to prevent leaving dangling |
| 5669 | * directory index entries in directories that were our parents but are |
| 5670 | * not anymore. Not doing this results in old parent directory being |
| 5671 | * impossible to delete after log replay (rmdir will always fail with |
| 5672 | * error -ENOTEMPTY). |
| 5673 | * |
| 5674 | * Example 1: |
| 5675 | * |
| 5676 | * mkdir testdir |
| 5677 | * touch testdir/foo |
| 5678 | * ln testdir/foo testdir/bar |
| 5679 | * sync |
| 5680 | * unlink testdir/bar |
| 5681 | * xfs_io -c fsync testdir/foo |
| 5682 | * <power failure> |
| 5683 | * mount fs, triggers log replay |
| 5684 | * |
| 5685 | * If we don't log the parent directory (testdir), after log replay the |
| 5686 | * directory still has an entry pointing to the file inode using the bar |
| 5687 | * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and |
| 5688 | * the file inode has a link count of 1. |
| 5689 | * |
| 5690 | * Example 2: |
| 5691 | * |
| 5692 | * mkdir testdir |
| 5693 | * touch foo |
| 5694 | * ln foo testdir/foo2 |
| 5695 | * ln foo testdir/foo3 |
| 5696 | * sync |
| 5697 | * unlink testdir/foo3 |
| 5698 | * xfs_io -c fsync foo |
| 5699 | * <power failure> |
| 5700 | * mount fs, triggers log replay |
| 5701 | * |
| 5702 | * Similar as the first example, after log replay the parent directory |
| 5703 | * testdir still has an entry pointing to the inode file with name foo3 |
| 5704 | * but the file inode does not have a matching BTRFS_INODE_REF_KEY item |
| 5705 | * and has a link count of 2. |
| 5706 | */ |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5707 | if (inode->last_unlink_trans > last_committed) { |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5708 | ret = btrfs_log_all_parents(trans, orig_inode, ctx); |
| 5709 | if (ret) |
| 5710 | goto end_trans; |
| 5711 | } |
| 5712 | |
Filipe Manana | 10b0421 | 2018-11-28 14:54:28 +0000 | [diff] [blame] | 5713 | /* |
| 5714 | * If a new hard link was added to the inode in the current transaction |
| 5715 | * and its link count is now greater than 1, we need to fallback to a |
| 5716 | * transaction commit, otherwise we can end up not logging all its new |
| 5717 | * parents for all the hard links. Here just from the dentry used to |
| 5718 | * fsync, we can not visit the ancestor inodes for all the other hard |
| 5719 | * links to figure out if any is new, so we fallback to a transaction |
| 5720 | * commit (instead of adding a lot of complexity of scanning a btree, |
| 5721 | * since this scenario is not a common use case). |
| 5722 | */ |
| 5723 | if (inode->vfs_inode.i_nlink > 1 && |
| 5724 | inode->last_link_trans > last_committed) { |
| 5725 | ret = -EMLINK; |
| 5726 | goto end_trans; |
| 5727 | } |
| 5728 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5729 | while (1) { |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5730 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5731 | break; |
| 5732 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5733 | inode = BTRFS_I(d_inode(parent)); |
| 5734 | if (root != inode->root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5735 | break; |
| 5736 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5737 | if (inode->generation > last_committed) { |
| 5738 | ret = btrfs_log_inode(trans, root, inode, |
| 5739 | LOG_INODE_EXISTS, 0, LLONG_MAX, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5740 | if (ret) |
| 5741 | goto end_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5742 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5743 | if (IS_ROOT(parent)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5744 | break; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5745 | |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5746 | parent = dget_parent(parent); |
| 5747 | dput(old_parent); |
| 5748 | old_parent = parent; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5749 | } |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5750 | if (log_dentries) |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5751 | ret = log_new_dir_dentries(trans, root, orig_inode, ctx); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5752 | else |
| 5753 | ret = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5754 | end_trans: |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5755 | dput(old_parent); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5756 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5757 | btrfs_set_log_full_commit(fs_info, trans); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5758 | ret = 1; |
| 5759 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5760 | |
| 5761 | if (ret) |
| 5762 | btrfs_remove_log_ctx(root, ctx); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5763 | btrfs_end_log_trans(root); |
| 5764 | end_no_trans: |
| 5765 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | /* |
| 5769 | * it is not safe to log dentry if the chunk root has added new |
| 5770 | * chunks. This returns 0 if the dentry was logged, and 1 otherwise. |
| 5771 | * If this returns 1, you must commit the transaction to safely get your |
| 5772 | * data on disk. |
| 5773 | */ |
| 5774 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e5b84f7a | 2018-02-27 17:37:18 +0200 | [diff] [blame] | 5775 | struct dentry *dentry, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5776 | const loff_t start, |
| 5777 | const loff_t end, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5778 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5779 | { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5780 | struct dentry *parent = dget_parent(dentry); |
| 5781 | int ret; |
| 5782 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5783 | ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent, |
| 5784 | start, end, LOG_INODE_ALL, ctx); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5785 | dput(parent); |
| 5786 | |
| 5787 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5788 | } |
| 5789 | |
| 5790 | /* |
| 5791 | * should be called during mount to recover any replay any log trees |
| 5792 | * from the FS |
| 5793 | */ |
| 5794 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) |
| 5795 | { |
| 5796 | int ret; |
| 5797 | struct btrfs_path *path; |
| 5798 | struct btrfs_trans_handle *trans; |
| 5799 | struct btrfs_key key; |
| 5800 | struct btrfs_key found_key; |
| 5801 | struct btrfs_key tmp_key; |
| 5802 | struct btrfs_root *log; |
| 5803 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; |
| 5804 | struct walk_control wc = { |
| 5805 | .process_func = process_one_buffer, |
| 5806 | .stage = 0, |
| 5807 | }; |
| 5808 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5809 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5810 | if (!path) |
| 5811 | return -ENOMEM; |
| 5812 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 5813 | set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5814 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5815 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5816 | if (IS_ERR(trans)) { |
| 5817 | ret = PTR_ERR(trans); |
| 5818 | goto error; |
| 5819 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5820 | |
| 5821 | wc.trans = trans; |
| 5822 | wc.pin = 1; |
| 5823 | |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5824 | ret = walk_log_tree(trans, log_root_tree, &wc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5825 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5826 | btrfs_handle_fs_error(fs_info, ret, |
| 5827 | "Failed to pin buffers while recovering log root tree."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5828 | goto error; |
| 5829 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5830 | |
| 5831 | again: |
| 5832 | key.objectid = BTRFS_TREE_LOG_OBJECTID; |
| 5833 | key.offset = (u64)-1; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5834 | key.type = BTRFS_ROOT_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5835 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5836 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5837 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5838 | |
| 5839 | if (ret < 0) { |
Anand Jain | 34d9700 | 2016-03-16 16:43:06 +0800 | [diff] [blame] | 5840 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5841 | "Couldn't find tree log root."); |
| 5842 | goto error; |
| 5843 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5844 | if (ret > 0) { |
| 5845 | if (path->slots[0] == 0) |
| 5846 | break; |
| 5847 | path->slots[0]--; |
| 5848 | } |
| 5849 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 5850 | path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5851 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5852 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 5853 | break; |
| 5854 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 5855 | log = btrfs_read_fs_root(log_root_tree, &found_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5856 | if (IS_ERR(log)) { |
| 5857 | ret = PTR_ERR(log); |
Anand Jain | 34d9700 | 2016-03-16 16:43:06 +0800 | [diff] [blame] | 5858 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5859 | "Couldn't read tree log root."); |
| 5860 | goto error; |
| 5861 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5862 | |
| 5863 | tmp_key.objectid = found_key.offset; |
| 5864 | tmp_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5865 | tmp_key.offset = (u64)-1; |
| 5866 | |
| 5867 | wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5868 | if (IS_ERR(wc.replay_dest)) { |
| 5869 | ret = PTR_ERR(wc.replay_dest); |
Josef Bacik | f5c9735 | 2019-12-06 09:37:17 -0500 | [diff] [blame] | 5870 | |
| 5871 | /* |
| 5872 | * We didn't find the subvol, likely because it was |
| 5873 | * deleted. This is ok, simply skip this log and go to |
| 5874 | * the next one. |
| 5875 | * |
| 5876 | * We need to exclude the root because we can't have |
| 5877 | * other log replays overwriting this log as we'll read |
| 5878 | * it back in a few more times. This will keep our |
| 5879 | * block from being modified, and we'll just bail for |
| 5880 | * each subsequent pass. |
| 5881 | */ |
| 5882 | if (ret == -ENOENT) |
| 5883 | ret = btrfs_pin_extent_for_log_replay(fs_info, |
| 5884 | log->node->start, |
| 5885 | log->node->len); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5886 | free_extent_buffer(log->node); |
| 5887 | free_extent_buffer(log->commit_root); |
| 5888 | kfree(log); |
Josef Bacik | f5c9735 | 2019-12-06 09:37:17 -0500 | [diff] [blame] | 5889 | |
| 5890 | if (!ret) |
| 5891 | goto next; |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5892 | btrfs_handle_fs_error(fs_info, ret, |
| 5893 | "Couldn't read target root for tree log recovery."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5894 | goto error; |
| 5895 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5896 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 5897 | wc.replay_dest->log_root = log; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5898 | btrfs_record_root_in_trans(trans, wc.replay_dest); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5899 | ret = walk_log_tree(trans, log, &wc); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5900 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5901 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5902 | ret = fixup_inode_link_counts(trans, wc.replay_dest, |
| 5903 | path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5904 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5905 | |
Liu Bo | 900c998 | 2018-01-25 11:02:56 -0700 | [diff] [blame] | 5906 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
| 5907 | struct btrfs_root *root = wc.replay_dest; |
| 5908 | |
| 5909 | btrfs_release_path(path); |
| 5910 | |
| 5911 | /* |
| 5912 | * We have just replayed everything, and the highest |
| 5913 | * objectid of fs roots probably has changed in case |
| 5914 | * some inode_item's got replayed. |
| 5915 | * |
| 5916 | * root->objectid_mutex is not acquired as log replay |
| 5917 | * could only happen during mount. |
| 5918 | */ |
| 5919 | ret = btrfs_find_highest_objectid(root, |
| 5920 | &root->highest_objectid); |
| 5921 | } |
| 5922 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 5923 | wc.replay_dest->log_root = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5924 | free_extent_buffer(log->node); |
Chris Mason | b263c2c | 2009-06-11 11:24:47 -0400 | [diff] [blame] | 5925 | free_extent_buffer(log->commit_root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5926 | kfree(log); |
| 5927 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5928 | if (ret) |
| 5929 | goto error; |
Josef Bacik | f5c9735 | 2019-12-06 09:37:17 -0500 | [diff] [blame] | 5930 | next: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5931 | if (found_key.offset == 0) |
| 5932 | break; |
Josef Bacik | f5c9735 | 2019-12-06 09:37:17 -0500 | [diff] [blame] | 5933 | key.offset = found_key.offset - 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5934 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5935 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5936 | |
| 5937 | /* step one is to pin it all, step two is to replay just inodes */ |
| 5938 | if (wc.pin) { |
| 5939 | wc.pin = 0; |
| 5940 | wc.process_func = replay_one_buffer; |
| 5941 | wc.stage = LOG_WALK_REPLAY_INODES; |
| 5942 | goto again; |
| 5943 | } |
| 5944 | /* step three is to replay everything */ |
| 5945 | if (wc.stage < LOG_WALK_REPLAY_ALL) { |
| 5946 | wc.stage++; |
| 5947 | goto again; |
| 5948 | } |
| 5949 | |
| 5950 | btrfs_free_path(path); |
| 5951 | |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 5952 | /* step 4: commit the transaction, which also unpins the blocks */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5953 | ret = btrfs_commit_transaction(trans); |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 5954 | if (ret) |
| 5955 | return ret; |
| 5956 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5957 | free_extent_buffer(log_root_tree->node); |
| 5958 | log_root_tree->log_root = NULL; |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 5959 | clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5960 | kfree(log_root_tree); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5961 | |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 5962 | return 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5963 | error: |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5964 | if (wc.trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5965 | btrfs_end_transaction(wc.trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5966 | btrfs_free_path(path); |
| 5967 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5968 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5969 | |
| 5970 | /* |
| 5971 | * there are some corner cases where we want to force a full |
| 5972 | * commit instead of allowing a directory to be logged. |
| 5973 | * |
| 5974 | * They revolve around files there were unlinked from the directory, and |
| 5975 | * this function updates the parent directory so that a full commit is |
| 5976 | * 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] | 5977 | * |
| 5978 | * Must be called before the unlink operations (updates to the subvolume tree, |
| 5979 | * inodes, etc) are done. |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5980 | */ |
| 5981 | void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5982 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5983 | int for_rename) |
| 5984 | { |
| 5985 | /* |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5986 | * when we're logging a file, if it hasn't been renamed |
| 5987 | * or unlinked, and its inode is fully committed on disk, |
| 5988 | * we don't have to worry about walking up the directory chain |
| 5989 | * to log its parents. |
| 5990 | * |
| 5991 | * So, we use the last_unlink_trans field to put this transid |
| 5992 | * into the file. When the file is logged we check it and |
| 5993 | * don't log the parents if the file is fully on disk. |
| 5994 | */ |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5995 | mutex_lock(&inode->log_mutex); |
| 5996 | inode->last_unlink_trans = trans->transid; |
| 5997 | mutex_unlock(&inode->log_mutex); |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5998 | |
| 5999 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6000 | * if this directory was already logged any new |
| 6001 | * names for this file/dir will get recorded |
| 6002 | */ |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 6003 | if (dir->logged_trans == trans->transid) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6004 | return; |
| 6005 | |
| 6006 | /* |
| 6007 | * if the inode we're about to unlink was logged, |
| 6008 | * the log will be properly updated for any new names |
| 6009 | */ |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 6010 | if (inode->logged_trans == trans->transid) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6011 | return; |
| 6012 | |
| 6013 | /* |
| 6014 | * when renaming files across directories, if the directory |
| 6015 | * there we're unlinking from gets fsync'd later on, there's |
| 6016 | * no way to find the destination directory later and fsync it |
| 6017 | * properly. So, we have to be conservative and force commits |
| 6018 | * so the new name gets discovered. |
| 6019 | */ |
| 6020 | if (for_rename) |
| 6021 | goto record; |
| 6022 | |
| 6023 | /* we can safely do the unlink without any special recording */ |
| 6024 | return; |
| 6025 | |
| 6026 | record: |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 6027 | mutex_lock(&dir->log_mutex); |
| 6028 | dir->last_unlink_trans = trans->transid; |
| 6029 | mutex_unlock(&dir->log_mutex); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6030 | } |
| 6031 | |
| 6032 | /* |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 6033 | * Make sure that if someone attempts to fsync the parent directory of a deleted |
| 6034 | * snapshot, it ends up triggering a transaction commit. This is to guarantee |
| 6035 | * that after replaying the log tree of the parent directory's root we will not |
| 6036 | * see the snapshot anymore and at log replay time we will not see any log tree |
| 6037 | * corresponding to the deleted snapshot's root, which could lead to replaying |
| 6038 | * it after replaying the log tree of the parent directory (which would replay |
| 6039 | * the snapshot delete operation). |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 6040 | * |
| 6041 | * Must be called before the actual snapshot destroy operation (updates to the |
| 6042 | * parent root and tree of tree roots trees, etc) are done. |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 6043 | */ |
| 6044 | void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4366355 | 2017-01-18 00:31:29 +0200 | [diff] [blame] | 6045 | struct btrfs_inode *dir) |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 6046 | { |
Nikolay Borisov | 4366355 | 2017-01-18 00:31:29 +0200 | [diff] [blame] | 6047 | mutex_lock(&dir->log_mutex); |
| 6048 | dir->last_unlink_trans = trans->transid; |
| 6049 | mutex_unlock(&dir->log_mutex); |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 6050 | } |
| 6051 | |
| 6052 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6053 | * Call this after adding a new name for a file and it will properly |
| 6054 | * update the log to reflect the new name. |
| 6055 | * |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6056 | * @ctx can not be NULL when @sync_log is false, and should be NULL when it's |
| 6057 | * true (because it's not used). |
| 6058 | * |
| 6059 | * Return value depends on whether @sync_log is true or false. |
| 6060 | * When true: returns BTRFS_NEED_TRANS_COMMIT if the transaction needs to be |
| 6061 | * committed by the caller, and BTRFS_DONT_NEED_TRANS_COMMIT |
| 6062 | * otherwise. |
| 6063 | * When false: returns BTRFS_DONT_NEED_LOG_SYNC if the caller does not need to |
| 6064 | * to sync the log, BTRFS_NEED_LOG_SYNC if it needs to sync the log, |
| 6065 | * or BTRFS_NEED_TRANS_COMMIT if the transaction needs to be |
| 6066 | * committed (without attempting to sync the log). |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6067 | */ |
| 6068 | int btrfs_log_new_name(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6069 | struct btrfs_inode *inode, struct btrfs_inode *old_dir, |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6070 | struct dentry *parent, |
| 6071 | bool sync_log, struct btrfs_log_ctx *ctx) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6072 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 6073 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6074 | int ret; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6075 | |
| 6076 | /* |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 6077 | * this will force the logging code to walk the dentry chain |
| 6078 | * up for the file |
| 6079 | */ |
Filipe Manana | 9a6509c | 2018-02-28 15:55:40 +0000 | [diff] [blame] | 6080 | if (!S_ISDIR(inode->vfs_inode.i_mode)) |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6081 | inode->last_unlink_trans = trans->transid; |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 6082 | |
| 6083 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6084 | * if this inode hasn't been logged and directory we're renaming it |
| 6085 | * from hasn't been logged, we don't need to log it |
| 6086 | */ |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6087 | if (inode->logged_trans <= fs_info->last_trans_committed && |
| 6088 | (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed)) |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6089 | return sync_log ? BTRFS_DONT_NEED_TRANS_COMMIT : |
| 6090 | BTRFS_DONT_NEED_LOG_SYNC; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6091 | |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6092 | if (sync_log) { |
| 6093 | struct btrfs_log_ctx ctx2; |
| 6094 | |
| 6095 | btrfs_init_log_ctx(&ctx2, &inode->vfs_inode); |
| 6096 | ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX, |
| 6097 | LOG_INODE_EXISTS, &ctx2); |
| 6098 | if (ret == BTRFS_NO_LOG_SYNC) |
| 6099 | return BTRFS_DONT_NEED_TRANS_COMMIT; |
| 6100 | else if (ret) |
| 6101 | return BTRFS_NEED_TRANS_COMMIT; |
| 6102 | |
| 6103 | ret = btrfs_sync_log(trans, inode->root, &ctx2); |
| 6104 | if (ret) |
| 6105 | return BTRFS_NEED_TRANS_COMMIT; |
| 6106 | return BTRFS_DONT_NEED_TRANS_COMMIT; |
| 6107 | } |
| 6108 | |
| 6109 | ASSERT(ctx); |
| 6110 | ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX, |
| 6111 | LOG_INODE_EXISTS, ctx); |
| 6112 | if (ret == BTRFS_NO_LOG_SYNC) |
| 6113 | return BTRFS_DONT_NEED_LOG_SYNC; |
| 6114 | else if (ret) |
| 6115 | return BTRFS_NEED_TRANS_COMMIT; |
| 6116 | |
| 6117 | return BTRFS_NEED_LOG_SYNC; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6118 | } |
| 6119 | |