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 | |
| 261 | /* the root we are currently replaying */ |
| 262 | struct btrfs_root *replay_dest; |
| 263 | |
| 264 | /* the trans handle for the current replay */ |
| 265 | struct btrfs_trans_handle *trans; |
| 266 | |
| 267 | /* the function that gets used to process blocks we find in the |
| 268 | * tree. Note the extent_buffer might not be up to date when it is |
| 269 | * passed in, and it must be checked or read if you need the data |
| 270 | * inside it |
| 271 | */ |
| 272 | int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 273 | struct walk_control *wc, u64 gen, int level); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | /* |
| 277 | * process_func used to pin down extents, write them or wait on them |
| 278 | */ |
| 279 | static int process_one_buffer(struct btrfs_root *log, |
| 280 | struct extent_buffer *eb, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 281 | struct walk_control *wc, u64 gen, int level) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 282 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 283 | struct btrfs_fs_info *fs_info = log->fs_info; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 284 | int ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 285 | |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 286 | /* |
| 287 | * If this fs is mixed then we need to be able to process the leaves to |
| 288 | * pin down any logged extents, so we have to read the block. |
| 289 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 290 | if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 291 | ret = btrfs_read_buffer(eb, gen, level, NULL); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 292 | if (ret) |
| 293 | return ret; |
| 294 | } |
| 295 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 296 | if (wc->pin) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 297 | ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start, |
| 298 | eb->len); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 299 | |
| 300 | if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) { |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 301 | if (wc->pin && btrfs_header_level(eb) == 0) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 302 | ret = btrfs_exclude_logged_extents(fs_info, eb); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 303 | if (wc->write) |
| 304 | btrfs_write_tree_block(eb); |
| 305 | if (wc->wait) |
| 306 | btrfs_wait_tree_block_writeback(eb); |
| 307 | } |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 308 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Item overwrite used by replay and tree logging. eb, slot and key all refer |
| 313 | * to the src data we are copying out. |
| 314 | * |
| 315 | * root is the tree we are copying into, and path is a scratch |
| 316 | * path for use in this function (it should be released on entry and |
| 317 | * will be released on exit). |
| 318 | * |
| 319 | * If the key is already in the destination tree the existing item is |
| 320 | * overwritten. If the existing item isn't big enough, it is extended. |
| 321 | * If it is too large, it is truncated. |
| 322 | * |
| 323 | * If the key isn't in the destination yet, a new item is inserted. |
| 324 | */ |
| 325 | static noinline int overwrite_item(struct btrfs_trans_handle *trans, |
| 326 | struct btrfs_root *root, |
| 327 | struct btrfs_path *path, |
| 328 | struct extent_buffer *eb, int slot, |
| 329 | struct btrfs_key *key) |
| 330 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 331 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 332 | int ret; |
| 333 | u32 item_size; |
| 334 | u64 saved_i_size = 0; |
| 335 | int save_old_i_size = 0; |
| 336 | unsigned long src_ptr; |
| 337 | unsigned long dst_ptr; |
| 338 | int overwrite_root = 0; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 339 | bool inode_item = key->type == BTRFS_INODE_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 340 | |
| 341 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 342 | overwrite_root = 1; |
| 343 | |
| 344 | item_size = btrfs_item_size_nr(eb, slot); |
| 345 | src_ptr = btrfs_item_ptr_offset(eb, slot); |
| 346 | |
| 347 | /* look for the key in the destination tree */ |
| 348 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 349 | if (ret < 0) |
| 350 | return ret; |
| 351 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 352 | if (ret == 0) { |
| 353 | char *src_copy; |
| 354 | char *dst_copy; |
| 355 | u32 dst_size = btrfs_item_size_nr(path->nodes[0], |
| 356 | path->slots[0]); |
| 357 | if (dst_size != item_size) |
| 358 | goto insert; |
| 359 | |
| 360 | if (item_size == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 361 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | dst_copy = kmalloc(item_size, GFP_NOFS); |
| 365 | src_copy = kmalloc(item_size, GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 366 | if (!dst_copy || !src_copy) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 367 | btrfs_release_path(path); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 368 | kfree(dst_copy); |
| 369 | kfree(src_copy); |
| 370 | return -ENOMEM; |
| 371 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 372 | |
| 373 | read_extent_buffer(eb, src_copy, src_ptr, item_size); |
| 374 | |
| 375 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
| 376 | read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, |
| 377 | item_size); |
| 378 | ret = memcmp(dst_copy, src_copy, item_size); |
| 379 | |
| 380 | kfree(dst_copy); |
| 381 | kfree(src_copy); |
| 382 | /* |
| 383 | * they have the same contents, just return, this saves |
| 384 | * us from cowing blocks in the destination tree and doing |
| 385 | * extra writes that may not have been done by a previous |
| 386 | * sync |
| 387 | */ |
| 388 | if (ret == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 389 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 393 | /* |
| 394 | * We need to load the old nbytes into the inode so when we |
| 395 | * replay the extents we've logged we get the right nbytes. |
| 396 | */ |
| 397 | if (inode_item) { |
| 398 | struct btrfs_inode_item *item; |
| 399 | u64 nbytes; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 400 | u32 mode; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 401 | |
| 402 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 403 | struct btrfs_inode_item); |
| 404 | nbytes = btrfs_inode_nbytes(path->nodes[0], item); |
| 405 | item = btrfs_item_ptr(eb, slot, |
| 406 | struct btrfs_inode_item); |
| 407 | btrfs_set_inode_nbytes(eb, item, nbytes); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 408 | |
| 409 | /* |
| 410 | * If this is a directory we need to reset the i_size to |
| 411 | * 0 so that we can set it up properly when replaying |
| 412 | * the rest of the items in this log. |
| 413 | */ |
| 414 | mode = btrfs_inode_mode(eb, item); |
| 415 | if (S_ISDIR(mode)) |
| 416 | btrfs_set_inode_size(eb, item, 0); |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 417 | } |
| 418 | } else if (inode_item) { |
| 419 | struct btrfs_inode_item *item; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 420 | u32 mode; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 421 | |
| 422 | /* |
| 423 | * New inode, set nbytes to 0 so that the nbytes comes out |
| 424 | * properly when we replay the extents. |
| 425 | */ |
| 426 | item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); |
| 427 | btrfs_set_inode_nbytes(eb, item, 0); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * If this is a directory we need to reset the i_size to 0 so |
| 431 | * that we can set it up properly when replaying the rest of |
| 432 | * the items in this log. |
| 433 | */ |
| 434 | mode = btrfs_inode_mode(eb, item); |
| 435 | if (S_ISDIR(mode)) |
| 436 | btrfs_set_inode_size(eb, item, 0); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 437 | } |
| 438 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 439 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 440 | /* try to insert the key into the destination tree */ |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 441 | path->skip_release_on_error = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 442 | ret = btrfs_insert_empty_item(trans, root, path, |
| 443 | key, item_size); |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 444 | path->skip_release_on_error = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 445 | |
| 446 | /* make sure any existing item is the correct size */ |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 447 | if (ret == -EEXIST || ret == -EOVERFLOW) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 448 | u32 found_size; |
| 449 | found_size = btrfs_item_size_nr(path->nodes[0], |
| 450 | path->slots[0]); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 451 | if (found_size > item_size) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 452 | btrfs_truncate_item(fs_info, path, item_size, 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 453 | else if (found_size < item_size) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 454 | btrfs_extend_item(fs_info, path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 455 | item_size - found_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 456 | } else if (ret) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 457 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 458 | } |
| 459 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], |
| 460 | path->slots[0]); |
| 461 | |
| 462 | /* don't overwrite an existing inode if the generation number |
| 463 | * was logged as zero. This is done when the tree logging code |
| 464 | * is just logging an inode to make sure it exists after recovery. |
| 465 | * |
| 466 | * Also, don't overwrite i_size on directories during replay. |
| 467 | * log replay inserts and removes directory items based on the |
| 468 | * state of the tree found in the subvolume, and i_size is modified |
| 469 | * as it goes |
| 470 | */ |
| 471 | if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) { |
| 472 | struct btrfs_inode_item *src_item; |
| 473 | struct btrfs_inode_item *dst_item; |
| 474 | |
| 475 | src_item = (struct btrfs_inode_item *)src_ptr; |
| 476 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 477 | |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 478 | if (btrfs_inode_generation(eb, src_item) == 0) { |
| 479 | struct extent_buffer *dst_eb = path->nodes[0]; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 480 | const u64 ino_size = btrfs_inode_size(eb, src_item); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 481 | |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 482 | /* |
| 483 | * For regular files an ino_size == 0 is used only when |
| 484 | * logging that an inode exists, as part of a directory |
| 485 | * fsync, and the inode wasn't fsynced before. In this |
| 486 | * case don't set the size of the inode in the fs/subvol |
| 487 | * tree, otherwise we would be throwing valid data away. |
| 488 | */ |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 489 | if (S_ISREG(btrfs_inode_mode(eb, src_item)) && |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 490 | S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) && |
| 491 | ino_size != 0) { |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 492 | struct btrfs_map_token token; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 493 | |
| 494 | btrfs_init_map_token(&token); |
| 495 | btrfs_set_token_inode_size(dst_eb, dst_item, |
| 496 | ino_size, &token); |
| 497 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 498 | goto no_copy; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 499 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 500 | |
| 501 | if (overwrite_root && |
| 502 | S_ISDIR(btrfs_inode_mode(eb, src_item)) && |
| 503 | S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { |
| 504 | save_old_i_size = 1; |
| 505 | saved_i_size = btrfs_inode_size(path->nodes[0], |
| 506 | dst_item); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | copy_extent_buffer(path->nodes[0], eb, dst_ptr, |
| 511 | src_ptr, item_size); |
| 512 | |
| 513 | if (save_old_i_size) { |
| 514 | struct btrfs_inode_item *dst_item; |
| 515 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 516 | btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); |
| 517 | } |
| 518 | |
| 519 | /* make sure the generation is filled in */ |
| 520 | if (key->type == BTRFS_INODE_ITEM_KEY) { |
| 521 | struct btrfs_inode_item *dst_item; |
| 522 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 523 | if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { |
| 524 | btrfs_set_inode_generation(path->nodes[0], dst_item, |
| 525 | trans->transid); |
| 526 | } |
| 527 | } |
| 528 | no_copy: |
| 529 | btrfs_mark_buffer_dirty(path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 530 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * simple helper to read an inode off the disk from a given root |
| 536 | * This can only be called for subvolume roots and not for the log |
| 537 | */ |
| 538 | static noinline struct inode *read_one_inode(struct btrfs_root *root, |
| 539 | u64 objectid) |
| 540 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 541 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 542 | struct inode *inode; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 543 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 544 | key.objectid = objectid; |
| 545 | key.type = BTRFS_INODE_ITEM_KEY; |
| 546 | key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 547 | inode = btrfs_iget(root->fs_info->sb, &key, root, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 548 | if (IS_ERR(inode)) { |
| 549 | inode = NULL; |
| 550 | } else if (is_bad_inode(inode)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 551 | iput(inode); |
| 552 | inode = NULL; |
| 553 | } |
| 554 | return inode; |
| 555 | } |
| 556 | |
| 557 | /* replays a single extent in 'eb' at 'slot' with 'key' into the |
| 558 | * subvolume 'root'. path is released on entry and should be released |
| 559 | * on exit. |
| 560 | * |
| 561 | * extents in the log tree have not been allocated out of the extent |
| 562 | * tree yet. So, this completes the allocation, taking a reference |
| 563 | * as required if the extent already exists or creating a new extent |
| 564 | * if it isn't in the extent allocation tree yet. |
| 565 | * |
| 566 | * The extent is inserted into the file, dropping any existing extents |
| 567 | * from the file that overlap the new one. |
| 568 | */ |
| 569 | static noinline int replay_one_extent(struct btrfs_trans_handle *trans, |
| 570 | struct btrfs_root *root, |
| 571 | struct btrfs_path *path, |
| 572 | struct extent_buffer *eb, int slot, |
| 573 | struct btrfs_key *key) |
| 574 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 575 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 576 | int found_type; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 577 | u64 extent_end; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 578 | u64 start = key->offset; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 579 | u64 nbytes = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 580 | struct btrfs_file_extent_item *item; |
| 581 | struct inode *inode = NULL; |
| 582 | unsigned long size; |
| 583 | int ret = 0; |
| 584 | |
| 585 | item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 586 | found_type = btrfs_file_extent_type(eb, item); |
| 587 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 588 | if (found_type == BTRFS_FILE_EXTENT_REG || |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 589 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 590 | nbytes = btrfs_file_extent_num_bytes(eb, item); |
| 591 | extent_end = start + nbytes; |
| 592 | |
| 593 | /* |
| 594 | * We don't add to the inodes nbytes if we are prealloc or a |
| 595 | * hole. |
| 596 | */ |
| 597 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 598 | nbytes = 0; |
| 599 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 600 | size = btrfs_file_extent_ram_bytes(eb, item); |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 601 | nbytes = btrfs_file_extent_ram_bytes(eb, item); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 602 | extent_end = ALIGN(start + size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 603 | fs_info->sectorsize); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 604 | } else { |
| 605 | ret = 0; |
| 606 | goto out; |
| 607 | } |
| 608 | |
| 609 | inode = read_one_inode(root, key->objectid); |
| 610 | if (!inode) { |
| 611 | ret = -EIO; |
| 612 | goto out; |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * first check to see if we already have this extent in the |
| 617 | * file. This must be done before the btrfs_drop_extents run |
| 618 | * so we don't try to drop this extent. |
| 619 | */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 620 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 621 | btrfs_ino(BTRFS_I(inode)), start, 0); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 622 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 623 | if (ret == 0 && |
| 624 | (found_type == BTRFS_FILE_EXTENT_REG || |
| 625 | found_type == BTRFS_FILE_EXTENT_PREALLOC)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 626 | struct btrfs_file_extent_item cmp1; |
| 627 | struct btrfs_file_extent_item cmp2; |
| 628 | struct btrfs_file_extent_item *existing; |
| 629 | struct extent_buffer *leaf; |
| 630 | |
| 631 | leaf = path->nodes[0]; |
| 632 | existing = btrfs_item_ptr(leaf, path->slots[0], |
| 633 | struct btrfs_file_extent_item); |
| 634 | |
| 635 | read_extent_buffer(eb, &cmp1, (unsigned long)item, |
| 636 | sizeof(cmp1)); |
| 637 | read_extent_buffer(leaf, &cmp2, (unsigned long)existing, |
| 638 | sizeof(cmp2)); |
| 639 | |
| 640 | /* |
| 641 | * we already have a pointer to this exact extent, |
| 642 | * we don't have to do anything |
| 643 | */ |
| 644 | if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 645 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 646 | goto out; |
| 647 | } |
| 648 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 649 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 650 | |
| 651 | /* drop any overlapping extents */ |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 652 | ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 653 | if (ret) |
| 654 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 655 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 656 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 657 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 658 | u64 offset; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 659 | unsigned long dest_offset; |
| 660 | struct btrfs_key ins; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 661 | |
Filipe Manana | 3168021c | 2017-02-01 14:58:02 +0000 | [diff] [blame] | 662 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0 && |
| 663 | btrfs_fs_incompat(fs_info, NO_HOLES)) |
| 664 | goto update_inode; |
| 665 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 666 | ret = btrfs_insert_empty_item(trans, root, path, key, |
| 667 | sizeof(*item)); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 668 | if (ret) |
| 669 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 670 | dest_offset = btrfs_item_ptr_offset(path->nodes[0], |
| 671 | path->slots[0]); |
| 672 | copy_extent_buffer(path->nodes[0], eb, dest_offset, |
| 673 | (unsigned long)item, sizeof(*item)); |
| 674 | |
| 675 | ins.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 676 | ins.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
| 677 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 678 | offset = key->offset - btrfs_file_extent_offset(eb, item); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 679 | |
Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 680 | /* |
| 681 | * Manually record dirty extent, as here we did a shallow |
| 682 | * file extent item copy and skip normal backref update, |
| 683 | * but modifying extent tree all by ourselves. |
| 684 | * So need to manually record dirty extent for qgroup, |
| 685 | * as the owner of the file extent changed from log tree |
| 686 | * (doesn't affect qgroup) to fs/file tree(affects qgroup) |
| 687 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 688 | ret = btrfs_qgroup_trace_extent(trans, fs_info, |
Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 689 | btrfs_file_extent_disk_bytenr(eb, item), |
| 690 | btrfs_file_extent_disk_num_bytes(eb, item), |
| 691 | GFP_NOFS); |
| 692 | if (ret < 0) |
| 693 | goto out; |
| 694 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 695 | if (ins.objectid > 0) { |
| 696 | u64 csum_start; |
| 697 | u64 csum_end; |
| 698 | LIST_HEAD(ordered_sums); |
| 699 | /* |
| 700 | * is this extent already allocated in the extent |
| 701 | * allocation tree? If so, just add a reference |
| 702 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 703 | ret = btrfs_lookup_data_extent(fs_info, ins.objectid, |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 704 | ins.offset); |
| 705 | if (ret == 0) { |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 706 | ret = btrfs_inc_extent_ref(trans, root, |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 707 | ins.objectid, ins.offset, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 708 | 0, root->root_key.objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 709 | key->objectid, offset); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 710 | if (ret) |
| 711 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 712 | } else { |
| 713 | /* |
| 714 | * insert the extent pointer in the extent |
| 715 | * allocation tree |
| 716 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 717 | ret = btrfs_alloc_logged_file_extent(trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 718 | root->root_key.objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 719 | key->objectid, offset, &ins); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 720 | if (ret) |
| 721 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 722 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 723 | btrfs_release_path(path); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 724 | |
| 725 | if (btrfs_file_extent_compression(eb, item)) { |
| 726 | csum_start = ins.objectid; |
| 727 | csum_end = csum_start + ins.offset; |
| 728 | } else { |
| 729 | csum_start = ins.objectid + |
| 730 | btrfs_file_extent_offset(eb, item); |
| 731 | csum_end = csum_start + |
| 732 | btrfs_file_extent_num_bytes(eb, item); |
| 733 | } |
| 734 | |
| 735 | ret = btrfs_lookup_csums_range(root->log_root, |
| 736 | csum_start, csum_end - 1, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 737 | &ordered_sums, 0); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 738 | if (ret) |
| 739 | goto out; |
Filipe Manana | b84b839 | 2015-08-19 11:09:40 +0100 | [diff] [blame] | 740 | /* |
| 741 | * Now delete all existing cums in the csum root that |
| 742 | * cover our range. We do this because we can have an |
| 743 | * extent that is completely referenced by one file |
| 744 | * extent item and partially referenced by another |
| 745 | * file extent item (like after using the clone or |
| 746 | * extent_same ioctls). In this case if we end up doing |
| 747 | * the replay of the one that partially references the |
| 748 | * extent first, and we do not do the csum deletion |
| 749 | * below, we can get 2 csum items in the csum tree that |
| 750 | * overlap each other. For example, imagine our log has |
| 751 | * the two following file extent items: |
| 752 | * |
| 753 | * key (257 EXTENT_DATA 409600) |
| 754 | * extent data disk byte 12845056 nr 102400 |
| 755 | * extent data offset 20480 nr 20480 ram 102400 |
| 756 | * |
| 757 | * key (257 EXTENT_DATA 819200) |
| 758 | * extent data disk byte 12845056 nr 102400 |
| 759 | * extent data offset 0 nr 102400 ram 102400 |
| 760 | * |
| 761 | * Where the second one fully references the 100K extent |
| 762 | * that starts at disk byte 12845056, and the log tree |
| 763 | * has a single csum item that covers the entire range |
| 764 | * of the extent: |
| 765 | * |
| 766 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 |
| 767 | * |
| 768 | * After the first file extent item is replayed, the |
| 769 | * csum tree gets the following csum item: |
| 770 | * |
| 771 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 |
| 772 | * |
| 773 | * Which covers the 20K sub-range starting at offset 20K |
| 774 | * of our extent. Now when we replay the second file |
| 775 | * extent item, if we do not delete existing csum items |
| 776 | * that cover any of its blocks, we end up getting two |
| 777 | * csum items in our csum tree that overlap each other: |
| 778 | * |
| 779 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 |
| 780 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 |
| 781 | * |
| 782 | * Which is a problem, because after this anyone trying |
| 783 | * to lookup up for the checksum of any block of our |
| 784 | * extent starting at an offset of 40K or higher, will |
| 785 | * end up looking at the second csum item only, which |
| 786 | * does not contain the checksum for any block starting |
| 787 | * at offset 40K or higher of our extent. |
| 788 | */ |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 789 | while (!list_empty(&ordered_sums)) { |
| 790 | struct btrfs_ordered_sum *sums; |
| 791 | sums = list_entry(ordered_sums.next, |
| 792 | struct btrfs_ordered_sum, |
| 793 | list); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 794 | if (!ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 795 | ret = btrfs_del_csums(trans, fs_info, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 796 | sums->bytenr, |
| 797 | sums->len); |
Filipe Manana | b84b839 | 2015-08-19 11:09:40 +0100 | [diff] [blame] | 798 | if (!ret) |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 799 | ret = btrfs_csum_file_blocks(trans, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 800 | fs_info->csum_root, sums); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 801 | list_del(&sums->list); |
| 802 | kfree(sums); |
| 803 | } |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 804 | if (ret) |
| 805 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 806 | } else { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 807 | btrfs_release_path(path); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 808 | } |
| 809 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 810 | /* inline extents are easy, we just overwrite them */ |
| 811 | ret = overwrite_item(trans, root, path, eb, slot, key); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 812 | if (ret) |
| 813 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 814 | } |
| 815 | |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 816 | inode_add_bytes(inode, nbytes); |
Filipe Manana | 3168021c | 2017-02-01 14:58:02 +0000 | [diff] [blame] | 817 | update_inode: |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 818 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 819 | out: |
| 820 | if (inode) |
| 821 | iput(inode); |
| 822 | return ret; |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | * when cleaning up conflicts between the directory names in the |
| 827 | * subvolume, directory names in the log and directory names in the |
| 828 | * inode back references, we may have to unlink inodes from directories. |
| 829 | * |
| 830 | * This is a helper function to do the unlink of a specific directory |
| 831 | * item |
| 832 | */ |
| 833 | static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, |
| 834 | struct btrfs_root *root, |
| 835 | struct btrfs_path *path, |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 836 | struct btrfs_inode *dir, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 837 | struct btrfs_dir_item *di) |
| 838 | { |
| 839 | struct inode *inode; |
| 840 | char *name; |
| 841 | int name_len; |
| 842 | struct extent_buffer *leaf; |
| 843 | struct btrfs_key location; |
| 844 | int ret; |
| 845 | |
| 846 | leaf = path->nodes[0]; |
| 847 | |
| 848 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
| 849 | name_len = btrfs_dir_name_len(leaf, di); |
| 850 | name = kmalloc(name_len, GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 851 | if (!name) |
| 852 | return -ENOMEM; |
| 853 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 854 | read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 855 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 856 | |
| 857 | inode = read_one_inode(root, location.objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 858 | if (!inode) { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 859 | ret = -EIO; |
| 860 | goto out; |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 861 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 862 | |
Yan Zheng | ec051c0 | 2009-01-05 15:43:42 -0500 | [diff] [blame] | 863 | ret = link_to_fixup_dir(trans, root, path, location.objectid); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 864 | if (ret) |
| 865 | goto out; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 866 | |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 867 | ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name, |
| 868 | name_len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 869 | if (ret) |
| 870 | goto out; |
Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 871 | else |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 872 | ret = btrfs_run_delayed_items(trans); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 873 | out: |
| 874 | kfree(name); |
| 875 | iput(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 876 | return ret; |
| 877 | } |
| 878 | |
| 879 | /* |
| 880 | * helper function to see if a given name and sequence number found |
| 881 | * in an inode back reference are already in a directory and correctly |
| 882 | * point to this inode |
| 883 | */ |
| 884 | static noinline int inode_in_dir(struct btrfs_root *root, |
| 885 | struct btrfs_path *path, |
| 886 | u64 dirid, u64 objectid, u64 index, |
| 887 | const char *name, int name_len) |
| 888 | { |
| 889 | struct btrfs_dir_item *di; |
| 890 | struct btrfs_key location; |
| 891 | int match = 0; |
| 892 | |
| 893 | di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, |
| 894 | index, name, name_len, 0); |
| 895 | if (di && !IS_ERR(di)) { |
| 896 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
| 897 | if (location.objectid != objectid) |
| 898 | goto out; |
| 899 | } else |
| 900 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 901 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 902 | |
| 903 | di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0); |
| 904 | if (di && !IS_ERR(di)) { |
| 905 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
| 906 | if (location.objectid != objectid) |
| 907 | goto out; |
| 908 | } else |
| 909 | goto out; |
| 910 | match = 1; |
| 911 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 912 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 913 | return match; |
| 914 | } |
| 915 | |
| 916 | /* |
| 917 | * helper function to check a log tree for a named back reference in |
| 918 | * an inode. This is used to decide if a back reference that is |
| 919 | * found in the subvolume conflicts with what we find in the log. |
| 920 | * |
| 921 | * inode backreferences may have multiple refs in a single item, |
| 922 | * during replay we process one reference at a time, and we don't |
| 923 | * want to delete valid links to a file from the subvolume if that |
| 924 | * link is also in the log. |
| 925 | */ |
| 926 | static noinline int backref_in_log(struct btrfs_root *log, |
| 927 | struct btrfs_key *key, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 928 | u64 ref_objectid, |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 929 | const char *name, int namelen) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 930 | { |
| 931 | struct btrfs_path *path; |
| 932 | struct btrfs_inode_ref *ref; |
| 933 | unsigned long ptr; |
| 934 | unsigned long ptr_end; |
| 935 | unsigned long name_ptr; |
| 936 | int found_name_len; |
| 937 | int item_size; |
| 938 | int ret; |
| 939 | int match = 0; |
| 940 | |
| 941 | path = btrfs_alloc_path(); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 942 | if (!path) |
| 943 | return -ENOMEM; |
| 944 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 945 | ret = btrfs_search_slot(NULL, log, key, path, 0, 0); |
| 946 | if (ret != 0) |
| 947 | goto out; |
| 948 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 949 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 950 | |
| 951 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 952 | if (btrfs_find_name_in_ext_backref(path->nodes[0], |
| 953 | path->slots[0], |
| 954 | ref_objectid, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 955 | name, namelen, NULL)) |
| 956 | match = 1; |
| 957 | |
| 958 | goto out; |
| 959 | } |
| 960 | |
| 961 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 962 | ptr_end = ptr + item_size; |
| 963 | while (ptr < ptr_end) { |
| 964 | ref = (struct btrfs_inode_ref *)ptr; |
| 965 | found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref); |
| 966 | if (found_name_len == namelen) { |
| 967 | name_ptr = (unsigned long)(ref + 1); |
| 968 | ret = memcmp_extent_buffer(path->nodes[0], name, |
| 969 | name_ptr, namelen); |
| 970 | if (ret == 0) { |
| 971 | match = 1; |
| 972 | goto out; |
| 973 | } |
| 974 | } |
| 975 | ptr = (unsigned long)(ref + 1) + found_name_len; |
| 976 | } |
| 977 | out: |
| 978 | btrfs_free_path(path); |
| 979 | return match; |
| 980 | } |
| 981 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 982 | static inline int __add_inode_ref(struct btrfs_trans_handle *trans, |
| 983 | struct btrfs_root *root, |
| 984 | struct btrfs_path *path, |
| 985 | struct btrfs_root *log_root, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 986 | struct btrfs_inode *dir, |
| 987 | struct btrfs_inode *inode, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 988 | u64 inode_objectid, u64 parent_objectid, |
| 989 | u64 ref_index, char *name, int namelen, |
| 990 | int *search_done) |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 991 | { |
| 992 | int ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 993 | char *victim_name; |
| 994 | int victim_name_len; |
| 995 | struct extent_buffer *leaf; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 996 | struct btrfs_dir_item *di; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 997 | struct btrfs_key search_key; |
| 998 | struct btrfs_inode_extref *extref; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 999 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1000 | again: |
| 1001 | /* Search old style refs */ |
| 1002 | search_key.objectid = inode_objectid; |
| 1003 | search_key.type = BTRFS_INODE_REF_KEY; |
| 1004 | search_key.offset = parent_objectid; |
| 1005 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1006 | if (ret == 0) { |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1007 | struct btrfs_inode_ref *victim_ref; |
| 1008 | unsigned long ptr; |
| 1009 | unsigned long ptr_end; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1010 | |
| 1011 | leaf = path->nodes[0]; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1012 | |
| 1013 | /* are we trying to overwrite a back ref for the root directory |
| 1014 | * if so, just jump out, we're done |
| 1015 | */ |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1016 | if (search_key.objectid == search_key.offset) |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1017 | return 1; |
| 1018 | |
| 1019 | /* check all the names in this back reference to see |
| 1020 | * if they are in the log. if so, we allow them to stay |
| 1021 | * otherwise they must be unlinked as a conflict |
| 1022 | */ |
| 1023 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1024 | ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1025 | while (ptr < ptr_end) { |
| 1026 | victim_ref = (struct btrfs_inode_ref *)ptr; |
| 1027 | victim_name_len = btrfs_inode_ref_name_len(leaf, |
| 1028 | victim_ref); |
| 1029 | victim_name = kmalloc(victim_name_len, GFP_NOFS); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1030 | if (!victim_name) |
| 1031 | return -ENOMEM; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1032 | |
| 1033 | read_extent_buffer(leaf, victim_name, |
| 1034 | (unsigned long)(victim_ref + 1), |
| 1035 | victim_name_len); |
| 1036 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1037 | if (!backref_in_log(log_root, &search_key, |
| 1038 | parent_objectid, |
| 1039 | victim_name, |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1040 | victim_name_len)) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1041 | inc_nlink(&inode->vfs_inode); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1042 | btrfs_release_path(path); |
| 1043 | |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1044 | ret = btrfs_unlink_inode(trans, root, dir, inode, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1045 | victim_name, victim_name_len); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1046 | kfree(victim_name); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1047 | if (ret) |
| 1048 | return ret; |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 1049 | ret = btrfs_run_delayed_items(trans); |
Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 1050 | if (ret) |
| 1051 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1052 | *search_done = 1; |
| 1053 | goto again; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1054 | } |
| 1055 | kfree(victim_name); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1056 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1057 | ptr = (unsigned long)(victim_ref + 1) + victim_name_len; |
| 1058 | } |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1059 | |
| 1060 | /* |
| 1061 | * NOTE: we have searched root tree and checked the |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 1062 | * corresponding ref, it does not need to check again. |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1063 | */ |
| 1064 | *search_done = 1; |
| 1065 | } |
| 1066 | btrfs_release_path(path); |
| 1067 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1068 | /* Same search but for extended refs */ |
| 1069 | extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen, |
| 1070 | inode_objectid, parent_objectid, 0, |
| 1071 | 0); |
| 1072 | if (!IS_ERR_OR_NULL(extref)) { |
| 1073 | u32 item_size; |
| 1074 | u32 cur_offset = 0; |
| 1075 | unsigned long base; |
| 1076 | struct inode *victim_parent; |
| 1077 | |
| 1078 | leaf = path->nodes[0]; |
| 1079 | |
| 1080 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1081 | base = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1082 | |
| 1083 | while (cur_offset < item_size) { |
Quentin Casasnovas | dd9ef13 | 2015-03-03 16:31:38 +0100 | [diff] [blame] | 1084 | extref = (struct btrfs_inode_extref *)(base + cur_offset); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1085 | |
| 1086 | victim_name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 1087 | |
| 1088 | if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid) |
| 1089 | goto next; |
| 1090 | |
| 1091 | victim_name = kmalloc(victim_name_len, GFP_NOFS); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1092 | if (!victim_name) |
| 1093 | return -ENOMEM; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1094 | read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name, |
| 1095 | victim_name_len); |
| 1096 | |
| 1097 | search_key.objectid = inode_objectid; |
| 1098 | search_key.type = BTRFS_INODE_EXTREF_KEY; |
| 1099 | search_key.offset = btrfs_extref_hash(parent_objectid, |
| 1100 | victim_name, |
| 1101 | victim_name_len); |
| 1102 | ret = 0; |
| 1103 | if (!backref_in_log(log_root, &search_key, |
| 1104 | parent_objectid, victim_name, |
| 1105 | victim_name_len)) { |
| 1106 | ret = -ENOENT; |
| 1107 | victim_parent = read_one_inode(root, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1108 | parent_objectid); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1109 | if (victim_parent) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1110 | inc_nlink(&inode->vfs_inode); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1111 | btrfs_release_path(path); |
| 1112 | |
| 1113 | ret = btrfs_unlink_inode(trans, root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1114 | BTRFS_I(victim_parent), |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1115 | inode, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1116 | victim_name, |
| 1117 | victim_name_len); |
Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 1118 | if (!ret) |
| 1119 | ret = btrfs_run_delayed_items( |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 1120 | trans); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1121 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1122 | iput(victim_parent); |
| 1123 | kfree(victim_name); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1124 | if (ret) |
| 1125 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1126 | *search_done = 1; |
| 1127 | goto again; |
| 1128 | } |
| 1129 | kfree(victim_name); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1130 | next: |
| 1131 | cur_offset += victim_name_len + sizeof(*extref); |
| 1132 | } |
| 1133 | *search_done = 1; |
| 1134 | } |
| 1135 | btrfs_release_path(path); |
| 1136 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1137 | /* look for a conflicting sequence number */ |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1138 | di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir), |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1139 | ref_index, name, namelen, 0); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1140 | if (di && !IS_ERR(di)) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1141 | ret = drop_one_dir_item(trans, root, path, dir, di); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1142 | if (ret) |
| 1143 | return ret; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1144 | } |
| 1145 | btrfs_release_path(path); |
| 1146 | |
| 1147 | /* look for a conflicing name */ |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1148 | di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1149 | name, namelen, 0); |
| 1150 | if (di && !IS_ERR(di)) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1151 | ret = drop_one_dir_item(trans, root, path, dir, di); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1152 | if (ret) |
| 1153 | return ret; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1154 | } |
| 1155 | btrfs_release_path(path); |
| 1156 | |
| 1157 | return 0; |
| 1158 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1159 | |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1160 | static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
| 1161 | u32 *namelen, char **name, u64 *index, |
| 1162 | u64 *parent_objectid) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1163 | { |
| 1164 | struct btrfs_inode_extref *extref; |
| 1165 | |
| 1166 | extref = (struct btrfs_inode_extref *)ref_ptr; |
| 1167 | |
| 1168 | *namelen = btrfs_inode_extref_name_len(eb, extref); |
| 1169 | *name = kmalloc(*namelen, GFP_NOFS); |
| 1170 | if (*name == NULL) |
| 1171 | return -ENOMEM; |
| 1172 | |
| 1173 | read_extent_buffer(eb, *name, (unsigned long)&extref->name, |
| 1174 | *namelen); |
| 1175 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1176 | if (index) |
| 1177 | *index = btrfs_inode_extref_index(eb, extref); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1178 | if (parent_objectid) |
| 1179 | *parent_objectid = btrfs_inode_extref_parent(eb, extref); |
| 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1184 | static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
| 1185 | u32 *namelen, char **name, u64 *index) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1186 | { |
| 1187 | struct btrfs_inode_ref *ref; |
| 1188 | |
| 1189 | ref = (struct btrfs_inode_ref *)ref_ptr; |
| 1190 | |
| 1191 | *namelen = btrfs_inode_ref_name_len(eb, ref); |
| 1192 | *name = kmalloc(*namelen, GFP_NOFS); |
| 1193 | if (*name == NULL) |
| 1194 | return -ENOMEM; |
| 1195 | |
| 1196 | read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen); |
| 1197 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1198 | if (index) |
| 1199 | *index = btrfs_inode_ref_index(eb, ref); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1200 | |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1204 | /* |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1205 | * Take an inode reference item from the log tree and iterate all names from the |
| 1206 | * inode reference item in the subvolume tree with the same key (if it exists). |
| 1207 | * For any name that is not in the inode reference item from the log tree, do a |
| 1208 | * proper unlink of that name (that is, remove its entry from the inode |
| 1209 | * reference item and both dir index keys). |
| 1210 | */ |
| 1211 | static int unlink_old_inode_refs(struct btrfs_trans_handle *trans, |
| 1212 | struct btrfs_root *root, |
| 1213 | struct btrfs_path *path, |
| 1214 | struct btrfs_inode *inode, |
| 1215 | struct extent_buffer *log_eb, |
| 1216 | int log_slot, |
| 1217 | struct btrfs_key *key) |
| 1218 | { |
| 1219 | int ret; |
| 1220 | unsigned long ref_ptr; |
| 1221 | unsigned long ref_end; |
| 1222 | struct extent_buffer *eb; |
| 1223 | |
| 1224 | again: |
| 1225 | btrfs_release_path(path); |
| 1226 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 1227 | if (ret > 0) { |
| 1228 | ret = 0; |
| 1229 | goto out; |
| 1230 | } |
| 1231 | if (ret < 0) |
| 1232 | goto out; |
| 1233 | |
| 1234 | eb = path->nodes[0]; |
| 1235 | ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]); |
| 1236 | ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]); |
| 1237 | while (ref_ptr < ref_end) { |
| 1238 | char *name = NULL; |
| 1239 | int namelen; |
| 1240 | u64 parent_id; |
| 1241 | |
| 1242 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
| 1243 | ret = extref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1244 | NULL, &parent_id); |
| 1245 | } else { |
| 1246 | parent_id = key->offset; |
| 1247 | ret = ref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1248 | NULL); |
| 1249 | } |
| 1250 | if (ret) |
| 1251 | goto out; |
| 1252 | |
| 1253 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
| 1254 | ret = btrfs_find_name_in_ext_backref(log_eb, log_slot, |
| 1255 | parent_id, name, |
| 1256 | namelen, NULL); |
| 1257 | else |
| 1258 | ret = btrfs_find_name_in_backref(log_eb, log_slot, name, |
| 1259 | namelen, NULL); |
| 1260 | |
| 1261 | if (!ret) { |
| 1262 | struct inode *dir; |
| 1263 | |
| 1264 | btrfs_release_path(path); |
| 1265 | dir = read_one_inode(root, parent_id); |
| 1266 | if (!dir) { |
| 1267 | ret = -ENOENT; |
| 1268 | kfree(name); |
| 1269 | goto out; |
| 1270 | } |
| 1271 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 1272 | inode, name, namelen); |
| 1273 | kfree(name); |
| 1274 | iput(dir); |
| 1275 | if (ret) |
| 1276 | goto out; |
| 1277 | goto again; |
| 1278 | } |
| 1279 | |
| 1280 | kfree(name); |
| 1281 | ref_ptr += namelen; |
| 1282 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
| 1283 | ref_ptr += sizeof(struct btrfs_inode_extref); |
| 1284 | else |
| 1285 | ref_ptr += sizeof(struct btrfs_inode_ref); |
| 1286 | } |
| 1287 | ret = 0; |
| 1288 | out: |
| 1289 | btrfs_release_path(path); |
| 1290 | return ret; |
| 1291 | } |
| 1292 | |
| 1293 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1294 | * replay one inode back reference item found in the log tree. |
| 1295 | * eb, slot and key refer to the buffer and key found in the log tree. |
| 1296 | * root is the destination we are replaying into, and path is for temp |
| 1297 | * use by this function. (it should be released on return). |
| 1298 | */ |
| 1299 | static noinline int add_inode_ref(struct btrfs_trans_handle *trans, |
| 1300 | struct btrfs_root *root, |
| 1301 | struct btrfs_root *log, |
| 1302 | struct btrfs_path *path, |
| 1303 | struct extent_buffer *eb, int slot, |
| 1304 | struct btrfs_key *key) |
| 1305 | { |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1306 | struct inode *dir = NULL; |
| 1307 | struct inode *inode = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1308 | unsigned long ref_ptr; |
| 1309 | unsigned long ref_end; |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1310 | char *name = NULL; |
liubo | 34f3e4f | 2011-08-06 08:35:23 +0000 | [diff] [blame] | 1311 | int namelen; |
| 1312 | int ret; |
liubo | c622ae6 | 2011-03-26 08:01:12 -0400 | [diff] [blame] | 1313 | int search_done = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1314 | int log_ref_ver = 0; |
| 1315 | u64 parent_objectid; |
| 1316 | u64 inode_objectid; |
Chris Mason | f46dbe3 | 2012-10-09 11:17:20 -0400 | [diff] [blame] | 1317 | u64 ref_index = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1318 | int ref_struct_size; |
| 1319 | |
| 1320 | ref_ptr = btrfs_item_ptr_offset(eb, slot); |
| 1321 | ref_end = ref_ptr + btrfs_item_size_nr(eb, slot); |
| 1322 | |
| 1323 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
| 1324 | struct btrfs_inode_extref *r; |
| 1325 | |
| 1326 | ref_struct_size = sizeof(struct btrfs_inode_extref); |
| 1327 | log_ref_ver = 1; |
| 1328 | r = (struct btrfs_inode_extref *)ref_ptr; |
| 1329 | parent_objectid = btrfs_inode_extref_parent(eb, r); |
| 1330 | } else { |
| 1331 | ref_struct_size = sizeof(struct btrfs_inode_ref); |
| 1332 | parent_objectid = key->offset; |
| 1333 | } |
| 1334 | inode_objectid = key->objectid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1335 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1336 | /* |
| 1337 | * it is possible that we didn't log all the parent directories |
| 1338 | * for a given inode. If we don't find the dir, just don't |
| 1339 | * copy the back ref in. The link count fixup code will take |
| 1340 | * care of the rest |
| 1341 | */ |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1342 | dir = read_one_inode(root, parent_objectid); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1343 | if (!dir) { |
| 1344 | ret = -ENOENT; |
| 1345 | goto out; |
| 1346 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1347 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1348 | inode = read_one_inode(root, inode_objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1349 | if (!inode) { |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1350 | ret = -EIO; |
| 1351 | goto out; |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1352 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1353 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1354 | while (ref_ptr < ref_end) { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1355 | if (log_ref_ver) { |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1356 | ret = extref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1357 | &ref_index, &parent_objectid); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1358 | /* |
| 1359 | * parent object can change from one array |
| 1360 | * item to another. |
| 1361 | */ |
| 1362 | if (!dir) |
| 1363 | dir = read_one_inode(root, parent_objectid); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1364 | if (!dir) { |
| 1365 | ret = -ENOENT; |
| 1366 | goto out; |
| 1367 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1368 | } else { |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1369 | ret = ref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1370 | &ref_index); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1371 | } |
| 1372 | if (ret) |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1373 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1374 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1375 | /* if we already have a perfect match, we're done */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 1376 | if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)), |
| 1377 | btrfs_ino(BTRFS_I(inode)), ref_index, |
| 1378 | name, namelen)) { |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1379 | /* |
| 1380 | * look for a conflicting back reference in the |
| 1381 | * metadata. if we find one we have to unlink that name |
| 1382 | * of the file before we add our new link. Later on, we |
| 1383 | * overwrite any existing back reference, and we don't |
| 1384 | * want to create dangling pointers in the directory. |
| 1385 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1386 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1387 | if (!search_done) { |
| 1388 | ret = __add_inode_ref(trans, root, path, log, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1389 | BTRFS_I(dir), |
David Sterba | d75eefd | 2017-02-10 20:20:19 +0100 | [diff] [blame] | 1390 | BTRFS_I(inode), |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1391 | inode_objectid, |
| 1392 | parent_objectid, |
| 1393 | ref_index, name, namelen, |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1394 | &search_done); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1395 | if (ret) { |
| 1396 | if (ret == 1) |
| 1397 | ret = 0; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1398 | goto out; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1399 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1400 | } |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1401 | |
| 1402 | /* insert our name */ |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 1403 | ret = btrfs_add_link(trans, BTRFS_I(dir), |
| 1404 | BTRFS_I(inode), |
| 1405 | name, namelen, 0, ref_index); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1406 | if (ret) |
| 1407 | goto out; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1408 | |
| 1409 | btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1410 | } |
liubo | c622ae6 | 2011-03-26 08:01:12 -0400 | [diff] [blame] | 1411 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1412 | ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1413 | kfree(name); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1414 | name = NULL; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1415 | if (log_ref_ver) { |
| 1416 | iput(dir); |
| 1417 | dir = NULL; |
| 1418 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1419 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1420 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1421 | /* |
| 1422 | * Before we overwrite the inode reference item in the subvolume tree |
| 1423 | * with the item from the log tree, we must unlink all names from the |
| 1424 | * parent directory that are in the subvolume's tree inode reference |
| 1425 | * item, otherwise we end up with an inconsistent subvolume tree where |
| 1426 | * dir index entries exist for a name but there is no inode reference |
| 1427 | * item with the same name. |
| 1428 | */ |
| 1429 | ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot, |
| 1430 | key); |
| 1431 | if (ret) |
| 1432 | goto out; |
| 1433 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1434 | /* finally write the back reference in the inode */ |
| 1435 | ret = overwrite_item(trans, root, path, eb, slot, key); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1436 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1437 | btrfs_release_path(path); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1438 | kfree(name); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1439 | iput(dir); |
| 1440 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1441 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1442 | } |
| 1443 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1444 | static int insert_orphan_item(struct btrfs_trans_handle *trans, |
David Sterba | 9c4f61f | 2015-01-02 19:12:57 +0100 | [diff] [blame] | 1445 | struct btrfs_root *root, u64 ino) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1446 | { |
| 1447 | int ret; |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 1448 | |
David Sterba | 9c4f61f | 2015-01-02 19:12:57 +0100 | [diff] [blame] | 1449 | ret = btrfs_insert_orphan_item(trans, root, ino); |
| 1450 | if (ret == -EEXIST) |
| 1451 | ret = 0; |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 1452 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1453 | return ret; |
| 1454 | } |
| 1455 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1456 | static int count_inode_extrefs(struct btrfs_root *root, |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1457 | struct btrfs_inode *inode, struct btrfs_path *path) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1458 | { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1459 | int ret = 0; |
| 1460 | int name_len; |
| 1461 | unsigned int nlink = 0; |
| 1462 | u32 item_size; |
| 1463 | u32 cur_offset = 0; |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1464 | u64 inode_objectid = btrfs_ino(inode); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1465 | u64 offset = 0; |
| 1466 | unsigned long ptr; |
| 1467 | struct btrfs_inode_extref *extref; |
| 1468 | struct extent_buffer *leaf; |
| 1469 | |
| 1470 | while (1) { |
| 1471 | ret = btrfs_find_one_extref(root, inode_objectid, offset, path, |
| 1472 | &extref, &offset); |
| 1473 | if (ret) |
| 1474 | break; |
| 1475 | |
| 1476 | leaf = path->nodes[0]; |
| 1477 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1478 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 1479 | cur_offset = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1480 | |
| 1481 | while (cur_offset < item_size) { |
| 1482 | extref = (struct btrfs_inode_extref *) (ptr + cur_offset); |
| 1483 | name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 1484 | |
| 1485 | nlink++; |
| 1486 | |
| 1487 | cur_offset += name_len + sizeof(*extref); |
| 1488 | } |
| 1489 | |
| 1490 | offset++; |
| 1491 | btrfs_release_path(path); |
| 1492 | } |
| 1493 | btrfs_release_path(path); |
| 1494 | |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 1495 | if (ret < 0 && ret != -ENOENT) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1496 | return ret; |
| 1497 | return nlink; |
| 1498 | } |
| 1499 | |
| 1500 | static int count_inode_refs(struct btrfs_root *root, |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1501 | struct btrfs_inode *inode, struct btrfs_path *path) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1502 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1503 | int ret; |
| 1504 | struct btrfs_key key; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1505 | unsigned int nlink = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1506 | unsigned long ptr; |
| 1507 | unsigned long ptr_end; |
| 1508 | int name_len; |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1509 | u64 ino = btrfs_ino(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1510 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1511 | key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1512 | key.type = BTRFS_INODE_REF_KEY; |
| 1513 | key.offset = (u64)-1; |
| 1514 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1515 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1516 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1517 | if (ret < 0) |
| 1518 | break; |
| 1519 | if (ret > 0) { |
| 1520 | if (path->slots[0] == 0) |
| 1521 | break; |
| 1522 | path->slots[0]--; |
| 1523 | } |
Filipe David Borba Manana | e93ae26 | 2013-10-14 22:49:11 +0100 | [diff] [blame] | 1524 | process_slot: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1525 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1526 | path->slots[0]); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1527 | if (key.objectid != ino || |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1528 | key.type != BTRFS_INODE_REF_KEY) |
| 1529 | break; |
| 1530 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
| 1531 | ptr_end = ptr + btrfs_item_size_nr(path->nodes[0], |
| 1532 | path->slots[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1533 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1534 | struct btrfs_inode_ref *ref; |
| 1535 | |
| 1536 | ref = (struct btrfs_inode_ref *)ptr; |
| 1537 | name_len = btrfs_inode_ref_name_len(path->nodes[0], |
| 1538 | ref); |
| 1539 | ptr = (unsigned long)(ref + 1) + name_len; |
| 1540 | nlink++; |
| 1541 | } |
| 1542 | |
| 1543 | if (key.offset == 0) |
| 1544 | break; |
Filipe David Borba Manana | e93ae26 | 2013-10-14 22:49:11 +0100 | [diff] [blame] | 1545 | if (path->slots[0] > 0) { |
| 1546 | path->slots[0]--; |
| 1547 | goto process_slot; |
| 1548 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1549 | key.offset--; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1550 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1551 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1552 | btrfs_release_path(path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1553 | |
| 1554 | return nlink; |
| 1555 | } |
| 1556 | |
| 1557 | /* |
| 1558 | * There are a few corners where the link count of the file can't |
| 1559 | * be properly maintained during replay. So, instead of adding |
| 1560 | * lots of complexity to the log code, we just scan the backrefs |
| 1561 | * for any file that has been through replay. |
| 1562 | * |
| 1563 | * The scan will update the link count on the inode to reflect the |
| 1564 | * number of back refs found. If it goes down to zero, the iput |
| 1565 | * will free the inode. |
| 1566 | */ |
| 1567 | static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, |
| 1568 | struct btrfs_root *root, |
| 1569 | struct inode *inode) |
| 1570 | { |
| 1571 | struct btrfs_path *path; |
| 1572 | int ret; |
| 1573 | u64 nlink = 0; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1574 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1575 | |
| 1576 | path = btrfs_alloc_path(); |
| 1577 | if (!path) |
| 1578 | return -ENOMEM; |
| 1579 | |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1580 | ret = count_inode_refs(root, BTRFS_I(inode), path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1581 | if (ret < 0) |
| 1582 | goto out; |
| 1583 | |
| 1584 | nlink = ret; |
| 1585 | |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1586 | ret = count_inode_extrefs(root, BTRFS_I(inode), path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1587 | if (ret < 0) |
| 1588 | goto out; |
| 1589 | |
| 1590 | nlink += ret; |
| 1591 | |
| 1592 | ret = 0; |
| 1593 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1594 | if (nlink != inode->i_nlink) { |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 1595 | set_nlink(inode, nlink); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1596 | btrfs_update_inode(trans, root, inode); |
| 1597 | } |
Chris Mason | 8d5bf1c | 2008-09-11 15:51:21 -0400 | [diff] [blame] | 1598 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1599 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1600 | if (inode->i_nlink == 0) { |
| 1601 | if (S_ISDIR(inode->i_mode)) { |
| 1602 | ret = replay_dir_deletes(trans, root, NULL, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1603 | ino, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1604 | if (ret) |
| 1605 | goto out; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1606 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1607 | ret = insert_orphan_item(trans, root, ino); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1608 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1609 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1610 | out: |
| 1611 | btrfs_free_path(path); |
| 1612 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, |
| 1616 | struct btrfs_root *root, |
| 1617 | struct btrfs_path *path) |
| 1618 | { |
| 1619 | int ret; |
| 1620 | struct btrfs_key key; |
| 1621 | struct inode *inode; |
| 1622 | |
| 1623 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; |
| 1624 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
| 1625 | key.offset = (u64)-1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1626 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1627 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1628 | if (ret < 0) |
| 1629 | break; |
| 1630 | |
| 1631 | if (ret == 1) { |
| 1632 | if (path->slots[0] == 0) |
| 1633 | break; |
| 1634 | path->slots[0]--; |
| 1635 | } |
| 1636 | |
| 1637 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 1638 | if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || |
| 1639 | key.type != BTRFS_ORPHAN_ITEM_KEY) |
| 1640 | break; |
| 1641 | |
| 1642 | ret = btrfs_del_item(trans, root, path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1643 | if (ret) |
| 1644 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1645 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1646 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1647 | inode = read_one_inode(root, key.offset); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1648 | if (!inode) |
| 1649 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1650 | |
| 1651 | ret = fixup_inode_link_count(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1652 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1653 | if (ret) |
| 1654 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1655 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1656 | /* |
| 1657 | * fixup on a directory may create new entries, |
| 1658 | * make sure we always look for the highset possible |
| 1659 | * offset |
| 1660 | */ |
| 1661 | key.offset = (u64)-1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1662 | } |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1663 | ret = 0; |
| 1664 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1665 | btrfs_release_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1666 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | |
| 1670 | /* |
| 1671 | * record a given inode in the fixup dir so we can check its link |
| 1672 | * count when replay is done. The link count is incremented here |
| 1673 | * so the inode won't go away until we check it |
| 1674 | */ |
| 1675 | static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, |
| 1676 | struct btrfs_root *root, |
| 1677 | struct btrfs_path *path, |
| 1678 | u64 objectid) |
| 1679 | { |
| 1680 | struct btrfs_key key; |
| 1681 | int ret = 0; |
| 1682 | struct inode *inode; |
| 1683 | |
| 1684 | inode = read_one_inode(root, objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1685 | if (!inode) |
| 1686 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1687 | |
| 1688 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1689 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1690 | key.offset = objectid; |
| 1691 | |
| 1692 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
| 1693 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1694 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1695 | if (ret == 0) { |
Josef Bacik | 9bf7a48 | 2013-03-01 13:35:47 -0500 | [diff] [blame] | 1696 | if (!inode->i_nlink) |
| 1697 | set_nlink(inode, 1); |
| 1698 | else |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 1699 | inc_nlink(inode); |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 1700 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1701 | } else if (ret == -EEXIST) { |
| 1702 | ret = 0; |
| 1703 | } else { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1704 | BUG(); /* Logic Error */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1705 | } |
| 1706 | iput(inode); |
| 1707 | |
| 1708 | return ret; |
| 1709 | } |
| 1710 | |
| 1711 | /* |
| 1712 | * when replaying the log for a directory, we only insert names |
| 1713 | * for inodes that actually exist. This means an fsync on a directory |
| 1714 | * does not implicitly fsync all the new files in it |
| 1715 | */ |
| 1716 | static noinline int insert_one_name(struct btrfs_trans_handle *trans, |
| 1717 | struct btrfs_root *root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1718 | u64 dirid, u64 index, |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 1719 | char *name, int name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1720 | struct btrfs_key *location) |
| 1721 | { |
| 1722 | struct inode *inode; |
| 1723 | struct inode *dir; |
| 1724 | int ret; |
| 1725 | |
| 1726 | inode = read_one_inode(root, location->objectid); |
| 1727 | if (!inode) |
| 1728 | return -ENOENT; |
| 1729 | |
| 1730 | dir = read_one_inode(root, dirid); |
| 1731 | if (!dir) { |
| 1732 | iput(inode); |
| 1733 | return -EIO; |
| 1734 | } |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1735 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 1736 | ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name, |
| 1737 | name_len, 1, index); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1738 | |
| 1739 | /* FIXME, put inode into FIXUP list */ |
| 1740 | |
| 1741 | iput(inode); |
| 1742 | iput(dir); |
| 1743 | return ret; |
| 1744 | } |
| 1745 | |
| 1746 | /* |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1747 | * Return true if an inode reference exists in the log for the given name, |
| 1748 | * inode and parent inode. |
| 1749 | */ |
| 1750 | static bool name_in_log_ref(struct btrfs_root *log_root, |
| 1751 | const char *name, const int name_len, |
| 1752 | const u64 dirid, const u64 ino) |
| 1753 | { |
| 1754 | struct btrfs_key search_key; |
| 1755 | |
| 1756 | search_key.objectid = ino; |
| 1757 | search_key.type = BTRFS_INODE_REF_KEY; |
| 1758 | search_key.offset = dirid; |
| 1759 | if (backref_in_log(log_root, &search_key, dirid, name, name_len)) |
| 1760 | return true; |
| 1761 | |
| 1762 | search_key.type = BTRFS_INODE_EXTREF_KEY; |
| 1763 | search_key.offset = btrfs_extref_hash(dirid, name, name_len); |
| 1764 | if (backref_in_log(log_root, &search_key, dirid, name, name_len)) |
| 1765 | return true; |
| 1766 | |
| 1767 | return false; |
| 1768 | } |
| 1769 | |
| 1770 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1771 | * take a single entry in a log directory item and replay it into |
| 1772 | * the subvolume. |
| 1773 | * |
| 1774 | * if a conflicting item exists in the subdirectory already, |
| 1775 | * the inode it points to is unlinked and put into the link count |
| 1776 | * fix up tree. |
| 1777 | * |
| 1778 | * If a name from the log points to a file or directory that does |
| 1779 | * not exist in the FS, it is skipped. fsyncs on directories |
| 1780 | * do not force down inodes inside that directory, just changes to the |
| 1781 | * names or unlinks in a directory. |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1782 | * |
| 1783 | * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a |
| 1784 | * non-existing inode) and 1 if the name was replayed. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1785 | */ |
| 1786 | static noinline int replay_one_name(struct btrfs_trans_handle *trans, |
| 1787 | struct btrfs_root *root, |
| 1788 | struct btrfs_path *path, |
| 1789 | struct extent_buffer *eb, |
| 1790 | struct btrfs_dir_item *di, |
| 1791 | struct btrfs_key *key) |
| 1792 | { |
| 1793 | char *name; |
| 1794 | int name_len; |
| 1795 | struct btrfs_dir_item *dst_di; |
| 1796 | struct btrfs_key found_key; |
| 1797 | struct btrfs_key log_key; |
| 1798 | struct inode *dir; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1799 | u8 log_type; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1800 | int exists; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1801 | int ret = 0; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1802 | bool update_size = (key->type == BTRFS_DIR_INDEX_KEY); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1803 | bool name_added = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1804 | |
| 1805 | dir = read_one_inode(root, key->objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1806 | if (!dir) |
| 1807 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1808 | |
| 1809 | name_len = btrfs_dir_name_len(eb, di); |
| 1810 | name = kmalloc(name_len, GFP_NOFS); |
Filipe David Borba Manana | 2bac325 | 2013-08-04 19:58:57 +0100 | [diff] [blame] | 1811 | if (!name) { |
| 1812 | ret = -ENOMEM; |
| 1813 | goto out; |
| 1814 | } |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 1815 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1816 | log_type = btrfs_dir_type(eb, di); |
| 1817 | read_extent_buffer(eb, name, (unsigned long)(di + 1), |
| 1818 | name_len); |
| 1819 | |
| 1820 | btrfs_dir_item_key_to_cpu(eb, di, &log_key); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1821 | exists = btrfs_lookup_inode(trans, root, path, &log_key, 0); |
| 1822 | if (exists == 0) |
| 1823 | exists = 1; |
| 1824 | else |
| 1825 | exists = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1826 | btrfs_release_path(path); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1827 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1828 | if (key->type == BTRFS_DIR_ITEM_KEY) { |
| 1829 | dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, |
| 1830 | name, name_len, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1831 | } else if (key->type == BTRFS_DIR_INDEX_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1832 | dst_di = btrfs_lookup_dir_index_item(trans, root, path, |
| 1833 | key->objectid, |
| 1834 | key->offset, name, |
| 1835 | name_len, 1); |
| 1836 | } else { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1837 | /* Corruption */ |
| 1838 | ret = -EINVAL; |
| 1839 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1840 | } |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 1841 | if (IS_ERR_OR_NULL(dst_di)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1842 | /* we need a sequence number to insert, so we only |
| 1843 | * do inserts for the BTRFS_DIR_INDEX_KEY types |
| 1844 | */ |
| 1845 | if (key->type != BTRFS_DIR_INDEX_KEY) |
| 1846 | goto out; |
| 1847 | goto insert; |
| 1848 | } |
| 1849 | |
| 1850 | btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); |
| 1851 | /* the existing item matches the logged item */ |
| 1852 | if (found_key.objectid == log_key.objectid && |
| 1853 | found_key.type == log_key.type && |
| 1854 | found_key.offset == log_key.offset && |
| 1855 | btrfs_dir_type(path->nodes[0], dst_di) == log_type) { |
Filipe Manana | a2cc11d | 2014-09-08 22:53:18 +0100 | [diff] [blame] | 1856 | update_size = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1857 | goto out; |
| 1858 | } |
| 1859 | |
| 1860 | /* |
| 1861 | * don't drop the conflicting directory entry if the inode |
| 1862 | * for the new entry doesn't exist |
| 1863 | */ |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1864 | if (!exists) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1865 | goto out; |
| 1866 | |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 1867 | 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] | 1868 | if (ret) |
| 1869 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1870 | |
| 1871 | if (key->type == BTRFS_DIR_INDEX_KEY) |
| 1872 | goto insert; |
| 1873 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1874 | btrfs_release_path(path); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1875 | if (!ret && update_size) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 1876 | 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] | 1877 | ret = btrfs_update_inode(trans, root, dir); |
| 1878 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1879 | kfree(name); |
| 1880 | iput(dir); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1881 | if (!ret && name_added) |
| 1882 | ret = 1; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1883 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1884 | |
| 1885 | insert: |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1886 | if (name_in_log_ref(root->log_root, name, name_len, |
| 1887 | key->objectid, log_key.objectid)) { |
| 1888 | /* The dentry will be added later. */ |
| 1889 | ret = 0; |
| 1890 | update_size = false; |
| 1891 | goto out; |
| 1892 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1893 | btrfs_release_path(path); |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 1894 | ret = insert_one_name(trans, root, key->objectid, key->offset, |
| 1895 | name, name_len, &log_key); |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1896 | if (ret && ret != -ENOENT && ret != -EEXIST) |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1897 | goto out; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1898 | if (!ret) |
| 1899 | name_added = true; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1900 | update_size = false; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1901 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1902 | goto out; |
| 1903 | } |
| 1904 | |
| 1905 | /* |
| 1906 | * find all the names in a directory item and reconcile them into |
| 1907 | * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than |
| 1908 | * one name in a directory item, but the same code gets used for |
| 1909 | * both directory index types |
| 1910 | */ |
| 1911 | static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, |
| 1912 | struct btrfs_root *root, |
| 1913 | struct btrfs_path *path, |
| 1914 | struct extent_buffer *eb, int slot, |
| 1915 | struct btrfs_key *key) |
| 1916 | { |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1917 | int ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1918 | u32 item_size = btrfs_item_size_nr(eb, slot); |
| 1919 | struct btrfs_dir_item *di; |
| 1920 | int name_len; |
| 1921 | unsigned long ptr; |
| 1922 | unsigned long ptr_end; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1923 | struct btrfs_path *fixup_path = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1924 | |
| 1925 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 1926 | ptr_end = ptr + item_size; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1927 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1928 | di = (struct btrfs_dir_item *)ptr; |
| 1929 | name_len = btrfs_dir_name_len(eb, di); |
| 1930 | ret = replay_one_name(trans, root, path, eb, di, key); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1931 | if (ret < 0) |
| 1932 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1933 | ptr = (unsigned long)(di + 1); |
| 1934 | ptr += name_len; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1935 | |
| 1936 | /* |
| 1937 | * If this entry refers to a non-directory (directories can not |
| 1938 | * have a link count > 1) and it was added in the transaction |
| 1939 | * that was not committed, make sure we fixup the link count of |
| 1940 | * the inode it the entry points to. Otherwise something like |
| 1941 | * the following would result in a directory pointing to an |
| 1942 | * inode with a wrong link that does not account for this dir |
| 1943 | * entry: |
| 1944 | * |
| 1945 | * mkdir testdir |
| 1946 | * touch testdir/foo |
| 1947 | * touch testdir/bar |
| 1948 | * sync |
| 1949 | * |
| 1950 | * ln testdir/bar testdir/bar_link |
| 1951 | * ln testdir/foo testdir/foo_link |
| 1952 | * xfs_io -c "fsync" testdir/bar |
| 1953 | * |
| 1954 | * <power failure> |
| 1955 | * |
| 1956 | * mount fs, log replay happens |
| 1957 | * |
| 1958 | * File foo would remain with a link count of 1 when it has two |
| 1959 | * entries pointing to it in the directory testdir. This would |
| 1960 | * make it impossible to ever delete the parent directory has |
| 1961 | * it would result in stale dentries that can never be deleted. |
| 1962 | */ |
| 1963 | if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) { |
| 1964 | struct btrfs_key di_key; |
| 1965 | |
| 1966 | if (!fixup_path) { |
| 1967 | fixup_path = btrfs_alloc_path(); |
| 1968 | if (!fixup_path) { |
| 1969 | ret = -ENOMEM; |
| 1970 | break; |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 1975 | ret = link_to_fixup_dir(trans, root, fixup_path, |
| 1976 | di_key.objectid); |
| 1977 | if (ret) |
| 1978 | break; |
| 1979 | } |
| 1980 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1981 | } |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1982 | btrfs_free_path(fixup_path); |
| 1983 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | /* |
| 1987 | * directory replay has two parts. There are the standard directory |
| 1988 | * items in the log copied from the subvolume, and range items |
| 1989 | * created in the log while the subvolume was logged. |
| 1990 | * |
| 1991 | * The range items tell us which parts of the key space the log |
| 1992 | * is authoritative for. During replay, if a key in the subvolume |
| 1993 | * directory is in a logged range item, but not actually in the log |
| 1994 | * that means it was deleted from the directory before the fsync |
| 1995 | * and should be removed. |
| 1996 | */ |
| 1997 | static noinline int find_dir_range(struct btrfs_root *root, |
| 1998 | struct btrfs_path *path, |
| 1999 | u64 dirid, int key_type, |
| 2000 | u64 *start_ret, u64 *end_ret) |
| 2001 | { |
| 2002 | struct btrfs_key key; |
| 2003 | u64 found_end; |
| 2004 | struct btrfs_dir_log_item *item; |
| 2005 | int ret; |
| 2006 | int nritems; |
| 2007 | |
| 2008 | if (*start_ret == (u64)-1) |
| 2009 | return 1; |
| 2010 | |
| 2011 | key.objectid = dirid; |
| 2012 | key.type = key_type; |
| 2013 | key.offset = *start_ret; |
| 2014 | |
| 2015 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2016 | if (ret < 0) |
| 2017 | goto out; |
| 2018 | if (ret > 0) { |
| 2019 | if (path->slots[0] == 0) |
| 2020 | goto out; |
| 2021 | path->slots[0]--; |
| 2022 | } |
| 2023 | if (ret != 0) |
| 2024 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 2025 | |
| 2026 | if (key.type != key_type || key.objectid != dirid) { |
| 2027 | ret = 1; |
| 2028 | goto next; |
| 2029 | } |
| 2030 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2031 | struct btrfs_dir_log_item); |
| 2032 | found_end = btrfs_dir_log_end(path->nodes[0], item); |
| 2033 | |
| 2034 | if (*start_ret >= key.offset && *start_ret <= found_end) { |
| 2035 | ret = 0; |
| 2036 | *start_ret = key.offset; |
| 2037 | *end_ret = found_end; |
| 2038 | goto out; |
| 2039 | } |
| 2040 | ret = 1; |
| 2041 | next: |
| 2042 | /* check the next slot in the tree to see if it is a valid item */ |
| 2043 | nritems = btrfs_header_nritems(path->nodes[0]); |
Robbie Ko | 2a7bf53 | 2016-10-07 17:30:47 +0800 | [diff] [blame] | 2044 | path->slots[0]++; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2045 | if (path->slots[0] >= nritems) { |
| 2046 | ret = btrfs_next_leaf(root, path); |
| 2047 | if (ret) |
| 2048 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 2052 | |
| 2053 | if (key.type != key_type || key.objectid != dirid) { |
| 2054 | ret = 1; |
| 2055 | goto out; |
| 2056 | } |
| 2057 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2058 | struct btrfs_dir_log_item); |
| 2059 | found_end = btrfs_dir_log_end(path->nodes[0], item); |
| 2060 | *start_ret = key.offset; |
| 2061 | *end_ret = found_end; |
| 2062 | ret = 0; |
| 2063 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2064 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2065 | return ret; |
| 2066 | } |
| 2067 | |
| 2068 | /* |
| 2069 | * this looks for a given directory item in the log. If the directory |
| 2070 | * item is not in the log, the item is removed and the inode it points |
| 2071 | * to is unlinked |
| 2072 | */ |
| 2073 | static noinline int check_item_in_log(struct btrfs_trans_handle *trans, |
| 2074 | struct btrfs_root *root, |
| 2075 | struct btrfs_root *log, |
| 2076 | struct btrfs_path *path, |
| 2077 | struct btrfs_path *log_path, |
| 2078 | struct inode *dir, |
| 2079 | struct btrfs_key *dir_key) |
| 2080 | { |
| 2081 | int ret; |
| 2082 | struct extent_buffer *eb; |
| 2083 | int slot; |
| 2084 | u32 item_size; |
| 2085 | struct btrfs_dir_item *di; |
| 2086 | struct btrfs_dir_item *log_di; |
| 2087 | int name_len; |
| 2088 | unsigned long ptr; |
| 2089 | unsigned long ptr_end; |
| 2090 | char *name; |
| 2091 | struct inode *inode; |
| 2092 | struct btrfs_key location; |
| 2093 | |
| 2094 | again: |
| 2095 | eb = path->nodes[0]; |
| 2096 | slot = path->slots[0]; |
| 2097 | item_size = btrfs_item_size_nr(eb, slot); |
| 2098 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 2099 | ptr_end = ptr + item_size; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2100 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2101 | di = (struct btrfs_dir_item *)ptr; |
| 2102 | name_len = btrfs_dir_name_len(eb, di); |
| 2103 | name = kmalloc(name_len, GFP_NOFS); |
| 2104 | if (!name) { |
| 2105 | ret = -ENOMEM; |
| 2106 | goto out; |
| 2107 | } |
| 2108 | read_extent_buffer(eb, name, (unsigned long)(di + 1), |
| 2109 | name_len); |
| 2110 | log_di = NULL; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2111 | if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2112 | log_di = btrfs_lookup_dir_item(trans, log, log_path, |
| 2113 | dir_key->objectid, |
| 2114 | name, name_len, 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2115 | } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2116 | log_di = btrfs_lookup_dir_index_item(trans, log, |
| 2117 | log_path, |
| 2118 | dir_key->objectid, |
| 2119 | dir_key->offset, |
| 2120 | name, name_len, 0); |
| 2121 | } |
Filipe David Borba Manana | 269d040 | 2013-10-28 17:39:21 +0000 | [diff] [blame] | 2122 | if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2123 | btrfs_dir_item_key_to_cpu(eb, di, &location); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2124 | btrfs_release_path(path); |
| 2125 | btrfs_release_path(log_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2126 | inode = read_one_inode(root, location.objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 2127 | if (!inode) { |
| 2128 | kfree(name); |
| 2129 | return -EIO; |
| 2130 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2131 | |
| 2132 | ret = link_to_fixup_dir(trans, root, |
| 2133 | path, location.objectid); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2134 | if (ret) { |
| 2135 | kfree(name); |
| 2136 | iput(inode); |
| 2137 | goto out; |
| 2138 | } |
| 2139 | |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 2140 | inc_nlink(inode); |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 2141 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 2142 | BTRFS_I(inode), name, name_len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2143 | if (!ret) |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 2144 | ret = btrfs_run_delayed_items(trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2145 | kfree(name); |
| 2146 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2147 | if (ret) |
| 2148 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2149 | |
| 2150 | /* there might still be more names under this key |
| 2151 | * check and repeat if required |
| 2152 | */ |
| 2153 | ret = btrfs_search_slot(NULL, root, dir_key, path, |
| 2154 | 0, 0); |
| 2155 | if (ret == 0) |
| 2156 | goto again; |
| 2157 | ret = 0; |
| 2158 | goto out; |
Filipe David Borba Manana | 269d040 | 2013-10-28 17:39:21 +0000 | [diff] [blame] | 2159 | } else if (IS_ERR(log_di)) { |
| 2160 | kfree(name); |
| 2161 | return PTR_ERR(log_di); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2162 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2163 | btrfs_release_path(log_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2164 | kfree(name); |
| 2165 | |
| 2166 | ptr = (unsigned long)(di + 1); |
| 2167 | ptr += name_len; |
| 2168 | } |
| 2169 | ret = 0; |
| 2170 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2171 | btrfs_release_path(path); |
| 2172 | btrfs_release_path(log_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2173 | return ret; |
| 2174 | } |
| 2175 | |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2176 | static int replay_xattr_deletes(struct btrfs_trans_handle *trans, |
| 2177 | struct btrfs_root *root, |
| 2178 | struct btrfs_root *log, |
| 2179 | struct btrfs_path *path, |
| 2180 | const u64 ino) |
| 2181 | { |
| 2182 | struct btrfs_key search_key; |
| 2183 | struct btrfs_path *log_path; |
| 2184 | int i; |
| 2185 | int nritems; |
| 2186 | int ret; |
| 2187 | |
| 2188 | log_path = btrfs_alloc_path(); |
| 2189 | if (!log_path) |
| 2190 | return -ENOMEM; |
| 2191 | |
| 2192 | search_key.objectid = ino; |
| 2193 | search_key.type = BTRFS_XATTR_ITEM_KEY; |
| 2194 | search_key.offset = 0; |
| 2195 | again: |
| 2196 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
| 2197 | if (ret < 0) |
| 2198 | goto out; |
| 2199 | process_leaf: |
| 2200 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2201 | for (i = path->slots[0]; i < nritems; i++) { |
| 2202 | struct btrfs_key key; |
| 2203 | struct btrfs_dir_item *di; |
| 2204 | struct btrfs_dir_item *log_di; |
| 2205 | u32 total_size; |
| 2206 | u32 cur; |
| 2207 | |
| 2208 | btrfs_item_key_to_cpu(path->nodes[0], &key, i); |
| 2209 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) { |
| 2210 | ret = 0; |
| 2211 | goto out; |
| 2212 | } |
| 2213 | |
| 2214 | di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item); |
| 2215 | total_size = btrfs_item_size_nr(path->nodes[0], i); |
| 2216 | cur = 0; |
| 2217 | while (cur < total_size) { |
| 2218 | u16 name_len = btrfs_dir_name_len(path->nodes[0], di); |
| 2219 | u16 data_len = btrfs_dir_data_len(path->nodes[0], di); |
| 2220 | u32 this_len = sizeof(*di) + name_len + data_len; |
| 2221 | char *name; |
| 2222 | |
| 2223 | name = kmalloc(name_len, GFP_NOFS); |
| 2224 | if (!name) { |
| 2225 | ret = -ENOMEM; |
| 2226 | goto out; |
| 2227 | } |
| 2228 | read_extent_buffer(path->nodes[0], name, |
| 2229 | (unsigned long)(di + 1), name_len); |
| 2230 | |
| 2231 | log_di = btrfs_lookup_xattr(NULL, log, log_path, ino, |
| 2232 | name, name_len, 0); |
| 2233 | btrfs_release_path(log_path); |
| 2234 | if (!log_di) { |
| 2235 | /* Doesn't exist in log tree, so delete it. */ |
| 2236 | btrfs_release_path(path); |
| 2237 | di = btrfs_lookup_xattr(trans, root, path, ino, |
| 2238 | name, name_len, -1); |
| 2239 | kfree(name); |
| 2240 | if (IS_ERR(di)) { |
| 2241 | ret = PTR_ERR(di); |
| 2242 | goto out; |
| 2243 | } |
| 2244 | ASSERT(di); |
| 2245 | ret = btrfs_delete_one_dir_name(trans, root, |
| 2246 | path, di); |
| 2247 | if (ret) |
| 2248 | goto out; |
| 2249 | btrfs_release_path(path); |
| 2250 | search_key = key; |
| 2251 | goto again; |
| 2252 | } |
| 2253 | kfree(name); |
| 2254 | if (IS_ERR(log_di)) { |
| 2255 | ret = PTR_ERR(log_di); |
| 2256 | goto out; |
| 2257 | } |
| 2258 | cur += this_len; |
| 2259 | di = (struct btrfs_dir_item *)((char *)di + this_len); |
| 2260 | } |
| 2261 | } |
| 2262 | ret = btrfs_next_leaf(root, path); |
| 2263 | if (ret > 0) |
| 2264 | ret = 0; |
| 2265 | else if (ret == 0) |
| 2266 | goto process_leaf; |
| 2267 | out: |
| 2268 | btrfs_free_path(log_path); |
| 2269 | btrfs_release_path(path); |
| 2270 | return ret; |
| 2271 | } |
| 2272 | |
| 2273 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2274 | /* |
| 2275 | * deletion replay happens before we copy any new directory items |
| 2276 | * out of the log or out of backreferences from inodes. It |
| 2277 | * scans the log to find ranges of keys that log is authoritative for, |
| 2278 | * and then scans the directory to find items in those ranges that are |
| 2279 | * not present in the log. |
| 2280 | * |
| 2281 | * Anything we don't find in the log is unlinked and removed from the |
| 2282 | * directory. |
| 2283 | */ |
| 2284 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, |
| 2285 | struct btrfs_root *root, |
| 2286 | struct btrfs_root *log, |
| 2287 | struct btrfs_path *path, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2288 | u64 dirid, int del_all) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2289 | { |
| 2290 | u64 range_start; |
| 2291 | u64 range_end; |
| 2292 | int key_type = BTRFS_DIR_LOG_ITEM_KEY; |
| 2293 | int ret = 0; |
| 2294 | struct btrfs_key dir_key; |
| 2295 | struct btrfs_key found_key; |
| 2296 | struct btrfs_path *log_path; |
| 2297 | struct inode *dir; |
| 2298 | |
| 2299 | dir_key.objectid = dirid; |
| 2300 | dir_key.type = BTRFS_DIR_ITEM_KEY; |
| 2301 | log_path = btrfs_alloc_path(); |
| 2302 | if (!log_path) |
| 2303 | return -ENOMEM; |
| 2304 | |
| 2305 | dir = read_one_inode(root, dirid); |
| 2306 | /* it isn't an error if the inode isn't there, that can happen |
| 2307 | * because we replay the deletes before we copy in the inode item |
| 2308 | * from the log |
| 2309 | */ |
| 2310 | if (!dir) { |
| 2311 | btrfs_free_path(log_path); |
| 2312 | return 0; |
| 2313 | } |
| 2314 | again: |
| 2315 | range_start = 0; |
| 2316 | range_end = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2317 | while (1) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2318 | if (del_all) |
| 2319 | range_end = (u64)-1; |
| 2320 | else { |
| 2321 | ret = find_dir_range(log, path, dirid, key_type, |
| 2322 | &range_start, &range_end); |
| 2323 | if (ret != 0) |
| 2324 | break; |
| 2325 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2326 | |
| 2327 | dir_key.offset = range_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2328 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2329 | int nritems; |
| 2330 | ret = btrfs_search_slot(NULL, root, &dir_key, path, |
| 2331 | 0, 0); |
| 2332 | if (ret < 0) |
| 2333 | goto out; |
| 2334 | |
| 2335 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2336 | if (path->slots[0] >= nritems) { |
| 2337 | ret = btrfs_next_leaf(root, path); |
Liu Bo | b98def7 | 2018-04-03 01:59:48 +0800 | [diff] [blame] | 2338 | if (ret == 1) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2339 | break; |
Liu Bo | b98def7 | 2018-04-03 01:59:48 +0800 | [diff] [blame] | 2340 | else if (ret < 0) |
| 2341 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2342 | } |
| 2343 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2344 | path->slots[0]); |
| 2345 | if (found_key.objectid != dirid || |
| 2346 | found_key.type != dir_key.type) |
| 2347 | goto next_type; |
| 2348 | |
| 2349 | if (found_key.offset > range_end) |
| 2350 | break; |
| 2351 | |
| 2352 | ret = check_item_in_log(trans, root, log, path, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2353 | log_path, dir, |
| 2354 | &found_key); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2355 | if (ret) |
| 2356 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2357 | if (found_key.offset == (u64)-1) |
| 2358 | break; |
| 2359 | dir_key.offset = found_key.offset + 1; |
| 2360 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2361 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2362 | if (range_end == (u64)-1) |
| 2363 | break; |
| 2364 | range_start = range_end + 1; |
| 2365 | } |
| 2366 | |
| 2367 | next_type: |
| 2368 | ret = 0; |
| 2369 | if (key_type == BTRFS_DIR_LOG_ITEM_KEY) { |
| 2370 | key_type = BTRFS_DIR_LOG_INDEX_KEY; |
| 2371 | dir_key.type = BTRFS_DIR_INDEX_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2372 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2373 | goto again; |
| 2374 | } |
| 2375 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2376 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2377 | btrfs_free_path(log_path); |
| 2378 | iput(dir); |
| 2379 | return ret; |
| 2380 | } |
| 2381 | |
| 2382 | /* |
| 2383 | * the process_func used to replay items from the log tree. This |
| 2384 | * gets called in two different stages. The first stage just looks |
| 2385 | * for inodes and makes sure they are all copied into the subvolume. |
| 2386 | * |
| 2387 | * The second stage copies all the other item types from the log into |
| 2388 | * the subvolume. The two stage approach is slower, but gets rid of |
| 2389 | * lots of complexity around inodes referencing other inodes that exist |
| 2390 | * only in the log (references come from either directory items or inode |
| 2391 | * back refs). |
| 2392 | */ |
| 2393 | 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] | 2394 | struct walk_control *wc, u64 gen, int level) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2395 | { |
| 2396 | int nritems; |
| 2397 | struct btrfs_path *path; |
| 2398 | struct btrfs_root *root = wc->replay_dest; |
| 2399 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2400 | int i; |
| 2401 | int ret; |
| 2402 | |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2403 | ret = btrfs_read_buffer(eb, gen, level, NULL); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2404 | if (ret) |
| 2405 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2406 | |
| 2407 | level = btrfs_header_level(eb); |
| 2408 | |
| 2409 | if (level != 0) |
| 2410 | return 0; |
| 2411 | |
| 2412 | path = btrfs_alloc_path(); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2413 | if (!path) |
| 2414 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2415 | |
| 2416 | nritems = btrfs_header_nritems(eb); |
| 2417 | for (i = 0; i < nritems; i++) { |
| 2418 | btrfs_item_key_to_cpu(eb, &key, i); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2419 | |
| 2420 | /* inode keys are done during the first stage */ |
| 2421 | if (key.type == BTRFS_INODE_ITEM_KEY && |
| 2422 | wc->stage == LOG_WALK_REPLAY_INODES) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2423 | struct btrfs_inode_item *inode_item; |
| 2424 | u32 mode; |
| 2425 | |
| 2426 | inode_item = btrfs_item_ptr(eb, i, |
| 2427 | struct btrfs_inode_item); |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2428 | ret = replay_xattr_deletes(wc->trans, root, log, |
| 2429 | path, key.objectid); |
| 2430 | if (ret) |
| 2431 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2432 | mode = btrfs_inode_mode(eb, inode_item); |
| 2433 | if (S_ISDIR(mode)) { |
| 2434 | ret = replay_dir_deletes(wc->trans, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2435 | root, log, path, key.objectid, 0); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2436 | if (ret) |
| 2437 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2438 | } |
| 2439 | ret = overwrite_item(wc->trans, root, path, |
| 2440 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2441 | if (ret) |
| 2442 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2443 | |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2444 | /* |
| 2445 | * Before replaying extents, truncate the inode to its |
| 2446 | * size. We need to do it now and not after log replay |
| 2447 | * because before an fsync we can have prealloc extents |
| 2448 | * added beyond the inode's i_size. If we did it after, |
| 2449 | * through orphan cleanup for example, we would drop |
| 2450 | * those prealloc extents just after replaying them. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2451 | */ |
| 2452 | if (S_ISREG(mode)) { |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2453 | struct inode *inode; |
| 2454 | u64 from; |
| 2455 | |
| 2456 | inode = read_one_inode(root, key.objectid); |
| 2457 | if (!inode) { |
| 2458 | ret = -EIO; |
| 2459 | break; |
| 2460 | } |
| 2461 | from = ALIGN(i_size_read(inode), |
| 2462 | root->fs_info->sectorsize); |
| 2463 | ret = btrfs_drop_extents(wc->trans, root, inode, |
| 2464 | from, (u64)-1, 1); |
| 2465 | /* |
| 2466 | * If the nlink count is zero here, the iput |
| 2467 | * will free the inode. We bump it to make |
| 2468 | * sure it doesn't get freed until the link |
| 2469 | * count fixup is done. |
| 2470 | */ |
| 2471 | if (!ret) { |
| 2472 | if (inode->i_nlink == 0) |
| 2473 | inc_nlink(inode); |
| 2474 | /* Update link count and nbytes. */ |
| 2475 | ret = btrfs_update_inode(wc->trans, |
| 2476 | root, inode); |
| 2477 | } |
| 2478 | iput(inode); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2479 | if (ret) |
| 2480 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2481 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2482 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2483 | ret = link_to_fixup_dir(wc->trans, root, |
| 2484 | path, key.objectid); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2485 | if (ret) |
| 2486 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2487 | } |
Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2488 | |
| 2489 | if (key.type == BTRFS_DIR_INDEX_KEY && |
| 2490 | wc->stage == LOG_WALK_REPLAY_DIR_INDEX) { |
| 2491 | ret = replay_one_dir_item(wc->trans, root, path, |
| 2492 | eb, i, &key); |
| 2493 | if (ret) |
| 2494 | break; |
| 2495 | } |
| 2496 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2497 | if (wc->stage < LOG_WALK_REPLAY_ALL) |
| 2498 | continue; |
| 2499 | |
| 2500 | /* these keys are simply copied */ |
| 2501 | if (key.type == BTRFS_XATTR_ITEM_KEY) { |
| 2502 | ret = overwrite_item(wc->trans, root, path, |
| 2503 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2504 | if (ret) |
| 2505 | break; |
Liu Bo | 2da1c66 | 2013-05-26 13:50:29 +0000 | [diff] [blame] | 2506 | } else if (key.type == BTRFS_INODE_REF_KEY || |
| 2507 | key.type == BTRFS_INODE_EXTREF_KEY) { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 2508 | ret = add_inode_ref(wc->trans, root, log, path, |
| 2509 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2510 | if (ret && ret != -ENOENT) |
| 2511 | break; |
| 2512 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2513 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 2514 | ret = replay_one_extent(wc->trans, root, path, |
| 2515 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2516 | if (ret) |
| 2517 | break; |
Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2518 | } else if (key.type == BTRFS_DIR_ITEM_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2519 | ret = replay_one_dir_item(wc->trans, root, path, |
| 2520 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2521 | if (ret) |
| 2522 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2523 | } |
| 2524 | } |
| 2525 | btrfs_free_path(path); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2526 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2527 | } |
| 2528 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2529 | static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2530 | struct btrfs_root *root, |
| 2531 | struct btrfs_path *path, int *level, |
| 2532 | struct walk_control *wc) |
| 2533 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2534 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2535 | u64 root_owner; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2536 | u64 bytenr; |
| 2537 | u64 ptr_gen; |
| 2538 | struct extent_buffer *next; |
| 2539 | struct extent_buffer *cur; |
| 2540 | struct extent_buffer *parent; |
| 2541 | u32 blocksize; |
| 2542 | int ret = 0; |
| 2543 | |
| 2544 | WARN_ON(*level < 0); |
| 2545 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 2546 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2547 | while (*level > 0) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2548 | struct btrfs_key first_key; |
| 2549 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2550 | WARN_ON(*level < 0); |
| 2551 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 2552 | cur = path->nodes[*level]; |
| 2553 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2554 | WARN_ON(btrfs_header_level(cur) != *level); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2555 | |
| 2556 | if (path->slots[*level] >= |
| 2557 | btrfs_header_nritems(cur)) |
| 2558 | break; |
| 2559 | |
| 2560 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 2561 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2562 | btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2563 | blocksize = fs_info->nodesize; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2564 | |
| 2565 | parent = path->nodes[*level]; |
| 2566 | root_owner = btrfs_header_owner(parent); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2567 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2568 | next = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 2569 | if (IS_ERR(next)) |
| 2570 | return PTR_ERR(next); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2571 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2572 | if (*level == 1) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2573 | ret = wc->process_func(root, next, wc, ptr_gen, |
| 2574 | *level - 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2575 | if (ret) { |
| 2576 | free_extent_buffer(next); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2577 | return ret; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2578 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2579 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2580 | path->slots[*level]++; |
| 2581 | if (wc->free) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2582 | ret = btrfs_read_buffer(next, ptr_gen, |
| 2583 | *level - 1, &first_key); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2584 | if (ret) { |
| 2585 | free_extent_buffer(next); |
| 2586 | return ret; |
| 2587 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2588 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2589 | if (trans) { |
| 2590 | btrfs_tree_lock(next); |
| 2591 | btrfs_set_lock_blocking(next); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2592 | clean_tree_block(fs_info, next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2593 | btrfs_wait_tree_block_writeback(next); |
| 2594 | btrfs_tree_unlock(next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2595 | } else { |
| 2596 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) |
| 2597 | clear_extent_buffer_dirty(next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2598 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2599 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2600 | WARN_ON(root_owner != |
| 2601 | BTRFS_TREE_LOG_OBJECTID); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2602 | ret = btrfs_free_and_pin_reserved_extent( |
| 2603 | fs_info, bytenr, |
| 2604 | blocksize); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2605 | if (ret) { |
| 2606 | free_extent_buffer(next); |
| 2607 | return ret; |
| 2608 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2609 | } |
| 2610 | free_extent_buffer(next); |
| 2611 | continue; |
| 2612 | } |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2613 | ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2614 | if (ret) { |
| 2615 | free_extent_buffer(next); |
| 2616 | return ret; |
| 2617 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2618 | |
| 2619 | WARN_ON(*level <= 0); |
| 2620 | if (path->nodes[*level-1]) |
| 2621 | free_extent_buffer(path->nodes[*level-1]); |
| 2622 | path->nodes[*level-1] = next; |
| 2623 | *level = btrfs_header_level(next); |
| 2624 | path->slots[*level] = 0; |
| 2625 | cond_resched(); |
| 2626 | } |
| 2627 | WARN_ON(*level < 0); |
| 2628 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 2629 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2630 | path->slots[*level] = btrfs_header_nritems(path->nodes[*level]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2631 | |
| 2632 | cond_resched(); |
| 2633 | return 0; |
| 2634 | } |
| 2635 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2636 | static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2637 | struct btrfs_root *root, |
| 2638 | struct btrfs_path *path, int *level, |
| 2639 | struct walk_control *wc) |
| 2640 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2641 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2642 | u64 root_owner; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2643 | int i; |
| 2644 | int slot; |
| 2645 | int ret; |
| 2646 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2647 | for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2648 | slot = path->slots[i]; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2649 | if (slot + 1 < btrfs_header_nritems(path->nodes[i])) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2650 | path->slots[i]++; |
| 2651 | *level = i; |
| 2652 | WARN_ON(*level == 0); |
| 2653 | return 0; |
| 2654 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2655 | struct extent_buffer *parent; |
| 2656 | if (path->nodes[*level] == root->node) |
| 2657 | parent = path->nodes[*level]; |
| 2658 | else |
| 2659 | parent = path->nodes[*level + 1]; |
| 2660 | |
| 2661 | root_owner = btrfs_header_owner(parent); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2662 | ret = wc->process_func(root, path->nodes[*level], wc, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2663 | btrfs_header_generation(path->nodes[*level]), |
| 2664 | *level); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2665 | if (ret) |
| 2666 | return ret; |
| 2667 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2668 | if (wc->free) { |
| 2669 | struct extent_buffer *next; |
| 2670 | |
| 2671 | next = path->nodes[*level]; |
| 2672 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2673 | if (trans) { |
| 2674 | btrfs_tree_lock(next); |
| 2675 | btrfs_set_lock_blocking(next); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2676 | clean_tree_block(fs_info, next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2677 | btrfs_wait_tree_block_writeback(next); |
| 2678 | btrfs_tree_unlock(next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2679 | } else { |
| 2680 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) |
| 2681 | clear_extent_buffer_dirty(next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2682 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2683 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2684 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2685 | ret = btrfs_free_and_pin_reserved_extent( |
| 2686 | fs_info, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2687 | path->nodes[*level]->start, |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2688 | path->nodes[*level]->len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2689 | if (ret) |
| 2690 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2691 | } |
| 2692 | free_extent_buffer(path->nodes[*level]); |
| 2693 | path->nodes[*level] = NULL; |
| 2694 | *level = i + 1; |
| 2695 | } |
| 2696 | } |
| 2697 | return 1; |
| 2698 | } |
| 2699 | |
| 2700 | /* |
| 2701 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 2702 | * the tree freeing any blocks that have a ref count of zero after being |
| 2703 | * decremented. |
| 2704 | */ |
| 2705 | static int walk_log_tree(struct btrfs_trans_handle *trans, |
| 2706 | struct btrfs_root *log, struct walk_control *wc) |
| 2707 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2708 | struct btrfs_fs_info *fs_info = log->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2709 | int ret = 0; |
| 2710 | int wret; |
| 2711 | int level; |
| 2712 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2713 | int orig_level; |
| 2714 | |
| 2715 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 2716 | if (!path) |
| 2717 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2718 | |
| 2719 | level = btrfs_header_level(log->node); |
| 2720 | orig_level = level; |
| 2721 | path->nodes[level] = log->node; |
| 2722 | extent_buffer_get(log->node); |
| 2723 | path->slots[level] = 0; |
| 2724 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2725 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2726 | wret = walk_down_log_tree(trans, log, path, &level, wc); |
| 2727 | if (wret > 0) |
| 2728 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2729 | if (wret < 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2730 | ret = wret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2731 | goto out; |
| 2732 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2733 | |
| 2734 | wret = walk_up_log_tree(trans, log, path, &level, wc); |
| 2735 | if (wret > 0) |
| 2736 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2737 | if (wret < 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2738 | ret = wret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2739 | goto out; |
| 2740 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | /* was the root node processed? if not, catch it here */ |
| 2744 | if (path->nodes[orig_level]) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2745 | ret = wc->process_func(log, path->nodes[orig_level], wc, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2746 | btrfs_header_generation(path->nodes[orig_level]), |
| 2747 | orig_level); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2748 | if (ret) |
| 2749 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2750 | if (wc->free) { |
| 2751 | struct extent_buffer *next; |
| 2752 | |
| 2753 | next = path->nodes[orig_level]; |
| 2754 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2755 | if (trans) { |
| 2756 | btrfs_tree_lock(next); |
| 2757 | btrfs_set_lock_blocking(next); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2758 | clean_tree_block(fs_info, next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2759 | btrfs_wait_tree_block_writeback(next); |
| 2760 | btrfs_tree_unlock(next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2761 | } else { |
| 2762 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) |
| 2763 | clear_extent_buffer_dirty(next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2764 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2765 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2766 | WARN_ON(log->root_key.objectid != |
| 2767 | BTRFS_TREE_LOG_OBJECTID); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2768 | ret = btrfs_free_and_pin_reserved_extent(fs_info, |
| 2769 | next->start, next->len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2770 | if (ret) |
| 2771 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2772 | } |
| 2773 | } |
| 2774 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2775 | out: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2776 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2777 | return ret; |
| 2778 | } |
| 2779 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2780 | /* |
| 2781 | * helper function to update the item for a given subvolumes log root |
| 2782 | * in the tree of log roots |
| 2783 | */ |
| 2784 | static int update_log_root(struct btrfs_trans_handle *trans, |
| 2785 | struct btrfs_root *log) |
| 2786 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2787 | struct btrfs_fs_info *fs_info = log->fs_info; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2788 | int ret; |
| 2789 | |
| 2790 | if (log->log_transid == 1) { |
| 2791 | /* insert root item on the first sync */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2792 | ret = btrfs_insert_root(trans, fs_info->log_root_tree, |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2793 | &log->root_key, &log->root_item); |
| 2794 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2795 | ret = btrfs_update_root(trans, fs_info->log_root_tree, |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2796 | &log->root_key, &log->root_item); |
| 2797 | } |
| 2798 | return ret; |
| 2799 | } |
| 2800 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2801 | static void wait_log_commit(struct btrfs_root *root, int transid) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2802 | { |
| 2803 | DEFINE_WAIT(wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2804 | int index = transid % 2; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2805 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2806 | /* |
| 2807 | * we only allow two pending log transactions at a time, |
| 2808 | * so we know that if ours is more than 2 older than the |
| 2809 | * current transaction, we're done |
| 2810 | */ |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2811 | for (;;) { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2812 | prepare_to_wait(&root->log_commit_wait[index], |
| 2813 | &wait, TASK_UNINTERRUPTIBLE); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2814 | |
| 2815 | if (!(root->log_transid_committed < transid && |
| 2816 | atomic_read(&root->log_commit[index]))) |
| 2817 | break; |
| 2818 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2819 | mutex_unlock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2820 | schedule(); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2821 | mutex_lock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2822 | } |
| 2823 | finish_wait(&root->log_commit_wait[index], &wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2824 | } |
| 2825 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2826 | static void wait_for_writer(struct btrfs_root *root) |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2827 | { |
| 2828 | DEFINE_WAIT(wait); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2829 | |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2830 | for (;;) { |
| 2831 | prepare_to_wait(&root->log_writer_wait, &wait, |
| 2832 | TASK_UNINTERRUPTIBLE); |
| 2833 | if (!atomic_read(&root->log_writers)) |
| 2834 | break; |
| 2835 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2836 | mutex_unlock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2837 | schedule(); |
Filipe Manana | 575849e | 2015-02-11 11:12:39 +0000 | [diff] [blame] | 2838 | mutex_lock(&root->log_mutex); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2839 | } |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2840 | finish_wait(&root->log_writer_wait, &wait); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2841 | } |
| 2842 | |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2843 | static inline void btrfs_remove_log_ctx(struct btrfs_root *root, |
| 2844 | struct btrfs_log_ctx *ctx) |
| 2845 | { |
| 2846 | if (!ctx) |
| 2847 | return; |
| 2848 | |
| 2849 | mutex_lock(&root->log_mutex); |
| 2850 | list_del_init(&ctx->list); |
| 2851 | mutex_unlock(&root->log_mutex); |
| 2852 | } |
| 2853 | |
| 2854 | /* |
| 2855 | * Invoked in log mutex context, or be sure there is no other task which |
| 2856 | * can access the list. |
| 2857 | */ |
| 2858 | static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root, |
| 2859 | int index, int error) |
| 2860 | { |
| 2861 | struct btrfs_log_ctx *ctx; |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2862 | struct btrfs_log_ctx *safe; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2863 | |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2864 | list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) { |
| 2865 | list_del_init(&ctx->list); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2866 | ctx->log_ret = error; |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2867 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2868 | |
| 2869 | INIT_LIST_HEAD(&root->log_ctxs[index]); |
| 2870 | } |
| 2871 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2872 | /* |
| 2873 | * btrfs_sync_log does sends a given tree log down to the disk and |
| 2874 | * updates the super blocks to record it. When this call is done, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2875 | * you know that any inodes previously logged are safely on disk only |
| 2876 | * if it returns 0. |
| 2877 | * |
| 2878 | * Any other return value means you need to call btrfs_commit_transaction. |
| 2879 | * Some of the edge cases for fsyncing directories that have had unlinks |
| 2880 | * or renames done in the past mean that sometimes the only safe |
| 2881 | * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN, |
| 2882 | * that has happened. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2883 | */ |
| 2884 | int btrfs_sync_log(struct btrfs_trans_handle *trans, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2885 | struct btrfs_root *root, struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2886 | { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2887 | int index1; |
| 2888 | int index2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2889 | int mark; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2890 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2891 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2892 | struct btrfs_root *log = root->log_root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2893 | struct btrfs_root *log_root_tree = fs_info->log_root_tree; |
Miao Xie | bb14a59 | 2014-02-20 18:08:56 +0800 | [diff] [blame] | 2894 | int log_transid = 0; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2895 | struct btrfs_log_ctx root_log_ctx; |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2896 | struct blk_plug plug; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2897 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2898 | mutex_lock(&root->log_mutex); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2899 | log_transid = ctx->log_transid; |
| 2900 | if (root->log_transid_committed >= log_transid) { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2901 | mutex_unlock(&root->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2902 | return ctx->log_ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2903 | } |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2904 | |
| 2905 | index1 = log_transid % 2; |
| 2906 | if (atomic_read(&root->log_commit[index1])) { |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2907 | wait_log_commit(root, log_transid); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2908 | mutex_unlock(&root->log_mutex); |
| 2909 | return ctx->log_ret; |
| 2910 | } |
| 2911 | ASSERT(log_transid == root->log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2912 | atomic_set(&root->log_commit[index1], 1); |
| 2913 | |
| 2914 | /* wait for previous tree log sync to complete */ |
| 2915 | if (atomic_read(&root->log_commit[(index1 + 1) % 2])) |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2916 | wait_log_commit(root, log_transid - 1); |
Miao Xie | 48cab2e | 2014-02-20 18:08:52 +0800 | [diff] [blame] | 2917 | |
Yan, Zheng | 86df7eb | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2918 | while (1) { |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2919 | int batch = atomic_read(&root->log_batch); |
Chris Mason | cd354ad | 2011-10-20 15:45:37 -0400 | [diff] [blame] | 2920 | /* when we're on an ssd, just kick the log commit out */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2921 | if (!btrfs_test_opt(fs_info, SSD) && |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 2922 | test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) { |
Yan, Zheng | 86df7eb | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2923 | mutex_unlock(&root->log_mutex); |
| 2924 | schedule_timeout_uninterruptible(1); |
| 2925 | mutex_lock(&root->log_mutex); |
| 2926 | } |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2927 | wait_for_writer(root); |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2928 | if (batch == atomic_read(&root->log_batch)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2929 | break; |
| 2930 | } |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2931 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2932 | /* bail out if we need to do a full commit */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2933 | if (btrfs_need_log_full_commit(fs_info, trans)) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2934 | ret = -EAGAIN; |
| 2935 | mutex_unlock(&root->log_mutex); |
| 2936 | goto out; |
| 2937 | } |
| 2938 | |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2939 | if (log_transid % 2 == 0) |
| 2940 | mark = EXTENT_DIRTY; |
| 2941 | else |
| 2942 | mark = EXTENT_NEW; |
| 2943 | |
Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 2944 | /* we start IO on all the marked extents here, but we don't actually |
| 2945 | * wait for them until later. |
| 2946 | */ |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2947 | blk_start_plug(&plug); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2948 | ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2949 | if (ret) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2950 | blk_finish_plug(&plug); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2951 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2952 | btrfs_set_log_full_commit(fs_info, trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2953 | mutex_unlock(&root->log_mutex); |
| 2954 | goto out; |
| 2955 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2956 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2957 | btrfs_set_root_node(&log->root_item, log->node); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2958 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2959 | root->log_transid++; |
| 2960 | log->log_transid = root->log_transid; |
Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 2961 | root->log_start_pid = 0; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2962 | /* |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2963 | * IO has been started, blocks of the log tree have WRITTEN flag set |
| 2964 | * in their headers. new modifications of the log will be written to |
| 2965 | * 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] | 2966 | */ |
| 2967 | mutex_unlock(&root->log_mutex); |
| 2968 | |
Filipe Manana | 28a2359 | 2016-08-23 21:13:51 +0100 | [diff] [blame] | 2969 | btrfs_init_log_ctx(&root_log_ctx, NULL); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2970 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2971 | mutex_lock(&log_root_tree->log_mutex); |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2972 | atomic_inc(&log_root_tree->log_batch); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2973 | atomic_inc(&log_root_tree->log_writers); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2974 | |
| 2975 | index2 = log_root_tree->log_transid % 2; |
| 2976 | list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]); |
| 2977 | root_log_ctx.log_transid = log_root_tree->log_transid; |
| 2978 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2979 | mutex_unlock(&log_root_tree->log_mutex); |
| 2980 | |
| 2981 | ret = update_log_root(trans, log); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2982 | |
| 2983 | mutex_lock(&log_root_tree->log_mutex); |
| 2984 | if (atomic_dec_and_test(&log_root_tree->log_writers)) { |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 2985 | /* atomic_dec_and_test implies a barrier */ |
| 2986 | cond_wake_up_nomb(&log_root_tree->log_writer_wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2987 | } |
| 2988 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2989 | if (ret) { |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2990 | if (!list_empty(&root_log_ctx.list)) |
| 2991 | list_del_init(&root_log_ctx.list); |
| 2992 | |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2993 | blk_finish_plug(&plug); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2994 | btrfs_set_log_full_commit(fs_info, trans); |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 2995 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2996 | if (ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2997 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2998 | mutex_unlock(&log_root_tree->log_mutex); |
| 2999 | goto out; |
| 3000 | } |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3001 | btrfs_wait_tree_log_extents(log, mark); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3002 | mutex_unlock(&log_root_tree->log_mutex); |
| 3003 | ret = -EAGAIN; |
| 3004 | goto out; |
| 3005 | } |
| 3006 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3007 | if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) { |
Forrest Liu | 3da5ab5 | 2015-01-30 19:42:12 +0800 | [diff] [blame] | 3008 | blk_finish_plug(&plug); |
Chris Mason | cbd60aa | 2016-09-06 05:37:40 -0700 | [diff] [blame] | 3009 | list_del_init(&root_log_ctx.list); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3010 | mutex_unlock(&log_root_tree->log_mutex); |
| 3011 | ret = root_log_ctx.log_ret; |
| 3012 | goto out; |
| 3013 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3014 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3015 | index2 = root_log_ctx.log_transid % 2; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3016 | if (atomic_read(&log_root_tree->log_commit[index2])) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3017 | blk_finish_plug(&plug); |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3018 | ret = btrfs_wait_tree_log_extents(log, mark); |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3019 | wait_log_commit(log_root_tree, |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3020 | root_log_ctx.log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3021 | mutex_unlock(&log_root_tree->log_mutex); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3022 | if (!ret) |
| 3023 | ret = root_log_ctx.log_ret; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3024 | goto out; |
| 3025 | } |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3026 | ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3027 | atomic_set(&log_root_tree->log_commit[index2], 1); |
| 3028 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3029 | if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) { |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3030 | wait_log_commit(log_root_tree, |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3031 | root_log_ctx.log_transid - 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3032 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3033 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3034 | wait_for_writer(log_root_tree); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3035 | |
| 3036 | /* |
| 3037 | * now that we've moved on to the tree of log tree roots, |
| 3038 | * check the full commit flag again |
| 3039 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3040 | if (btrfs_need_log_full_commit(fs_info, trans)) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3041 | blk_finish_plug(&plug); |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3042 | btrfs_wait_tree_log_extents(log, mark); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3043 | mutex_unlock(&log_root_tree->log_mutex); |
| 3044 | ret = -EAGAIN; |
| 3045 | goto out_wake_log_root; |
| 3046 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3047 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3048 | ret = btrfs_write_marked_extents(fs_info, |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3049 | &log_root_tree->dirty_log_pages, |
| 3050 | EXTENT_DIRTY | EXTENT_NEW); |
| 3051 | blk_finish_plug(&plug); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3052 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3053 | btrfs_set_log_full_commit(fs_info, trans); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3054 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3055 | mutex_unlock(&log_root_tree->log_mutex); |
| 3056 | goto out_wake_log_root; |
| 3057 | } |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3058 | ret = btrfs_wait_tree_log_extents(log, mark); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3059 | if (!ret) |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3060 | ret = btrfs_wait_tree_log_extents(log_root_tree, |
| 3061 | EXTENT_NEW | EXTENT_DIRTY); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3062 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3063 | btrfs_set_log_full_commit(fs_info, trans); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3064 | mutex_unlock(&log_root_tree->log_mutex); |
| 3065 | goto out_wake_log_root; |
| 3066 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3067 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3068 | btrfs_set_super_log_root(fs_info->super_for_commit, |
| 3069 | log_root_tree->node->start); |
| 3070 | btrfs_set_super_log_root_level(fs_info->super_for_commit, |
| 3071 | btrfs_header_level(log_root_tree->node)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3072 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3073 | log_root_tree->log_transid++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3074 | mutex_unlock(&log_root_tree->log_mutex); |
| 3075 | |
| 3076 | /* |
| 3077 | * nobody else is going to jump in and write the the ctree |
| 3078 | * super here because the log_commit atomic below is protecting |
| 3079 | * us. We must be called with a transaction handle pinning |
| 3080 | * the running transaction open, so a full commit can't hop |
| 3081 | * in and cause problems either. |
| 3082 | */ |
David Sterba | eece6a9 | 2017-02-10 19:04:32 +0100 | [diff] [blame] | 3083 | ret = write_all_supers(fs_info, 1); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3084 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3085 | btrfs_set_log_full_commit(fs_info, trans); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3086 | btrfs_abort_transaction(trans, ret); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3087 | goto out_wake_log_root; |
| 3088 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3089 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 3090 | mutex_lock(&root->log_mutex); |
| 3091 | if (root->last_log_commit < log_transid) |
| 3092 | root->last_log_commit = log_transid; |
| 3093 | mutex_unlock(&root->log_mutex); |
| 3094 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3095 | out_wake_log_root: |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 3096 | mutex_lock(&log_root_tree->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3097 | btrfs_remove_all_log_ctxs(log_root_tree, index2, ret); |
| 3098 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3099 | log_root_tree->log_transid_committed++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3100 | atomic_set(&log_root_tree->log_commit[index2], 0); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3101 | mutex_unlock(&log_root_tree->log_mutex); |
| 3102 | |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3103 | /* |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3104 | * The barrier before waitqueue_active (in cond_wake_up) is needed so |
| 3105 | * all the updates above are seen by the woken threads. It might not be |
| 3106 | * necessary, but proving that seems to be hard. |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3107 | */ |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3108 | cond_wake_up(&log_root_tree->log_commit_wait[index2]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3109 | out: |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3110 | mutex_lock(&root->log_mutex); |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 3111 | btrfs_remove_all_log_ctxs(root, index1, ret); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3112 | root->log_transid_committed++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3113 | atomic_set(&root->log_commit[index1], 0); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3114 | mutex_unlock(&root->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3115 | |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3116 | /* |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3117 | * The barrier before waitqueue_active (in cond_wake_up) is needed so |
| 3118 | * all the updates above are seen by the woken threads. It might not be |
| 3119 | * necessary, but proving that seems to be hard. |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3120 | */ |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3121 | cond_wake_up(&root->log_commit_wait[index1]); |
Chris Mason | b31eabd | 2011-01-31 16:48:24 -0500 | [diff] [blame] | 3122 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3123 | } |
| 3124 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3125 | static void free_log_tree(struct btrfs_trans_handle *trans, |
| 3126 | struct btrfs_root *log) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3127 | { |
| 3128 | int ret; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3129 | u64 start; |
| 3130 | u64 end; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3131 | struct walk_control wc = { |
| 3132 | .free = 1, |
| 3133 | .process_func = process_one_buffer |
| 3134 | }; |
| 3135 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 3136 | ret = walk_log_tree(trans, log, &wc); |
| 3137 | /* I don't think this can happen but just in case */ |
| 3138 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3139 | btrfs_abort_transaction(trans, ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3140 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3141 | while (1) { |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3142 | ret = find_first_extent_bit(&log->dirty_log_pages, |
Liu Bo | 55237a5 | 2018-01-25 11:02:52 -0700 | [diff] [blame] | 3143 | 0, &start, &end, |
| 3144 | EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 3145 | NULL); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3146 | if (ret) |
| 3147 | break; |
| 3148 | |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 3149 | clear_extent_bits(&log->dirty_log_pages, start, end, |
Liu Bo | 55237a5 | 2018-01-25 11:02:52 -0700 | [diff] [blame] | 3150 | EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3151 | } |
| 3152 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3153 | free_extent_buffer(log->node); |
| 3154 | kfree(log); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | /* |
| 3158 | * free all the extents used by the tree log. This should be called |
| 3159 | * at commit time of the full transaction |
| 3160 | */ |
| 3161 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) |
| 3162 | { |
| 3163 | if (root->log_root) { |
| 3164 | free_log_tree(trans, root->log_root); |
| 3165 | root->log_root = NULL; |
| 3166 | } |
| 3167 | return 0; |
| 3168 | } |
| 3169 | |
| 3170 | int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, |
| 3171 | struct btrfs_fs_info *fs_info) |
| 3172 | { |
| 3173 | if (fs_info->log_root_tree) { |
| 3174 | free_log_tree(trans, fs_info->log_root_tree); |
| 3175 | fs_info->log_root_tree = NULL; |
| 3176 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3177 | return 0; |
| 3178 | } |
| 3179 | |
| 3180 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3181 | * If both a file and directory are logged, and unlinks or renames are |
| 3182 | * mixed in, we have a few interesting corners: |
| 3183 | * |
| 3184 | * create file X in dir Y |
| 3185 | * link file X to X.link in dir Y |
| 3186 | * fsync file X |
| 3187 | * unlink file X but leave X.link |
| 3188 | * fsync dir Y |
| 3189 | * |
| 3190 | * After a crash we would expect only X.link to exist. But file X |
| 3191 | * didn't get fsync'd again so the log has back refs for X and X.link. |
| 3192 | * |
| 3193 | * We solve this by removing directory entries and inode backrefs from the |
| 3194 | * log when a file that was logged in the current transaction is |
| 3195 | * unlinked. Any later fsync will include the updated log entries, and |
| 3196 | * we'll be able to reconstruct the proper directory items from backrefs. |
| 3197 | * |
| 3198 | * This optimizations allows us to avoid relogging the entire inode |
| 3199 | * or the entire directory. |
| 3200 | */ |
| 3201 | int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, |
| 3202 | struct btrfs_root *root, |
| 3203 | const char *name, int name_len, |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3204 | struct btrfs_inode *dir, u64 index) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3205 | { |
| 3206 | struct btrfs_root *log; |
| 3207 | struct btrfs_dir_item *di; |
| 3208 | struct btrfs_path *path; |
| 3209 | int ret; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3210 | int err = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3211 | int bytes_del = 0; |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3212 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3213 | |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3214 | if (dir->logged_trans < trans->transid) |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 3215 | return 0; |
| 3216 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3217 | ret = join_running_log_trans(root); |
| 3218 | if (ret) |
| 3219 | return 0; |
| 3220 | |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3221 | mutex_lock(&dir->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3222 | |
| 3223 | log = root->log_root; |
| 3224 | path = btrfs_alloc_path(); |
Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3225 | if (!path) { |
| 3226 | err = -ENOMEM; |
| 3227 | goto out_unlock; |
| 3228 | } |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 3229 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3230 | di = btrfs_lookup_dir_item(trans, log, path, dir_ino, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3231 | name, name_len, -1); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3232 | if (IS_ERR(di)) { |
| 3233 | err = PTR_ERR(di); |
| 3234 | goto fail; |
| 3235 | } |
| 3236 | if (di) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3237 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
| 3238 | bytes_del += name_len; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3239 | if (ret) { |
| 3240 | err = ret; |
| 3241 | goto fail; |
| 3242 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3243 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3244 | btrfs_release_path(path); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3245 | di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3246 | index, name, name_len, -1); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3247 | if (IS_ERR(di)) { |
| 3248 | err = PTR_ERR(di); |
| 3249 | goto fail; |
| 3250 | } |
| 3251 | if (di) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3252 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
| 3253 | bytes_del += name_len; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3254 | if (ret) { |
| 3255 | err = ret; |
| 3256 | goto fail; |
| 3257 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | /* update the directory size in the log to reflect the names |
| 3261 | * we have removed |
| 3262 | */ |
| 3263 | if (bytes_del) { |
| 3264 | struct btrfs_key key; |
| 3265 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3266 | key.objectid = dir_ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3267 | key.offset = 0; |
| 3268 | key.type = BTRFS_INODE_ITEM_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3269 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3270 | |
| 3271 | ret = btrfs_search_slot(trans, log, &key, path, 0, 1); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3272 | if (ret < 0) { |
| 3273 | err = ret; |
| 3274 | goto fail; |
| 3275 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3276 | if (ret == 0) { |
| 3277 | struct btrfs_inode_item *item; |
| 3278 | u64 i_size; |
| 3279 | |
| 3280 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3281 | struct btrfs_inode_item); |
| 3282 | i_size = btrfs_inode_size(path->nodes[0], item); |
| 3283 | if (i_size > bytes_del) |
| 3284 | i_size -= bytes_del; |
| 3285 | else |
| 3286 | i_size = 0; |
| 3287 | btrfs_set_inode_size(path->nodes[0], item, i_size); |
| 3288 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 3289 | } else |
| 3290 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3291 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3292 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3293 | fail: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3294 | btrfs_free_path(path); |
Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3295 | out_unlock: |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3296 | mutex_unlock(&dir->log_mutex); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3297 | if (ret == -ENOSPC) { |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 3298 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3299 | ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3300 | } else if (ret < 0) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3301 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3302 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3303 | btrfs_end_log_trans(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3304 | |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 3305 | return err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3306 | } |
| 3307 | |
| 3308 | /* see comments for btrfs_del_dir_entries_in_log */ |
| 3309 | int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, |
| 3310 | struct btrfs_root *root, |
| 3311 | const char *name, int name_len, |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3312 | struct btrfs_inode *inode, u64 dirid) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3313 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3314 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3315 | struct btrfs_root *log; |
| 3316 | u64 index; |
| 3317 | int ret; |
| 3318 | |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3319 | if (inode->logged_trans < trans->transid) |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 3320 | return 0; |
| 3321 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3322 | ret = join_running_log_trans(root); |
| 3323 | if (ret) |
| 3324 | return 0; |
| 3325 | log = root->log_root; |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3326 | mutex_lock(&inode->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3327 | |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3328 | 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] | 3329 | dirid, &index); |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3330 | mutex_unlock(&inode->log_mutex); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3331 | if (ret == -ENOSPC) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3332 | btrfs_set_log_full_commit(fs_info, trans); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3333 | ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3334 | } else if (ret < 0 && ret != -ENOENT) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3335 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3336 | btrfs_end_log_trans(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3337 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3338 | return ret; |
| 3339 | } |
| 3340 | |
| 3341 | /* |
| 3342 | * creates a range item in the log for 'dirid'. first_offset and |
| 3343 | * last_offset tell us which parts of the key space the log should |
| 3344 | * be considered authoritative for. |
| 3345 | */ |
| 3346 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, |
| 3347 | struct btrfs_root *log, |
| 3348 | struct btrfs_path *path, |
| 3349 | int key_type, u64 dirid, |
| 3350 | u64 first_offset, u64 last_offset) |
| 3351 | { |
| 3352 | int ret; |
| 3353 | struct btrfs_key key; |
| 3354 | struct btrfs_dir_log_item *item; |
| 3355 | |
| 3356 | key.objectid = dirid; |
| 3357 | key.offset = first_offset; |
| 3358 | if (key_type == BTRFS_DIR_ITEM_KEY) |
| 3359 | key.type = BTRFS_DIR_LOG_ITEM_KEY; |
| 3360 | else |
| 3361 | key.type = BTRFS_DIR_LOG_INDEX_KEY; |
| 3362 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3363 | if (ret) |
| 3364 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3365 | |
| 3366 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3367 | struct btrfs_dir_log_item); |
| 3368 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); |
| 3369 | btrfs_mark_buffer_dirty(path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3370 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3371 | return 0; |
| 3372 | } |
| 3373 | |
| 3374 | /* |
| 3375 | * log all the items included in the current transaction for a given |
| 3376 | * directory. This also creates the range items in the log tree required |
| 3377 | * to replay anything deleted before the fsync |
| 3378 | */ |
| 3379 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 684a577 | 2017-01-18 00:31:41 +0200 | [diff] [blame] | 3380 | struct btrfs_root *root, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3381 | struct btrfs_path *path, |
| 3382 | struct btrfs_path *dst_path, int key_type, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3383 | struct btrfs_log_ctx *ctx, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3384 | u64 min_offset, u64 *last_offset_ret) |
| 3385 | { |
| 3386 | struct btrfs_key min_key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3387 | struct btrfs_root *log = root->log_root; |
| 3388 | struct extent_buffer *src; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3389 | int err = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3390 | int ret; |
| 3391 | int i; |
| 3392 | int nritems; |
| 3393 | u64 first_offset = min_offset; |
| 3394 | u64 last_offset = (u64)-1; |
Nikolay Borisov | 684a577 | 2017-01-18 00:31:41 +0200 | [diff] [blame] | 3395 | u64 ino = btrfs_ino(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3396 | |
| 3397 | log = root->log_root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3398 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3399 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3400 | min_key.type = key_type; |
| 3401 | min_key.offset = min_offset; |
| 3402 | |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3403 | ret = btrfs_search_forward(root, &min_key, path, trans->transid); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3404 | |
| 3405 | /* |
| 3406 | * we didn't find anything from this transaction, see if there |
| 3407 | * is anything at all |
| 3408 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3409 | if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) { |
| 3410 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3411 | min_key.type = key_type; |
| 3412 | min_key.offset = (u64)-1; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3413 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3414 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
| 3415 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3416 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3417 | return ret; |
| 3418 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3419 | ret = btrfs_previous_item(root, path, ino, key_type); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3420 | |
| 3421 | /* if ret == 0 there are items for this type, |
| 3422 | * create a range to tell us the last key of this type. |
| 3423 | * otherwise, there are no items in this directory after |
| 3424 | * *min_offset, and we create a range to indicate that. |
| 3425 | */ |
| 3426 | if (ret == 0) { |
| 3427 | struct btrfs_key tmp; |
| 3428 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, |
| 3429 | path->slots[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3430 | if (key_type == tmp.type) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3431 | first_offset = max(min_offset, tmp.offset) + 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3432 | } |
| 3433 | goto done; |
| 3434 | } |
| 3435 | |
| 3436 | /* go backward to find any previous key */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3437 | ret = btrfs_previous_item(root, path, ino, key_type); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3438 | if (ret == 0) { |
| 3439 | struct btrfs_key tmp; |
| 3440 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
| 3441 | if (key_type == tmp.type) { |
| 3442 | first_offset = tmp.offset; |
| 3443 | ret = overwrite_item(trans, log, dst_path, |
| 3444 | path->nodes[0], path->slots[0], |
| 3445 | &tmp); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3446 | if (ret) { |
| 3447 | err = ret; |
| 3448 | goto done; |
| 3449 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3450 | } |
| 3451 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3452 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3453 | |
| 3454 | /* find the first key from this transaction again */ |
| 3455 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3456 | if (WARN_ON(ret != 0)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3457 | goto done; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3458 | |
| 3459 | /* |
| 3460 | * we have a block from this transaction, log every item in it |
| 3461 | * from our directory |
| 3462 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3463 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3464 | struct btrfs_key tmp; |
| 3465 | src = path->nodes[0]; |
| 3466 | nritems = btrfs_header_nritems(src); |
| 3467 | for (i = path->slots[0]; i < nritems; i++) { |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3468 | struct btrfs_dir_item *di; |
| 3469 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3470 | btrfs_item_key_to_cpu(src, &min_key, i); |
| 3471 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3472 | if (min_key.objectid != ino || min_key.type != key_type) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3473 | goto done; |
| 3474 | ret = overwrite_item(trans, log, dst_path, src, i, |
| 3475 | &min_key); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3476 | if (ret) { |
| 3477 | err = ret; |
| 3478 | goto done; |
| 3479 | } |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3480 | |
| 3481 | /* |
| 3482 | * We must make sure that when we log a directory entry, |
| 3483 | * the corresponding inode, after log replay, has a |
| 3484 | * matching link count. For example: |
| 3485 | * |
| 3486 | * touch foo |
| 3487 | * mkdir mydir |
| 3488 | * sync |
| 3489 | * ln foo mydir/bar |
| 3490 | * xfs_io -c "fsync" mydir |
| 3491 | * <crash> |
| 3492 | * <mount fs and log replay> |
| 3493 | * |
| 3494 | * Would result in a fsync log that when replayed, our |
| 3495 | * file inode would have a link count of 1, but we get |
| 3496 | * two directory entries pointing to the same inode. |
| 3497 | * After removing one of the names, it would not be |
| 3498 | * possible to remove the other name, which resulted |
| 3499 | * always in stale file handle errors, and would not |
| 3500 | * be possible to rmdir the parent directory, since |
| 3501 | * its i_size could never decrement to the value |
| 3502 | * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors. |
| 3503 | */ |
| 3504 | di = btrfs_item_ptr(src, i, struct btrfs_dir_item); |
| 3505 | btrfs_dir_item_key_to_cpu(src, di, &tmp); |
| 3506 | if (ctx && |
| 3507 | (btrfs_dir_transid(src, di) == trans->transid || |
| 3508 | btrfs_dir_type(src, di) == BTRFS_FT_DIR) && |
| 3509 | tmp.type != BTRFS_ROOT_ITEM_KEY) |
| 3510 | ctx->log_new_dentries = true; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3511 | } |
| 3512 | path->slots[0] = nritems; |
| 3513 | |
| 3514 | /* |
| 3515 | * look ahead to the next item and see if it is also |
| 3516 | * from this directory and from this transaction |
| 3517 | */ |
| 3518 | ret = btrfs_next_leaf(root, path); |
Liu Bo | 80c0b42 | 2018-04-03 01:59:47 +0800 | [diff] [blame] | 3519 | if (ret) { |
| 3520 | if (ret == 1) |
| 3521 | last_offset = (u64)-1; |
| 3522 | else |
| 3523 | err = ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3524 | goto done; |
| 3525 | } |
| 3526 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3527 | if (tmp.objectid != ino || tmp.type != key_type) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3528 | last_offset = (u64)-1; |
| 3529 | goto done; |
| 3530 | } |
| 3531 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { |
| 3532 | ret = overwrite_item(trans, log, dst_path, |
| 3533 | path->nodes[0], path->slots[0], |
| 3534 | &tmp); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3535 | if (ret) |
| 3536 | err = ret; |
| 3537 | else |
| 3538 | last_offset = tmp.offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3539 | goto done; |
| 3540 | } |
| 3541 | } |
| 3542 | done: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3543 | btrfs_release_path(path); |
| 3544 | btrfs_release_path(dst_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3545 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3546 | if (err == 0) { |
| 3547 | *last_offset_ret = last_offset; |
| 3548 | /* |
| 3549 | * insert the log range keys to indicate where the log |
| 3550 | * is valid |
| 3551 | */ |
| 3552 | ret = insert_dir_log_key(trans, log, path, key_type, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3553 | ino, first_offset, last_offset); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3554 | if (ret) |
| 3555 | err = ret; |
| 3556 | } |
| 3557 | return err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | /* |
| 3561 | * logging directories is very similar to logging inodes, We find all the items |
| 3562 | * from the current transaction and write them to the log. |
| 3563 | * |
| 3564 | * The recovery code scans the directory in the subvolume, and if it finds a |
| 3565 | * key in the range logged that is not present in the log tree, then it means |
| 3566 | * that dir entry was unlinked during the transaction. |
| 3567 | * |
| 3568 | * In order for that scan to work, we must include one key smaller than |
| 3569 | * the smallest logged by this transaction and one key larger than the largest |
| 3570 | * key logged by this transaction. |
| 3571 | */ |
| 3572 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, |
Nikolay Borisov | dbf39ea | 2017-01-18 00:31:42 +0200 | [diff] [blame] | 3573 | struct btrfs_root *root, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3574 | struct btrfs_path *path, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3575 | struct btrfs_path *dst_path, |
| 3576 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3577 | { |
| 3578 | u64 min_key; |
| 3579 | u64 max_key; |
| 3580 | int ret; |
| 3581 | int key_type = BTRFS_DIR_ITEM_KEY; |
| 3582 | |
| 3583 | again: |
| 3584 | min_key = 0; |
| 3585 | max_key = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3586 | while (1) { |
Nikolay Borisov | dbf39ea | 2017-01-18 00:31:42 +0200 | [diff] [blame] | 3587 | ret = log_dir_items(trans, root, inode, path, dst_path, key_type, |
| 3588 | ctx, min_key, &max_key); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3589 | if (ret) |
| 3590 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3591 | if (max_key == (u64)-1) |
| 3592 | break; |
| 3593 | min_key = max_key + 1; |
| 3594 | } |
| 3595 | |
| 3596 | if (key_type == BTRFS_DIR_ITEM_KEY) { |
| 3597 | key_type = BTRFS_DIR_INDEX_KEY; |
| 3598 | goto again; |
| 3599 | } |
| 3600 | return 0; |
| 3601 | } |
| 3602 | |
| 3603 | /* |
| 3604 | * a helper function to drop items from the log before we relog an |
| 3605 | * inode. max_key_type indicates the highest item type to remove. |
| 3606 | * This cannot be run for file data extents because it does not |
| 3607 | * free the extents they point to. |
| 3608 | */ |
| 3609 | static int drop_objectid_items(struct btrfs_trans_handle *trans, |
| 3610 | struct btrfs_root *log, |
| 3611 | struct btrfs_path *path, |
| 3612 | u64 objectid, int max_key_type) |
| 3613 | { |
| 3614 | int ret; |
| 3615 | struct btrfs_key key; |
| 3616 | struct btrfs_key found_key; |
Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 3617 | int start_slot; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3618 | |
| 3619 | key.objectid = objectid; |
| 3620 | key.type = max_key_type; |
| 3621 | key.offset = (u64)-1; |
| 3622 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3623 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3624 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3625 | BUG_ON(ret == 0); /* Logic error */ |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3626 | if (ret < 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3627 | break; |
| 3628 | |
| 3629 | if (path->slots[0] == 0) |
| 3630 | break; |
| 3631 | |
| 3632 | path->slots[0]--; |
| 3633 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 3634 | path->slots[0]); |
| 3635 | |
| 3636 | if (found_key.objectid != objectid) |
| 3637 | break; |
| 3638 | |
Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 3639 | found_key.offset = 0; |
| 3640 | found_key.type = 0; |
| 3641 | ret = btrfs_bin_search(path->nodes[0], &found_key, 0, |
| 3642 | &start_slot); |
| 3643 | |
| 3644 | ret = btrfs_del_items(trans, log, path, start_slot, |
| 3645 | path->slots[0] - start_slot + 1); |
| 3646 | /* |
| 3647 | * If start slot isn't 0 then we don't need to re-search, we've |
| 3648 | * found the last guy with the objectid in this tree. |
| 3649 | */ |
| 3650 | if (ret || start_slot != 0) |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 3651 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3652 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3653 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3654 | btrfs_release_path(path); |
Josef Bacik | 5bdbeb2 | 2012-05-29 16:59:49 -0400 | [diff] [blame] | 3655 | if (ret > 0) |
| 3656 | ret = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3657 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3658 | } |
| 3659 | |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3660 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3661 | struct extent_buffer *leaf, |
| 3662 | struct btrfs_inode_item *item, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3663 | struct inode *inode, int log_inode_only, |
| 3664 | u64 logged_isize) |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3665 | { |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3666 | struct btrfs_map_token token; |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3667 | |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3668 | btrfs_init_map_token(&token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3669 | |
| 3670 | if (log_inode_only) { |
| 3671 | /* set the generation to zero so the recover code |
| 3672 | * can tell the difference between an logging |
| 3673 | * just to say 'this inode exists' and a logging |
| 3674 | * to say 'update this inode with these values' |
| 3675 | */ |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3676 | btrfs_set_token_inode_generation(leaf, item, 0, &token); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3677 | btrfs_set_token_inode_size(leaf, item, logged_isize, &token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3678 | } else { |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3679 | btrfs_set_token_inode_generation(leaf, item, |
| 3680 | BTRFS_I(inode)->generation, |
| 3681 | &token); |
| 3682 | btrfs_set_token_inode_size(leaf, item, inode->i_size, &token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3683 | } |
| 3684 | |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3685 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3686 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3687 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3688 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
| 3689 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3690 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3691 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3692 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3693 | inode->i_atime.tv_nsec, &token); |
| 3694 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3695 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3696 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3697 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3698 | inode->i_mtime.tv_nsec, &token); |
| 3699 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3700 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3701 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3702 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3703 | inode->i_ctime.tv_nsec, &token); |
| 3704 | |
| 3705 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3706 | &token); |
| 3707 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3708 | btrfs_set_token_inode_sequence(leaf, item, |
| 3709 | inode_peek_iversion(inode), &token); |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3710 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3711 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3712 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3713 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3714 | } |
| 3715 | |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3716 | static int log_inode_item(struct btrfs_trans_handle *trans, |
| 3717 | struct btrfs_root *log, struct btrfs_path *path, |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3718 | struct btrfs_inode *inode) |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3719 | { |
| 3720 | struct btrfs_inode_item *inode_item; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3721 | int ret; |
| 3722 | |
Filipe David Borba Manana | efd0c40 | 2013-10-07 21:20:44 +0100 | [diff] [blame] | 3723 | ret = btrfs_insert_empty_item(trans, log, path, |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3724 | &inode->location, sizeof(*inode_item)); |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3725 | if (ret && ret != -EEXIST) |
| 3726 | return ret; |
| 3727 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3728 | struct btrfs_inode_item); |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3729 | fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode, |
| 3730 | 0, 0); |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3731 | btrfs_release_path(path); |
| 3732 | return 0; |
| 3733 | } |
| 3734 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3735 | static noinline int copy_items(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3736 | struct btrfs_inode *inode, |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3737 | struct btrfs_path *dst_path, |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3738 | struct btrfs_path *src_path, u64 *last_extent, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3739 | int start_slot, int nr, int inode_only, |
| 3740 | u64 logged_isize) |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3741 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame^] | 3742 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3743 | unsigned long src_offset; |
| 3744 | unsigned long dst_offset; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3745 | struct btrfs_root *log = inode->root->log_root; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3746 | struct btrfs_file_extent_item *extent; |
| 3747 | struct btrfs_inode_item *inode_item; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3748 | struct extent_buffer *src = src_path->nodes[0]; |
| 3749 | struct btrfs_key first_key, last_key, key; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3750 | int ret; |
| 3751 | struct btrfs_key *ins_keys; |
| 3752 | u32 *ins_sizes; |
| 3753 | char *ins_data; |
| 3754 | int i; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3755 | struct list_head ordered_sums; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3756 | int skip_csum = inode->flags & BTRFS_INODE_NODATASUM; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3757 | bool has_extents = false; |
Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 3758 | bool need_find_last_extent = true; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3759 | bool done = false; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3760 | |
| 3761 | INIT_LIST_HEAD(&ordered_sums); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3762 | |
| 3763 | ins_data = kmalloc(nr * sizeof(struct btrfs_key) + |
| 3764 | nr * sizeof(u32), GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 3765 | if (!ins_data) |
| 3766 | return -ENOMEM; |
| 3767 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3768 | first_key.objectid = (u64)-1; |
| 3769 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3770 | ins_sizes = (u32 *)ins_data; |
| 3771 | ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); |
| 3772 | |
| 3773 | for (i = 0; i < nr; i++) { |
| 3774 | ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot); |
| 3775 | btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot); |
| 3776 | } |
| 3777 | ret = btrfs_insert_empty_items(trans, log, dst_path, |
| 3778 | ins_keys, ins_sizes, nr); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3779 | if (ret) { |
| 3780 | kfree(ins_data); |
| 3781 | return ret; |
| 3782 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3783 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3784 | for (i = 0; i < nr; i++, dst_path->slots[0]++) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3785 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], |
| 3786 | dst_path->slots[0]); |
| 3787 | |
| 3788 | src_offset = btrfs_item_ptr_offset(src, start_slot + i); |
| 3789 | |
Matthias Kaehlcke | 0dde10b | 2017-07-27 14:30:23 -0700 | [diff] [blame] | 3790 | if (i == nr - 1) |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3791 | last_key = ins_keys[i]; |
| 3792 | |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3793 | if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3794 | inode_item = btrfs_item_ptr(dst_path->nodes[0], |
| 3795 | dst_path->slots[0], |
| 3796 | struct btrfs_inode_item); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3797 | fill_inode_item(trans, dst_path->nodes[0], inode_item, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3798 | &inode->vfs_inode, |
| 3799 | inode_only == LOG_INODE_EXISTS, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3800 | logged_isize); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3801 | } else { |
| 3802 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, |
| 3803 | src_offset, ins_sizes[i]); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3804 | } |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3805 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3806 | /* |
| 3807 | * We set need_find_last_extent here in case we know we were |
| 3808 | * processing other items and then walk into the first extent in |
| 3809 | * the inode. If we don't hit an extent then nothing changes, |
| 3810 | * we'll do the last search the next time around. |
| 3811 | */ |
| 3812 | if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) { |
| 3813 | has_extents = true; |
Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 3814 | if (first_key.objectid == (u64)-1) |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3815 | first_key = ins_keys[i]; |
| 3816 | } else { |
| 3817 | need_find_last_extent = false; |
| 3818 | } |
| 3819 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3820 | /* take a reference on file data extents so that truncates |
| 3821 | * or deletes of this inode don't have to relog the inode |
| 3822 | * again |
| 3823 | */ |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3824 | if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY && |
Liu Bo | d279440 | 2012-08-29 01:07:56 -0600 | [diff] [blame] | 3825 | !skip_csum) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3826 | int found_type; |
| 3827 | extent = btrfs_item_ptr(src, start_slot + i, |
| 3828 | struct btrfs_file_extent_item); |
| 3829 | |
liubo | 8e531cd | 2011-05-06 10:36:09 +0800 | [diff] [blame] | 3830 | if (btrfs_file_extent_generation(src, extent) < trans->transid) |
| 3831 | continue; |
| 3832 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3833 | found_type = btrfs_file_extent_type(src, extent); |
Josef Bacik | 6f1fed7 | 2012-09-26 11:07:06 -0400 | [diff] [blame] | 3834 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3835 | u64 ds, dl, cs, cl; |
| 3836 | ds = btrfs_file_extent_disk_bytenr(src, |
| 3837 | extent); |
| 3838 | /* ds == 0 is a hole */ |
| 3839 | if (ds == 0) |
| 3840 | continue; |
| 3841 | |
| 3842 | dl = btrfs_file_extent_disk_num_bytes(src, |
| 3843 | extent); |
| 3844 | cs = btrfs_file_extent_offset(src, extent); |
| 3845 | cl = btrfs_file_extent_num_bytes(src, |
Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 3846 | extent); |
Chris Mason | 580afd7 | 2008-12-08 19:15:39 -0500 | [diff] [blame] | 3847 | if (btrfs_file_extent_compression(src, |
| 3848 | extent)) { |
| 3849 | cs = 0; |
| 3850 | cl = dl; |
| 3851 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3852 | |
| 3853 | ret = btrfs_lookup_csums_range( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3854 | fs_info->csum_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3855 | ds + cs, ds + cs + cl - 1, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3856 | &ordered_sums, 0); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3857 | if (ret) { |
| 3858 | btrfs_release_path(dst_path); |
| 3859 | kfree(ins_data); |
| 3860 | return ret; |
| 3861 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3862 | } |
| 3863 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | btrfs_mark_buffer_dirty(dst_path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3867 | btrfs_release_path(dst_path); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3868 | kfree(ins_data); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3869 | |
| 3870 | /* |
| 3871 | * we have to do this after the loop above to avoid changing the |
| 3872 | * log tree while trying to change the log tree. |
| 3873 | */ |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3874 | ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3875 | while (!list_empty(&ordered_sums)) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3876 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, |
| 3877 | struct btrfs_ordered_sum, |
| 3878 | list); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3879 | if (!ret) |
| 3880 | ret = btrfs_csum_file_blocks(trans, log, sums); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3881 | list_del(&sums->list); |
| 3882 | kfree(sums); |
| 3883 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3884 | |
| 3885 | if (!has_extents) |
| 3886 | return ret; |
| 3887 | |
Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 3888 | if (need_find_last_extent && *last_extent == first_key.offset) { |
| 3889 | /* |
| 3890 | * We don't have any leafs between our current one and the one |
| 3891 | * we processed before that can have file extent items for our |
| 3892 | * inode (and have a generation number smaller than our current |
| 3893 | * transaction id). |
| 3894 | */ |
| 3895 | need_find_last_extent = false; |
| 3896 | } |
| 3897 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3898 | /* |
| 3899 | * Because we use btrfs_search_forward we could skip leaves that were |
| 3900 | * not modified and then assume *last_extent is valid when it really |
| 3901 | * isn't. So back up to the previous leaf and read the end of the last |
| 3902 | * extent before we go and fill in holes. |
| 3903 | */ |
| 3904 | if (need_find_last_extent) { |
| 3905 | u64 len; |
| 3906 | |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3907 | ret = btrfs_prev_leaf(inode->root, src_path); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3908 | if (ret < 0) |
| 3909 | return ret; |
| 3910 | if (ret) |
| 3911 | goto fill_holes; |
| 3912 | if (src_path->slots[0]) |
| 3913 | src_path->slots[0]--; |
| 3914 | src = src_path->nodes[0]; |
| 3915 | btrfs_item_key_to_cpu(src, &key, src_path->slots[0]); |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3916 | if (key.objectid != btrfs_ino(inode) || |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3917 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 3918 | goto fill_holes; |
| 3919 | extent = btrfs_item_ptr(src, src_path->slots[0], |
| 3920 | struct btrfs_file_extent_item); |
| 3921 | if (btrfs_file_extent_type(src, extent) == |
| 3922 | BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 3923 | len = btrfs_file_extent_ram_bytes(src, extent); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3924 | *last_extent = ALIGN(key.offset + len, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3925 | fs_info->sectorsize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3926 | } else { |
| 3927 | len = btrfs_file_extent_num_bytes(src, extent); |
| 3928 | *last_extent = key.offset + len; |
| 3929 | } |
| 3930 | } |
| 3931 | fill_holes: |
| 3932 | /* So we did prev_leaf, now we need to move to the next leaf, but a few |
| 3933 | * things could have happened |
| 3934 | * |
| 3935 | * 1) A merge could have happened, so we could currently be on a leaf |
| 3936 | * that holds what we were copying in the first place. |
| 3937 | * 2) A split could have happened, and now not all of the items we want |
| 3938 | * are on the same leaf. |
| 3939 | * |
| 3940 | * So we need to adjust how we search for holes, we need to drop the |
| 3941 | * path and re-search for the first extent key we found, and then walk |
| 3942 | * forward until we hit the last one we copied. |
| 3943 | */ |
| 3944 | if (need_find_last_extent) { |
| 3945 | /* btrfs_prev_leaf could return 1 without releasing the path */ |
| 3946 | btrfs_release_path(src_path); |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3947 | ret = btrfs_search_slot(NULL, inode->root, &first_key, |
| 3948 | src_path, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3949 | if (ret < 0) |
| 3950 | return ret; |
| 3951 | ASSERT(ret == 0); |
| 3952 | src = src_path->nodes[0]; |
| 3953 | i = src_path->slots[0]; |
| 3954 | } else { |
| 3955 | i = start_slot; |
| 3956 | } |
| 3957 | |
| 3958 | /* |
| 3959 | * Ok so here we need to go through and fill in any holes we may have |
| 3960 | * to make sure that holes are punched for those areas in case they had |
| 3961 | * extents previously. |
| 3962 | */ |
| 3963 | while (!done) { |
| 3964 | u64 offset, len; |
| 3965 | u64 extent_end; |
| 3966 | |
| 3967 | if (i >= btrfs_header_nritems(src_path->nodes[0])) { |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3968 | ret = btrfs_next_leaf(inode->root, src_path); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3969 | if (ret < 0) |
| 3970 | return ret; |
| 3971 | ASSERT(ret == 0); |
| 3972 | src = src_path->nodes[0]; |
| 3973 | i = 0; |
Filipe Manana | 8434ec4 | 2018-03-26 23:59:12 +0100 | [diff] [blame] | 3974 | need_find_last_extent = true; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | btrfs_item_key_to_cpu(src, &key, i); |
| 3978 | if (!btrfs_comp_cpu_keys(&key, &last_key)) |
| 3979 | done = true; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3980 | if (key.objectid != btrfs_ino(inode) || |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3981 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 3982 | i++; |
| 3983 | continue; |
| 3984 | } |
| 3985 | extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item); |
| 3986 | if (btrfs_file_extent_type(src, extent) == |
| 3987 | BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 3988 | len = btrfs_file_extent_ram_bytes(src, extent); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 3989 | extent_end = ALIGN(key.offset + len, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3990 | fs_info->sectorsize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3991 | } else { |
| 3992 | len = btrfs_file_extent_num_bytes(src, extent); |
| 3993 | extent_end = key.offset + len; |
| 3994 | } |
| 3995 | i++; |
| 3996 | |
| 3997 | if (*last_extent == key.offset) { |
| 3998 | *last_extent = extent_end; |
| 3999 | continue; |
| 4000 | } |
| 4001 | offset = *last_extent; |
| 4002 | len = key.offset - *last_extent; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 4003 | ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode), |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4004 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4005 | if (ret) |
| 4006 | break; |
Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 4007 | *last_extent = extent_end; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4008 | } |
Filipe Manana | 4ee3fad | 2018-03-26 23:59:00 +0100 | [diff] [blame] | 4009 | |
| 4010 | /* |
| 4011 | * Check if there is a hole between the last extent found in our leaf |
| 4012 | * and the first extent in the next leaf. If there is one, we need to |
| 4013 | * log an explicit hole so that at replay time we can punch the hole. |
| 4014 | */ |
| 4015 | if (ret == 0 && |
| 4016 | key.objectid == btrfs_ino(inode) && |
| 4017 | key.type == BTRFS_EXTENT_DATA_KEY && |
| 4018 | i == btrfs_header_nritems(src_path->nodes[0])) { |
| 4019 | ret = btrfs_next_leaf(inode->root, src_path); |
| 4020 | need_find_last_extent = true; |
| 4021 | if (ret > 0) { |
| 4022 | ret = 0; |
| 4023 | } else if (ret == 0) { |
| 4024 | btrfs_item_key_to_cpu(src_path->nodes[0], &key, |
| 4025 | src_path->slots[0]); |
| 4026 | if (key.objectid == btrfs_ino(inode) && |
| 4027 | key.type == BTRFS_EXTENT_DATA_KEY && |
| 4028 | *last_extent < key.offset) { |
| 4029 | const u64 len = key.offset - *last_extent; |
| 4030 | |
| 4031 | ret = btrfs_insert_file_extent(trans, log, |
| 4032 | btrfs_ino(inode), |
| 4033 | *last_extent, 0, |
| 4034 | 0, len, 0, len, |
| 4035 | 0, 0, 0); |
| 4036 | } |
| 4037 | } |
| 4038 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4039 | /* |
| 4040 | * Need to let the callers know we dropped the path so they should |
| 4041 | * re-search. |
| 4042 | */ |
| 4043 | if (!ret && need_find_last_extent) |
| 4044 | ret = 1; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4045 | return ret; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4046 | } |
| 4047 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4048 | static int extent_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 4049 | { |
| 4050 | struct extent_map *em1, *em2; |
| 4051 | |
| 4052 | em1 = list_entry(a, struct extent_map, list); |
| 4053 | em2 = list_entry(b, struct extent_map, list); |
| 4054 | |
| 4055 | if (em1->start < em2->start) |
| 4056 | return -1; |
| 4057 | else if (em1->start > em2->start) |
| 4058 | return 1; |
| 4059 | return 0; |
| 4060 | } |
| 4061 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4062 | static int log_extent_csums(struct btrfs_trans_handle *trans, |
| 4063 | struct btrfs_inode *inode, |
Nikolay Borisov | a9ecb65 | 2018-06-20 17:26:42 +0300 | [diff] [blame] | 4064 | struct btrfs_root *log_root, |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4065 | const struct extent_map *em) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4066 | { |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4067 | u64 csum_offset; |
| 4068 | u64 csum_len; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4069 | LIST_HEAD(ordered_sums); |
| 4070 | int ret = 0; |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4071 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4072 | if (inode->flags & BTRFS_INODE_NODATASUM || |
| 4073 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4074 | em->block_start == EXTENT_MAP_HOLE) |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4075 | return 0; |
| 4076 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4077 | /* 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] | 4078 | if (em->compress_type) { |
| 4079 | csum_offset = 0; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4080 | csum_len = max(em->block_len, em->orig_block_len); |
Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4081 | } else { |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4082 | csum_offset = em->mod_start - em->start; |
| 4083 | csum_len = em->mod_len; |
Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4084 | } |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4085 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4086 | /* block start is already adjusted for the file extent offset. */ |
Nikolay Borisov | a9ecb65 | 2018-06-20 17:26:42 +0300 | [diff] [blame] | 4087 | ret = btrfs_lookup_csums_range(trans->fs_info->csum_root, |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4088 | em->block_start + csum_offset, |
| 4089 | em->block_start + csum_offset + |
| 4090 | csum_len - 1, &ordered_sums, 0); |
| 4091 | if (ret) |
| 4092 | return ret; |
| 4093 | |
| 4094 | while (!list_empty(&ordered_sums)) { |
| 4095 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, |
| 4096 | struct btrfs_ordered_sum, |
| 4097 | list); |
| 4098 | if (!ret) |
Nikolay Borisov | a9ecb65 | 2018-06-20 17:26:42 +0300 | [diff] [blame] | 4099 | ret = btrfs_csum_file_blocks(trans, log_root, sums); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4100 | list_del(&sums->list); |
| 4101 | kfree(sums); |
| 4102 | } |
| 4103 | |
| 4104 | return ret; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4105 | } |
| 4106 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4107 | static int log_one_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4108 | struct btrfs_inode *inode, struct btrfs_root *root, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4109 | const struct extent_map *em, |
| 4110 | struct btrfs_path *path, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4111 | struct btrfs_log_ctx *ctx) |
| 4112 | { |
| 4113 | struct btrfs_root *log = root->log_root; |
| 4114 | struct btrfs_file_extent_item *fi; |
| 4115 | struct extent_buffer *leaf; |
| 4116 | struct btrfs_map_token token; |
| 4117 | struct btrfs_key key; |
| 4118 | u64 extent_offset = em->start - em->orig_start; |
| 4119 | u64 block_len; |
| 4120 | int ret; |
| 4121 | int extent_inserted = 0; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4122 | |
Nikolay Borisov | a9ecb65 | 2018-06-20 17:26:42 +0300 | [diff] [blame] | 4123 | ret = log_extent_csums(trans, inode, log, em); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4124 | if (ret) |
| 4125 | return ret; |
| 4126 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4127 | btrfs_init_map_token(&token); |
| 4128 | |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4129 | ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4130 | em->start + em->len, NULL, 0, 1, |
| 4131 | sizeof(*fi), &extent_inserted); |
| 4132 | if (ret) |
| 4133 | return ret; |
| 4134 | |
| 4135 | if (!extent_inserted) { |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4136 | key.objectid = btrfs_ino(inode); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4137 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4138 | key.offset = em->start; |
| 4139 | |
| 4140 | ret = btrfs_insert_empty_item(trans, log, path, &key, |
| 4141 | sizeof(*fi)); |
| 4142 | if (ret) |
| 4143 | return ret; |
| 4144 | } |
| 4145 | leaf = path->nodes[0]; |
| 4146 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4147 | struct btrfs_file_extent_item); |
| 4148 | |
Josef Bacik | 50d9aa9 | 2014-11-21 14:52:38 -0500 | [diff] [blame] | 4149 | btrfs_set_token_file_extent_generation(leaf, fi, trans->transid, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4150 | &token); |
| 4151 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 4152 | btrfs_set_token_file_extent_type(leaf, fi, |
| 4153 | BTRFS_FILE_EXTENT_PREALLOC, |
| 4154 | &token); |
| 4155 | else |
| 4156 | btrfs_set_token_file_extent_type(leaf, fi, |
| 4157 | BTRFS_FILE_EXTENT_REG, |
| 4158 | &token); |
| 4159 | |
| 4160 | block_len = max(em->block_len, em->orig_block_len); |
| 4161 | if (em->compress_type != BTRFS_COMPRESS_NONE) { |
| 4162 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, |
| 4163 | em->block_start, |
| 4164 | &token); |
| 4165 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len, |
| 4166 | &token); |
| 4167 | } else if (em->block_start < EXTENT_MAP_LAST_BYTE) { |
| 4168 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, |
| 4169 | em->block_start - |
| 4170 | extent_offset, &token); |
| 4171 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len, |
| 4172 | &token); |
| 4173 | } else { |
| 4174 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token); |
| 4175 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0, |
| 4176 | &token); |
| 4177 | } |
| 4178 | |
| 4179 | btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token); |
| 4180 | btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token); |
| 4181 | btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token); |
| 4182 | btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type, |
| 4183 | &token); |
| 4184 | btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token); |
| 4185 | btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token); |
| 4186 | btrfs_mark_buffer_dirty(leaf); |
| 4187 | |
| 4188 | btrfs_release_path(path); |
| 4189 | |
| 4190 | return ret; |
| 4191 | } |
| 4192 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4193 | /* |
| 4194 | * Log all prealloc extents beyond the inode's i_size to make sure we do not |
| 4195 | * lose them after doing a fast fsync and replaying the log. We scan the |
| 4196 | * subvolume's root instead of iterating the inode's extent map tree because |
| 4197 | * otherwise we can log incorrect extent items based on extent map conversion. |
| 4198 | * That can happen due to the fact that extent maps are merged when they |
| 4199 | * are not in the extent map tree's list of modified extents. |
| 4200 | */ |
| 4201 | static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans, |
| 4202 | struct btrfs_inode *inode, |
| 4203 | struct btrfs_path *path) |
| 4204 | { |
| 4205 | struct btrfs_root *root = inode->root; |
| 4206 | struct btrfs_key key; |
| 4207 | const u64 i_size = i_size_read(&inode->vfs_inode); |
| 4208 | const u64 ino = btrfs_ino(inode); |
| 4209 | struct btrfs_path *dst_path = NULL; |
| 4210 | u64 last_extent = (u64)-1; |
| 4211 | int ins_nr = 0; |
| 4212 | int start_slot; |
| 4213 | int ret; |
| 4214 | |
| 4215 | if (!(inode->flags & BTRFS_INODE_PREALLOC)) |
| 4216 | return 0; |
| 4217 | |
| 4218 | key.objectid = ino; |
| 4219 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4220 | key.offset = i_size; |
| 4221 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4222 | if (ret < 0) |
| 4223 | goto out; |
| 4224 | |
| 4225 | while (true) { |
| 4226 | struct extent_buffer *leaf = path->nodes[0]; |
| 4227 | int slot = path->slots[0]; |
| 4228 | |
| 4229 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4230 | if (ins_nr > 0) { |
| 4231 | ret = copy_items(trans, inode, dst_path, path, |
| 4232 | &last_extent, start_slot, |
| 4233 | ins_nr, 1, 0); |
| 4234 | if (ret < 0) |
| 4235 | goto out; |
| 4236 | ins_nr = 0; |
| 4237 | } |
| 4238 | ret = btrfs_next_leaf(root, path); |
| 4239 | if (ret < 0) |
| 4240 | goto out; |
| 4241 | if (ret > 0) { |
| 4242 | ret = 0; |
| 4243 | break; |
| 4244 | } |
| 4245 | continue; |
| 4246 | } |
| 4247 | |
| 4248 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 4249 | if (key.objectid > ino) |
| 4250 | break; |
| 4251 | if (WARN_ON_ONCE(key.objectid < ino) || |
| 4252 | key.type < BTRFS_EXTENT_DATA_KEY || |
| 4253 | key.offset < i_size) { |
| 4254 | path->slots[0]++; |
| 4255 | continue; |
| 4256 | } |
| 4257 | if (last_extent == (u64)-1) { |
| 4258 | last_extent = key.offset; |
| 4259 | /* |
| 4260 | * Avoid logging extent items logged in past fsync calls |
| 4261 | * and leading to duplicate keys in the log tree. |
| 4262 | */ |
| 4263 | do { |
| 4264 | ret = btrfs_truncate_inode_items(trans, |
| 4265 | root->log_root, |
| 4266 | &inode->vfs_inode, |
| 4267 | i_size, |
| 4268 | BTRFS_EXTENT_DATA_KEY); |
| 4269 | } while (ret == -EAGAIN); |
| 4270 | if (ret) |
| 4271 | goto out; |
| 4272 | } |
| 4273 | if (ins_nr == 0) |
| 4274 | start_slot = slot; |
| 4275 | ins_nr++; |
| 4276 | path->slots[0]++; |
| 4277 | if (!dst_path) { |
| 4278 | dst_path = btrfs_alloc_path(); |
| 4279 | if (!dst_path) { |
| 4280 | ret = -ENOMEM; |
| 4281 | goto out; |
| 4282 | } |
| 4283 | } |
| 4284 | } |
| 4285 | if (ins_nr > 0) { |
| 4286 | ret = copy_items(trans, inode, dst_path, path, &last_extent, |
| 4287 | start_slot, ins_nr, 1, 0); |
| 4288 | if (ret > 0) |
| 4289 | ret = 0; |
| 4290 | } |
| 4291 | out: |
| 4292 | btrfs_release_path(path); |
| 4293 | btrfs_free_path(dst_path); |
| 4294 | return ret; |
| 4295 | } |
| 4296 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4297 | static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, |
| 4298 | struct btrfs_root *root, |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4299 | struct btrfs_inode *inode, |
Miao Xie | 827463c | 2014-01-14 20:31:51 +0800 | [diff] [blame] | 4300 | struct btrfs_path *path, |
Filipe Manana | de0ee0e | 2016-01-21 10:17:54 +0000 | [diff] [blame] | 4301 | struct btrfs_log_ctx *ctx, |
| 4302 | const u64 start, |
| 4303 | const u64 end) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4304 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4305 | struct extent_map *em, *n; |
| 4306 | struct list_head extents; |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4307 | struct extent_map_tree *tree = &inode->extent_tree; |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4308 | u64 logged_start, logged_end; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4309 | u64 test_gen; |
| 4310 | int ret = 0; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4311 | int num = 0; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4312 | |
| 4313 | INIT_LIST_HEAD(&extents); |
| 4314 | |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4315 | down_write(&inode->dio_sem); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4316 | write_lock(&tree->lock); |
| 4317 | test_gen = root->fs_info->last_trans_committed; |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4318 | logged_start = start; |
| 4319 | logged_end = end; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4320 | |
| 4321 | list_for_each_entry_safe(em, n, &tree->modified_extents, list) { |
| 4322 | list_del_init(&em->list); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4323 | /* |
| 4324 | * Just an arbitrary number, this can be really CPU intensive |
| 4325 | * once we start getting a lot of extents, and really once we |
| 4326 | * have a bunch of extents we just want to commit since it will |
| 4327 | * be faster. |
| 4328 | */ |
| 4329 | if (++num > 32768) { |
| 4330 | list_del_init(&tree->modified_extents); |
| 4331 | ret = -EFBIG; |
| 4332 | goto process; |
| 4333 | } |
| 4334 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4335 | if (em->generation <= test_gen) |
| 4336 | continue; |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4337 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4338 | /* We log prealloc extents beyond eof later. */ |
| 4339 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && |
| 4340 | em->start >= i_size_read(&inode->vfs_inode)) |
| 4341 | continue; |
| 4342 | |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4343 | if (em->start < logged_start) |
| 4344 | logged_start = em->start; |
| 4345 | if ((em->start + em->len - 1) > logged_end) |
| 4346 | logged_end = em->start + em->len - 1; |
| 4347 | |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4348 | /* Need a ref to keep it from getting evicted from cache */ |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 4349 | refcount_inc(&em->refs); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4350 | set_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4351 | list_add_tail(&em->list, &extents); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4352 | num++; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | list_sort(NULL, &extents, extent_cmp); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4356 | process: |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4357 | while (!list_empty(&extents)) { |
| 4358 | em = list_entry(extents.next, struct extent_map, list); |
| 4359 | |
| 4360 | list_del_init(&em->list); |
| 4361 | |
| 4362 | /* |
| 4363 | * If we had an error we just need to delete everybody from our |
| 4364 | * private list. |
| 4365 | */ |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4366 | if (ret) { |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 4367 | clear_em_logging(tree, em); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4368 | free_extent_map(em); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4369 | continue; |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | write_unlock(&tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4373 | |
Josef Bacik | a2120a4 | 2018-05-23 11:58:35 -0400 | [diff] [blame] | 4374 | ret = log_one_extent(trans, inode, root, em, path, ctx); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4375 | write_lock(&tree->lock); |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 4376 | clear_em_logging(tree, em); |
| 4377 | free_extent_map(em); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4378 | } |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4379 | WARN_ON(!list_empty(&extents)); |
| 4380 | write_unlock(&tree->lock); |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4381 | up_write(&inode->dio_sem); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4382 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4383 | btrfs_release_path(path); |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4384 | if (!ret) |
| 4385 | ret = btrfs_log_prealloc_extents(trans, inode, path); |
| 4386 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4387 | return ret; |
| 4388 | } |
| 4389 | |
Nikolay Borisov | 481b01c | 2017-01-18 00:31:34 +0200 | [diff] [blame] | 4390 | 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] | 4391 | struct btrfs_path *path, u64 *size_ret) |
| 4392 | { |
| 4393 | struct btrfs_key key; |
| 4394 | int ret; |
| 4395 | |
Nikolay Borisov | 481b01c | 2017-01-18 00:31:34 +0200 | [diff] [blame] | 4396 | key.objectid = btrfs_ino(inode); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4397 | key.type = BTRFS_INODE_ITEM_KEY; |
| 4398 | key.offset = 0; |
| 4399 | |
| 4400 | ret = btrfs_search_slot(NULL, log, &key, path, 0, 0); |
| 4401 | if (ret < 0) { |
| 4402 | return ret; |
| 4403 | } else if (ret > 0) { |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 4404 | *size_ret = 0; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4405 | } else { |
| 4406 | struct btrfs_inode_item *item; |
| 4407 | |
| 4408 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4409 | struct btrfs_inode_item); |
| 4410 | *size_ret = btrfs_inode_size(path->nodes[0], item); |
| 4411 | } |
| 4412 | |
| 4413 | btrfs_release_path(path); |
| 4414 | return 0; |
| 4415 | } |
| 4416 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4417 | /* |
| 4418 | * At the moment we always log all xattrs. This is to figure out at log replay |
| 4419 | * time which xattrs must have their deletion replayed. If a xattr is missing |
| 4420 | * in the log tree and exists in the fs/subvol tree, we delete it. This is |
| 4421 | * because if a xattr is deleted, the inode is fsynced and a power failure |
| 4422 | * happens, causing the log to be replayed the next time the fs is mounted, |
| 4423 | * we want the xattr to not exist anymore (same behaviour as other filesystems |
| 4424 | * with a journal, ext3/4, xfs, f2fs, etc). |
| 4425 | */ |
| 4426 | static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans, |
| 4427 | struct btrfs_root *root, |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4428 | struct btrfs_inode *inode, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4429 | struct btrfs_path *path, |
| 4430 | struct btrfs_path *dst_path) |
| 4431 | { |
| 4432 | int ret; |
| 4433 | struct btrfs_key key; |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4434 | const u64 ino = btrfs_ino(inode); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4435 | int ins_nr = 0; |
| 4436 | int start_slot = 0; |
| 4437 | |
| 4438 | key.objectid = ino; |
| 4439 | key.type = BTRFS_XATTR_ITEM_KEY; |
| 4440 | key.offset = 0; |
| 4441 | |
| 4442 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4443 | if (ret < 0) |
| 4444 | return ret; |
| 4445 | |
| 4446 | while (true) { |
| 4447 | int slot = path->slots[0]; |
| 4448 | struct extent_buffer *leaf = path->nodes[0]; |
| 4449 | int nritems = btrfs_header_nritems(leaf); |
| 4450 | |
| 4451 | if (slot >= nritems) { |
| 4452 | if (ins_nr > 0) { |
| 4453 | u64 last_extent = 0; |
| 4454 | |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4455 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4456 | &last_extent, start_slot, |
| 4457 | ins_nr, 1, 0); |
| 4458 | /* can't be 1, extent items aren't processed */ |
| 4459 | ASSERT(ret <= 0); |
| 4460 | if (ret < 0) |
| 4461 | return ret; |
| 4462 | ins_nr = 0; |
| 4463 | } |
| 4464 | ret = btrfs_next_leaf(root, path); |
| 4465 | if (ret < 0) |
| 4466 | return ret; |
| 4467 | else if (ret > 0) |
| 4468 | break; |
| 4469 | continue; |
| 4470 | } |
| 4471 | |
| 4472 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 4473 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) |
| 4474 | break; |
| 4475 | |
| 4476 | if (ins_nr == 0) |
| 4477 | start_slot = slot; |
| 4478 | ins_nr++; |
| 4479 | path->slots[0]++; |
| 4480 | cond_resched(); |
| 4481 | } |
| 4482 | if (ins_nr > 0) { |
| 4483 | u64 last_extent = 0; |
| 4484 | |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4485 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4486 | &last_extent, start_slot, |
| 4487 | ins_nr, 1, 0); |
| 4488 | /* can't be 1, extent items aren't processed */ |
| 4489 | ASSERT(ret <= 0); |
| 4490 | if (ret < 0) |
| 4491 | return ret; |
| 4492 | } |
| 4493 | |
| 4494 | return 0; |
| 4495 | } |
| 4496 | |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4497 | /* |
| 4498 | * If the no holes feature is enabled we need to make sure any hole between the |
| 4499 | * last extent and the i_size of our inode is explicitly marked in the log. This |
| 4500 | * is to make sure that doing something like: |
| 4501 | * |
| 4502 | * 1) create file with 128Kb of data |
| 4503 | * 2) truncate file to 64Kb |
| 4504 | * 3) truncate file to 256Kb |
| 4505 | * 4) fsync file |
| 4506 | * 5) <crash/power failure> |
| 4507 | * 6) mount fs and trigger log replay |
| 4508 | * |
| 4509 | * Will give us a file with a size of 256Kb, the first 64Kb of data match what |
| 4510 | * the file had in its first 64Kb of data at step 1 and the last 192Kb of the |
| 4511 | * file correspond to a hole. The presence of explicit holes in a log tree is |
| 4512 | * what guarantees that log replay will remove/adjust file extent items in the |
| 4513 | * fs/subvol tree. |
| 4514 | * |
| 4515 | * Here we do not need to care about holes between extents, that is already done |
| 4516 | * by copy_items(). We also only need to do this in the full sync path, where we |
| 4517 | * lookup for extents from the fs/subvol tree only. In the fast path case, we |
| 4518 | * lookup the list of modified extent maps and if any represents a hole, we |
| 4519 | * insert a corresponding extent representing a hole in the log tree. |
| 4520 | */ |
| 4521 | static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans, |
| 4522 | struct btrfs_root *root, |
Nikolay Borisov | a0308dd | 2017-01-18 00:31:38 +0200 | [diff] [blame] | 4523 | struct btrfs_inode *inode, |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4524 | struct btrfs_path *path) |
| 4525 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4526 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4527 | int ret; |
| 4528 | struct btrfs_key key; |
| 4529 | u64 hole_start; |
| 4530 | u64 hole_size; |
| 4531 | struct extent_buffer *leaf; |
| 4532 | struct btrfs_root *log = root->log_root; |
Nikolay Borisov | a0308dd | 2017-01-18 00:31:38 +0200 | [diff] [blame] | 4533 | const u64 ino = btrfs_ino(inode); |
| 4534 | const u64 i_size = i_size_read(&inode->vfs_inode); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4535 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4536 | if (!btrfs_fs_incompat(fs_info, NO_HOLES)) |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4537 | return 0; |
| 4538 | |
| 4539 | key.objectid = ino; |
| 4540 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4541 | key.offset = (u64)-1; |
| 4542 | |
| 4543 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4544 | ASSERT(ret != 0); |
| 4545 | if (ret < 0) |
| 4546 | return ret; |
| 4547 | |
| 4548 | ASSERT(path->slots[0] > 0); |
| 4549 | path->slots[0]--; |
| 4550 | leaf = path->nodes[0]; |
| 4551 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4552 | |
| 4553 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) { |
| 4554 | /* inode does not have any extents */ |
| 4555 | hole_start = 0; |
| 4556 | hole_size = i_size; |
| 4557 | } else { |
| 4558 | struct btrfs_file_extent_item *extent; |
| 4559 | u64 len; |
| 4560 | |
| 4561 | /* |
| 4562 | * If there's an extent beyond i_size, an explicit hole was |
| 4563 | * already inserted by copy_items(). |
| 4564 | */ |
| 4565 | if (key.offset >= i_size) |
| 4566 | return 0; |
| 4567 | |
| 4568 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 4569 | struct btrfs_file_extent_item); |
| 4570 | |
| 4571 | if (btrfs_file_extent_type(leaf, extent) == |
| 4572 | BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 4573 | len = btrfs_file_extent_ram_bytes(leaf, extent); |
Filipe Manana | 6399fb5 | 2017-07-28 15:22:36 +0100 | [diff] [blame] | 4574 | ASSERT(len == i_size || |
| 4575 | (len == fs_info->sectorsize && |
| 4576 | btrfs_file_extent_compression(leaf, extent) != |
| 4577 | BTRFS_COMPRESS_NONE)); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4578 | return 0; |
| 4579 | } |
| 4580 | |
| 4581 | len = btrfs_file_extent_num_bytes(leaf, extent); |
| 4582 | /* Last extent goes beyond i_size, no need to log a hole. */ |
| 4583 | if (key.offset + len > i_size) |
| 4584 | return 0; |
| 4585 | hole_start = key.offset + len; |
| 4586 | hole_size = i_size - hole_start; |
| 4587 | } |
| 4588 | btrfs_release_path(path); |
| 4589 | |
| 4590 | /* Last extent ends at i_size. */ |
| 4591 | if (hole_size == 0) |
| 4592 | return 0; |
| 4593 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4594 | hole_size = ALIGN(hole_size, fs_info->sectorsize); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4595 | ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0, |
| 4596 | hole_size, 0, hole_size, 0, 0, 0); |
| 4597 | return ret; |
| 4598 | } |
| 4599 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4600 | /* |
| 4601 | * When we are logging a new inode X, check if it doesn't have a reference that |
| 4602 | * matches the reference from some other inode Y created in a past transaction |
| 4603 | * and that was renamed in the current transaction. If we don't do this, then at |
| 4604 | * log replay time we can lose inode Y (and all its files if it's a directory): |
| 4605 | * |
| 4606 | * mkdir /mnt/x |
| 4607 | * echo "hello world" > /mnt/x/foobar |
| 4608 | * sync |
| 4609 | * mv /mnt/x /mnt/y |
| 4610 | * mkdir /mnt/x # or touch /mnt/x |
| 4611 | * xfs_io -c fsync /mnt/x |
| 4612 | * <power fail> |
| 4613 | * mount fs, trigger log replay |
| 4614 | * |
| 4615 | * After the log replay procedure, we would lose the first directory and all its |
| 4616 | * files (file foobar). |
| 4617 | * For the case where inode Y is not a directory we simply end up losing it: |
| 4618 | * |
| 4619 | * echo "123" > /mnt/foo |
| 4620 | * sync |
| 4621 | * mv /mnt/foo /mnt/bar |
| 4622 | * echo "abc" > /mnt/foo |
| 4623 | * xfs_io -c fsync /mnt/foo |
| 4624 | * <power fail> |
| 4625 | * |
| 4626 | * We also need this for cases where a snapshot entry is replaced by some other |
| 4627 | * entry (file or directory) otherwise we end up with an unreplayable log due to |
| 4628 | * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as |
| 4629 | * if it were a regular entry: |
| 4630 | * |
| 4631 | * mkdir /mnt/x |
| 4632 | * btrfs subvolume snapshot /mnt /mnt/x/snap |
| 4633 | * btrfs subvolume delete /mnt/x/snap |
| 4634 | * rmdir /mnt/x |
| 4635 | * mkdir /mnt/x |
| 4636 | * fsync /mnt/x or fsync some new file inside it |
| 4637 | * <power fail> |
| 4638 | * |
| 4639 | * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in |
| 4640 | * the same transaction. |
| 4641 | */ |
| 4642 | static int btrfs_check_ref_name_override(struct extent_buffer *eb, |
| 4643 | const int slot, |
| 4644 | const struct btrfs_key *key, |
Nikolay Borisov | 4791c8f | 2017-01-18 00:31:35 +0200 | [diff] [blame] | 4645 | struct btrfs_inode *inode, |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4646 | u64 *other_ino) |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4647 | { |
| 4648 | int ret; |
| 4649 | struct btrfs_path *search_path; |
| 4650 | char *name = NULL; |
| 4651 | u32 name_len = 0; |
| 4652 | u32 item_size = btrfs_item_size_nr(eb, slot); |
| 4653 | u32 cur_offset = 0; |
| 4654 | unsigned long ptr = btrfs_item_ptr_offset(eb, slot); |
| 4655 | |
| 4656 | search_path = btrfs_alloc_path(); |
| 4657 | if (!search_path) |
| 4658 | return -ENOMEM; |
| 4659 | search_path->search_commit_root = 1; |
| 4660 | search_path->skip_locking = 1; |
| 4661 | |
| 4662 | while (cur_offset < item_size) { |
| 4663 | u64 parent; |
| 4664 | u32 this_name_len; |
| 4665 | u32 this_len; |
| 4666 | unsigned long name_ptr; |
| 4667 | struct btrfs_dir_item *di; |
| 4668 | |
| 4669 | if (key->type == BTRFS_INODE_REF_KEY) { |
| 4670 | struct btrfs_inode_ref *iref; |
| 4671 | |
| 4672 | iref = (struct btrfs_inode_ref *)(ptr + cur_offset); |
| 4673 | parent = key->offset; |
| 4674 | this_name_len = btrfs_inode_ref_name_len(eb, iref); |
| 4675 | name_ptr = (unsigned long)(iref + 1); |
| 4676 | this_len = sizeof(*iref) + this_name_len; |
| 4677 | } else { |
| 4678 | struct btrfs_inode_extref *extref; |
| 4679 | |
| 4680 | extref = (struct btrfs_inode_extref *)(ptr + |
| 4681 | cur_offset); |
| 4682 | parent = btrfs_inode_extref_parent(eb, extref); |
| 4683 | this_name_len = btrfs_inode_extref_name_len(eb, extref); |
| 4684 | name_ptr = (unsigned long)&extref->name; |
| 4685 | this_len = sizeof(*extref) + this_name_len; |
| 4686 | } |
| 4687 | |
| 4688 | if (this_name_len > name_len) { |
| 4689 | char *new_name; |
| 4690 | |
| 4691 | new_name = krealloc(name, this_name_len, GFP_NOFS); |
| 4692 | if (!new_name) { |
| 4693 | ret = -ENOMEM; |
| 4694 | goto out; |
| 4695 | } |
| 4696 | name_len = this_name_len; |
| 4697 | name = new_name; |
| 4698 | } |
| 4699 | |
| 4700 | read_extent_buffer(eb, name, name_ptr, this_name_len); |
Nikolay Borisov | 4791c8f | 2017-01-18 00:31:35 +0200 | [diff] [blame] | 4701 | di = btrfs_lookup_dir_item(NULL, inode->root, search_path, |
| 4702 | parent, name, this_name_len, 0); |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4703 | if (di && !IS_ERR(di)) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4704 | struct btrfs_key di_key; |
| 4705 | |
| 4706 | btrfs_dir_item_key_to_cpu(search_path->nodes[0], |
| 4707 | di, &di_key); |
| 4708 | if (di_key.type == BTRFS_INODE_ITEM_KEY) { |
| 4709 | ret = 1; |
| 4710 | *other_ino = di_key.objectid; |
| 4711 | } else { |
| 4712 | ret = -EAGAIN; |
| 4713 | } |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4714 | goto out; |
| 4715 | } else if (IS_ERR(di)) { |
| 4716 | ret = PTR_ERR(di); |
| 4717 | goto out; |
| 4718 | } |
| 4719 | btrfs_release_path(search_path); |
| 4720 | |
| 4721 | cur_offset += this_len; |
| 4722 | } |
| 4723 | ret = 0; |
| 4724 | out: |
| 4725 | btrfs_free_path(search_path); |
| 4726 | kfree(name); |
| 4727 | return ret; |
| 4728 | } |
| 4729 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4730 | /* log a single inode in the tree log. |
| 4731 | * At least one parent directory for this inode must exist in the tree |
| 4732 | * or be logged already. |
| 4733 | * |
| 4734 | * Any items from this inode changed by the current transaction are copied |
| 4735 | * to the log tree. An extra reference is taken on any extents in this |
| 4736 | * file, allowing us to avoid a whole pile of corner cases around logging |
| 4737 | * blocks that have been removed from the tree. |
| 4738 | * |
| 4739 | * See LOG_INODE_ALL and related defines for a description of what inode_only |
| 4740 | * does. |
| 4741 | * |
| 4742 | * This handles both files and directories. |
| 4743 | */ |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4744 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4745 | struct btrfs_root *root, struct btrfs_inode *inode, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 4746 | int inode_only, |
| 4747 | const loff_t start, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4748 | const loff_t end, |
| 4749 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4750 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4751 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4752 | struct btrfs_path *path; |
| 4753 | struct btrfs_path *dst_path; |
| 4754 | struct btrfs_key min_key; |
| 4755 | struct btrfs_key max_key; |
| 4756 | struct btrfs_root *log = root->log_root; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4757 | u64 last_extent = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4758 | int err = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4759 | int ret; |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 4760 | int nritems; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4761 | int ins_start_slot = 0; |
| 4762 | int ins_nr; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4763 | bool fast_search = false; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4764 | u64 ino = btrfs_ino(inode); |
| 4765 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4766 | u64 logged_isize = 0; |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 4767 | bool need_log_inode_item = true; |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 4768 | bool xattrs_logged = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4769 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4770 | path = btrfs_alloc_path(); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 4771 | if (!path) |
| 4772 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4773 | dst_path = btrfs_alloc_path(); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 4774 | if (!dst_path) { |
| 4775 | btrfs_free_path(path); |
| 4776 | return -ENOMEM; |
| 4777 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4778 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4779 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4780 | min_key.type = BTRFS_INODE_ITEM_KEY; |
| 4781 | min_key.offset = 0; |
| 4782 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4783 | max_key.objectid = ino; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4784 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4785 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4786 | /* today the code can only do partial logging of directories */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4787 | if (S_ISDIR(inode->vfs_inode.i_mode) || |
Miao Xie | 5269b67 | 2012-11-01 07:35:23 +0000 | [diff] [blame] | 4788 | (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4789 | &inode->runtime_flags) && |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4790 | inode_only >= LOG_INODE_EXISTS)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4791 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
| 4792 | else |
| 4793 | max_key.type = (u8)-1; |
| 4794 | max_key.offset = (u64)-1; |
| 4795 | |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4796 | /* |
| 4797 | * Only run delayed items if we are a dir or a new file. |
| 4798 | * Otherwise commit the delayed inode only, which is needed in |
| 4799 | * order for the log replay code to mark inodes for link count |
| 4800 | * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items). |
| 4801 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4802 | if (S_ISDIR(inode->vfs_inode.i_mode) || |
| 4803 | inode->generation > fs_info->last_trans_committed) |
| 4804 | ret = btrfs_commit_inode_delayed_items(trans, inode); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4805 | else |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4806 | ret = btrfs_commit_inode_delayed_inode(inode); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4807 | |
| 4808 | if (ret) { |
| 4809 | btrfs_free_path(path); |
| 4810 | btrfs_free_path(dst_path); |
| 4811 | return ret; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4812 | } |
| 4813 | |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4814 | if (inode_only == LOG_OTHER_INODE) { |
| 4815 | inode_only = LOG_INODE_EXISTS; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4816 | mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING); |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4817 | } else { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4818 | mutex_lock(&inode->log_mutex); |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4819 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4820 | |
Filipe Manana | 5e33a2b | 2016-02-25 23:19:38 +0000 | [diff] [blame] | 4821 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4822 | * a brute force approach to making sure we get the most uptodate |
| 4823 | * copies of everything. |
| 4824 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4825 | if (S_ISDIR(inode->vfs_inode.i_mode)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4826 | int max_key_type = BTRFS_DIR_LOG_INDEX_KEY; |
| 4827 | |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4828 | if (inode_only == LOG_INODE_EXISTS) |
| 4829 | max_key_type = BTRFS_XATTR_ITEM_KEY; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4830 | ret = drop_objectid_items(trans, log, path, ino, max_key_type); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4831 | } else { |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4832 | if (inode_only == LOG_INODE_EXISTS) { |
| 4833 | /* |
| 4834 | * Make sure the new inode item we write to the log has |
| 4835 | * the same isize as the current one (if it exists). |
| 4836 | * This is necessary to prevent data loss after log |
| 4837 | * replay, and also to prevent doing a wrong expanding |
| 4838 | * truncate - for e.g. create file, write 4K into offset |
| 4839 | * 0, fsync, write 4K into offset 4096, add hard link, |
| 4840 | * fsync some other file (to sync log), power fail - if |
| 4841 | * we use the inode's current i_size, after log replay |
| 4842 | * we get a 8Kb file, with the last 4Kb extent as a hole |
| 4843 | * (zeroes), as if an expanding truncate happened, |
| 4844 | * instead of getting a file of 4Kb only. |
| 4845 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4846 | err = logged_inode_size(log, inode, path, &logged_isize); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4847 | if (err) |
| 4848 | goto out_unlock; |
| 4849 | } |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4850 | if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4851 | &inode->runtime_flags)) { |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4852 | if (inode_only == LOG_INODE_EXISTS) { |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4853 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4854 | ret = drop_objectid_items(trans, log, path, ino, |
| 4855 | max_key.type); |
| 4856 | } else { |
| 4857 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4858 | &inode->runtime_flags); |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4859 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4860 | &inode->runtime_flags); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4861 | while(1) { |
| 4862 | ret = btrfs_truncate_inode_items(trans, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4863 | log, &inode->vfs_inode, 0, 0); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4864 | if (ret != -EAGAIN) |
| 4865 | break; |
| 4866 | } |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4867 | } |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4868 | } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4869 | &inode->runtime_flags) || |
Josef Bacik | 6cfab85 | 2013-11-12 16:25:58 -0500 | [diff] [blame] | 4870 | inode_only == LOG_INODE_EXISTS) { |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4871 | if (inode_only == LOG_INODE_ALL) |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4872 | fast_search = true; |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4873 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4874 | ret = drop_objectid_items(trans, log, path, ino, |
| 4875 | max_key.type); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4876 | } else { |
Liu Bo | 183f37f | 2012-11-01 06:38:47 +0000 | [diff] [blame] | 4877 | if (inode_only == LOG_INODE_ALL) |
| 4878 | fast_search = true; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4879 | goto log_extents; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4880 | } |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4881 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4882 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4883 | if (ret) { |
| 4884 | err = ret; |
| 4885 | goto out_unlock; |
| 4886 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4887 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4888 | while (1) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4889 | ins_nr = 0; |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 4890 | ret = btrfs_search_forward(root, &min_key, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4891 | path, trans->transid); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 4892 | if (ret < 0) { |
| 4893 | err = ret; |
| 4894 | goto out_unlock; |
| 4895 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4896 | if (ret != 0) |
| 4897 | break; |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 4898 | again: |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4899 | /* note, ins_nr might be > 0 here, cleanup outside the loop */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4900 | if (min_key.objectid != ino) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4901 | break; |
| 4902 | if (min_key.type > max_key.type) |
| 4903 | break; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4904 | |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 4905 | if (min_key.type == BTRFS_INODE_ITEM_KEY) |
| 4906 | need_log_inode_item = false; |
| 4907 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4908 | if ((min_key.type == BTRFS_INODE_REF_KEY || |
| 4909 | min_key.type == BTRFS_INODE_EXTREF_KEY) && |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4910 | inode->generation == trans->transid) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4911 | u64 other_ino = 0; |
| 4912 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4913 | ret = btrfs_check_ref_name_override(path->nodes[0], |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4914 | path->slots[0], &min_key, inode, |
| 4915 | &other_ino); |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4916 | if (ret < 0) { |
| 4917 | err = ret; |
| 4918 | goto out_unlock; |
Filipe Manana | 28a2359 | 2016-08-23 21:13:51 +0100 | [diff] [blame] | 4919 | } else if (ret > 0 && ctx && |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4920 | other_ino != btrfs_ino(BTRFS_I(ctx->inode))) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4921 | struct btrfs_key inode_key; |
| 4922 | struct inode *other_inode; |
| 4923 | |
| 4924 | if (ins_nr > 0) { |
| 4925 | ins_nr++; |
| 4926 | } else { |
| 4927 | ins_nr = 1; |
| 4928 | ins_start_slot = path->slots[0]; |
| 4929 | } |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4930 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4931 | &last_extent, ins_start_slot, |
| 4932 | ins_nr, inode_only, |
| 4933 | logged_isize); |
| 4934 | if (ret < 0) { |
| 4935 | err = ret; |
| 4936 | goto out_unlock; |
| 4937 | } |
| 4938 | ins_nr = 0; |
| 4939 | btrfs_release_path(path); |
| 4940 | inode_key.objectid = other_ino; |
| 4941 | inode_key.type = BTRFS_INODE_ITEM_KEY; |
| 4942 | inode_key.offset = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4943 | other_inode = btrfs_iget(fs_info->sb, |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4944 | &inode_key, root, |
| 4945 | NULL); |
| 4946 | /* |
| 4947 | * If the other inode that had a conflicting dir |
| 4948 | * entry was deleted in the current transaction, |
| 4949 | * we don't need to do more work nor fallback to |
| 4950 | * a transaction commit. |
| 4951 | */ |
| 4952 | if (IS_ERR(other_inode) && |
| 4953 | PTR_ERR(other_inode) == -ENOENT) { |
| 4954 | goto next_key; |
| 4955 | } else if (IS_ERR(other_inode)) { |
| 4956 | err = PTR_ERR(other_inode); |
| 4957 | goto out_unlock; |
| 4958 | } |
| 4959 | /* |
| 4960 | * We are safe logging the other inode without |
| 4961 | * acquiring its i_mutex as long as we log with |
| 4962 | * the LOG_INODE_EXISTS mode. We're safe against |
| 4963 | * concurrent renames of the other inode as well |
| 4964 | * because during a rename we pin the log and |
| 4965 | * update the log with the new name before we |
| 4966 | * unpin it. |
| 4967 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4968 | err = btrfs_log_inode(trans, root, |
| 4969 | BTRFS_I(other_inode), |
| 4970 | LOG_OTHER_INODE, 0, LLONG_MAX, |
| 4971 | ctx); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4972 | iput(other_inode); |
| 4973 | if (err) |
| 4974 | goto out_unlock; |
| 4975 | else |
| 4976 | goto next_key; |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4977 | } |
| 4978 | } |
| 4979 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4980 | /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */ |
| 4981 | if (min_key.type == BTRFS_XATTR_ITEM_KEY) { |
| 4982 | if (ins_nr == 0) |
| 4983 | goto next_slot; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4984 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4985 | &last_extent, ins_start_slot, |
| 4986 | ins_nr, inode_only, logged_isize); |
| 4987 | if (ret < 0) { |
| 4988 | err = ret; |
| 4989 | goto out_unlock; |
| 4990 | } |
| 4991 | ins_nr = 0; |
| 4992 | if (ret) { |
| 4993 | btrfs_release_path(path); |
| 4994 | continue; |
| 4995 | } |
| 4996 | goto next_slot; |
| 4997 | } |
| 4998 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4999 | if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { |
| 5000 | ins_nr++; |
| 5001 | goto next_slot; |
| 5002 | } else if (!ins_nr) { |
| 5003 | ins_start_slot = path->slots[0]; |
| 5004 | ins_nr = 1; |
| 5005 | goto next_slot; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5006 | } |
| 5007 | |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5008 | ret = copy_items(trans, inode, dst_path, path, &last_extent, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5009 | ins_start_slot, ins_nr, inode_only, |
| 5010 | logged_isize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5011 | if (ret < 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5012 | err = ret; |
| 5013 | goto out_unlock; |
Rasmus Villemoes | a71db86 | 2014-06-20 21:51:43 +0200 | [diff] [blame] | 5014 | } |
| 5015 | if (ret) { |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5016 | ins_nr = 0; |
| 5017 | btrfs_release_path(path); |
| 5018 | continue; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5019 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5020 | ins_nr = 1; |
| 5021 | ins_start_slot = path->slots[0]; |
| 5022 | next_slot: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5023 | |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 5024 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 5025 | path->slots[0]++; |
| 5026 | if (path->slots[0] < nritems) { |
| 5027 | btrfs_item_key_to_cpu(path->nodes[0], &min_key, |
| 5028 | path->slots[0]); |
| 5029 | goto again; |
| 5030 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5031 | if (ins_nr) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5032 | ret = copy_items(trans, inode, dst_path, path, |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5033 | &last_extent, ins_start_slot, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5034 | ins_nr, inode_only, logged_isize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5035 | if (ret < 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5036 | err = ret; |
| 5037 | goto out_unlock; |
| 5038 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5039 | ret = 0; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5040 | ins_nr = 0; |
| 5041 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5042 | btrfs_release_path(path); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5043 | next_key: |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5044 | if (min_key.offset < (u64)-1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5045 | min_key.offset++; |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5046 | } else if (min_key.type < max_key.type) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5047 | min_key.type++; |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5048 | min_key.offset = 0; |
| 5049 | } else { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5050 | break; |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5051 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5052 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5053 | if (ins_nr) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5054 | ret = copy_items(trans, inode, dst_path, path, &last_extent, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5055 | ins_start_slot, ins_nr, inode_only, |
| 5056 | logged_isize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5057 | if (ret < 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5058 | err = ret; |
| 5059 | goto out_unlock; |
| 5060 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5061 | ret = 0; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5062 | ins_nr = 0; |
| 5063 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5064 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5065 | btrfs_release_path(path); |
| 5066 | btrfs_release_path(dst_path); |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5067 | err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5068 | if (err) |
| 5069 | goto out_unlock; |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 5070 | xattrs_logged = true; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5071 | if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) { |
| 5072 | btrfs_release_path(path); |
| 5073 | btrfs_release_path(dst_path); |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5074 | err = btrfs_log_trailing_hole(trans, root, inode, path); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5075 | if (err) |
| 5076 | goto out_unlock; |
| 5077 | } |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 5078 | log_extents: |
Josef Bacik | f3b15cc | 2013-07-22 12:54:30 -0400 | [diff] [blame] | 5079 | btrfs_release_path(path); |
| 5080 | btrfs_release_path(dst_path); |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 5081 | if (need_log_inode_item) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5082 | err = log_inode_item(trans, log, dst_path, inode); |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 5083 | if (!err && !xattrs_logged) { |
| 5084 | err = btrfs_log_all_xattrs(trans, root, inode, path, |
| 5085 | dst_path); |
| 5086 | btrfs_release_path(path); |
| 5087 | } |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 5088 | if (err) |
| 5089 | goto out_unlock; |
| 5090 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5091 | if (fast_search) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5092 | ret = btrfs_log_changed_extents(trans, root, inode, dst_path, |
Josef Bacik | a2120a4 | 2018-05-23 11:58:35 -0400 | [diff] [blame] | 5093 | ctx, start, end); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5094 | if (ret) { |
| 5095 | err = ret; |
| 5096 | goto out_unlock; |
| 5097 | } |
Josef Bacik | d006a04 | 2013-11-12 20:54:09 -0500 | [diff] [blame] | 5098 | } else if (inode_only == LOG_INODE_ALL) { |
Liu Bo | 06d3d22 | 2012-08-27 10:52:19 -0600 | [diff] [blame] | 5099 | struct extent_map *em, *n; |
| 5100 | |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5101 | write_lock(&em_tree->lock); |
| 5102 | /* |
| 5103 | * We can't just remove every em if we're called for a ranged |
| 5104 | * fsync - that is, one that doesn't cover the whole possible |
| 5105 | * file range (0 to LLONG_MAX). This is because we can have |
| 5106 | * em's that fall outside the range we're logging and therefore |
| 5107 | * their ordered operations haven't completed yet |
| 5108 | * (btrfs_finish_ordered_io() not invoked yet). This means we |
| 5109 | * didn't get their respective file extent item in the fs/subvol |
| 5110 | * tree yet, and need to let the next fast fsync (one which |
| 5111 | * consults the list of modified extent maps) find the em so |
| 5112 | * that it logs a matching file extent item and waits for the |
| 5113 | * respective ordered operation to complete (if it's still |
| 5114 | * running). |
| 5115 | * |
| 5116 | * Removing every em outside the range we're logging would make |
| 5117 | * the next fast fsync not log their matching file extent items, |
| 5118 | * therefore making us lose data after a log replay. |
| 5119 | */ |
| 5120 | list_for_each_entry_safe(em, n, &em_tree->modified_extents, |
| 5121 | list) { |
| 5122 | const u64 mod_end = em->mod_start + em->mod_len - 1; |
| 5123 | |
| 5124 | if (em->mod_start >= start && mod_end <= end) |
| 5125 | list_del_init(&em->list); |
| 5126 | } |
| 5127 | write_unlock(&em_tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5128 | } |
| 5129 | |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5130 | if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) { |
| 5131 | ret = log_directory_changes(trans, root, inode, path, dst_path, |
| 5132 | ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5133 | if (ret) { |
| 5134 | err = ret; |
| 5135 | goto out_unlock; |
| 5136 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5137 | } |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5138 | |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5139 | spin_lock(&inode->lock); |
| 5140 | inode->logged_trans = trans->transid; |
| 5141 | inode->last_log_commit = inode->last_sub_trans; |
| 5142 | spin_unlock(&inode->lock); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5143 | out_unlock: |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5144 | mutex_unlock(&inode->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5145 | |
| 5146 | btrfs_free_path(path); |
| 5147 | btrfs_free_path(dst_path); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5148 | return err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5149 | } |
| 5150 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5151 | /* |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5152 | * Check if we must fallback to a transaction commit when logging an inode. |
| 5153 | * This must be called after logging the inode and is used only in the context |
| 5154 | * when fsyncing an inode requires the need to log some other inode - in which |
| 5155 | * case we can't lock the i_mutex of each other inode we need to log as that |
| 5156 | * can lead to deadlocks with concurrent fsync against other inodes (as we can |
| 5157 | * log inodes up or down in the hierarchy) or rename operations for example. So |
| 5158 | * we take the log_mutex of the inode after we have logged it and then check for |
| 5159 | * its last_unlink_trans value - this is safe because any task setting |
| 5160 | * last_unlink_trans must take the log_mutex and it must do this before it does |
| 5161 | * the actual unlink operation, so if we do this check before a concurrent task |
| 5162 | * sets last_unlink_trans it means we've logged a consistent version/state of |
| 5163 | * 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] | 5164 | * commit (the concurrent task might have only updated last_unlink_trans before |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5165 | * we logged the inode or it might have also done the unlink). |
| 5166 | */ |
| 5167 | static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans, |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5168 | struct btrfs_inode *inode) |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5169 | { |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5170 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5171 | bool ret = false; |
| 5172 | |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5173 | mutex_lock(&inode->log_mutex); |
| 5174 | if (inode->last_unlink_trans > fs_info->last_trans_committed) { |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5175 | /* |
| 5176 | * Make sure any commits to the log are forced to be full |
| 5177 | * commits. |
| 5178 | */ |
| 5179 | btrfs_set_log_full_commit(fs_info, trans); |
| 5180 | ret = true; |
| 5181 | } |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5182 | mutex_unlock(&inode->log_mutex); |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5183 | |
| 5184 | return ret; |
| 5185 | } |
| 5186 | |
| 5187 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5188 | * follow the dentry parent pointers up the chain and see if any |
| 5189 | * of the directories in it require a full commit before they can |
| 5190 | * be logged. Returns zero if nothing special needs to be done or 1 if |
| 5191 | * a full commit is required. |
| 5192 | */ |
| 5193 | 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] | 5194 | struct btrfs_inode *inode, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5195 | struct dentry *parent, |
| 5196 | struct super_block *sb, |
| 5197 | u64 last_committed) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5198 | { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5199 | int ret = 0; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5200 | struct dentry *old_parent = NULL; |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5201 | struct btrfs_inode *orig_inode = inode; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5202 | |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5203 | /* |
| 5204 | * for regular files, if its inode is already on disk, we don't |
| 5205 | * have to worry about the parents at all. This is because |
| 5206 | * we can use the last_unlink_trans field to record renames |
| 5207 | * and other fun in this file. |
| 5208 | */ |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5209 | if (S_ISREG(inode->vfs_inode.i_mode) && |
| 5210 | inode->generation <= last_committed && |
| 5211 | inode->last_unlink_trans <= last_committed) |
| 5212 | goto out; |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5213 | |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5214 | if (!S_ISDIR(inode->vfs_inode.i_mode)) { |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5215 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5216 | goto out; |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5217 | inode = BTRFS_I(d_inode(parent)); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | while (1) { |
Josef Bacik | de2b530 | 2013-09-11 09:36:30 -0400 | [diff] [blame] | 5221 | /* |
| 5222 | * If we are logging a directory then we start with our inode, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5223 | * not our parent's inode, so we need to skip setting the |
Josef Bacik | de2b530 | 2013-09-11 09:36:30 -0400 | [diff] [blame] | 5224 | * logged_trans so that further down in the log code we don't |
| 5225 | * think this inode has already been logged. |
| 5226 | */ |
| 5227 | if (inode != orig_inode) |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5228 | inode->logged_trans = trans->transid; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5229 | smp_mb(); |
| 5230 | |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5231 | if (btrfs_must_commit_transaction(trans, inode)) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5232 | ret = 1; |
| 5233 | break; |
| 5234 | } |
| 5235 | |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5236 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5237 | break; |
| 5238 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5239 | if (IS_ROOT(parent)) { |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5240 | inode = BTRFS_I(d_inode(parent)); |
| 5241 | if (btrfs_must_commit_transaction(trans, inode)) |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5242 | ret = 1; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5243 | break; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5244 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5245 | |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5246 | parent = dget_parent(parent); |
| 5247 | dput(old_parent); |
| 5248 | old_parent = parent; |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5249 | inode = BTRFS_I(d_inode(parent)); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5250 | |
| 5251 | } |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5252 | dput(old_parent); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5253 | out: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5254 | return ret; |
| 5255 | } |
| 5256 | |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5257 | struct btrfs_dir_list { |
| 5258 | u64 ino; |
| 5259 | struct list_head list; |
| 5260 | }; |
| 5261 | |
| 5262 | /* |
| 5263 | * Log the inodes of the new dentries of a directory. See log_dir_items() for |
| 5264 | * details about the why it is needed. |
| 5265 | * This is a recursive operation - if an existing dentry corresponds to a |
| 5266 | * directory, that directory's new entries are logged too (same behaviour as |
| 5267 | * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes |
| 5268 | * the dentries point to we do not lock their i_mutex, otherwise lockdep |
| 5269 | * complains about the following circular lock dependency / possible deadlock: |
| 5270 | * |
| 5271 | * CPU0 CPU1 |
| 5272 | * ---- ---- |
| 5273 | * lock(&type->i_mutex_dir_key#3/2); |
| 5274 | * lock(sb_internal#2); |
| 5275 | * lock(&type->i_mutex_dir_key#3/2); |
| 5276 | * lock(&sb->s_type->i_mutex_key#14); |
| 5277 | * |
| 5278 | * Where sb_internal is the lock (a counter that works as a lock) acquired by |
| 5279 | * sb_start_intwrite() in btrfs_start_transaction(). |
| 5280 | * Not locking i_mutex of the inodes is still safe because: |
| 5281 | * |
| 5282 | * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible |
| 5283 | * that while logging the inode new references (names) are added or removed |
| 5284 | * from the inode, leaving the logged inode item with a link count that does |
| 5285 | * not match the number of logged inode reference items. This is fine because |
| 5286 | * at log replay time we compute the real number of links and correct the |
| 5287 | * link count in the inode item (see replay_one_buffer() and |
| 5288 | * link_to_fixup_dir()); |
| 5289 | * |
| 5290 | * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that |
| 5291 | * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and |
| 5292 | * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item |
| 5293 | * has a size that doesn't match the sum of the lengths of all the logged |
| 5294 | * names. This does not result in a problem because if a dir_item key is |
| 5295 | * logged but its matching dir_index key is not logged, at log replay time we |
| 5296 | * don't use it to replay the respective name (see replay_one_name()). On the |
| 5297 | * other hand if only the dir_index key ends up being logged, the respective |
| 5298 | * name is added to the fs/subvol tree with both the dir_item and dir_index |
| 5299 | * keys created (see replay_one_name()). |
| 5300 | * The directory's inode item with a wrong i_size is not a problem as well, |
| 5301 | * since we don't use it at log replay time to set the i_size in the inode |
| 5302 | * item of the fs/subvol tree (see overwrite_item()). |
| 5303 | */ |
| 5304 | static int log_new_dir_dentries(struct btrfs_trans_handle *trans, |
| 5305 | struct btrfs_root *root, |
Nikolay Borisov | 51cc0d3 | 2017-01-18 00:31:43 +0200 | [diff] [blame] | 5306 | struct btrfs_inode *start_inode, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5307 | struct btrfs_log_ctx *ctx) |
| 5308 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5309 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5310 | struct btrfs_root *log = root->log_root; |
| 5311 | struct btrfs_path *path; |
| 5312 | LIST_HEAD(dir_list); |
| 5313 | struct btrfs_dir_list *dir_elem; |
| 5314 | int ret = 0; |
| 5315 | |
| 5316 | path = btrfs_alloc_path(); |
| 5317 | if (!path) |
| 5318 | return -ENOMEM; |
| 5319 | |
| 5320 | dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS); |
| 5321 | if (!dir_elem) { |
| 5322 | btrfs_free_path(path); |
| 5323 | return -ENOMEM; |
| 5324 | } |
Nikolay Borisov | 51cc0d3 | 2017-01-18 00:31:43 +0200 | [diff] [blame] | 5325 | dir_elem->ino = btrfs_ino(start_inode); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5326 | list_add_tail(&dir_elem->list, &dir_list); |
| 5327 | |
| 5328 | while (!list_empty(&dir_list)) { |
| 5329 | struct extent_buffer *leaf; |
| 5330 | struct btrfs_key min_key; |
| 5331 | int nritems; |
| 5332 | int i; |
| 5333 | |
| 5334 | dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, |
| 5335 | list); |
| 5336 | if (ret) |
| 5337 | goto next_dir_inode; |
| 5338 | |
| 5339 | min_key.objectid = dir_elem->ino; |
| 5340 | min_key.type = BTRFS_DIR_ITEM_KEY; |
| 5341 | min_key.offset = 0; |
| 5342 | again: |
| 5343 | btrfs_release_path(path); |
| 5344 | ret = btrfs_search_forward(log, &min_key, path, trans->transid); |
| 5345 | if (ret < 0) { |
| 5346 | goto next_dir_inode; |
| 5347 | } else if (ret > 0) { |
| 5348 | ret = 0; |
| 5349 | goto next_dir_inode; |
| 5350 | } |
| 5351 | |
| 5352 | process_leaf: |
| 5353 | leaf = path->nodes[0]; |
| 5354 | nritems = btrfs_header_nritems(leaf); |
| 5355 | for (i = path->slots[0]; i < nritems; i++) { |
| 5356 | struct btrfs_dir_item *di; |
| 5357 | struct btrfs_key di_key; |
| 5358 | struct inode *di_inode; |
| 5359 | struct btrfs_dir_list *new_dir_elem; |
| 5360 | int log_mode = LOG_INODE_EXISTS; |
| 5361 | int type; |
| 5362 | |
| 5363 | btrfs_item_key_to_cpu(leaf, &min_key, i); |
| 5364 | if (min_key.objectid != dir_elem->ino || |
| 5365 | min_key.type != BTRFS_DIR_ITEM_KEY) |
| 5366 | goto next_dir_inode; |
| 5367 | |
| 5368 | di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item); |
| 5369 | type = btrfs_dir_type(leaf, di); |
| 5370 | if (btrfs_dir_transid(leaf, di) < trans->transid && |
| 5371 | type != BTRFS_FT_DIR) |
| 5372 | continue; |
| 5373 | btrfs_dir_item_key_to_cpu(leaf, di, &di_key); |
| 5374 | if (di_key.type == BTRFS_ROOT_ITEM_KEY) |
| 5375 | continue; |
| 5376 | |
Robbie Ko | ec125cf | 2016-10-28 10:48:26 +0800 | [diff] [blame] | 5377 | btrfs_release_path(path); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5378 | di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5379 | if (IS_ERR(di_inode)) { |
| 5380 | ret = PTR_ERR(di_inode); |
| 5381 | goto next_dir_inode; |
| 5382 | } |
| 5383 | |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 5384 | if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) { |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5385 | iput(di_inode); |
Robbie Ko | ec125cf | 2016-10-28 10:48:26 +0800 | [diff] [blame] | 5386 | break; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
| 5389 | ctx->log_new_dentries = false; |
Filipe Manana | 3f9749f | 2016-04-25 04:45:02 +0100 | [diff] [blame] | 5390 | if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK) |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5391 | log_mode = LOG_INODE_ALL; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5392 | ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode), |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5393 | log_mode, 0, LLONG_MAX, ctx); |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5394 | if (!ret && |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5395 | btrfs_must_commit_transaction(trans, BTRFS_I(di_inode))) |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5396 | ret = 1; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5397 | iput(di_inode); |
| 5398 | if (ret) |
| 5399 | goto next_dir_inode; |
| 5400 | if (ctx->log_new_dentries) { |
| 5401 | new_dir_elem = kmalloc(sizeof(*new_dir_elem), |
| 5402 | GFP_NOFS); |
| 5403 | if (!new_dir_elem) { |
| 5404 | ret = -ENOMEM; |
| 5405 | goto next_dir_inode; |
| 5406 | } |
| 5407 | new_dir_elem->ino = di_key.objectid; |
| 5408 | list_add_tail(&new_dir_elem->list, &dir_list); |
| 5409 | } |
| 5410 | break; |
| 5411 | } |
| 5412 | if (i == nritems) { |
| 5413 | ret = btrfs_next_leaf(log, path); |
| 5414 | if (ret < 0) { |
| 5415 | goto next_dir_inode; |
| 5416 | } else if (ret > 0) { |
| 5417 | ret = 0; |
| 5418 | goto next_dir_inode; |
| 5419 | } |
| 5420 | goto process_leaf; |
| 5421 | } |
| 5422 | if (min_key.offset < (u64)-1) { |
| 5423 | min_key.offset++; |
| 5424 | goto again; |
| 5425 | } |
| 5426 | next_dir_inode: |
| 5427 | list_del(&dir_elem->list); |
| 5428 | kfree(dir_elem); |
| 5429 | } |
| 5430 | |
| 5431 | btrfs_free_path(path); |
| 5432 | return ret; |
| 5433 | } |
| 5434 | |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5435 | static int btrfs_log_all_parents(struct btrfs_trans_handle *trans, |
Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 5436 | struct btrfs_inode *inode, |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5437 | struct btrfs_log_ctx *ctx) |
| 5438 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame^] | 5439 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5440 | int ret; |
| 5441 | struct btrfs_path *path; |
| 5442 | struct btrfs_key key; |
Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 5443 | struct btrfs_root *root = inode->root; |
| 5444 | const u64 ino = btrfs_ino(inode); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5445 | |
| 5446 | path = btrfs_alloc_path(); |
| 5447 | if (!path) |
| 5448 | return -ENOMEM; |
| 5449 | path->skip_locking = 1; |
| 5450 | path->search_commit_root = 1; |
| 5451 | |
| 5452 | key.objectid = ino; |
| 5453 | key.type = BTRFS_INODE_REF_KEY; |
| 5454 | key.offset = 0; |
| 5455 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5456 | if (ret < 0) |
| 5457 | goto out; |
| 5458 | |
| 5459 | while (true) { |
| 5460 | struct extent_buffer *leaf = path->nodes[0]; |
| 5461 | int slot = path->slots[0]; |
| 5462 | u32 cur_offset = 0; |
| 5463 | u32 item_size; |
| 5464 | unsigned long ptr; |
| 5465 | |
| 5466 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5467 | ret = btrfs_next_leaf(root, path); |
| 5468 | if (ret < 0) |
| 5469 | goto out; |
| 5470 | else if (ret > 0) |
| 5471 | break; |
| 5472 | continue; |
| 5473 | } |
| 5474 | |
| 5475 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5476 | /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */ |
| 5477 | if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY) |
| 5478 | break; |
| 5479 | |
| 5480 | item_size = btrfs_item_size_nr(leaf, slot); |
| 5481 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 5482 | while (cur_offset < item_size) { |
| 5483 | struct btrfs_key inode_key; |
| 5484 | struct inode *dir_inode; |
| 5485 | |
| 5486 | inode_key.type = BTRFS_INODE_ITEM_KEY; |
| 5487 | inode_key.offset = 0; |
| 5488 | |
| 5489 | if (key.type == BTRFS_INODE_EXTREF_KEY) { |
| 5490 | struct btrfs_inode_extref *extref; |
| 5491 | |
| 5492 | extref = (struct btrfs_inode_extref *) |
| 5493 | (ptr + cur_offset); |
| 5494 | inode_key.objectid = btrfs_inode_extref_parent( |
| 5495 | leaf, extref); |
| 5496 | cur_offset += sizeof(*extref); |
| 5497 | cur_offset += btrfs_inode_extref_name_len(leaf, |
| 5498 | extref); |
| 5499 | } else { |
| 5500 | inode_key.objectid = key.offset; |
| 5501 | cur_offset = item_size; |
| 5502 | } |
| 5503 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5504 | dir_inode = btrfs_iget(fs_info->sb, &inode_key, |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5505 | root, NULL); |
| 5506 | /* If parent inode was deleted, skip it. */ |
| 5507 | if (IS_ERR(dir_inode)) |
| 5508 | continue; |
| 5509 | |
Filipe Manana | 657ed1a | 2016-04-06 17:11:56 +0100 | [diff] [blame] | 5510 | if (ctx) |
| 5511 | ctx->log_new_dentries = false; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5512 | ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode), |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5513 | LOG_INODE_ALL, 0, LLONG_MAX, ctx); |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5514 | if (!ret && |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5515 | btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode))) |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5516 | ret = 1; |
Filipe Manana | 657ed1a | 2016-04-06 17:11:56 +0100 | [diff] [blame] | 5517 | if (!ret && ctx && ctx->log_new_dentries) |
| 5518 | ret = log_new_dir_dentries(trans, root, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5519 | BTRFS_I(dir_inode), ctx); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5520 | iput(dir_inode); |
| 5521 | if (ret) |
| 5522 | goto out; |
| 5523 | } |
| 5524 | path->slots[0]++; |
| 5525 | } |
| 5526 | ret = 0; |
| 5527 | out: |
| 5528 | btrfs_free_path(path); |
| 5529 | return ret; |
| 5530 | } |
| 5531 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5532 | /* |
| 5533 | * helper function around btrfs_log_inode to make sure newly created |
| 5534 | * parent directories also end up in the log. A minimal inode and backref |
| 5535 | * only logging is done of any parent directories that are older than |
| 5536 | * the last committed transaction |
| 5537 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5538 | static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5539 | struct btrfs_inode *inode, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5540 | struct dentry *parent, |
| 5541 | const loff_t start, |
| 5542 | const loff_t end, |
Edmund Nadolski | 41a1ead | 2017-11-20 13:24:47 -0700 | [diff] [blame] | 5543 | int inode_only, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5544 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5545 | { |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5546 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5547 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5548 | struct super_block *sb; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5549 | struct dentry *old_parent = NULL; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5550 | int ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5551 | u64 last_committed = fs_info->last_trans_committed; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5552 | bool log_dentries = false; |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5553 | struct btrfs_inode *orig_inode = inode; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5554 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5555 | sb = inode->vfs_inode.i_sb; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5556 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5557 | if (btrfs_test_opt(fs_info, NOTREELOG)) { |
Sage Weil | 3a5e140 | 2009-04-02 16:49:40 -0400 | [diff] [blame] | 5558 | ret = 1; |
| 5559 | goto end_no_trans; |
| 5560 | } |
| 5561 | |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 5562 | /* |
| 5563 | * The prev transaction commit doesn't complete, we need do |
| 5564 | * full commit by ourselves. |
| 5565 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5566 | if (fs_info->last_trans_log_full_commit > |
| 5567 | fs_info->last_trans_committed) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5568 | ret = 1; |
| 5569 | goto end_no_trans; |
| 5570 | } |
| 5571 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5572 | if (btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5573 | ret = 1; |
| 5574 | goto end_no_trans; |
| 5575 | } |
| 5576 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5577 | ret = check_parent_dirs_for_sync(trans, inode, parent, sb, |
| 5578 | last_committed); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5579 | if (ret) |
| 5580 | goto end_no_trans; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5581 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5582 | if (btrfs_inode_in_log(inode, trans->transid)) { |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5583 | ret = BTRFS_NO_LOG_SYNC; |
| 5584 | goto end_no_trans; |
| 5585 | } |
| 5586 | |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5587 | ret = start_log_trans(trans, root, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5588 | if (ret) |
Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 5589 | goto end_no_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5590 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5591 | ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5592 | if (ret) |
| 5593 | goto end_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5594 | |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5595 | /* |
| 5596 | * for regular files, if its inode is already on disk, we don't |
| 5597 | * have to worry about the parents at all. This is because |
| 5598 | * we can use the last_unlink_trans field to record renames |
| 5599 | * and other fun in this file. |
| 5600 | */ |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5601 | if (S_ISREG(inode->vfs_inode.i_mode) && |
| 5602 | inode->generation <= last_committed && |
| 5603 | inode->last_unlink_trans <= last_committed) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5604 | ret = 0; |
| 5605 | goto end_trans; |
| 5606 | } |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5607 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5608 | 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] | 5609 | log_dentries = true; |
| 5610 | |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5611 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5612 | * 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] | 5613 | * inodes are fully logged. This is to prevent leaving dangling |
| 5614 | * directory index entries in directories that were our parents but are |
| 5615 | * not anymore. Not doing this results in old parent directory being |
| 5616 | * impossible to delete after log replay (rmdir will always fail with |
| 5617 | * error -ENOTEMPTY). |
| 5618 | * |
| 5619 | * Example 1: |
| 5620 | * |
| 5621 | * mkdir testdir |
| 5622 | * touch testdir/foo |
| 5623 | * ln testdir/foo testdir/bar |
| 5624 | * sync |
| 5625 | * unlink testdir/bar |
| 5626 | * xfs_io -c fsync testdir/foo |
| 5627 | * <power failure> |
| 5628 | * mount fs, triggers log replay |
| 5629 | * |
| 5630 | * If we don't log the parent directory (testdir), after log replay the |
| 5631 | * directory still has an entry pointing to the file inode using the bar |
| 5632 | * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and |
| 5633 | * the file inode has a link count of 1. |
| 5634 | * |
| 5635 | * Example 2: |
| 5636 | * |
| 5637 | * mkdir testdir |
| 5638 | * touch foo |
| 5639 | * ln foo testdir/foo2 |
| 5640 | * ln foo testdir/foo3 |
| 5641 | * sync |
| 5642 | * unlink testdir/foo3 |
| 5643 | * xfs_io -c fsync foo |
| 5644 | * <power failure> |
| 5645 | * mount fs, triggers log replay |
| 5646 | * |
| 5647 | * Similar as the first example, after log replay the parent directory |
| 5648 | * testdir still has an entry pointing to the inode file with name foo3 |
| 5649 | * but the file inode does not have a matching BTRFS_INODE_REF_KEY item |
| 5650 | * and has a link count of 2. |
| 5651 | */ |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5652 | if (inode->last_unlink_trans > last_committed) { |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5653 | ret = btrfs_log_all_parents(trans, orig_inode, ctx); |
| 5654 | if (ret) |
| 5655 | goto end_trans; |
| 5656 | } |
| 5657 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5658 | while (1) { |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5659 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5660 | break; |
| 5661 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5662 | inode = BTRFS_I(d_inode(parent)); |
| 5663 | if (root != inode->root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5664 | break; |
| 5665 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5666 | if (inode->generation > last_committed) { |
| 5667 | ret = btrfs_log_inode(trans, root, inode, |
| 5668 | LOG_INODE_EXISTS, 0, LLONG_MAX, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5669 | if (ret) |
| 5670 | goto end_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5671 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5672 | if (IS_ROOT(parent)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5673 | break; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5674 | |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5675 | parent = dget_parent(parent); |
| 5676 | dput(old_parent); |
| 5677 | old_parent = parent; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5678 | } |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5679 | if (log_dentries) |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5680 | ret = log_new_dir_dentries(trans, root, orig_inode, ctx); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5681 | else |
| 5682 | ret = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5683 | end_trans: |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5684 | dput(old_parent); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5685 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5686 | btrfs_set_log_full_commit(fs_info, trans); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5687 | ret = 1; |
| 5688 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5689 | |
| 5690 | if (ret) |
| 5691 | btrfs_remove_log_ctx(root, ctx); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5692 | btrfs_end_log_trans(root); |
| 5693 | end_no_trans: |
| 5694 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5695 | } |
| 5696 | |
| 5697 | /* |
| 5698 | * it is not safe to log dentry if the chunk root has added new |
| 5699 | * chunks. This returns 0 if the dentry was logged, and 1 otherwise. |
| 5700 | * If this returns 1, you must commit the transaction to safely get your |
| 5701 | * data on disk. |
| 5702 | */ |
| 5703 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e5b84f7a | 2018-02-27 17:37:18 +0200 | [diff] [blame] | 5704 | struct dentry *dentry, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5705 | const loff_t start, |
| 5706 | const loff_t end, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5707 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5708 | { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5709 | struct dentry *parent = dget_parent(dentry); |
| 5710 | int ret; |
| 5711 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5712 | ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent, |
| 5713 | start, end, LOG_INODE_ALL, ctx); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5714 | dput(parent); |
| 5715 | |
| 5716 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5717 | } |
| 5718 | |
| 5719 | /* |
| 5720 | * should be called during mount to recover any replay any log trees |
| 5721 | * from the FS |
| 5722 | */ |
| 5723 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) |
| 5724 | { |
| 5725 | int ret; |
| 5726 | struct btrfs_path *path; |
| 5727 | struct btrfs_trans_handle *trans; |
| 5728 | struct btrfs_key key; |
| 5729 | struct btrfs_key found_key; |
| 5730 | struct btrfs_key tmp_key; |
| 5731 | struct btrfs_root *log; |
| 5732 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; |
| 5733 | struct walk_control wc = { |
| 5734 | .process_func = process_one_buffer, |
| 5735 | .stage = 0, |
| 5736 | }; |
| 5737 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5738 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5739 | if (!path) |
| 5740 | return -ENOMEM; |
| 5741 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 5742 | set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5743 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5744 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5745 | if (IS_ERR(trans)) { |
| 5746 | ret = PTR_ERR(trans); |
| 5747 | goto error; |
| 5748 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5749 | |
| 5750 | wc.trans = trans; |
| 5751 | wc.pin = 1; |
| 5752 | |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5753 | ret = walk_log_tree(trans, log_root_tree, &wc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5754 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5755 | btrfs_handle_fs_error(fs_info, ret, |
| 5756 | "Failed to pin buffers while recovering log root tree."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5757 | goto error; |
| 5758 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5759 | |
| 5760 | again: |
| 5761 | key.objectid = BTRFS_TREE_LOG_OBJECTID; |
| 5762 | key.offset = (u64)-1; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5763 | key.type = BTRFS_ROOT_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5764 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5765 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5766 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5767 | |
| 5768 | if (ret < 0) { |
Anand Jain | 34d9700 | 2016-03-16 16:43:06 +0800 | [diff] [blame] | 5769 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5770 | "Couldn't find tree log root."); |
| 5771 | goto error; |
| 5772 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5773 | if (ret > 0) { |
| 5774 | if (path->slots[0] == 0) |
| 5775 | break; |
| 5776 | path->slots[0]--; |
| 5777 | } |
| 5778 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 5779 | path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5780 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5781 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 5782 | break; |
| 5783 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 5784 | log = btrfs_read_fs_root(log_root_tree, &found_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5785 | if (IS_ERR(log)) { |
| 5786 | ret = PTR_ERR(log); |
Anand Jain | 34d9700 | 2016-03-16 16:43:06 +0800 | [diff] [blame] | 5787 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5788 | "Couldn't read tree log root."); |
| 5789 | goto error; |
| 5790 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5791 | |
| 5792 | tmp_key.objectid = found_key.offset; |
| 5793 | tmp_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5794 | tmp_key.offset = (u64)-1; |
| 5795 | |
| 5796 | 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] | 5797 | if (IS_ERR(wc.replay_dest)) { |
| 5798 | ret = PTR_ERR(wc.replay_dest); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5799 | free_extent_buffer(log->node); |
| 5800 | free_extent_buffer(log->commit_root); |
| 5801 | kfree(log); |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5802 | btrfs_handle_fs_error(fs_info, ret, |
| 5803 | "Couldn't read target root for tree log recovery."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5804 | goto error; |
| 5805 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5806 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 5807 | wc.replay_dest->log_root = log; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5808 | btrfs_record_root_in_trans(trans, wc.replay_dest); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5809 | ret = walk_log_tree(trans, log, &wc); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5810 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5811 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5812 | ret = fixup_inode_link_counts(trans, wc.replay_dest, |
| 5813 | path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5814 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5815 | |
Liu Bo | 900c998 | 2018-01-25 11:02:56 -0700 | [diff] [blame] | 5816 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
| 5817 | struct btrfs_root *root = wc.replay_dest; |
| 5818 | |
| 5819 | btrfs_release_path(path); |
| 5820 | |
| 5821 | /* |
| 5822 | * We have just replayed everything, and the highest |
| 5823 | * objectid of fs roots probably has changed in case |
| 5824 | * some inode_item's got replayed. |
| 5825 | * |
| 5826 | * root->objectid_mutex is not acquired as log replay |
| 5827 | * could only happen during mount. |
| 5828 | */ |
| 5829 | ret = btrfs_find_highest_objectid(root, |
| 5830 | &root->highest_objectid); |
| 5831 | } |
| 5832 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5833 | key.offset = found_key.offset - 1; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 5834 | wc.replay_dest->log_root = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5835 | free_extent_buffer(log->node); |
Chris Mason | b263c2c | 2009-06-11 11:24:47 -0400 | [diff] [blame] | 5836 | free_extent_buffer(log->commit_root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5837 | kfree(log); |
| 5838 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5839 | if (ret) |
| 5840 | goto error; |
| 5841 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5842 | if (found_key.offset == 0) |
| 5843 | break; |
| 5844 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5845 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5846 | |
| 5847 | /* step one is to pin it all, step two is to replay just inodes */ |
| 5848 | if (wc.pin) { |
| 5849 | wc.pin = 0; |
| 5850 | wc.process_func = replay_one_buffer; |
| 5851 | wc.stage = LOG_WALK_REPLAY_INODES; |
| 5852 | goto again; |
| 5853 | } |
| 5854 | /* step three is to replay everything */ |
| 5855 | if (wc.stage < LOG_WALK_REPLAY_ALL) { |
| 5856 | wc.stage++; |
| 5857 | goto again; |
| 5858 | } |
| 5859 | |
| 5860 | btrfs_free_path(path); |
| 5861 | |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 5862 | /* step 4: commit the transaction, which also unpins the blocks */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5863 | ret = btrfs_commit_transaction(trans); |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 5864 | if (ret) |
| 5865 | return ret; |
| 5866 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5867 | free_extent_buffer(log_root_tree->node); |
| 5868 | log_root_tree->log_root = NULL; |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 5869 | clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5870 | kfree(log_root_tree); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5871 | |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 5872 | return 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5873 | error: |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5874 | if (wc.trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5875 | btrfs_end_transaction(wc.trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5876 | btrfs_free_path(path); |
| 5877 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5878 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5879 | |
| 5880 | /* |
| 5881 | * there are some corner cases where we want to force a full |
| 5882 | * commit instead of allowing a directory to be logged. |
| 5883 | * |
| 5884 | * They revolve around files there were unlinked from the directory, and |
| 5885 | * this function updates the parent directory so that a full commit is |
| 5886 | * 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] | 5887 | * |
| 5888 | * Must be called before the unlink operations (updates to the subvolume tree, |
| 5889 | * inodes, etc) are done. |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5890 | */ |
| 5891 | void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5892 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5893 | int for_rename) |
| 5894 | { |
| 5895 | /* |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5896 | * when we're logging a file, if it hasn't been renamed |
| 5897 | * or unlinked, and its inode is fully committed on disk, |
| 5898 | * we don't have to worry about walking up the directory chain |
| 5899 | * to log its parents. |
| 5900 | * |
| 5901 | * So, we use the last_unlink_trans field to put this transid |
| 5902 | * into the file. When the file is logged we check it and |
| 5903 | * don't log the parents if the file is fully on disk. |
| 5904 | */ |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5905 | mutex_lock(&inode->log_mutex); |
| 5906 | inode->last_unlink_trans = trans->transid; |
| 5907 | mutex_unlock(&inode->log_mutex); |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5908 | |
| 5909 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5910 | * if this directory was already logged any new |
| 5911 | * names for this file/dir will get recorded |
| 5912 | */ |
| 5913 | smp_mb(); |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5914 | if (dir->logged_trans == trans->transid) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5915 | return; |
| 5916 | |
| 5917 | /* |
| 5918 | * if the inode we're about to unlink was logged, |
| 5919 | * the log will be properly updated for any new names |
| 5920 | */ |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5921 | if (inode->logged_trans == trans->transid) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5922 | return; |
| 5923 | |
| 5924 | /* |
| 5925 | * when renaming files across directories, if the directory |
| 5926 | * there we're unlinking from gets fsync'd later on, there's |
| 5927 | * no way to find the destination directory later and fsync it |
| 5928 | * properly. So, we have to be conservative and force commits |
| 5929 | * so the new name gets discovered. |
| 5930 | */ |
| 5931 | if (for_rename) |
| 5932 | goto record; |
| 5933 | |
| 5934 | /* we can safely do the unlink without any special recording */ |
| 5935 | return; |
| 5936 | |
| 5937 | record: |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5938 | mutex_lock(&dir->log_mutex); |
| 5939 | dir->last_unlink_trans = trans->transid; |
| 5940 | mutex_unlock(&dir->log_mutex); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5941 | } |
| 5942 | |
| 5943 | /* |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 5944 | * Make sure that if someone attempts to fsync the parent directory of a deleted |
| 5945 | * snapshot, it ends up triggering a transaction commit. This is to guarantee |
| 5946 | * that after replaying the log tree of the parent directory's root we will not |
| 5947 | * see the snapshot anymore and at log replay time we will not see any log tree |
| 5948 | * corresponding to the deleted snapshot's root, which could lead to replaying |
| 5949 | * it after replaying the log tree of the parent directory (which would replay |
| 5950 | * the snapshot delete operation). |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5951 | * |
| 5952 | * Must be called before the actual snapshot destroy operation (updates to the |
| 5953 | * parent root and tree of tree roots trees, etc) are done. |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 5954 | */ |
| 5955 | void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4366355 | 2017-01-18 00:31:29 +0200 | [diff] [blame] | 5956 | struct btrfs_inode *dir) |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 5957 | { |
Nikolay Borisov | 4366355 | 2017-01-18 00:31:29 +0200 | [diff] [blame] | 5958 | mutex_lock(&dir->log_mutex); |
| 5959 | dir->last_unlink_trans = trans->transid; |
| 5960 | mutex_unlock(&dir->log_mutex); |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 5961 | } |
| 5962 | |
| 5963 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5964 | * Call this after adding a new name for a file and it will properly |
| 5965 | * update the log to reflect the new name. |
| 5966 | * |
| 5967 | * It will return zero if all goes well, and it will return 1 if a |
| 5968 | * full transaction commit is required. |
| 5969 | */ |
| 5970 | int btrfs_log_new_name(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 5971 | struct btrfs_inode *inode, struct btrfs_inode *old_dir, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5972 | struct dentry *parent) |
| 5973 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame^] | 5974 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5975 | |
| 5976 | /* |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5977 | * this will force the logging code to walk the dentry chain |
| 5978 | * up for the file |
| 5979 | */ |
Filipe Manana | 9a6509c | 2018-02-28 15:55:40 +0000 | [diff] [blame] | 5980 | if (!S_ISDIR(inode->vfs_inode.i_mode)) |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 5981 | inode->last_unlink_trans = trans->transid; |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5982 | |
| 5983 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5984 | * if this inode hasn't been logged and directory we're renaming it |
| 5985 | * from hasn't been logged, we don't need to log it |
| 5986 | */ |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 5987 | if (inode->logged_trans <= fs_info->last_trans_committed && |
| 5988 | (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed)) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5989 | return 0; |
| 5990 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5991 | return btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX, |
| 5992 | LOG_INODE_EXISTS, NULL); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5993 | } |
| 5994 | |