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) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 600 | size = btrfs_file_extent_inline_len(eb, slot, 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 | fs_info, |
| 719 | root->root_key.objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 720 | key->objectid, offset, &ins); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 721 | if (ret) |
| 722 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 723 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 724 | btrfs_release_path(path); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 725 | |
| 726 | if (btrfs_file_extent_compression(eb, item)) { |
| 727 | csum_start = ins.objectid; |
| 728 | csum_end = csum_start + ins.offset; |
| 729 | } else { |
| 730 | csum_start = ins.objectid + |
| 731 | btrfs_file_extent_offset(eb, item); |
| 732 | csum_end = csum_start + |
| 733 | btrfs_file_extent_num_bytes(eb, item); |
| 734 | } |
| 735 | |
| 736 | ret = btrfs_lookup_csums_range(root->log_root, |
| 737 | csum_start, csum_end - 1, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 738 | &ordered_sums, 0); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 739 | if (ret) |
| 740 | goto out; |
Filipe Manana | b84b839 | 2015-08-19 11:09:40 +0100 | [diff] [blame] | 741 | /* |
| 742 | * Now delete all existing cums in the csum root that |
| 743 | * cover our range. We do this because we can have an |
| 744 | * extent that is completely referenced by one file |
| 745 | * extent item and partially referenced by another |
| 746 | * file extent item (like after using the clone or |
| 747 | * extent_same ioctls). In this case if we end up doing |
| 748 | * the replay of the one that partially references the |
| 749 | * extent first, and we do not do the csum deletion |
| 750 | * below, we can get 2 csum items in the csum tree that |
| 751 | * overlap each other. For example, imagine our log has |
| 752 | * the two following file extent items: |
| 753 | * |
| 754 | * key (257 EXTENT_DATA 409600) |
| 755 | * extent data disk byte 12845056 nr 102400 |
| 756 | * extent data offset 20480 nr 20480 ram 102400 |
| 757 | * |
| 758 | * key (257 EXTENT_DATA 819200) |
| 759 | * extent data disk byte 12845056 nr 102400 |
| 760 | * extent data offset 0 nr 102400 ram 102400 |
| 761 | * |
| 762 | * Where the second one fully references the 100K extent |
| 763 | * that starts at disk byte 12845056, and the log tree |
| 764 | * has a single csum item that covers the entire range |
| 765 | * of the extent: |
| 766 | * |
| 767 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 |
| 768 | * |
| 769 | * After the first file extent item is replayed, the |
| 770 | * csum tree gets the following csum item: |
| 771 | * |
| 772 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 |
| 773 | * |
| 774 | * Which covers the 20K sub-range starting at offset 20K |
| 775 | * of our extent. Now when we replay the second file |
| 776 | * extent item, if we do not delete existing csum items |
| 777 | * that cover any of its blocks, we end up getting two |
| 778 | * csum items in our csum tree that overlap each other: |
| 779 | * |
| 780 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 |
| 781 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 |
| 782 | * |
| 783 | * Which is a problem, because after this anyone trying |
| 784 | * to lookup up for the checksum of any block of our |
| 785 | * extent starting at an offset of 40K or higher, will |
| 786 | * end up looking at the second csum item only, which |
| 787 | * does not contain the checksum for any block starting |
| 788 | * at offset 40K or higher of our extent. |
| 789 | */ |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 790 | while (!list_empty(&ordered_sums)) { |
| 791 | struct btrfs_ordered_sum *sums; |
| 792 | sums = list_entry(ordered_sums.next, |
| 793 | struct btrfs_ordered_sum, |
| 794 | list); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 795 | if (!ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 796 | ret = btrfs_del_csums(trans, fs_info, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 797 | sums->bytenr, |
| 798 | sums->len); |
Filipe Manana | b84b839 | 2015-08-19 11:09:40 +0100 | [diff] [blame] | 799 | if (!ret) |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 800 | ret = btrfs_csum_file_blocks(trans, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 801 | fs_info->csum_root, sums); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 802 | list_del(&sums->list); |
| 803 | kfree(sums); |
| 804 | } |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 805 | if (ret) |
| 806 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 807 | } else { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 808 | btrfs_release_path(path); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 809 | } |
| 810 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 811 | /* inline extents are easy, we just overwrite them */ |
| 812 | ret = overwrite_item(trans, root, path, eb, slot, key); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 813 | if (ret) |
| 814 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 815 | } |
| 816 | |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 817 | inode_add_bytes(inode, nbytes); |
Filipe Manana | 3168021c | 2017-02-01 14:58:02 +0000 | [diff] [blame] | 818 | update_inode: |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 819 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 820 | out: |
| 821 | if (inode) |
| 822 | iput(inode); |
| 823 | return ret; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * when cleaning up conflicts between the directory names in the |
| 828 | * subvolume, directory names in the log and directory names in the |
| 829 | * inode back references, we may have to unlink inodes from directories. |
| 830 | * |
| 831 | * This is a helper function to do the unlink of a specific directory |
| 832 | * item |
| 833 | */ |
| 834 | static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, |
| 835 | struct btrfs_root *root, |
| 836 | struct btrfs_path *path, |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 837 | struct btrfs_inode *dir, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 838 | struct btrfs_dir_item *di) |
| 839 | { |
| 840 | struct inode *inode; |
| 841 | char *name; |
| 842 | int name_len; |
| 843 | struct extent_buffer *leaf; |
| 844 | struct btrfs_key location; |
| 845 | int ret; |
| 846 | |
| 847 | leaf = path->nodes[0]; |
| 848 | |
| 849 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
| 850 | name_len = btrfs_dir_name_len(leaf, di); |
| 851 | name = kmalloc(name_len, GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 852 | if (!name) |
| 853 | return -ENOMEM; |
| 854 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 855 | read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 856 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 857 | |
| 858 | inode = read_one_inode(root, location.objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 859 | if (!inode) { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 860 | ret = -EIO; |
| 861 | goto out; |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 862 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 863 | |
Yan Zheng | ec051c0 | 2009-01-05 15:43:42 -0500 | [diff] [blame] | 864 | ret = link_to_fixup_dir(trans, root, path, location.objectid); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 865 | if (ret) |
| 866 | goto out; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 867 | |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 868 | ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name, |
| 869 | name_len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 870 | if (ret) |
| 871 | goto out; |
Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 872 | else |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 873 | ret = btrfs_run_delayed_items(trans); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 874 | out: |
| 875 | kfree(name); |
| 876 | iput(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 877 | return ret; |
| 878 | } |
| 879 | |
| 880 | /* |
| 881 | * helper function to see if a given name and sequence number found |
| 882 | * in an inode back reference are already in a directory and correctly |
| 883 | * point to this inode |
| 884 | */ |
| 885 | static noinline int inode_in_dir(struct btrfs_root *root, |
| 886 | struct btrfs_path *path, |
| 887 | u64 dirid, u64 objectid, u64 index, |
| 888 | const char *name, int name_len) |
| 889 | { |
| 890 | struct btrfs_dir_item *di; |
| 891 | struct btrfs_key location; |
| 892 | int match = 0; |
| 893 | |
| 894 | di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, |
| 895 | index, name, name_len, 0); |
| 896 | if (di && !IS_ERR(di)) { |
| 897 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
| 898 | if (location.objectid != objectid) |
| 899 | goto out; |
| 900 | } else |
| 901 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 902 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 903 | |
| 904 | di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0); |
| 905 | if (di && !IS_ERR(di)) { |
| 906 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
| 907 | if (location.objectid != objectid) |
| 908 | goto out; |
| 909 | } else |
| 910 | goto out; |
| 911 | match = 1; |
| 912 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 913 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 914 | return match; |
| 915 | } |
| 916 | |
| 917 | /* |
| 918 | * helper function to check a log tree for a named back reference in |
| 919 | * an inode. This is used to decide if a back reference that is |
| 920 | * found in the subvolume conflicts with what we find in the log. |
| 921 | * |
| 922 | * inode backreferences may have multiple refs in a single item, |
| 923 | * during replay we process one reference at a time, and we don't |
| 924 | * want to delete valid links to a file from the subvolume if that |
| 925 | * link is also in the log. |
| 926 | */ |
| 927 | static noinline int backref_in_log(struct btrfs_root *log, |
| 928 | struct btrfs_key *key, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 929 | u64 ref_objectid, |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 930 | const char *name, int namelen) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 931 | { |
| 932 | struct btrfs_path *path; |
| 933 | struct btrfs_inode_ref *ref; |
| 934 | unsigned long ptr; |
| 935 | unsigned long ptr_end; |
| 936 | unsigned long name_ptr; |
| 937 | int found_name_len; |
| 938 | int item_size; |
| 939 | int ret; |
| 940 | int match = 0; |
| 941 | |
| 942 | path = btrfs_alloc_path(); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 943 | if (!path) |
| 944 | return -ENOMEM; |
| 945 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 946 | ret = btrfs_search_slot(NULL, log, key, path, 0, 0); |
| 947 | if (ret != 0) |
| 948 | goto out; |
| 949 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 950 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 951 | |
| 952 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 953 | if (btrfs_find_name_in_ext_backref(path->nodes[0], |
| 954 | path->slots[0], |
| 955 | ref_objectid, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 956 | name, namelen, NULL)) |
| 957 | match = 1; |
| 958 | |
| 959 | goto out; |
| 960 | } |
| 961 | |
| 962 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 963 | ptr_end = ptr + item_size; |
| 964 | while (ptr < ptr_end) { |
| 965 | ref = (struct btrfs_inode_ref *)ptr; |
| 966 | found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref); |
| 967 | if (found_name_len == namelen) { |
| 968 | name_ptr = (unsigned long)(ref + 1); |
| 969 | ret = memcmp_extent_buffer(path->nodes[0], name, |
| 970 | name_ptr, namelen); |
| 971 | if (ret == 0) { |
| 972 | match = 1; |
| 973 | goto out; |
| 974 | } |
| 975 | } |
| 976 | ptr = (unsigned long)(ref + 1) + found_name_len; |
| 977 | } |
| 978 | out: |
| 979 | btrfs_free_path(path); |
| 980 | return match; |
| 981 | } |
| 982 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 983 | static inline int __add_inode_ref(struct btrfs_trans_handle *trans, |
| 984 | struct btrfs_root *root, |
| 985 | struct btrfs_path *path, |
| 986 | struct btrfs_root *log_root, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 987 | struct btrfs_inode *dir, |
| 988 | struct btrfs_inode *inode, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 989 | u64 inode_objectid, u64 parent_objectid, |
| 990 | u64 ref_index, char *name, int namelen, |
| 991 | int *search_done) |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 992 | { |
| 993 | int ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 994 | char *victim_name; |
| 995 | int victim_name_len; |
| 996 | struct extent_buffer *leaf; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 997 | struct btrfs_dir_item *di; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 998 | struct btrfs_key search_key; |
| 999 | struct btrfs_inode_extref *extref; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1000 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1001 | again: |
| 1002 | /* Search old style refs */ |
| 1003 | search_key.objectid = inode_objectid; |
| 1004 | search_key.type = BTRFS_INODE_REF_KEY; |
| 1005 | search_key.offset = parent_objectid; |
| 1006 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1007 | if (ret == 0) { |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1008 | struct btrfs_inode_ref *victim_ref; |
| 1009 | unsigned long ptr; |
| 1010 | unsigned long ptr_end; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1011 | |
| 1012 | leaf = path->nodes[0]; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1013 | |
| 1014 | /* are we trying to overwrite a back ref for the root directory |
| 1015 | * if so, just jump out, we're done |
| 1016 | */ |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1017 | if (search_key.objectid == search_key.offset) |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1018 | return 1; |
| 1019 | |
| 1020 | /* check all the names in this back reference to see |
| 1021 | * if they are in the log. if so, we allow them to stay |
| 1022 | * otherwise they must be unlinked as a conflict |
| 1023 | */ |
| 1024 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1025 | ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1026 | while (ptr < ptr_end) { |
| 1027 | victim_ref = (struct btrfs_inode_ref *)ptr; |
| 1028 | victim_name_len = btrfs_inode_ref_name_len(leaf, |
| 1029 | victim_ref); |
| 1030 | victim_name = kmalloc(victim_name_len, GFP_NOFS); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1031 | if (!victim_name) |
| 1032 | return -ENOMEM; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1033 | |
| 1034 | read_extent_buffer(leaf, victim_name, |
| 1035 | (unsigned long)(victim_ref + 1), |
| 1036 | victim_name_len); |
| 1037 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1038 | if (!backref_in_log(log_root, &search_key, |
| 1039 | parent_objectid, |
| 1040 | victim_name, |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1041 | victim_name_len)) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1042 | inc_nlink(&inode->vfs_inode); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1043 | btrfs_release_path(path); |
| 1044 | |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1045 | ret = btrfs_unlink_inode(trans, root, dir, inode, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1046 | victim_name, victim_name_len); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1047 | kfree(victim_name); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1048 | if (ret) |
| 1049 | return ret; |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 1050 | ret = btrfs_run_delayed_items(trans); |
Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 1051 | if (ret) |
| 1052 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1053 | *search_done = 1; |
| 1054 | goto again; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1055 | } |
| 1056 | kfree(victim_name); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1057 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1058 | ptr = (unsigned long)(victim_ref + 1) + victim_name_len; |
| 1059 | } |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1060 | |
| 1061 | /* |
| 1062 | * NOTE: we have searched root tree and checked the |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 1063 | * corresponding ref, it does not need to check again. |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1064 | */ |
| 1065 | *search_done = 1; |
| 1066 | } |
| 1067 | btrfs_release_path(path); |
| 1068 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1069 | /* Same search but for extended refs */ |
| 1070 | extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen, |
| 1071 | inode_objectid, parent_objectid, 0, |
| 1072 | 0); |
| 1073 | if (!IS_ERR_OR_NULL(extref)) { |
| 1074 | u32 item_size; |
| 1075 | u32 cur_offset = 0; |
| 1076 | unsigned long base; |
| 1077 | struct inode *victim_parent; |
| 1078 | |
| 1079 | leaf = path->nodes[0]; |
| 1080 | |
| 1081 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1082 | base = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1083 | |
| 1084 | while (cur_offset < item_size) { |
Quentin Casasnovas | dd9ef13 | 2015-03-03 16:31:38 +0100 | [diff] [blame] | 1085 | extref = (struct btrfs_inode_extref *)(base + cur_offset); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1086 | |
| 1087 | victim_name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 1088 | |
| 1089 | if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid) |
| 1090 | goto next; |
| 1091 | |
| 1092 | victim_name = kmalloc(victim_name_len, GFP_NOFS); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1093 | if (!victim_name) |
| 1094 | return -ENOMEM; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1095 | read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name, |
| 1096 | victim_name_len); |
| 1097 | |
| 1098 | search_key.objectid = inode_objectid; |
| 1099 | search_key.type = BTRFS_INODE_EXTREF_KEY; |
| 1100 | search_key.offset = btrfs_extref_hash(parent_objectid, |
| 1101 | victim_name, |
| 1102 | victim_name_len); |
| 1103 | ret = 0; |
| 1104 | if (!backref_in_log(log_root, &search_key, |
| 1105 | parent_objectid, victim_name, |
| 1106 | victim_name_len)) { |
| 1107 | ret = -ENOENT; |
| 1108 | victim_parent = read_one_inode(root, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1109 | parent_objectid); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1110 | if (victim_parent) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1111 | inc_nlink(&inode->vfs_inode); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1112 | btrfs_release_path(path); |
| 1113 | |
| 1114 | ret = btrfs_unlink_inode(trans, root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1115 | BTRFS_I(victim_parent), |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1116 | inode, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1117 | victim_name, |
| 1118 | victim_name_len); |
Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 1119 | if (!ret) |
| 1120 | ret = btrfs_run_delayed_items( |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 1121 | trans); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1122 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1123 | iput(victim_parent); |
| 1124 | kfree(victim_name); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1125 | if (ret) |
| 1126 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1127 | *search_done = 1; |
| 1128 | goto again; |
| 1129 | } |
| 1130 | kfree(victim_name); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1131 | next: |
| 1132 | cur_offset += victim_name_len + sizeof(*extref); |
| 1133 | } |
| 1134 | *search_done = 1; |
| 1135 | } |
| 1136 | btrfs_release_path(path); |
| 1137 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1138 | /* look for a conflicting sequence number */ |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1139 | di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir), |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1140 | ref_index, name, namelen, 0); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1141 | if (di && !IS_ERR(di)) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1142 | ret = drop_one_dir_item(trans, root, path, dir, di); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1143 | if (ret) |
| 1144 | return ret; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1145 | } |
| 1146 | btrfs_release_path(path); |
| 1147 | |
| 1148 | /* look for a conflicing name */ |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1149 | di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1150 | name, namelen, 0); |
| 1151 | if (di && !IS_ERR(di)) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1152 | ret = drop_one_dir_item(trans, root, path, dir, di); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1153 | if (ret) |
| 1154 | return ret; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1155 | } |
| 1156 | btrfs_release_path(path); |
| 1157 | |
| 1158 | return 0; |
| 1159 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1160 | |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1161 | static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
| 1162 | u32 *namelen, char **name, u64 *index, |
| 1163 | u64 *parent_objectid) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1164 | { |
| 1165 | struct btrfs_inode_extref *extref; |
| 1166 | |
| 1167 | extref = (struct btrfs_inode_extref *)ref_ptr; |
| 1168 | |
| 1169 | *namelen = btrfs_inode_extref_name_len(eb, extref); |
| 1170 | *name = kmalloc(*namelen, GFP_NOFS); |
| 1171 | if (*name == NULL) |
| 1172 | return -ENOMEM; |
| 1173 | |
| 1174 | read_extent_buffer(eb, *name, (unsigned long)&extref->name, |
| 1175 | *namelen); |
| 1176 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1177 | if (index) |
| 1178 | *index = btrfs_inode_extref_index(eb, extref); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1179 | if (parent_objectid) |
| 1180 | *parent_objectid = btrfs_inode_extref_parent(eb, extref); |
| 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1185 | static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
| 1186 | u32 *namelen, char **name, u64 *index) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1187 | { |
| 1188 | struct btrfs_inode_ref *ref; |
| 1189 | |
| 1190 | ref = (struct btrfs_inode_ref *)ref_ptr; |
| 1191 | |
| 1192 | *namelen = btrfs_inode_ref_name_len(eb, ref); |
| 1193 | *name = kmalloc(*namelen, GFP_NOFS); |
| 1194 | if (*name == NULL) |
| 1195 | return -ENOMEM; |
| 1196 | |
| 1197 | read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen); |
| 1198 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1199 | if (index) |
| 1200 | *index = btrfs_inode_ref_index(eb, ref); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1201 | |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1205 | /* |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1206 | * Take an inode reference item from the log tree and iterate all names from the |
| 1207 | * inode reference item in the subvolume tree with the same key (if it exists). |
| 1208 | * For any name that is not in the inode reference item from the log tree, do a |
| 1209 | * proper unlink of that name (that is, remove its entry from the inode |
| 1210 | * reference item and both dir index keys). |
| 1211 | */ |
| 1212 | static int unlink_old_inode_refs(struct btrfs_trans_handle *trans, |
| 1213 | struct btrfs_root *root, |
| 1214 | struct btrfs_path *path, |
| 1215 | struct btrfs_inode *inode, |
| 1216 | struct extent_buffer *log_eb, |
| 1217 | int log_slot, |
| 1218 | struct btrfs_key *key) |
| 1219 | { |
| 1220 | int ret; |
| 1221 | unsigned long ref_ptr; |
| 1222 | unsigned long ref_end; |
| 1223 | struct extent_buffer *eb; |
| 1224 | |
| 1225 | again: |
| 1226 | btrfs_release_path(path); |
| 1227 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 1228 | if (ret > 0) { |
| 1229 | ret = 0; |
| 1230 | goto out; |
| 1231 | } |
| 1232 | if (ret < 0) |
| 1233 | goto out; |
| 1234 | |
| 1235 | eb = path->nodes[0]; |
| 1236 | ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]); |
| 1237 | ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]); |
| 1238 | while (ref_ptr < ref_end) { |
| 1239 | char *name = NULL; |
| 1240 | int namelen; |
| 1241 | u64 parent_id; |
| 1242 | |
| 1243 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
| 1244 | ret = extref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1245 | NULL, &parent_id); |
| 1246 | } else { |
| 1247 | parent_id = key->offset; |
| 1248 | ret = ref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1249 | NULL); |
| 1250 | } |
| 1251 | if (ret) |
| 1252 | goto out; |
| 1253 | |
| 1254 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
| 1255 | ret = btrfs_find_name_in_ext_backref(log_eb, log_slot, |
| 1256 | parent_id, name, |
| 1257 | namelen, NULL); |
| 1258 | else |
| 1259 | ret = btrfs_find_name_in_backref(log_eb, log_slot, name, |
| 1260 | namelen, NULL); |
| 1261 | |
| 1262 | if (!ret) { |
| 1263 | struct inode *dir; |
| 1264 | |
| 1265 | btrfs_release_path(path); |
| 1266 | dir = read_one_inode(root, parent_id); |
| 1267 | if (!dir) { |
| 1268 | ret = -ENOENT; |
| 1269 | kfree(name); |
| 1270 | goto out; |
| 1271 | } |
| 1272 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 1273 | inode, name, namelen); |
| 1274 | kfree(name); |
| 1275 | iput(dir); |
| 1276 | if (ret) |
| 1277 | goto out; |
| 1278 | goto again; |
| 1279 | } |
| 1280 | |
| 1281 | kfree(name); |
| 1282 | ref_ptr += namelen; |
| 1283 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
| 1284 | ref_ptr += sizeof(struct btrfs_inode_extref); |
| 1285 | else |
| 1286 | ref_ptr += sizeof(struct btrfs_inode_ref); |
| 1287 | } |
| 1288 | ret = 0; |
| 1289 | out: |
| 1290 | btrfs_release_path(path); |
| 1291 | return ret; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1295 | * replay one inode back reference item found in the log tree. |
| 1296 | * eb, slot and key refer to the buffer and key found in the log tree. |
| 1297 | * root is the destination we are replaying into, and path is for temp |
| 1298 | * use by this function. (it should be released on return). |
| 1299 | */ |
| 1300 | static noinline int add_inode_ref(struct btrfs_trans_handle *trans, |
| 1301 | struct btrfs_root *root, |
| 1302 | struct btrfs_root *log, |
| 1303 | struct btrfs_path *path, |
| 1304 | struct extent_buffer *eb, int slot, |
| 1305 | struct btrfs_key *key) |
| 1306 | { |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1307 | struct inode *dir = NULL; |
| 1308 | struct inode *inode = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1309 | unsigned long ref_ptr; |
| 1310 | unsigned long ref_end; |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1311 | char *name = NULL; |
liubo | 34f3e4f | 2011-08-06 08:35:23 +0000 | [diff] [blame] | 1312 | int namelen; |
| 1313 | int ret; |
liubo | c622ae6 | 2011-03-26 08:01:12 -0400 | [diff] [blame] | 1314 | int search_done = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1315 | int log_ref_ver = 0; |
| 1316 | u64 parent_objectid; |
| 1317 | u64 inode_objectid; |
Chris Mason | f46dbe3 | 2012-10-09 11:17:20 -0400 | [diff] [blame] | 1318 | u64 ref_index = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1319 | int ref_struct_size; |
| 1320 | |
| 1321 | ref_ptr = btrfs_item_ptr_offset(eb, slot); |
| 1322 | ref_end = ref_ptr + btrfs_item_size_nr(eb, slot); |
| 1323 | |
| 1324 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
| 1325 | struct btrfs_inode_extref *r; |
| 1326 | |
| 1327 | ref_struct_size = sizeof(struct btrfs_inode_extref); |
| 1328 | log_ref_ver = 1; |
| 1329 | r = (struct btrfs_inode_extref *)ref_ptr; |
| 1330 | parent_objectid = btrfs_inode_extref_parent(eb, r); |
| 1331 | } else { |
| 1332 | ref_struct_size = sizeof(struct btrfs_inode_ref); |
| 1333 | parent_objectid = key->offset; |
| 1334 | } |
| 1335 | inode_objectid = key->objectid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1336 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1337 | /* |
| 1338 | * it is possible that we didn't log all the parent directories |
| 1339 | * for a given inode. If we don't find the dir, just don't |
| 1340 | * copy the back ref in. The link count fixup code will take |
| 1341 | * care of the rest |
| 1342 | */ |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1343 | dir = read_one_inode(root, parent_objectid); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1344 | if (!dir) { |
| 1345 | ret = -ENOENT; |
| 1346 | goto out; |
| 1347 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1348 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1349 | inode = read_one_inode(root, inode_objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1350 | if (!inode) { |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1351 | ret = -EIO; |
| 1352 | goto out; |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1353 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1354 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1355 | while (ref_ptr < ref_end) { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1356 | if (log_ref_ver) { |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1357 | ret = extref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1358 | &ref_index, &parent_objectid); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1359 | /* |
| 1360 | * parent object can change from one array |
| 1361 | * item to another. |
| 1362 | */ |
| 1363 | if (!dir) |
| 1364 | dir = read_one_inode(root, parent_objectid); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1365 | if (!dir) { |
| 1366 | ret = -ENOENT; |
| 1367 | goto out; |
| 1368 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1369 | } else { |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1370 | ret = ref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1371 | &ref_index); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1372 | } |
| 1373 | if (ret) |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1374 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1375 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1376 | /* if we already have a perfect match, we're done */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 1377 | if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)), |
| 1378 | btrfs_ino(BTRFS_I(inode)), ref_index, |
| 1379 | name, namelen)) { |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1380 | /* |
| 1381 | * look for a conflicting back reference in the |
| 1382 | * metadata. if we find one we have to unlink that name |
| 1383 | * of the file before we add our new link. Later on, we |
| 1384 | * overwrite any existing back reference, and we don't |
| 1385 | * want to create dangling pointers in the directory. |
| 1386 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1387 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1388 | if (!search_done) { |
| 1389 | ret = __add_inode_ref(trans, root, path, log, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1390 | BTRFS_I(dir), |
David Sterba | d75eefd | 2017-02-10 20:20:19 +0100 | [diff] [blame] | 1391 | BTRFS_I(inode), |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1392 | inode_objectid, |
| 1393 | parent_objectid, |
| 1394 | ref_index, name, namelen, |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1395 | &search_done); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1396 | if (ret) { |
| 1397 | if (ret == 1) |
| 1398 | ret = 0; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1399 | goto out; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1400 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1401 | } |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1402 | |
| 1403 | /* insert our name */ |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 1404 | ret = btrfs_add_link(trans, BTRFS_I(dir), |
| 1405 | BTRFS_I(inode), |
| 1406 | name, namelen, 0, ref_index); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1407 | if (ret) |
| 1408 | goto out; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1409 | |
| 1410 | btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1411 | } |
liubo | c622ae6 | 2011-03-26 08:01:12 -0400 | [diff] [blame] | 1412 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1413 | ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1414 | kfree(name); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1415 | name = NULL; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1416 | if (log_ref_ver) { |
| 1417 | iput(dir); |
| 1418 | dir = NULL; |
| 1419 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1420 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1421 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1422 | /* |
| 1423 | * Before we overwrite the inode reference item in the subvolume tree |
| 1424 | * with the item from the log tree, we must unlink all names from the |
| 1425 | * parent directory that are in the subvolume's tree inode reference |
| 1426 | * item, otherwise we end up with an inconsistent subvolume tree where |
| 1427 | * dir index entries exist for a name but there is no inode reference |
| 1428 | * item with the same name. |
| 1429 | */ |
| 1430 | ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot, |
| 1431 | key); |
| 1432 | if (ret) |
| 1433 | goto out; |
| 1434 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1435 | /* finally write the back reference in the inode */ |
| 1436 | ret = overwrite_item(trans, root, path, eb, slot, key); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1437 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1438 | btrfs_release_path(path); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1439 | kfree(name); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1440 | iput(dir); |
| 1441 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1442 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1443 | } |
| 1444 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1445 | static int insert_orphan_item(struct btrfs_trans_handle *trans, |
David Sterba | 9c4f61f | 2015-01-02 19:12:57 +0100 | [diff] [blame] | 1446 | struct btrfs_root *root, u64 ino) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1447 | { |
| 1448 | int ret; |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 1449 | |
David Sterba | 9c4f61f | 2015-01-02 19:12:57 +0100 | [diff] [blame] | 1450 | ret = btrfs_insert_orphan_item(trans, root, ino); |
| 1451 | if (ret == -EEXIST) |
| 1452 | ret = 0; |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 1453 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1454 | return ret; |
| 1455 | } |
| 1456 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1457 | static int count_inode_extrefs(struct btrfs_root *root, |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1458 | struct btrfs_inode *inode, struct btrfs_path *path) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1459 | { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1460 | int ret = 0; |
| 1461 | int name_len; |
| 1462 | unsigned int nlink = 0; |
| 1463 | u32 item_size; |
| 1464 | u32 cur_offset = 0; |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1465 | u64 inode_objectid = btrfs_ino(inode); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1466 | u64 offset = 0; |
| 1467 | unsigned long ptr; |
| 1468 | struct btrfs_inode_extref *extref; |
| 1469 | struct extent_buffer *leaf; |
| 1470 | |
| 1471 | while (1) { |
| 1472 | ret = btrfs_find_one_extref(root, inode_objectid, offset, path, |
| 1473 | &extref, &offset); |
| 1474 | if (ret) |
| 1475 | break; |
| 1476 | |
| 1477 | leaf = path->nodes[0]; |
| 1478 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1479 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 1480 | cur_offset = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1481 | |
| 1482 | while (cur_offset < item_size) { |
| 1483 | extref = (struct btrfs_inode_extref *) (ptr + cur_offset); |
| 1484 | name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 1485 | |
| 1486 | nlink++; |
| 1487 | |
| 1488 | cur_offset += name_len + sizeof(*extref); |
| 1489 | } |
| 1490 | |
| 1491 | offset++; |
| 1492 | btrfs_release_path(path); |
| 1493 | } |
| 1494 | btrfs_release_path(path); |
| 1495 | |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 1496 | if (ret < 0 && ret != -ENOENT) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1497 | return ret; |
| 1498 | return nlink; |
| 1499 | } |
| 1500 | |
| 1501 | static int count_inode_refs(struct btrfs_root *root, |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1502 | struct btrfs_inode *inode, struct btrfs_path *path) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1503 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1504 | int ret; |
| 1505 | struct btrfs_key key; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1506 | unsigned int nlink = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1507 | unsigned long ptr; |
| 1508 | unsigned long ptr_end; |
| 1509 | int name_len; |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1510 | u64 ino = btrfs_ino(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1511 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1512 | key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1513 | key.type = BTRFS_INODE_REF_KEY; |
| 1514 | key.offset = (u64)-1; |
| 1515 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1516 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1517 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1518 | if (ret < 0) |
| 1519 | break; |
| 1520 | if (ret > 0) { |
| 1521 | if (path->slots[0] == 0) |
| 1522 | break; |
| 1523 | path->slots[0]--; |
| 1524 | } |
Filipe David Borba Manana | e93ae26 | 2013-10-14 22:49:11 +0100 | [diff] [blame] | 1525 | process_slot: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1526 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1527 | path->slots[0]); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1528 | if (key.objectid != ino || |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1529 | key.type != BTRFS_INODE_REF_KEY) |
| 1530 | break; |
| 1531 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
| 1532 | ptr_end = ptr + btrfs_item_size_nr(path->nodes[0], |
| 1533 | path->slots[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1534 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1535 | struct btrfs_inode_ref *ref; |
| 1536 | |
| 1537 | ref = (struct btrfs_inode_ref *)ptr; |
| 1538 | name_len = btrfs_inode_ref_name_len(path->nodes[0], |
| 1539 | ref); |
| 1540 | ptr = (unsigned long)(ref + 1) + name_len; |
| 1541 | nlink++; |
| 1542 | } |
| 1543 | |
| 1544 | if (key.offset == 0) |
| 1545 | break; |
Filipe David Borba Manana | e93ae26 | 2013-10-14 22:49:11 +0100 | [diff] [blame] | 1546 | if (path->slots[0] > 0) { |
| 1547 | path->slots[0]--; |
| 1548 | goto process_slot; |
| 1549 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1550 | key.offset--; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1551 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1552 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1553 | btrfs_release_path(path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1554 | |
| 1555 | return nlink; |
| 1556 | } |
| 1557 | |
| 1558 | /* |
| 1559 | * There are a few corners where the link count of the file can't |
| 1560 | * be properly maintained during replay. So, instead of adding |
| 1561 | * lots of complexity to the log code, we just scan the backrefs |
| 1562 | * for any file that has been through replay. |
| 1563 | * |
| 1564 | * The scan will update the link count on the inode to reflect the |
| 1565 | * number of back refs found. If it goes down to zero, the iput |
| 1566 | * will free the inode. |
| 1567 | */ |
| 1568 | static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, |
| 1569 | struct btrfs_root *root, |
| 1570 | struct inode *inode) |
| 1571 | { |
| 1572 | struct btrfs_path *path; |
| 1573 | int ret; |
| 1574 | u64 nlink = 0; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1575 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1576 | |
| 1577 | path = btrfs_alloc_path(); |
| 1578 | if (!path) |
| 1579 | return -ENOMEM; |
| 1580 | |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1581 | ret = count_inode_refs(root, BTRFS_I(inode), path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1582 | if (ret < 0) |
| 1583 | goto out; |
| 1584 | |
| 1585 | nlink = ret; |
| 1586 | |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1587 | ret = count_inode_extrefs(root, BTRFS_I(inode), path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1588 | if (ret < 0) |
| 1589 | goto out; |
| 1590 | |
| 1591 | nlink += ret; |
| 1592 | |
| 1593 | ret = 0; |
| 1594 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1595 | if (nlink != inode->i_nlink) { |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 1596 | set_nlink(inode, nlink); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1597 | btrfs_update_inode(trans, root, inode); |
| 1598 | } |
Chris Mason | 8d5bf1c | 2008-09-11 15:51:21 -0400 | [diff] [blame] | 1599 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1600 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1601 | if (inode->i_nlink == 0) { |
| 1602 | if (S_ISDIR(inode->i_mode)) { |
| 1603 | ret = replay_dir_deletes(trans, root, NULL, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1604 | ino, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1605 | if (ret) |
| 1606 | goto out; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1607 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1608 | ret = insert_orphan_item(trans, root, ino); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1609 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1610 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1611 | out: |
| 1612 | btrfs_free_path(path); |
| 1613 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, |
| 1617 | struct btrfs_root *root, |
| 1618 | struct btrfs_path *path) |
| 1619 | { |
| 1620 | int ret; |
| 1621 | struct btrfs_key key; |
| 1622 | struct inode *inode; |
| 1623 | |
| 1624 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; |
| 1625 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
| 1626 | key.offset = (u64)-1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1627 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1628 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1629 | if (ret < 0) |
| 1630 | break; |
| 1631 | |
| 1632 | if (ret == 1) { |
| 1633 | if (path->slots[0] == 0) |
| 1634 | break; |
| 1635 | path->slots[0]--; |
| 1636 | } |
| 1637 | |
| 1638 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 1639 | if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || |
| 1640 | key.type != BTRFS_ORPHAN_ITEM_KEY) |
| 1641 | break; |
| 1642 | |
| 1643 | ret = btrfs_del_item(trans, root, path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1644 | if (ret) |
| 1645 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1646 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1647 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1648 | inode = read_one_inode(root, key.offset); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1649 | if (!inode) |
| 1650 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1651 | |
| 1652 | ret = fixup_inode_link_count(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1653 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1654 | if (ret) |
| 1655 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1656 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1657 | /* |
| 1658 | * fixup on a directory may create new entries, |
| 1659 | * make sure we always look for the highset possible |
| 1660 | * offset |
| 1661 | */ |
| 1662 | key.offset = (u64)-1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1663 | } |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1664 | ret = 0; |
| 1665 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1666 | btrfs_release_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1667 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | |
| 1671 | /* |
| 1672 | * record a given inode in the fixup dir so we can check its link |
| 1673 | * count when replay is done. The link count is incremented here |
| 1674 | * so the inode won't go away until we check it |
| 1675 | */ |
| 1676 | static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, |
| 1677 | struct btrfs_root *root, |
| 1678 | struct btrfs_path *path, |
| 1679 | u64 objectid) |
| 1680 | { |
| 1681 | struct btrfs_key key; |
| 1682 | int ret = 0; |
| 1683 | struct inode *inode; |
| 1684 | |
| 1685 | inode = read_one_inode(root, objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1686 | if (!inode) |
| 1687 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1688 | |
| 1689 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1690 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1691 | key.offset = objectid; |
| 1692 | |
| 1693 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
| 1694 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1695 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1696 | if (ret == 0) { |
Josef Bacik | 9bf7a48 | 2013-03-01 13:35:47 -0500 | [diff] [blame] | 1697 | if (!inode->i_nlink) |
| 1698 | set_nlink(inode, 1); |
| 1699 | else |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 1700 | inc_nlink(inode); |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 1701 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1702 | } else if (ret == -EEXIST) { |
| 1703 | ret = 0; |
| 1704 | } else { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1705 | BUG(); /* Logic Error */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1706 | } |
| 1707 | iput(inode); |
| 1708 | |
| 1709 | return ret; |
| 1710 | } |
| 1711 | |
| 1712 | /* |
| 1713 | * when replaying the log for a directory, we only insert names |
| 1714 | * for inodes that actually exist. This means an fsync on a directory |
| 1715 | * does not implicitly fsync all the new files in it |
| 1716 | */ |
| 1717 | static noinline int insert_one_name(struct btrfs_trans_handle *trans, |
| 1718 | struct btrfs_root *root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1719 | u64 dirid, u64 index, |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 1720 | char *name, int name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1721 | struct btrfs_key *location) |
| 1722 | { |
| 1723 | struct inode *inode; |
| 1724 | struct inode *dir; |
| 1725 | int ret; |
| 1726 | |
| 1727 | inode = read_one_inode(root, location->objectid); |
| 1728 | if (!inode) |
| 1729 | return -ENOENT; |
| 1730 | |
| 1731 | dir = read_one_inode(root, dirid); |
| 1732 | if (!dir) { |
| 1733 | iput(inode); |
| 1734 | return -EIO; |
| 1735 | } |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1736 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 1737 | ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name, |
| 1738 | name_len, 1, index); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1739 | |
| 1740 | /* FIXME, put inode into FIXUP list */ |
| 1741 | |
| 1742 | iput(inode); |
| 1743 | iput(dir); |
| 1744 | return ret; |
| 1745 | } |
| 1746 | |
| 1747 | /* |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1748 | * Return true if an inode reference exists in the log for the given name, |
| 1749 | * inode and parent inode. |
| 1750 | */ |
| 1751 | static bool name_in_log_ref(struct btrfs_root *log_root, |
| 1752 | const char *name, const int name_len, |
| 1753 | const u64 dirid, const u64 ino) |
| 1754 | { |
| 1755 | struct btrfs_key search_key; |
| 1756 | |
| 1757 | search_key.objectid = ino; |
| 1758 | search_key.type = BTRFS_INODE_REF_KEY; |
| 1759 | search_key.offset = dirid; |
| 1760 | if (backref_in_log(log_root, &search_key, dirid, name, name_len)) |
| 1761 | return true; |
| 1762 | |
| 1763 | search_key.type = BTRFS_INODE_EXTREF_KEY; |
| 1764 | search_key.offset = btrfs_extref_hash(dirid, name, name_len); |
| 1765 | if (backref_in_log(log_root, &search_key, dirid, name, name_len)) |
| 1766 | return true; |
| 1767 | |
| 1768 | return false; |
| 1769 | } |
| 1770 | |
| 1771 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1772 | * take a single entry in a log directory item and replay it into |
| 1773 | * the subvolume. |
| 1774 | * |
| 1775 | * if a conflicting item exists in the subdirectory already, |
| 1776 | * the inode it points to is unlinked and put into the link count |
| 1777 | * fix up tree. |
| 1778 | * |
| 1779 | * If a name from the log points to a file or directory that does |
| 1780 | * not exist in the FS, it is skipped. fsyncs on directories |
| 1781 | * do not force down inodes inside that directory, just changes to the |
| 1782 | * names or unlinks in a directory. |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1783 | * |
| 1784 | * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a |
| 1785 | * non-existing inode) and 1 if the name was replayed. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1786 | */ |
| 1787 | static noinline int replay_one_name(struct btrfs_trans_handle *trans, |
| 1788 | struct btrfs_root *root, |
| 1789 | struct btrfs_path *path, |
| 1790 | struct extent_buffer *eb, |
| 1791 | struct btrfs_dir_item *di, |
| 1792 | struct btrfs_key *key) |
| 1793 | { |
| 1794 | char *name; |
| 1795 | int name_len; |
| 1796 | struct btrfs_dir_item *dst_di; |
| 1797 | struct btrfs_key found_key; |
| 1798 | struct btrfs_key log_key; |
| 1799 | struct inode *dir; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1800 | u8 log_type; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1801 | int exists; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1802 | int ret = 0; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1803 | bool update_size = (key->type == BTRFS_DIR_INDEX_KEY); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1804 | bool name_added = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1805 | |
| 1806 | dir = read_one_inode(root, key->objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1807 | if (!dir) |
| 1808 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1809 | |
| 1810 | name_len = btrfs_dir_name_len(eb, di); |
| 1811 | name = kmalloc(name_len, GFP_NOFS); |
Filipe David Borba Manana | 2bac325 | 2013-08-04 19:58:57 +0100 | [diff] [blame] | 1812 | if (!name) { |
| 1813 | ret = -ENOMEM; |
| 1814 | goto out; |
| 1815 | } |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 1816 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1817 | log_type = btrfs_dir_type(eb, di); |
| 1818 | read_extent_buffer(eb, name, (unsigned long)(di + 1), |
| 1819 | name_len); |
| 1820 | |
| 1821 | btrfs_dir_item_key_to_cpu(eb, di, &log_key); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1822 | exists = btrfs_lookup_inode(trans, root, path, &log_key, 0); |
| 1823 | if (exists == 0) |
| 1824 | exists = 1; |
| 1825 | else |
| 1826 | exists = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1827 | btrfs_release_path(path); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1828 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1829 | if (key->type == BTRFS_DIR_ITEM_KEY) { |
| 1830 | dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, |
| 1831 | name, name_len, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1832 | } else if (key->type == BTRFS_DIR_INDEX_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1833 | dst_di = btrfs_lookup_dir_index_item(trans, root, path, |
| 1834 | key->objectid, |
| 1835 | key->offset, name, |
| 1836 | name_len, 1); |
| 1837 | } else { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1838 | /* Corruption */ |
| 1839 | ret = -EINVAL; |
| 1840 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1841 | } |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 1842 | if (IS_ERR_OR_NULL(dst_di)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1843 | /* we need a sequence number to insert, so we only |
| 1844 | * do inserts for the BTRFS_DIR_INDEX_KEY types |
| 1845 | */ |
| 1846 | if (key->type != BTRFS_DIR_INDEX_KEY) |
| 1847 | goto out; |
| 1848 | goto insert; |
| 1849 | } |
| 1850 | |
| 1851 | btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); |
| 1852 | /* the existing item matches the logged item */ |
| 1853 | if (found_key.objectid == log_key.objectid && |
| 1854 | found_key.type == log_key.type && |
| 1855 | found_key.offset == log_key.offset && |
| 1856 | btrfs_dir_type(path->nodes[0], dst_di) == log_type) { |
Filipe Manana | a2cc11d | 2014-09-08 22:53:18 +0100 | [diff] [blame] | 1857 | update_size = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1858 | goto out; |
| 1859 | } |
| 1860 | |
| 1861 | /* |
| 1862 | * don't drop the conflicting directory entry if the inode |
| 1863 | * for the new entry doesn't exist |
| 1864 | */ |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1865 | if (!exists) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1866 | goto out; |
| 1867 | |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 1868 | 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] | 1869 | if (ret) |
| 1870 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1871 | |
| 1872 | if (key->type == BTRFS_DIR_INDEX_KEY) |
| 1873 | goto insert; |
| 1874 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1875 | btrfs_release_path(path); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1876 | if (!ret && update_size) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 1877 | 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] | 1878 | ret = btrfs_update_inode(trans, root, dir); |
| 1879 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1880 | kfree(name); |
| 1881 | iput(dir); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1882 | if (!ret && name_added) |
| 1883 | ret = 1; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1884 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1885 | |
| 1886 | insert: |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1887 | if (name_in_log_ref(root->log_root, name, name_len, |
| 1888 | key->objectid, log_key.objectid)) { |
| 1889 | /* The dentry will be added later. */ |
| 1890 | ret = 0; |
| 1891 | update_size = false; |
| 1892 | goto out; |
| 1893 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1894 | btrfs_release_path(path); |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 1895 | ret = insert_one_name(trans, root, key->objectid, key->offset, |
| 1896 | name, name_len, &log_key); |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1897 | if (ret && ret != -ENOENT && ret != -EEXIST) |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1898 | goto out; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1899 | if (!ret) |
| 1900 | name_added = true; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1901 | update_size = false; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1902 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1903 | goto out; |
| 1904 | } |
| 1905 | |
| 1906 | /* |
| 1907 | * find all the names in a directory item and reconcile them into |
| 1908 | * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than |
| 1909 | * one name in a directory item, but the same code gets used for |
| 1910 | * both directory index types |
| 1911 | */ |
| 1912 | static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, |
| 1913 | struct btrfs_root *root, |
| 1914 | struct btrfs_path *path, |
| 1915 | struct extent_buffer *eb, int slot, |
| 1916 | struct btrfs_key *key) |
| 1917 | { |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1918 | int ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1919 | u32 item_size = btrfs_item_size_nr(eb, slot); |
| 1920 | struct btrfs_dir_item *di; |
| 1921 | int name_len; |
| 1922 | unsigned long ptr; |
| 1923 | unsigned long ptr_end; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1924 | struct btrfs_path *fixup_path = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1925 | |
| 1926 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 1927 | ptr_end = ptr + item_size; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1928 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1929 | di = (struct btrfs_dir_item *)ptr; |
| 1930 | name_len = btrfs_dir_name_len(eb, di); |
| 1931 | ret = replay_one_name(trans, root, path, eb, di, key); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1932 | if (ret < 0) |
| 1933 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1934 | ptr = (unsigned long)(di + 1); |
| 1935 | ptr += name_len; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1936 | |
| 1937 | /* |
| 1938 | * If this entry refers to a non-directory (directories can not |
| 1939 | * have a link count > 1) and it was added in the transaction |
| 1940 | * that was not committed, make sure we fixup the link count of |
| 1941 | * the inode it the entry points to. Otherwise something like |
| 1942 | * the following would result in a directory pointing to an |
| 1943 | * inode with a wrong link that does not account for this dir |
| 1944 | * entry: |
| 1945 | * |
| 1946 | * mkdir testdir |
| 1947 | * touch testdir/foo |
| 1948 | * touch testdir/bar |
| 1949 | * sync |
| 1950 | * |
| 1951 | * ln testdir/bar testdir/bar_link |
| 1952 | * ln testdir/foo testdir/foo_link |
| 1953 | * xfs_io -c "fsync" testdir/bar |
| 1954 | * |
| 1955 | * <power failure> |
| 1956 | * |
| 1957 | * mount fs, log replay happens |
| 1958 | * |
| 1959 | * File foo would remain with a link count of 1 when it has two |
| 1960 | * entries pointing to it in the directory testdir. This would |
| 1961 | * make it impossible to ever delete the parent directory has |
| 1962 | * it would result in stale dentries that can never be deleted. |
| 1963 | */ |
| 1964 | if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) { |
| 1965 | struct btrfs_key di_key; |
| 1966 | |
| 1967 | if (!fixup_path) { |
| 1968 | fixup_path = btrfs_alloc_path(); |
| 1969 | if (!fixup_path) { |
| 1970 | ret = -ENOMEM; |
| 1971 | break; |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 1976 | ret = link_to_fixup_dir(trans, root, fixup_path, |
| 1977 | di_key.objectid); |
| 1978 | if (ret) |
| 1979 | break; |
| 1980 | } |
| 1981 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1982 | } |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1983 | btrfs_free_path(fixup_path); |
| 1984 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * directory replay has two parts. There are the standard directory |
| 1989 | * items in the log copied from the subvolume, and range items |
| 1990 | * created in the log while the subvolume was logged. |
| 1991 | * |
| 1992 | * The range items tell us which parts of the key space the log |
| 1993 | * is authoritative for. During replay, if a key in the subvolume |
| 1994 | * directory is in a logged range item, but not actually in the log |
| 1995 | * that means it was deleted from the directory before the fsync |
| 1996 | * and should be removed. |
| 1997 | */ |
| 1998 | static noinline int find_dir_range(struct btrfs_root *root, |
| 1999 | struct btrfs_path *path, |
| 2000 | u64 dirid, int key_type, |
| 2001 | u64 *start_ret, u64 *end_ret) |
| 2002 | { |
| 2003 | struct btrfs_key key; |
| 2004 | u64 found_end; |
| 2005 | struct btrfs_dir_log_item *item; |
| 2006 | int ret; |
| 2007 | int nritems; |
| 2008 | |
| 2009 | if (*start_ret == (u64)-1) |
| 2010 | return 1; |
| 2011 | |
| 2012 | key.objectid = dirid; |
| 2013 | key.type = key_type; |
| 2014 | key.offset = *start_ret; |
| 2015 | |
| 2016 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2017 | if (ret < 0) |
| 2018 | goto out; |
| 2019 | if (ret > 0) { |
| 2020 | if (path->slots[0] == 0) |
| 2021 | goto out; |
| 2022 | path->slots[0]--; |
| 2023 | } |
| 2024 | if (ret != 0) |
| 2025 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 2026 | |
| 2027 | if (key.type != key_type || key.objectid != dirid) { |
| 2028 | ret = 1; |
| 2029 | goto next; |
| 2030 | } |
| 2031 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2032 | struct btrfs_dir_log_item); |
| 2033 | found_end = btrfs_dir_log_end(path->nodes[0], item); |
| 2034 | |
| 2035 | if (*start_ret >= key.offset && *start_ret <= found_end) { |
| 2036 | ret = 0; |
| 2037 | *start_ret = key.offset; |
| 2038 | *end_ret = found_end; |
| 2039 | goto out; |
| 2040 | } |
| 2041 | ret = 1; |
| 2042 | next: |
| 2043 | /* check the next slot in the tree to see if it is a valid item */ |
| 2044 | nritems = btrfs_header_nritems(path->nodes[0]); |
Robbie Ko | 2a7bf53 | 2016-10-07 17:30:47 +0800 | [diff] [blame] | 2045 | path->slots[0]++; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2046 | if (path->slots[0] >= nritems) { |
| 2047 | ret = btrfs_next_leaf(root, path); |
| 2048 | if (ret) |
| 2049 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 2053 | |
| 2054 | if (key.type != key_type || key.objectid != dirid) { |
| 2055 | ret = 1; |
| 2056 | goto out; |
| 2057 | } |
| 2058 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2059 | struct btrfs_dir_log_item); |
| 2060 | found_end = btrfs_dir_log_end(path->nodes[0], item); |
| 2061 | *start_ret = key.offset; |
| 2062 | *end_ret = found_end; |
| 2063 | ret = 0; |
| 2064 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2065 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2066 | return ret; |
| 2067 | } |
| 2068 | |
| 2069 | /* |
| 2070 | * this looks for a given directory item in the log. If the directory |
| 2071 | * item is not in the log, the item is removed and the inode it points |
| 2072 | * to is unlinked |
| 2073 | */ |
| 2074 | static noinline int check_item_in_log(struct btrfs_trans_handle *trans, |
| 2075 | struct btrfs_root *root, |
| 2076 | struct btrfs_root *log, |
| 2077 | struct btrfs_path *path, |
| 2078 | struct btrfs_path *log_path, |
| 2079 | struct inode *dir, |
| 2080 | struct btrfs_key *dir_key) |
| 2081 | { |
| 2082 | int ret; |
| 2083 | struct extent_buffer *eb; |
| 2084 | int slot; |
| 2085 | u32 item_size; |
| 2086 | struct btrfs_dir_item *di; |
| 2087 | struct btrfs_dir_item *log_di; |
| 2088 | int name_len; |
| 2089 | unsigned long ptr; |
| 2090 | unsigned long ptr_end; |
| 2091 | char *name; |
| 2092 | struct inode *inode; |
| 2093 | struct btrfs_key location; |
| 2094 | |
| 2095 | again: |
| 2096 | eb = path->nodes[0]; |
| 2097 | slot = path->slots[0]; |
| 2098 | item_size = btrfs_item_size_nr(eb, slot); |
| 2099 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 2100 | ptr_end = ptr + item_size; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2101 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2102 | di = (struct btrfs_dir_item *)ptr; |
| 2103 | name_len = btrfs_dir_name_len(eb, di); |
| 2104 | name = kmalloc(name_len, GFP_NOFS); |
| 2105 | if (!name) { |
| 2106 | ret = -ENOMEM; |
| 2107 | goto out; |
| 2108 | } |
| 2109 | read_extent_buffer(eb, name, (unsigned long)(di + 1), |
| 2110 | name_len); |
| 2111 | log_di = NULL; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2112 | if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2113 | log_di = btrfs_lookup_dir_item(trans, log, log_path, |
| 2114 | dir_key->objectid, |
| 2115 | name, name_len, 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2116 | } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2117 | log_di = btrfs_lookup_dir_index_item(trans, log, |
| 2118 | log_path, |
| 2119 | dir_key->objectid, |
| 2120 | dir_key->offset, |
| 2121 | name, name_len, 0); |
| 2122 | } |
Filipe David Borba Manana | 269d040 | 2013-10-28 17:39:21 +0000 | [diff] [blame] | 2123 | if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2124 | btrfs_dir_item_key_to_cpu(eb, di, &location); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2125 | btrfs_release_path(path); |
| 2126 | btrfs_release_path(log_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2127 | inode = read_one_inode(root, location.objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 2128 | if (!inode) { |
| 2129 | kfree(name); |
| 2130 | return -EIO; |
| 2131 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2132 | |
| 2133 | ret = link_to_fixup_dir(trans, root, |
| 2134 | path, location.objectid); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2135 | if (ret) { |
| 2136 | kfree(name); |
| 2137 | iput(inode); |
| 2138 | goto out; |
| 2139 | } |
| 2140 | |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 2141 | inc_nlink(inode); |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 2142 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 2143 | BTRFS_I(inode), name, name_len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2144 | if (!ret) |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 2145 | ret = btrfs_run_delayed_items(trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2146 | kfree(name); |
| 2147 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2148 | if (ret) |
| 2149 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2150 | |
| 2151 | /* there might still be more names under this key |
| 2152 | * check and repeat if required |
| 2153 | */ |
| 2154 | ret = btrfs_search_slot(NULL, root, dir_key, path, |
| 2155 | 0, 0); |
| 2156 | if (ret == 0) |
| 2157 | goto again; |
| 2158 | ret = 0; |
| 2159 | goto out; |
Filipe David Borba Manana | 269d040 | 2013-10-28 17:39:21 +0000 | [diff] [blame] | 2160 | } else if (IS_ERR(log_di)) { |
| 2161 | kfree(name); |
| 2162 | return PTR_ERR(log_di); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2163 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2164 | btrfs_release_path(log_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2165 | kfree(name); |
| 2166 | |
| 2167 | ptr = (unsigned long)(di + 1); |
| 2168 | ptr += name_len; |
| 2169 | } |
| 2170 | ret = 0; |
| 2171 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2172 | btrfs_release_path(path); |
| 2173 | btrfs_release_path(log_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2174 | return ret; |
| 2175 | } |
| 2176 | |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2177 | static int replay_xattr_deletes(struct btrfs_trans_handle *trans, |
| 2178 | struct btrfs_root *root, |
| 2179 | struct btrfs_root *log, |
| 2180 | struct btrfs_path *path, |
| 2181 | const u64 ino) |
| 2182 | { |
| 2183 | struct btrfs_key search_key; |
| 2184 | struct btrfs_path *log_path; |
| 2185 | int i; |
| 2186 | int nritems; |
| 2187 | int ret; |
| 2188 | |
| 2189 | log_path = btrfs_alloc_path(); |
| 2190 | if (!log_path) |
| 2191 | return -ENOMEM; |
| 2192 | |
| 2193 | search_key.objectid = ino; |
| 2194 | search_key.type = BTRFS_XATTR_ITEM_KEY; |
| 2195 | search_key.offset = 0; |
| 2196 | again: |
| 2197 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
| 2198 | if (ret < 0) |
| 2199 | goto out; |
| 2200 | process_leaf: |
| 2201 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2202 | for (i = path->slots[0]; i < nritems; i++) { |
| 2203 | struct btrfs_key key; |
| 2204 | struct btrfs_dir_item *di; |
| 2205 | struct btrfs_dir_item *log_di; |
| 2206 | u32 total_size; |
| 2207 | u32 cur; |
| 2208 | |
| 2209 | btrfs_item_key_to_cpu(path->nodes[0], &key, i); |
| 2210 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) { |
| 2211 | ret = 0; |
| 2212 | goto out; |
| 2213 | } |
| 2214 | |
| 2215 | di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item); |
| 2216 | total_size = btrfs_item_size_nr(path->nodes[0], i); |
| 2217 | cur = 0; |
| 2218 | while (cur < total_size) { |
| 2219 | u16 name_len = btrfs_dir_name_len(path->nodes[0], di); |
| 2220 | u16 data_len = btrfs_dir_data_len(path->nodes[0], di); |
| 2221 | u32 this_len = sizeof(*di) + name_len + data_len; |
| 2222 | char *name; |
| 2223 | |
| 2224 | name = kmalloc(name_len, GFP_NOFS); |
| 2225 | if (!name) { |
| 2226 | ret = -ENOMEM; |
| 2227 | goto out; |
| 2228 | } |
| 2229 | read_extent_buffer(path->nodes[0], name, |
| 2230 | (unsigned long)(di + 1), name_len); |
| 2231 | |
| 2232 | log_di = btrfs_lookup_xattr(NULL, log, log_path, ino, |
| 2233 | name, name_len, 0); |
| 2234 | btrfs_release_path(log_path); |
| 2235 | if (!log_di) { |
| 2236 | /* Doesn't exist in log tree, so delete it. */ |
| 2237 | btrfs_release_path(path); |
| 2238 | di = btrfs_lookup_xattr(trans, root, path, ino, |
| 2239 | name, name_len, -1); |
| 2240 | kfree(name); |
| 2241 | if (IS_ERR(di)) { |
| 2242 | ret = PTR_ERR(di); |
| 2243 | goto out; |
| 2244 | } |
| 2245 | ASSERT(di); |
| 2246 | ret = btrfs_delete_one_dir_name(trans, root, |
| 2247 | path, di); |
| 2248 | if (ret) |
| 2249 | goto out; |
| 2250 | btrfs_release_path(path); |
| 2251 | search_key = key; |
| 2252 | goto again; |
| 2253 | } |
| 2254 | kfree(name); |
| 2255 | if (IS_ERR(log_di)) { |
| 2256 | ret = PTR_ERR(log_di); |
| 2257 | goto out; |
| 2258 | } |
| 2259 | cur += this_len; |
| 2260 | di = (struct btrfs_dir_item *)((char *)di + this_len); |
| 2261 | } |
| 2262 | } |
| 2263 | ret = btrfs_next_leaf(root, path); |
| 2264 | if (ret > 0) |
| 2265 | ret = 0; |
| 2266 | else if (ret == 0) |
| 2267 | goto process_leaf; |
| 2268 | out: |
| 2269 | btrfs_free_path(log_path); |
| 2270 | btrfs_release_path(path); |
| 2271 | return ret; |
| 2272 | } |
| 2273 | |
| 2274 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2275 | /* |
| 2276 | * deletion replay happens before we copy any new directory items |
| 2277 | * out of the log or out of backreferences from inodes. It |
| 2278 | * scans the log to find ranges of keys that log is authoritative for, |
| 2279 | * and then scans the directory to find items in those ranges that are |
| 2280 | * not present in the log. |
| 2281 | * |
| 2282 | * Anything we don't find in the log is unlinked and removed from the |
| 2283 | * directory. |
| 2284 | */ |
| 2285 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, |
| 2286 | struct btrfs_root *root, |
| 2287 | struct btrfs_root *log, |
| 2288 | struct btrfs_path *path, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2289 | u64 dirid, int del_all) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2290 | { |
| 2291 | u64 range_start; |
| 2292 | u64 range_end; |
| 2293 | int key_type = BTRFS_DIR_LOG_ITEM_KEY; |
| 2294 | int ret = 0; |
| 2295 | struct btrfs_key dir_key; |
| 2296 | struct btrfs_key found_key; |
| 2297 | struct btrfs_path *log_path; |
| 2298 | struct inode *dir; |
| 2299 | |
| 2300 | dir_key.objectid = dirid; |
| 2301 | dir_key.type = BTRFS_DIR_ITEM_KEY; |
| 2302 | log_path = btrfs_alloc_path(); |
| 2303 | if (!log_path) |
| 2304 | return -ENOMEM; |
| 2305 | |
| 2306 | dir = read_one_inode(root, dirid); |
| 2307 | /* it isn't an error if the inode isn't there, that can happen |
| 2308 | * because we replay the deletes before we copy in the inode item |
| 2309 | * from the log |
| 2310 | */ |
| 2311 | if (!dir) { |
| 2312 | btrfs_free_path(log_path); |
| 2313 | return 0; |
| 2314 | } |
| 2315 | again: |
| 2316 | range_start = 0; |
| 2317 | range_end = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2318 | while (1) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2319 | if (del_all) |
| 2320 | range_end = (u64)-1; |
| 2321 | else { |
| 2322 | ret = find_dir_range(log, path, dirid, key_type, |
| 2323 | &range_start, &range_end); |
| 2324 | if (ret != 0) |
| 2325 | break; |
| 2326 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2327 | |
| 2328 | dir_key.offset = range_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2329 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2330 | int nritems; |
| 2331 | ret = btrfs_search_slot(NULL, root, &dir_key, path, |
| 2332 | 0, 0); |
| 2333 | if (ret < 0) |
| 2334 | goto out; |
| 2335 | |
| 2336 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2337 | if (path->slots[0] >= nritems) { |
| 2338 | ret = btrfs_next_leaf(root, path); |
Liu Bo | b98def7 | 2018-04-03 01:59:48 +0800 | [diff] [blame] | 2339 | if (ret == 1) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2340 | break; |
Liu Bo | b98def7 | 2018-04-03 01:59:48 +0800 | [diff] [blame] | 2341 | else if (ret < 0) |
| 2342 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2343 | } |
| 2344 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2345 | path->slots[0]); |
| 2346 | if (found_key.objectid != dirid || |
| 2347 | found_key.type != dir_key.type) |
| 2348 | goto next_type; |
| 2349 | |
| 2350 | if (found_key.offset > range_end) |
| 2351 | break; |
| 2352 | |
| 2353 | ret = check_item_in_log(trans, root, log, path, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2354 | log_path, dir, |
| 2355 | &found_key); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2356 | if (ret) |
| 2357 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2358 | if (found_key.offset == (u64)-1) |
| 2359 | break; |
| 2360 | dir_key.offset = found_key.offset + 1; |
| 2361 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2362 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2363 | if (range_end == (u64)-1) |
| 2364 | break; |
| 2365 | range_start = range_end + 1; |
| 2366 | } |
| 2367 | |
| 2368 | next_type: |
| 2369 | ret = 0; |
| 2370 | if (key_type == BTRFS_DIR_LOG_ITEM_KEY) { |
| 2371 | key_type = BTRFS_DIR_LOG_INDEX_KEY; |
| 2372 | dir_key.type = BTRFS_DIR_INDEX_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2373 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2374 | goto again; |
| 2375 | } |
| 2376 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2377 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2378 | btrfs_free_path(log_path); |
| 2379 | iput(dir); |
| 2380 | return ret; |
| 2381 | } |
| 2382 | |
| 2383 | /* |
| 2384 | * the process_func used to replay items from the log tree. This |
| 2385 | * gets called in two different stages. The first stage just looks |
| 2386 | * for inodes and makes sure they are all copied into the subvolume. |
| 2387 | * |
| 2388 | * The second stage copies all the other item types from the log into |
| 2389 | * the subvolume. The two stage approach is slower, but gets rid of |
| 2390 | * lots of complexity around inodes referencing other inodes that exist |
| 2391 | * only in the log (references come from either directory items or inode |
| 2392 | * back refs). |
| 2393 | */ |
| 2394 | 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] | 2395 | struct walk_control *wc, u64 gen, int level) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2396 | { |
| 2397 | int nritems; |
| 2398 | struct btrfs_path *path; |
| 2399 | struct btrfs_root *root = wc->replay_dest; |
| 2400 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2401 | int i; |
| 2402 | int ret; |
| 2403 | |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2404 | ret = btrfs_read_buffer(eb, gen, level, NULL); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2405 | if (ret) |
| 2406 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2407 | |
| 2408 | level = btrfs_header_level(eb); |
| 2409 | |
| 2410 | if (level != 0) |
| 2411 | return 0; |
| 2412 | |
| 2413 | path = btrfs_alloc_path(); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2414 | if (!path) |
| 2415 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2416 | |
| 2417 | nritems = btrfs_header_nritems(eb); |
| 2418 | for (i = 0; i < nritems; i++) { |
| 2419 | btrfs_item_key_to_cpu(eb, &key, i); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2420 | |
| 2421 | /* inode keys are done during the first stage */ |
| 2422 | if (key.type == BTRFS_INODE_ITEM_KEY && |
| 2423 | wc->stage == LOG_WALK_REPLAY_INODES) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2424 | struct btrfs_inode_item *inode_item; |
| 2425 | u32 mode; |
| 2426 | |
| 2427 | inode_item = btrfs_item_ptr(eb, i, |
| 2428 | struct btrfs_inode_item); |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2429 | ret = replay_xattr_deletes(wc->trans, root, log, |
| 2430 | path, key.objectid); |
| 2431 | if (ret) |
| 2432 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2433 | mode = btrfs_inode_mode(eb, inode_item); |
| 2434 | if (S_ISDIR(mode)) { |
| 2435 | ret = replay_dir_deletes(wc->trans, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2436 | root, log, path, key.objectid, 0); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2437 | if (ret) |
| 2438 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2439 | } |
| 2440 | ret = overwrite_item(wc->trans, root, path, |
| 2441 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2442 | if (ret) |
| 2443 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2444 | |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2445 | /* |
| 2446 | * Before replaying extents, truncate the inode to its |
| 2447 | * size. We need to do it now and not after log replay |
| 2448 | * because before an fsync we can have prealloc extents |
| 2449 | * added beyond the inode's i_size. If we did it after, |
| 2450 | * through orphan cleanup for example, we would drop |
| 2451 | * those prealloc extents just after replaying them. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2452 | */ |
| 2453 | if (S_ISREG(mode)) { |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2454 | struct inode *inode; |
| 2455 | u64 from; |
| 2456 | |
| 2457 | inode = read_one_inode(root, key.objectid); |
| 2458 | if (!inode) { |
| 2459 | ret = -EIO; |
| 2460 | break; |
| 2461 | } |
| 2462 | from = ALIGN(i_size_read(inode), |
| 2463 | root->fs_info->sectorsize); |
| 2464 | ret = btrfs_drop_extents(wc->trans, root, inode, |
| 2465 | from, (u64)-1, 1); |
| 2466 | /* |
| 2467 | * If the nlink count is zero here, the iput |
| 2468 | * will free the inode. We bump it to make |
| 2469 | * sure it doesn't get freed until the link |
| 2470 | * count fixup is done. |
| 2471 | */ |
| 2472 | if (!ret) { |
| 2473 | if (inode->i_nlink == 0) |
| 2474 | inc_nlink(inode); |
| 2475 | /* Update link count and nbytes. */ |
| 2476 | ret = btrfs_update_inode(wc->trans, |
| 2477 | root, inode); |
| 2478 | } |
| 2479 | iput(inode); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2480 | if (ret) |
| 2481 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2482 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2483 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2484 | ret = link_to_fixup_dir(wc->trans, root, |
| 2485 | path, key.objectid); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2486 | if (ret) |
| 2487 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2488 | } |
Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2489 | |
| 2490 | if (key.type == BTRFS_DIR_INDEX_KEY && |
| 2491 | wc->stage == LOG_WALK_REPLAY_DIR_INDEX) { |
| 2492 | ret = replay_one_dir_item(wc->trans, root, path, |
| 2493 | eb, i, &key); |
| 2494 | if (ret) |
| 2495 | break; |
| 2496 | } |
| 2497 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2498 | if (wc->stage < LOG_WALK_REPLAY_ALL) |
| 2499 | continue; |
| 2500 | |
| 2501 | /* these keys are simply copied */ |
| 2502 | if (key.type == BTRFS_XATTR_ITEM_KEY) { |
| 2503 | ret = overwrite_item(wc->trans, root, path, |
| 2504 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2505 | if (ret) |
| 2506 | break; |
Liu Bo | 2da1c66 | 2013-05-26 13:50:29 +0000 | [diff] [blame] | 2507 | } else if (key.type == BTRFS_INODE_REF_KEY || |
| 2508 | key.type == BTRFS_INODE_EXTREF_KEY) { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 2509 | ret = add_inode_ref(wc->trans, root, log, path, |
| 2510 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2511 | if (ret && ret != -ENOENT) |
| 2512 | break; |
| 2513 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2514 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 2515 | ret = replay_one_extent(wc->trans, root, path, |
| 2516 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2517 | if (ret) |
| 2518 | break; |
Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2519 | } else if (key.type == BTRFS_DIR_ITEM_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2520 | ret = replay_one_dir_item(wc->trans, root, path, |
| 2521 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2522 | if (ret) |
| 2523 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2524 | } |
| 2525 | } |
| 2526 | btrfs_free_path(path); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2527 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2528 | } |
| 2529 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2530 | static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2531 | struct btrfs_root *root, |
| 2532 | struct btrfs_path *path, int *level, |
| 2533 | struct walk_control *wc) |
| 2534 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2535 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2536 | u64 root_owner; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2537 | u64 bytenr; |
| 2538 | u64 ptr_gen; |
| 2539 | struct extent_buffer *next; |
| 2540 | struct extent_buffer *cur; |
| 2541 | struct extent_buffer *parent; |
| 2542 | u32 blocksize; |
| 2543 | int ret = 0; |
| 2544 | |
| 2545 | WARN_ON(*level < 0); |
| 2546 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 2547 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2548 | while (*level > 0) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2549 | struct btrfs_key first_key; |
| 2550 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2551 | WARN_ON(*level < 0); |
| 2552 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 2553 | cur = path->nodes[*level]; |
| 2554 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2555 | WARN_ON(btrfs_header_level(cur) != *level); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2556 | |
| 2557 | if (path->slots[*level] >= |
| 2558 | btrfs_header_nritems(cur)) |
| 2559 | break; |
| 2560 | |
| 2561 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 2562 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2563 | btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2564 | blocksize = fs_info->nodesize; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2565 | |
| 2566 | parent = path->nodes[*level]; |
| 2567 | root_owner = btrfs_header_owner(parent); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2568 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2569 | next = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 2570 | if (IS_ERR(next)) |
| 2571 | return PTR_ERR(next); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2572 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2573 | if (*level == 1) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2574 | ret = wc->process_func(root, next, wc, ptr_gen, |
| 2575 | *level - 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2576 | if (ret) { |
| 2577 | free_extent_buffer(next); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2578 | return ret; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2579 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2580 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2581 | path->slots[*level]++; |
| 2582 | if (wc->free) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2583 | ret = btrfs_read_buffer(next, ptr_gen, |
| 2584 | *level - 1, &first_key); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2585 | if (ret) { |
| 2586 | free_extent_buffer(next); |
| 2587 | return ret; |
| 2588 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2589 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2590 | if (trans) { |
| 2591 | btrfs_tree_lock(next); |
| 2592 | btrfs_set_lock_blocking(next); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2593 | clean_tree_block(fs_info, next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2594 | btrfs_wait_tree_block_writeback(next); |
| 2595 | btrfs_tree_unlock(next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2596 | } else { |
| 2597 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) |
| 2598 | clear_extent_buffer_dirty(next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2599 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2600 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2601 | WARN_ON(root_owner != |
| 2602 | BTRFS_TREE_LOG_OBJECTID); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2603 | ret = btrfs_free_and_pin_reserved_extent( |
| 2604 | fs_info, bytenr, |
| 2605 | blocksize); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2606 | if (ret) { |
| 2607 | free_extent_buffer(next); |
| 2608 | return ret; |
| 2609 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2610 | } |
| 2611 | free_extent_buffer(next); |
| 2612 | continue; |
| 2613 | } |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2614 | ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2615 | if (ret) { |
| 2616 | free_extent_buffer(next); |
| 2617 | return ret; |
| 2618 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2619 | |
| 2620 | WARN_ON(*level <= 0); |
| 2621 | if (path->nodes[*level-1]) |
| 2622 | free_extent_buffer(path->nodes[*level-1]); |
| 2623 | path->nodes[*level-1] = next; |
| 2624 | *level = btrfs_header_level(next); |
| 2625 | path->slots[*level] = 0; |
| 2626 | cond_resched(); |
| 2627 | } |
| 2628 | WARN_ON(*level < 0); |
| 2629 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 2630 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2631 | path->slots[*level] = btrfs_header_nritems(path->nodes[*level]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2632 | |
| 2633 | cond_resched(); |
| 2634 | return 0; |
| 2635 | } |
| 2636 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2637 | static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2638 | struct btrfs_root *root, |
| 2639 | struct btrfs_path *path, int *level, |
| 2640 | struct walk_control *wc) |
| 2641 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2642 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2643 | u64 root_owner; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2644 | int i; |
| 2645 | int slot; |
| 2646 | int ret; |
| 2647 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2648 | for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2649 | slot = path->slots[i]; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2650 | if (slot + 1 < btrfs_header_nritems(path->nodes[i])) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2651 | path->slots[i]++; |
| 2652 | *level = i; |
| 2653 | WARN_ON(*level == 0); |
| 2654 | return 0; |
| 2655 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2656 | struct extent_buffer *parent; |
| 2657 | if (path->nodes[*level] == root->node) |
| 2658 | parent = path->nodes[*level]; |
| 2659 | else |
| 2660 | parent = path->nodes[*level + 1]; |
| 2661 | |
| 2662 | root_owner = btrfs_header_owner(parent); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2663 | ret = wc->process_func(root, path->nodes[*level], wc, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2664 | btrfs_header_generation(path->nodes[*level]), |
| 2665 | *level); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2666 | if (ret) |
| 2667 | return ret; |
| 2668 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2669 | if (wc->free) { |
| 2670 | struct extent_buffer *next; |
| 2671 | |
| 2672 | next = path->nodes[*level]; |
| 2673 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2674 | if (trans) { |
| 2675 | btrfs_tree_lock(next); |
| 2676 | btrfs_set_lock_blocking(next); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2677 | clean_tree_block(fs_info, next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2678 | btrfs_wait_tree_block_writeback(next); |
| 2679 | btrfs_tree_unlock(next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2680 | } else { |
| 2681 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) |
| 2682 | clear_extent_buffer_dirty(next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2683 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2684 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2685 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2686 | ret = btrfs_free_and_pin_reserved_extent( |
| 2687 | fs_info, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2688 | path->nodes[*level]->start, |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2689 | path->nodes[*level]->len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2690 | if (ret) |
| 2691 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2692 | } |
| 2693 | free_extent_buffer(path->nodes[*level]); |
| 2694 | path->nodes[*level] = NULL; |
| 2695 | *level = i + 1; |
| 2696 | } |
| 2697 | } |
| 2698 | return 1; |
| 2699 | } |
| 2700 | |
| 2701 | /* |
| 2702 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 2703 | * the tree freeing any blocks that have a ref count of zero after being |
| 2704 | * decremented. |
| 2705 | */ |
| 2706 | static int walk_log_tree(struct btrfs_trans_handle *trans, |
| 2707 | struct btrfs_root *log, struct walk_control *wc) |
| 2708 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2709 | struct btrfs_fs_info *fs_info = log->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2710 | int ret = 0; |
| 2711 | int wret; |
| 2712 | int level; |
| 2713 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2714 | int orig_level; |
| 2715 | |
| 2716 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 2717 | if (!path) |
| 2718 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2719 | |
| 2720 | level = btrfs_header_level(log->node); |
| 2721 | orig_level = level; |
| 2722 | path->nodes[level] = log->node; |
| 2723 | extent_buffer_get(log->node); |
| 2724 | path->slots[level] = 0; |
| 2725 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2726 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2727 | wret = walk_down_log_tree(trans, log, path, &level, wc); |
| 2728 | if (wret > 0) |
| 2729 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2730 | if (wret < 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2731 | ret = wret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2732 | goto out; |
| 2733 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2734 | |
| 2735 | wret = walk_up_log_tree(trans, log, path, &level, wc); |
| 2736 | if (wret > 0) |
| 2737 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2738 | if (wret < 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2739 | ret = wret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2740 | goto out; |
| 2741 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | /* was the root node processed? if not, catch it here */ |
| 2745 | if (path->nodes[orig_level]) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2746 | ret = wc->process_func(log, path->nodes[orig_level], wc, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2747 | btrfs_header_generation(path->nodes[orig_level]), |
| 2748 | orig_level); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2749 | if (ret) |
| 2750 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2751 | if (wc->free) { |
| 2752 | struct extent_buffer *next; |
| 2753 | |
| 2754 | next = path->nodes[orig_level]; |
| 2755 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2756 | if (trans) { |
| 2757 | btrfs_tree_lock(next); |
| 2758 | btrfs_set_lock_blocking(next); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2759 | clean_tree_block(fs_info, next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2760 | btrfs_wait_tree_block_writeback(next); |
| 2761 | btrfs_tree_unlock(next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2762 | } else { |
| 2763 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) |
| 2764 | clear_extent_buffer_dirty(next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2765 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2766 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2767 | WARN_ON(log->root_key.objectid != |
| 2768 | BTRFS_TREE_LOG_OBJECTID); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2769 | ret = btrfs_free_and_pin_reserved_extent(fs_info, |
| 2770 | next->start, next->len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2771 | if (ret) |
| 2772 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2773 | } |
| 2774 | } |
| 2775 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2776 | out: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2777 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2778 | return ret; |
| 2779 | } |
| 2780 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2781 | /* |
| 2782 | * helper function to update the item for a given subvolumes log root |
| 2783 | * in the tree of log roots |
| 2784 | */ |
| 2785 | static int update_log_root(struct btrfs_trans_handle *trans, |
| 2786 | struct btrfs_root *log) |
| 2787 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2788 | struct btrfs_fs_info *fs_info = log->fs_info; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2789 | int ret; |
| 2790 | |
| 2791 | if (log->log_transid == 1) { |
| 2792 | /* insert root item on the first sync */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2793 | ret = btrfs_insert_root(trans, fs_info->log_root_tree, |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2794 | &log->root_key, &log->root_item); |
| 2795 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2796 | ret = btrfs_update_root(trans, fs_info->log_root_tree, |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2797 | &log->root_key, &log->root_item); |
| 2798 | } |
| 2799 | return ret; |
| 2800 | } |
| 2801 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2802 | static void wait_log_commit(struct btrfs_root *root, int transid) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2803 | { |
| 2804 | DEFINE_WAIT(wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2805 | int index = transid % 2; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2806 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2807 | /* |
| 2808 | * we only allow two pending log transactions at a time, |
| 2809 | * so we know that if ours is more than 2 older than the |
| 2810 | * current transaction, we're done |
| 2811 | */ |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2812 | for (;;) { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2813 | prepare_to_wait(&root->log_commit_wait[index], |
| 2814 | &wait, TASK_UNINTERRUPTIBLE); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2815 | |
| 2816 | if (!(root->log_transid_committed < transid && |
| 2817 | atomic_read(&root->log_commit[index]))) |
| 2818 | break; |
| 2819 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2820 | mutex_unlock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2821 | schedule(); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2822 | mutex_lock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2823 | } |
| 2824 | finish_wait(&root->log_commit_wait[index], &wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2825 | } |
| 2826 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2827 | static void wait_for_writer(struct btrfs_root *root) |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2828 | { |
| 2829 | DEFINE_WAIT(wait); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2830 | |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2831 | for (;;) { |
| 2832 | prepare_to_wait(&root->log_writer_wait, &wait, |
| 2833 | TASK_UNINTERRUPTIBLE); |
| 2834 | if (!atomic_read(&root->log_writers)) |
| 2835 | break; |
| 2836 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2837 | mutex_unlock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2838 | schedule(); |
Filipe Manana | 575849e | 2015-02-11 11:12:39 +0000 | [diff] [blame] | 2839 | mutex_lock(&root->log_mutex); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2840 | } |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2841 | finish_wait(&root->log_writer_wait, &wait); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2842 | } |
| 2843 | |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2844 | static inline void btrfs_remove_log_ctx(struct btrfs_root *root, |
| 2845 | struct btrfs_log_ctx *ctx) |
| 2846 | { |
| 2847 | if (!ctx) |
| 2848 | return; |
| 2849 | |
| 2850 | mutex_lock(&root->log_mutex); |
| 2851 | list_del_init(&ctx->list); |
| 2852 | mutex_unlock(&root->log_mutex); |
| 2853 | } |
| 2854 | |
| 2855 | /* |
| 2856 | * Invoked in log mutex context, or be sure there is no other task which |
| 2857 | * can access the list. |
| 2858 | */ |
| 2859 | static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root, |
| 2860 | int index, int error) |
| 2861 | { |
| 2862 | struct btrfs_log_ctx *ctx; |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2863 | struct btrfs_log_ctx *safe; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2864 | |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2865 | list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) { |
| 2866 | list_del_init(&ctx->list); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2867 | ctx->log_ret = error; |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2868 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2869 | |
| 2870 | INIT_LIST_HEAD(&root->log_ctxs[index]); |
| 2871 | } |
| 2872 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2873 | /* |
| 2874 | * btrfs_sync_log does sends a given tree log down to the disk and |
| 2875 | * updates the super blocks to record it. When this call is done, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2876 | * you know that any inodes previously logged are safely on disk only |
| 2877 | * if it returns 0. |
| 2878 | * |
| 2879 | * Any other return value means you need to call btrfs_commit_transaction. |
| 2880 | * Some of the edge cases for fsyncing directories that have had unlinks |
| 2881 | * or renames done in the past mean that sometimes the only safe |
| 2882 | * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN, |
| 2883 | * that has happened. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2884 | */ |
| 2885 | int btrfs_sync_log(struct btrfs_trans_handle *trans, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2886 | struct btrfs_root *root, struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2887 | { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2888 | int index1; |
| 2889 | int index2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2890 | int mark; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2891 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2892 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2893 | struct btrfs_root *log = root->log_root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2894 | struct btrfs_root *log_root_tree = fs_info->log_root_tree; |
Miao Xie | bb14a59 | 2014-02-20 18:08:56 +0800 | [diff] [blame] | 2895 | int log_transid = 0; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2896 | struct btrfs_log_ctx root_log_ctx; |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2897 | struct blk_plug plug; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2898 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2899 | mutex_lock(&root->log_mutex); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2900 | log_transid = ctx->log_transid; |
| 2901 | if (root->log_transid_committed >= log_transid) { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2902 | mutex_unlock(&root->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2903 | return ctx->log_ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2904 | } |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2905 | |
| 2906 | index1 = log_transid % 2; |
| 2907 | if (atomic_read(&root->log_commit[index1])) { |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2908 | wait_log_commit(root, log_transid); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2909 | mutex_unlock(&root->log_mutex); |
| 2910 | return ctx->log_ret; |
| 2911 | } |
| 2912 | ASSERT(log_transid == root->log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2913 | atomic_set(&root->log_commit[index1], 1); |
| 2914 | |
| 2915 | /* wait for previous tree log sync to complete */ |
| 2916 | if (atomic_read(&root->log_commit[(index1 + 1) % 2])) |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2917 | wait_log_commit(root, log_transid - 1); |
Miao Xie | 48cab2e | 2014-02-20 18:08:52 +0800 | [diff] [blame] | 2918 | |
Yan, Zheng | 86df7eb | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2919 | while (1) { |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2920 | int batch = atomic_read(&root->log_batch); |
Chris Mason | cd354ad | 2011-10-20 15:45:37 -0400 | [diff] [blame] | 2921 | /* when we're on an ssd, just kick the log commit out */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2922 | if (!btrfs_test_opt(fs_info, SSD) && |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 2923 | test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) { |
Yan, Zheng | 86df7eb | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2924 | mutex_unlock(&root->log_mutex); |
| 2925 | schedule_timeout_uninterruptible(1); |
| 2926 | mutex_lock(&root->log_mutex); |
| 2927 | } |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2928 | wait_for_writer(root); |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2929 | if (batch == atomic_read(&root->log_batch)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2930 | break; |
| 2931 | } |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2932 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2933 | /* bail out if we need to do a full commit */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2934 | if (btrfs_need_log_full_commit(fs_info, trans)) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2935 | ret = -EAGAIN; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 2936 | btrfs_free_logged_extents(log, log_transid); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2937 | mutex_unlock(&root->log_mutex); |
| 2938 | goto out; |
| 2939 | } |
| 2940 | |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2941 | if (log_transid % 2 == 0) |
| 2942 | mark = EXTENT_DIRTY; |
| 2943 | else |
| 2944 | mark = EXTENT_NEW; |
| 2945 | |
Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 2946 | /* we start IO on all the marked extents here, but we don't actually |
| 2947 | * wait for them until later. |
| 2948 | */ |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2949 | blk_start_plug(&plug); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2950 | ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2951 | if (ret) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2952 | blk_finish_plug(&plug); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2953 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 2954 | btrfs_free_logged_extents(log, log_transid); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2955 | btrfs_set_log_full_commit(fs_info, trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2956 | mutex_unlock(&root->log_mutex); |
| 2957 | goto out; |
| 2958 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2959 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2960 | btrfs_set_root_node(&log->root_item, log->node); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2961 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2962 | root->log_transid++; |
| 2963 | log->log_transid = root->log_transid; |
Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 2964 | root->log_start_pid = 0; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2965 | /* |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2966 | * IO has been started, blocks of the log tree have WRITTEN flag set |
| 2967 | * in their headers. new modifications of the log will be written to |
| 2968 | * 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] | 2969 | */ |
| 2970 | mutex_unlock(&root->log_mutex); |
| 2971 | |
Filipe Manana | 28a2359 | 2016-08-23 21:13:51 +0100 | [diff] [blame] | 2972 | btrfs_init_log_ctx(&root_log_ctx, NULL); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2973 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2974 | mutex_lock(&log_root_tree->log_mutex); |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2975 | atomic_inc(&log_root_tree->log_batch); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2976 | atomic_inc(&log_root_tree->log_writers); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2977 | |
| 2978 | index2 = log_root_tree->log_transid % 2; |
| 2979 | list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]); |
| 2980 | root_log_ctx.log_transid = log_root_tree->log_transid; |
| 2981 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2982 | mutex_unlock(&log_root_tree->log_mutex); |
| 2983 | |
| 2984 | ret = update_log_root(trans, log); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2985 | |
| 2986 | mutex_lock(&log_root_tree->log_mutex); |
| 2987 | if (atomic_dec_and_test(&log_root_tree->log_writers)) { |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 2988 | /* atomic_dec_and_test implies a barrier */ |
| 2989 | cond_wake_up_nomb(&log_root_tree->log_writer_wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2990 | } |
| 2991 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2992 | if (ret) { |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2993 | if (!list_empty(&root_log_ctx.list)) |
| 2994 | list_del_init(&root_log_ctx.list); |
| 2995 | |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2996 | blk_finish_plug(&plug); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2997 | btrfs_set_log_full_commit(fs_info, trans); |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 2998 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2999 | if (ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3000 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3001 | mutex_unlock(&log_root_tree->log_mutex); |
| 3002 | goto out; |
| 3003 | } |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3004 | btrfs_wait_tree_log_extents(log, mark); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 3005 | btrfs_free_logged_extents(log, log_transid); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3006 | mutex_unlock(&log_root_tree->log_mutex); |
| 3007 | ret = -EAGAIN; |
| 3008 | goto out; |
| 3009 | } |
| 3010 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3011 | if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) { |
Forrest Liu | 3da5ab5 | 2015-01-30 19:42:12 +0800 | [diff] [blame] | 3012 | blk_finish_plug(&plug); |
Chris Mason | cbd60aa | 2016-09-06 05:37:40 -0700 | [diff] [blame] | 3013 | list_del_init(&root_log_ctx.list); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3014 | mutex_unlock(&log_root_tree->log_mutex); |
| 3015 | ret = root_log_ctx.log_ret; |
| 3016 | goto out; |
| 3017 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3018 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3019 | index2 = root_log_ctx.log_transid % 2; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3020 | if (atomic_read(&log_root_tree->log_commit[index2])) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3021 | blk_finish_plug(&plug); |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3022 | ret = btrfs_wait_tree_log_extents(log, mark); |
Josef Bacik | 50d9aa9 | 2014-11-21 14:52:38 -0500 | [diff] [blame] | 3023 | btrfs_wait_logged_extents(trans, log, log_transid); |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3024 | wait_log_commit(log_root_tree, |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3025 | root_log_ctx.log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3026 | mutex_unlock(&log_root_tree->log_mutex); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3027 | if (!ret) |
| 3028 | ret = root_log_ctx.log_ret; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3029 | goto out; |
| 3030 | } |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3031 | ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3032 | atomic_set(&log_root_tree->log_commit[index2], 1); |
| 3033 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3034 | if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) { |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3035 | wait_log_commit(log_root_tree, |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3036 | root_log_ctx.log_transid - 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3037 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3038 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3039 | wait_for_writer(log_root_tree); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3040 | |
| 3041 | /* |
| 3042 | * now that we've moved on to the tree of log tree roots, |
| 3043 | * check the full commit flag again |
| 3044 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3045 | if (btrfs_need_log_full_commit(fs_info, trans)) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3046 | blk_finish_plug(&plug); |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3047 | btrfs_wait_tree_log_extents(log, mark); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 3048 | btrfs_free_logged_extents(log, log_transid); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3049 | mutex_unlock(&log_root_tree->log_mutex); |
| 3050 | ret = -EAGAIN; |
| 3051 | goto out_wake_log_root; |
| 3052 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3053 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3054 | ret = btrfs_write_marked_extents(fs_info, |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3055 | &log_root_tree->dirty_log_pages, |
| 3056 | EXTENT_DIRTY | EXTENT_NEW); |
| 3057 | blk_finish_plug(&plug); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3058 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3059 | btrfs_set_log_full_commit(fs_info, trans); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3060 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 3061 | btrfs_free_logged_extents(log, log_transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3062 | mutex_unlock(&log_root_tree->log_mutex); |
| 3063 | goto out_wake_log_root; |
| 3064 | } |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3065 | ret = btrfs_wait_tree_log_extents(log, mark); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3066 | if (!ret) |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3067 | ret = btrfs_wait_tree_log_extents(log_root_tree, |
| 3068 | EXTENT_NEW | EXTENT_DIRTY); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3069 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3070 | btrfs_set_log_full_commit(fs_info, trans); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3071 | btrfs_free_logged_extents(log, log_transid); |
| 3072 | mutex_unlock(&log_root_tree->log_mutex); |
| 3073 | goto out_wake_log_root; |
| 3074 | } |
Josef Bacik | 50d9aa9 | 2014-11-21 14:52:38 -0500 | [diff] [blame] | 3075 | btrfs_wait_logged_extents(trans, log, log_transid); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3076 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3077 | btrfs_set_super_log_root(fs_info->super_for_commit, |
| 3078 | log_root_tree->node->start); |
| 3079 | btrfs_set_super_log_root_level(fs_info->super_for_commit, |
| 3080 | btrfs_header_level(log_root_tree->node)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3081 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3082 | log_root_tree->log_transid++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3083 | mutex_unlock(&log_root_tree->log_mutex); |
| 3084 | |
| 3085 | /* |
| 3086 | * nobody else is going to jump in and write the the ctree |
| 3087 | * super here because the log_commit atomic below is protecting |
| 3088 | * us. We must be called with a transaction handle pinning |
| 3089 | * the running transaction open, so a full commit can't hop |
| 3090 | * in and cause problems either. |
| 3091 | */ |
David Sterba | eece6a9 | 2017-02-10 19:04:32 +0100 | [diff] [blame] | 3092 | ret = write_all_supers(fs_info, 1); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3093 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3094 | btrfs_set_log_full_commit(fs_info, trans); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3095 | btrfs_abort_transaction(trans, ret); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3096 | goto out_wake_log_root; |
| 3097 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3098 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 3099 | mutex_lock(&root->log_mutex); |
| 3100 | if (root->last_log_commit < log_transid) |
| 3101 | root->last_log_commit = log_transid; |
| 3102 | mutex_unlock(&root->log_mutex); |
| 3103 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3104 | out_wake_log_root: |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 3105 | mutex_lock(&log_root_tree->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3106 | btrfs_remove_all_log_ctxs(log_root_tree, index2, ret); |
| 3107 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3108 | log_root_tree->log_transid_committed++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3109 | atomic_set(&log_root_tree->log_commit[index2], 0); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3110 | mutex_unlock(&log_root_tree->log_mutex); |
| 3111 | |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3112 | /* |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3113 | * The barrier before waitqueue_active (in cond_wake_up) is needed so |
| 3114 | * all the updates above are seen by the woken threads. It might not be |
| 3115 | * necessary, but proving that seems to be hard. |
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 | cond_wake_up(&log_root_tree->log_commit_wait[index2]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3118 | out: |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3119 | mutex_lock(&root->log_mutex); |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 3120 | btrfs_remove_all_log_ctxs(root, index1, ret); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3121 | root->log_transid_committed++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3122 | atomic_set(&root->log_commit[index1], 0); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3123 | mutex_unlock(&root->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3124 | |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3125 | /* |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3126 | * The barrier before waitqueue_active (in cond_wake_up) is needed so |
| 3127 | * all the updates above are seen by the woken threads. It might not be |
| 3128 | * necessary, but proving that seems to be hard. |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3129 | */ |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3130 | cond_wake_up(&root->log_commit_wait[index1]); |
Chris Mason | b31eabd | 2011-01-31 16:48:24 -0500 | [diff] [blame] | 3131 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3132 | } |
| 3133 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3134 | static void free_log_tree(struct btrfs_trans_handle *trans, |
| 3135 | struct btrfs_root *log) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3136 | { |
| 3137 | int ret; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3138 | u64 start; |
| 3139 | u64 end; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3140 | struct walk_control wc = { |
| 3141 | .free = 1, |
| 3142 | .process_func = process_one_buffer |
| 3143 | }; |
| 3144 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 3145 | ret = walk_log_tree(trans, log, &wc); |
| 3146 | /* I don't think this can happen but just in case */ |
| 3147 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3148 | btrfs_abort_transaction(trans, ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3149 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3150 | while (1) { |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3151 | ret = find_first_extent_bit(&log->dirty_log_pages, |
Liu Bo | 55237a5 | 2018-01-25 11:02:52 -0700 | [diff] [blame] | 3152 | 0, &start, &end, |
| 3153 | EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 3154 | NULL); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3155 | if (ret) |
| 3156 | break; |
| 3157 | |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 3158 | clear_extent_bits(&log->dirty_log_pages, start, end, |
Liu Bo | 55237a5 | 2018-01-25 11:02:52 -0700 | [diff] [blame] | 3159 | EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3160 | } |
| 3161 | |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 3162 | /* |
| 3163 | * We may have short-circuited the log tree with the full commit logic |
| 3164 | * and left ordered extents on our list, so clear these out to keep us |
| 3165 | * from leaking inodes and memory. |
| 3166 | */ |
| 3167 | btrfs_free_logged_extents(log, 0); |
| 3168 | btrfs_free_logged_extents(log, 1); |
| 3169 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3170 | free_extent_buffer(log->node); |
| 3171 | kfree(log); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | /* |
| 3175 | * free all the extents used by the tree log. This should be called |
| 3176 | * at commit time of the full transaction |
| 3177 | */ |
| 3178 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) |
| 3179 | { |
| 3180 | if (root->log_root) { |
| 3181 | free_log_tree(trans, root->log_root); |
| 3182 | root->log_root = NULL; |
| 3183 | } |
| 3184 | return 0; |
| 3185 | } |
| 3186 | |
| 3187 | int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, |
| 3188 | struct btrfs_fs_info *fs_info) |
| 3189 | { |
| 3190 | if (fs_info->log_root_tree) { |
| 3191 | free_log_tree(trans, fs_info->log_root_tree); |
| 3192 | fs_info->log_root_tree = NULL; |
| 3193 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3194 | return 0; |
| 3195 | } |
| 3196 | |
| 3197 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3198 | * If both a file and directory are logged, and unlinks or renames are |
| 3199 | * mixed in, we have a few interesting corners: |
| 3200 | * |
| 3201 | * create file X in dir Y |
| 3202 | * link file X to X.link in dir Y |
| 3203 | * fsync file X |
| 3204 | * unlink file X but leave X.link |
| 3205 | * fsync dir Y |
| 3206 | * |
| 3207 | * After a crash we would expect only X.link to exist. But file X |
| 3208 | * didn't get fsync'd again so the log has back refs for X and X.link. |
| 3209 | * |
| 3210 | * We solve this by removing directory entries and inode backrefs from the |
| 3211 | * log when a file that was logged in the current transaction is |
| 3212 | * unlinked. Any later fsync will include the updated log entries, and |
| 3213 | * we'll be able to reconstruct the proper directory items from backrefs. |
| 3214 | * |
| 3215 | * This optimizations allows us to avoid relogging the entire inode |
| 3216 | * or the entire directory. |
| 3217 | */ |
| 3218 | int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, |
| 3219 | struct btrfs_root *root, |
| 3220 | const char *name, int name_len, |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3221 | struct btrfs_inode *dir, u64 index) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3222 | { |
| 3223 | struct btrfs_root *log; |
| 3224 | struct btrfs_dir_item *di; |
| 3225 | struct btrfs_path *path; |
| 3226 | int ret; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3227 | int err = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3228 | int bytes_del = 0; |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3229 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3230 | |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3231 | if (dir->logged_trans < trans->transid) |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 3232 | return 0; |
| 3233 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3234 | ret = join_running_log_trans(root); |
| 3235 | if (ret) |
| 3236 | return 0; |
| 3237 | |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3238 | mutex_lock(&dir->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3239 | |
| 3240 | log = root->log_root; |
| 3241 | path = btrfs_alloc_path(); |
Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3242 | if (!path) { |
| 3243 | err = -ENOMEM; |
| 3244 | goto out_unlock; |
| 3245 | } |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 3246 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3247 | di = btrfs_lookup_dir_item(trans, log, path, dir_ino, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3248 | name, name_len, -1); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3249 | if (IS_ERR(di)) { |
| 3250 | err = PTR_ERR(di); |
| 3251 | goto fail; |
| 3252 | } |
| 3253 | if (di) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3254 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
| 3255 | bytes_del += name_len; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3256 | if (ret) { |
| 3257 | err = ret; |
| 3258 | goto fail; |
| 3259 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3260 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3261 | btrfs_release_path(path); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3262 | di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3263 | index, name, name_len, -1); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3264 | if (IS_ERR(di)) { |
| 3265 | err = PTR_ERR(di); |
| 3266 | goto fail; |
| 3267 | } |
| 3268 | if (di) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3269 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
| 3270 | bytes_del += name_len; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3271 | if (ret) { |
| 3272 | err = ret; |
| 3273 | goto fail; |
| 3274 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3275 | } |
| 3276 | |
| 3277 | /* update the directory size in the log to reflect the names |
| 3278 | * we have removed |
| 3279 | */ |
| 3280 | if (bytes_del) { |
| 3281 | struct btrfs_key key; |
| 3282 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3283 | key.objectid = dir_ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3284 | key.offset = 0; |
| 3285 | key.type = BTRFS_INODE_ITEM_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3286 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3287 | |
| 3288 | ret = btrfs_search_slot(trans, log, &key, path, 0, 1); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3289 | if (ret < 0) { |
| 3290 | err = ret; |
| 3291 | goto fail; |
| 3292 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3293 | if (ret == 0) { |
| 3294 | struct btrfs_inode_item *item; |
| 3295 | u64 i_size; |
| 3296 | |
| 3297 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3298 | struct btrfs_inode_item); |
| 3299 | i_size = btrfs_inode_size(path->nodes[0], item); |
| 3300 | if (i_size > bytes_del) |
| 3301 | i_size -= bytes_del; |
| 3302 | else |
| 3303 | i_size = 0; |
| 3304 | btrfs_set_inode_size(path->nodes[0], item, i_size); |
| 3305 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 3306 | } else |
| 3307 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3308 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3309 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3310 | fail: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3311 | btrfs_free_path(path); |
Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3312 | out_unlock: |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3313 | mutex_unlock(&dir->log_mutex); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3314 | if (ret == -ENOSPC) { |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 3315 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3316 | ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3317 | } else if (ret < 0) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3318 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3319 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3320 | btrfs_end_log_trans(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3321 | |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 3322 | return err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | /* see comments for btrfs_del_dir_entries_in_log */ |
| 3326 | int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, |
| 3327 | struct btrfs_root *root, |
| 3328 | const char *name, int name_len, |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3329 | struct btrfs_inode *inode, u64 dirid) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3330 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3331 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3332 | struct btrfs_root *log; |
| 3333 | u64 index; |
| 3334 | int ret; |
| 3335 | |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3336 | if (inode->logged_trans < trans->transid) |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 3337 | return 0; |
| 3338 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3339 | ret = join_running_log_trans(root); |
| 3340 | if (ret) |
| 3341 | return 0; |
| 3342 | log = root->log_root; |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3343 | mutex_lock(&inode->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3344 | |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3345 | 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] | 3346 | dirid, &index); |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3347 | mutex_unlock(&inode->log_mutex); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3348 | if (ret == -ENOSPC) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3349 | btrfs_set_log_full_commit(fs_info, trans); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3350 | ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3351 | } else if (ret < 0 && ret != -ENOENT) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3352 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3353 | btrfs_end_log_trans(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3354 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3355 | return ret; |
| 3356 | } |
| 3357 | |
| 3358 | /* |
| 3359 | * creates a range item in the log for 'dirid'. first_offset and |
| 3360 | * last_offset tell us which parts of the key space the log should |
| 3361 | * be considered authoritative for. |
| 3362 | */ |
| 3363 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, |
| 3364 | struct btrfs_root *log, |
| 3365 | struct btrfs_path *path, |
| 3366 | int key_type, u64 dirid, |
| 3367 | u64 first_offset, u64 last_offset) |
| 3368 | { |
| 3369 | int ret; |
| 3370 | struct btrfs_key key; |
| 3371 | struct btrfs_dir_log_item *item; |
| 3372 | |
| 3373 | key.objectid = dirid; |
| 3374 | key.offset = first_offset; |
| 3375 | if (key_type == BTRFS_DIR_ITEM_KEY) |
| 3376 | key.type = BTRFS_DIR_LOG_ITEM_KEY; |
| 3377 | else |
| 3378 | key.type = BTRFS_DIR_LOG_INDEX_KEY; |
| 3379 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3380 | if (ret) |
| 3381 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3382 | |
| 3383 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3384 | struct btrfs_dir_log_item); |
| 3385 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); |
| 3386 | btrfs_mark_buffer_dirty(path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3387 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3388 | return 0; |
| 3389 | } |
| 3390 | |
| 3391 | /* |
| 3392 | * log all the items included in the current transaction for a given |
| 3393 | * directory. This also creates the range items in the log tree required |
| 3394 | * to replay anything deleted before the fsync |
| 3395 | */ |
| 3396 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 684a577 | 2017-01-18 00:31:41 +0200 | [diff] [blame] | 3397 | struct btrfs_root *root, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3398 | struct btrfs_path *path, |
| 3399 | struct btrfs_path *dst_path, int key_type, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3400 | struct btrfs_log_ctx *ctx, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3401 | u64 min_offset, u64 *last_offset_ret) |
| 3402 | { |
| 3403 | struct btrfs_key min_key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3404 | struct btrfs_root *log = root->log_root; |
| 3405 | struct extent_buffer *src; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3406 | int err = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3407 | int ret; |
| 3408 | int i; |
| 3409 | int nritems; |
| 3410 | u64 first_offset = min_offset; |
| 3411 | u64 last_offset = (u64)-1; |
Nikolay Borisov | 684a577 | 2017-01-18 00:31:41 +0200 | [diff] [blame] | 3412 | u64 ino = btrfs_ino(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3413 | |
| 3414 | log = root->log_root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3415 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3416 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3417 | min_key.type = key_type; |
| 3418 | min_key.offset = min_offset; |
| 3419 | |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3420 | ret = btrfs_search_forward(root, &min_key, path, trans->transid); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3421 | |
| 3422 | /* |
| 3423 | * we didn't find anything from this transaction, see if there |
| 3424 | * is anything at all |
| 3425 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3426 | if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) { |
| 3427 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3428 | min_key.type = key_type; |
| 3429 | min_key.offset = (u64)-1; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3430 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3431 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
| 3432 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3433 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3434 | return ret; |
| 3435 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3436 | ret = btrfs_previous_item(root, path, ino, key_type); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3437 | |
| 3438 | /* if ret == 0 there are items for this type, |
| 3439 | * create a range to tell us the last key of this type. |
| 3440 | * otherwise, there are no items in this directory after |
| 3441 | * *min_offset, and we create a range to indicate that. |
| 3442 | */ |
| 3443 | if (ret == 0) { |
| 3444 | struct btrfs_key tmp; |
| 3445 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, |
| 3446 | path->slots[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3447 | if (key_type == tmp.type) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3448 | first_offset = max(min_offset, tmp.offset) + 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3449 | } |
| 3450 | goto done; |
| 3451 | } |
| 3452 | |
| 3453 | /* go backward to find any previous key */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3454 | ret = btrfs_previous_item(root, path, ino, key_type); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3455 | if (ret == 0) { |
| 3456 | struct btrfs_key tmp; |
| 3457 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
| 3458 | if (key_type == tmp.type) { |
| 3459 | first_offset = tmp.offset; |
| 3460 | ret = overwrite_item(trans, log, dst_path, |
| 3461 | path->nodes[0], path->slots[0], |
| 3462 | &tmp); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3463 | if (ret) { |
| 3464 | err = ret; |
| 3465 | goto done; |
| 3466 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3467 | } |
| 3468 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3469 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3470 | |
| 3471 | /* find the first key from this transaction again */ |
| 3472 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3473 | if (WARN_ON(ret != 0)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3474 | goto done; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3475 | |
| 3476 | /* |
| 3477 | * we have a block from this transaction, log every item in it |
| 3478 | * from our directory |
| 3479 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3480 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3481 | struct btrfs_key tmp; |
| 3482 | src = path->nodes[0]; |
| 3483 | nritems = btrfs_header_nritems(src); |
| 3484 | for (i = path->slots[0]; i < nritems; i++) { |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3485 | struct btrfs_dir_item *di; |
| 3486 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3487 | btrfs_item_key_to_cpu(src, &min_key, i); |
| 3488 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3489 | if (min_key.objectid != ino || min_key.type != key_type) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3490 | goto done; |
| 3491 | ret = overwrite_item(trans, log, dst_path, src, i, |
| 3492 | &min_key); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3493 | if (ret) { |
| 3494 | err = ret; |
| 3495 | goto done; |
| 3496 | } |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3497 | |
| 3498 | /* |
| 3499 | * We must make sure that when we log a directory entry, |
| 3500 | * the corresponding inode, after log replay, has a |
| 3501 | * matching link count. For example: |
| 3502 | * |
| 3503 | * touch foo |
| 3504 | * mkdir mydir |
| 3505 | * sync |
| 3506 | * ln foo mydir/bar |
| 3507 | * xfs_io -c "fsync" mydir |
| 3508 | * <crash> |
| 3509 | * <mount fs and log replay> |
| 3510 | * |
| 3511 | * Would result in a fsync log that when replayed, our |
| 3512 | * file inode would have a link count of 1, but we get |
| 3513 | * two directory entries pointing to the same inode. |
| 3514 | * After removing one of the names, it would not be |
| 3515 | * possible to remove the other name, which resulted |
| 3516 | * always in stale file handle errors, and would not |
| 3517 | * be possible to rmdir the parent directory, since |
| 3518 | * its i_size could never decrement to the value |
| 3519 | * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors. |
| 3520 | */ |
| 3521 | di = btrfs_item_ptr(src, i, struct btrfs_dir_item); |
| 3522 | btrfs_dir_item_key_to_cpu(src, di, &tmp); |
| 3523 | if (ctx && |
| 3524 | (btrfs_dir_transid(src, di) == trans->transid || |
| 3525 | btrfs_dir_type(src, di) == BTRFS_FT_DIR) && |
| 3526 | tmp.type != BTRFS_ROOT_ITEM_KEY) |
| 3527 | ctx->log_new_dentries = true; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3528 | } |
| 3529 | path->slots[0] = nritems; |
| 3530 | |
| 3531 | /* |
| 3532 | * look ahead to the next item and see if it is also |
| 3533 | * from this directory and from this transaction |
| 3534 | */ |
| 3535 | ret = btrfs_next_leaf(root, path); |
Liu Bo | 80c0b42 | 2018-04-03 01:59:47 +0800 | [diff] [blame] | 3536 | if (ret) { |
| 3537 | if (ret == 1) |
| 3538 | last_offset = (u64)-1; |
| 3539 | else |
| 3540 | err = ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3541 | goto done; |
| 3542 | } |
| 3543 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3544 | if (tmp.objectid != ino || tmp.type != key_type) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3545 | last_offset = (u64)-1; |
| 3546 | goto done; |
| 3547 | } |
| 3548 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { |
| 3549 | ret = overwrite_item(trans, log, dst_path, |
| 3550 | path->nodes[0], path->slots[0], |
| 3551 | &tmp); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3552 | if (ret) |
| 3553 | err = ret; |
| 3554 | else |
| 3555 | last_offset = tmp.offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3556 | goto done; |
| 3557 | } |
| 3558 | } |
| 3559 | done: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3560 | btrfs_release_path(path); |
| 3561 | btrfs_release_path(dst_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3562 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3563 | if (err == 0) { |
| 3564 | *last_offset_ret = last_offset; |
| 3565 | /* |
| 3566 | * insert the log range keys to indicate where the log |
| 3567 | * is valid |
| 3568 | */ |
| 3569 | ret = insert_dir_log_key(trans, log, path, key_type, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3570 | ino, first_offset, last_offset); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3571 | if (ret) |
| 3572 | err = ret; |
| 3573 | } |
| 3574 | return err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | /* |
| 3578 | * logging directories is very similar to logging inodes, We find all the items |
| 3579 | * from the current transaction and write them to the log. |
| 3580 | * |
| 3581 | * The recovery code scans the directory in the subvolume, and if it finds a |
| 3582 | * key in the range logged that is not present in the log tree, then it means |
| 3583 | * that dir entry was unlinked during the transaction. |
| 3584 | * |
| 3585 | * In order for that scan to work, we must include one key smaller than |
| 3586 | * the smallest logged by this transaction and one key larger than the largest |
| 3587 | * key logged by this transaction. |
| 3588 | */ |
| 3589 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, |
Nikolay Borisov | dbf39ea | 2017-01-18 00:31:42 +0200 | [diff] [blame] | 3590 | struct btrfs_root *root, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3591 | struct btrfs_path *path, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3592 | struct btrfs_path *dst_path, |
| 3593 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3594 | { |
| 3595 | u64 min_key; |
| 3596 | u64 max_key; |
| 3597 | int ret; |
| 3598 | int key_type = BTRFS_DIR_ITEM_KEY; |
| 3599 | |
| 3600 | again: |
| 3601 | min_key = 0; |
| 3602 | max_key = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3603 | while (1) { |
Nikolay Borisov | dbf39ea | 2017-01-18 00:31:42 +0200 | [diff] [blame] | 3604 | ret = log_dir_items(trans, root, inode, path, dst_path, key_type, |
| 3605 | ctx, min_key, &max_key); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3606 | if (ret) |
| 3607 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3608 | if (max_key == (u64)-1) |
| 3609 | break; |
| 3610 | min_key = max_key + 1; |
| 3611 | } |
| 3612 | |
| 3613 | if (key_type == BTRFS_DIR_ITEM_KEY) { |
| 3614 | key_type = BTRFS_DIR_INDEX_KEY; |
| 3615 | goto again; |
| 3616 | } |
| 3617 | return 0; |
| 3618 | } |
| 3619 | |
| 3620 | /* |
| 3621 | * a helper function to drop items from the log before we relog an |
| 3622 | * inode. max_key_type indicates the highest item type to remove. |
| 3623 | * This cannot be run for file data extents because it does not |
| 3624 | * free the extents they point to. |
| 3625 | */ |
| 3626 | static int drop_objectid_items(struct btrfs_trans_handle *trans, |
| 3627 | struct btrfs_root *log, |
| 3628 | struct btrfs_path *path, |
| 3629 | u64 objectid, int max_key_type) |
| 3630 | { |
| 3631 | int ret; |
| 3632 | struct btrfs_key key; |
| 3633 | struct btrfs_key found_key; |
Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 3634 | int start_slot; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3635 | |
| 3636 | key.objectid = objectid; |
| 3637 | key.type = max_key_type; |
| 3638 | key.offset = (u64)-1; |
| 3639 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3640 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3641 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3642 | BUG_ON(ret == 0); /* Logic error */ |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3643 | if (ret < 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3644 | break; |
| 3645 | |
| 3646 | if (path->slots[0] == 0) |
| 3647 | break; |
| 3648 | |
| 3649 | path->slots[0]--; |
| 3650 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 3651 | path->slots[0]); |
| 3652 | |
| 3653 | if (found_key.objectid != objectid) |
| 3654 | break; |
| 3655 | |
Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 3656 | found_key.offset = 0; |
| 3657 | found_key.type = 0; |
| 3658 | ret = btrfs_bin_search(path->nodes[0], &found_key, 0, |
| 3659 | &start_slot); |
| 3660 | |
| 3661 | ret = btrfs_del_items(trans, log, path, start_slot, |
| 3662 | path->slots[0] - start_slot + 1); |
| 3663 | /* |
| 3664 | * If start slot isn't 0 then we don't need to re-search, we've |
| 3665 | * found the last guy with the objectid in this tree. |
| 3666 | */ |
| 3667 | if (ret || start_slot != 0) |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 3668 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3669 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3670 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3671 | btrfs_release_path(path); |
Josef Bacik | 5bdbeb2 | 2012-05-29 16:59:49 -0400 | [diff] [blame] | 3672 | if (ret > 0) |
| 3673 | ret = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3674 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3675 | } |
| 3676 | |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3677 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3678 | struct extent_buffer *leaf, |
| 3679 | struct btrfs_inode_item *item, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3680 | struct inode *inode, int log_inode_only, |
| 3681 | u64 logged_isize) |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3682 | { |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3683 | struct btrfs_map_token token; |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3684 | |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3685 | btrfs_init_map_token(&token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3686 | |
| 3687 | if (log_inode_only) { |
| 3688 | /* set the generation to zero so the recover code |
| 3689 | * can tell the difference between an logging |
| 3690 | * just to say 'this inode exists' and a logging |
| 3691 | * to say 'update this inode with these values' |
| 3692 | */ |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3693 | btrfs_set_token_inode_generation(leaf, item, 0, &token); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3694 | btrfs_set_token_inode_size(leaf, item, logged_isize, &token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3695 | } else { |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3696 | btrfs_set_token_inode_generation(leaf, item, |
| 3697 | BTRFS_I(inode)->generation, |
| 3698 | &token); |
| 3699 | btrfs_set_token_inode_size(leaf, item, inode->i_size, &token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3700 | } |
| 3701 | |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3702 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3703 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3704 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3705 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
| 3706 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3707 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3708 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3709 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3710 | inode->i_atime.tv_nsec, &token); |
| 3711 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3712 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3713 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3714 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3715 | inode->i_mtime.tv_nsec, &token); |
| 3716 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3717 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3718 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3719 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3720 | inode->i_ctime.tv_nsec, &token); |
| 3721 | |
| 3722 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3723 | &token); |
| 3724 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3725 | btrfs_set_token_inode_sequence(leaf, item, |
| 3726 | inode_peek_iversion(inode), &token); |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3727 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3728 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3729 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3730 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3731 | } |
| 3732 | |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3733 | static int log_inode_item(struct btrfs_trans_handle *trans, |
| 3734 | struct btrfs_root *log, struct btrfs_path *path, |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3735 | struct btrfs_inode *inode) |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3736 | { |
| 3737 | struct btrfs_inode_item *inode_item; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3738 | int ret; |
| 3739 | |
Filipe David Borba Manana | efd0c40 | 2013-10-07 21:20:44 +0100 | [diff] [blame] | 3740 | ret = btrfs_insert_empty_item(trans, log, path, |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3741 | &inode->location, sizeof(*inode_item)); |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3742 | if (ret && ret != -EEXIST) |
| 3743 | return ret; |
| 3744 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3745 | struct btrfs_inode_item); |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3746 | fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode, |
| 3747 | 0, 0); |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3748 | btrfs_release_path(path); |
| 3749 | return 0; |
| 3750 | } |
| 3751 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3752 | static noinline int copy_items(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3753 | struct btrfs_inode *inode, |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3754 | struct btrfs_path *dst_path, |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3755 | struct btrfs_path *src_path, u64 *last_extent, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3756 | int start_slot, int nr, int inode_only, |
| 3757 | u64 logged_isize) |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3758 | { |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3759 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3760 | unsigned long src_offset; |
| 3761 | unsigned long dst_offset; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3762 | struct btrfs_root *log = inode->root->log_root; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3763 | struct btrfs_file_extent_item *extent; |
| 3764 | struct btrfs_inode_item *inode_item; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3765 | struct extent_buffer *src = src_path->nodes[0]; |
| 3766 | struct btrfs_key first_key, last_key, key; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3767 | int ret; |
| 3768 | struct btrfs_key *ins_keys; |
| 3769 | u32 *ins_sizes; |
| 3770 | char *ins_data; |
| 3771 | int i; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3772 | struct list_head ordered_sums; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3773 | int skip_csum = inode->flags & BTRFS_INODE_NODATASUM; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3774 | bool has_extents = false; |
Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 3775 | bool need_find_last_extent = true; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3776 | bool done = false; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3777 | |
| 3778 | INIT_LIST_HEAD(&ordered_sums); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3779 | |
| 3780 | ins_data = kmalloc(nr * sizeof(struct btrfs_key) + |
| 3781 | nr * sizeof(u32), GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 3782 | if (!ins_data) |
| 3783 | return -ENOMEM; |
| 3784 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3785 | first_key.objectid = (u64)-1; |
| 3786 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3787 | ins_sizes = (u32 *)ins_data; |
| 3788 | ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); |
| 3789 | |
| 3790 | for (i = 0; i < nr; i++) { |
| 3791 | ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot); |
| 3792 | btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot); |
| 3793 | } |
| 3794 | ret = btrfs_insert_empty_items(trans, log, dst_path, |
| 3795 | ins_keys, ins_sizes, nr); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3796 | if (ret) { |
| 3797 | kfree(ins_data); |
| 3798 | return ret; |
| 3799 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3800 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3801 | for (i = 0; i < nr; i++, dst_path->slots[0]++) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3802 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], |
| 3803 | dst_path->slots[0]); |
| 3804 | |
| 3805 | src_offset = btrfs_item_ptr_offset(src, start_slot + i); |
| 3806 | |
Matthias Kaehlcke | 0dde10b | 2017-07-27 14:30:23 -0700 | [diff] [blame] | 3807 | if (i == nr - 1) |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3808 | last_key = ins_keys[i]; |
| 3809 | |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3810 | if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3811 | inode_item = btrfs_item_ptr(dst_path->nodes[0], |
| 3812 | dst_path->slots[0], |
| 3813 | struct btrfs_inode_item); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3814 | fill_inode_item(trans, dst_path->nodes[0], inode_item, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3815 | &inode->vfs_inode, |
| 3816 | inode_only == LOG_INODE_EXISTS, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3817 | logged_isize); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3818 | } else { |
| 3819 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, |
| 3820 | src_offset, ins_sizes[i]); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3821 | } |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3822 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3823 | /* |
| 3824 | * We set need_find_last_extent here in case we know we were |
| 3825 | * processing other items and then walk into the first extent in |
| 3826 | * the inode. If we don't hit an extent then nothing changes, |
| 3827 | * we'll do the last search the next time around. |
| 3828 | */ |
| 3829 | if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) { |
| 3830 | has_extents = true; |
Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 3831 | if (first_key.objectid == (u64)-1) |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3832 | first_key = ins_keys[i]; |
| 3833 | } else { |
| 3834 | need_find_last_extent = false; |
| 3835 | } |
| 3836 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3837 | /* take a reference on file data extents so that truncates |
| 3838 | * or deletes of this inode don't have to relog the inode |
| 3839 | * again |
| 3840 | */ |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3841 | if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY && |
Liu Bo | d279440 | 2012-08-29 01:07:56 -0600 | [diff] [blame] | 3842 | !skip_csum) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3843 | int found_type; |
| 3844 | extent = btrfs_item_ptr(src, start_slot + i, |
| 3845 | struct btrfs_file_extent_item); |
| 3846 | |
liubo | 8e531cd | 2011-05-06 10:36:09 +0800 | [diff] [blame] | 3847 | if (btrfs_file_extent_generation(src, extent) < trans->transid) |
| 3848 | continue; |
| 3849 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3850 | found_type = btrfs_file_extent_type(src, extent); |
Josef Bacik | 6f1fed7 | 2012-09-26 11:07:06 -0400 | [diff] [blame] | 3851 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3852 | u64 ds, dl, cs, cl; |
| 3853 | ds = btrfs_file_extent_disk_bytenr(src, |
| 3854 | extent); |
| 3855 | /* ds == 0 is a hole */ |
| 3856 | if (ds == 0) |
| 3857 | continue; |
| 3858 | |
| 3859 | dl = btrfs_file_extent_disk_num_bytes(src, |
| 3860 | extent); |
| 3861 | cs = btrfs_file_extent_offset(src, extent); |
| 3862 | cl = btrfs_file_extent_num_bytes(src, |
Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 3863 | extent); |
Chris Mason | 580afd7 | 2008-12-08 19:15:39 -0500 | [diff] [blame] | 3864 | if (btrfs_file_extent_compression(src, |
| 3865 | extent)) { |
| 3866 | cs = 0; |
| 3867 | cl = dl; |
| 3868 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3869 | |
| 3870 | ret = btrfs_lookup_csums_range( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3871 | fs_info->csum_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3872 | ds + cs, ds + cs + cl - 1, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3873 | &ordered_sums, 0); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3874 | if (ret) { |
| 3875 | btrfs_release_path(dst_path); |
| 3876 | kfree(ins_data); |
| 3877 | return ret; |
| 3878 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3879 | } |
| 3880 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | btrfs_mark_buffer_dirty(dst_path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3884 | btrfs_release_path(dst_path); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3885 | kfree(ins_data); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3886 | |
| 3887 | /* |
| 3888 | * we have to do this after the loop above to avoid changing the |
| 3889 | * log tree while trying to change the log tree. |
| 3890 | */ |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3891 | ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3892 | while (!list_empty(&ordered_sums)) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3893 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, |
| 3894 | struct btrfs_ordered_sum, |
| 3895 | list); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3896 | if (!ret) |
| 3897 | ret = btrfs_csum_file_blocks(trans, log, sums); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3898 | list_del(&sums->list); |
| 3899 | kfree(sums); |
| 3900 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3901 | |
| 3902 | if (!has_extents) |
| 3903 | return ret; |
| 3904 | |
Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 3905 | if (need_find_last_extent && *last_extent == first_key.offset) { |
| 3906 | /* |
| 3907 | * We don't have any leafs between our current one and the one |
| 3908 | * we processed before that can have file extent items for our |
| 3909 | * inode (and have a generation number smaller than our current |
| 3910 | * transaction id). |
| 3911 | */ |
| 3912 | need_find_last_extent = false; |
| 3913 | } |
| 3914 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3915 | /* |
| 3916 | * Because we use btrfs_search_forward we could skip leaves that were |
| 3917 | * not modified and then assume *last_extent is valid when it really |
| 3918 | * isn't. So back up to the previous leaf and read the end of the last |
| 3919 | * extent before we go and fill in holes. |
| 3920 | */ |
| 3921 | if (need_find_last_extent) { |
| 3922 | u64 len; |
| 3923 | |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3924 | ret = btrfs_prev_leaf(inode->root, src_path); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3925 | if (ret < 0) |
| 3926 | return ret; |
| 3927 | if (ret) |
| 3928 | goto fill_holes; |
| 3929 | if (src_path->slots[0]) |
| 3930 | src_path->slots[0]--; |
| 3931 | src = src_path->nodes[0]; |
| 3932 | btrfs_item_key_to_cpu(src, &key, src_path->slots[0]); |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3933 | if (key.objectid != btrfs_ino(inode) || |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3934 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 3935 | goto fill_holes; |
| 3936 | extent = btrfs_item_ptr(src, src_path->slots[0], |
| 3937 | struct btrfs_file_extent_item); |
| 3938 | if (btrfs_file_extent_type(src, extent) == |
| 3939 | BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 3940 | len = btrfs_file_extent_inline_len(src, |
| 3941 | src_path->slots[0], |
| 3942 | extent); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3943 | *last_extent = ALIGN(key.offset + len, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3944 | fs_info->sectorsize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3945 | } else { |
| 3946 | len = btrfs_file_extent_num_bytes(src, extent); |
| 3947 | *last_extent = key.offset + len; |
| 3948 | } |
| 3949 | } |
| 3950 | fill_holes: |
| 3951 | /* So we did prev_leaf, now we need to move to the next leaf, but a few |
| 3952 | * things could have happened |
| 3953 | * |
| 3954 | * 1) A merge could have happened, so we could currently be on a leaf |
| 3955 | * that holds what we were copying in the first place. |
| 3956 | * 2) A split could have happened, and now not all of the items we want |
| 3957 | * are on the same leaf. |
| 3958 | * |
| 3959 | * So we need to adjust how we search for holes, we need to drop the |
| 3960 | * path and re-search for the first extent key we found, and then walk |
| 3961 | * forward until we hit the last one we copied. |
| 3962 | */ |
| 3963 | if (need_find_last_extent) { |
| 3964 | /* btrfs_prev_leaf could return 1 without releasing the path */ |
| 3965 | btrfs_release_path(src_path); |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3966 | ret = btrfs_search_slot(NULL, inode->root, &first_key, |
| 3967 | src_path, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3968 | if (ret < 0) |
| 3969 | return ret; |
| 3970 | ASSERT(ret == 0); |
| 3971 | src = src_path->nodes[0]; |
| 3972 | i = src_path->slots[0]; |
| 3973 | } else { |
| 3974 | i = start_slot; |
| 3975 | } |
| 3976 | |
| 3977 | /* |
| 3978 | * Ok so here we need to go through and fill in any holes we may have |
| 3979 | * to make sure that holes are punched for those areas in case they had |
| 3980 | * extents previously. |
| 3981 | */ |
| 3982 | while (!done) { |
| 3983 | u64 offset, len; |
| 3984 | u64 extent_end; |
| 3985 | |
| 3986 | if (i >= btrfs_header_nritems(src_path->nodes[0])) { |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3987 | ret = btrfs_next_leaf(inode->root, src_path); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3988 | if (ret < 0) |
| 3989 | return ret; |
| 3990 | ASSERT(ret == 0); |
| 3991 | src = src_path->nodes[0]; |
| 3992 | i = 0; |
Filipe Manana | 8434ec4 | 2018-03-26 23:59:12 +0100 | [diff] [blame] | 3993 | need_find_last_extent = true; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3994 | } |
| 3995 | |
| 3996 | btrfs_item_key_to_cpu(src, &key, i); |
| 3997 | if (!btrfs_comp_cpu_keys(&key, &last_key)) |
| 3998 | done = true; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3999 | if (key.objectid != btrfs_ino(inode) || |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4000 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 4001 | i++; |
| 4002 | continue; |
| 4003 | } |
| 4004 | extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item); |
| 4005 | if (btrfs_file_extent_type(src, extent) == |
| 4006 | BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4007 | len = btrfs_file_extent_inline_len(src, i, extent); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4008 | extent_end = ALIGN(key.offset + len, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4009 | fs_info->sectorsize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4010 | } else { |
| 4011 | len = btrfs_file_extent_num_bytes(src, extent); |
| 4012 | extent_end = key.offset + len; |
| 4013 | } |
| 4014 | i++; |
| 4015 | |
| 4016 | if (*last_extent == key.offset) { |
| 4017 | *last_extent = extent_end; |
| 4018 | continue; |
| 4019 | } |
| 4020 | offset = *last_extent; |
| 4021 | len = key.offset - *last_extent; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 4022 | ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode), |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4023 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4024 | if (ret) |
| 4025 | break; |
Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 4026 | *last_extent = extent_end; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4027 | } |
Filipe Manana | 4ee3fad | 2018-03-26 23:59:00 +0100 | [diff] [blame] | 4028 | |
| 4029 | /* |
| 4030 | * Check if there is a hole between the last extent found in our leaf |
| 4031 | * and the first extent in the next leaf. If there is one, we need to |
| 4032 | * log an explicit hole so that at replay time we can punch the hole. |
| 4033 | */ |
| 4034 | if (ret == 0 && |
| 4035 | key.objectid == btrfs_ino(inode) && |
| 4036 | key.type == BTRFS_EXTENT_DATA_KEY && |
| 4037 | i == btrfs_header_nritems(src_path->nodes[0])) { |
| 4038 | ret = btrfs_next_leaf(inode->root, src_path); |
| 4039 | need_find_last_extent = true; |
| 4040 | if (ret > 0) { |
| 4041 | ret = 0; |
| 4042 | } else if (ret == 0) { |
| 4043 | btrfs_item_key_to_cpu(src_path->nodes[0], &key, |
| 4044 | src_path->slots[0]); |
| 4045 | if (key.objectid == btrfs_ino(inode) && |
| 4046 | key.type == BTRFS_EXTENT_DATA_KEY && |
| 4047 | *last_extent < key.offset) { |
| 4048 | const u64 len = key.offset - *last_extent; |
| 4049 | |
| 4050 | ret = btrfs_insert_file_extent(trans, log, |
| 4051 | btrfs_ino(inode), |
| 4052 | *last_extent, 0, |
| 4053 | 0, len, 0, len, |
| 4054 | 0, 0, 0); |
| 4055 | } |
| 4056 | } |
| 4057 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4058 | /* |
| 4059 | * Need to let the callers know we dropped the path so they should |
| 4060 | * re-search. |
| 4061 | */ |
| 4062 | if (!ret && need_find_last_extent) |
| 4063 | ret = 1; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4064 | return ret; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4065 | } |
| 4066 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4067 | static int extent_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 4068 | { |
| 4069 | struct extent_map *em1, *em2; |
| 4070 | |
| 4071 | em1 = list_entry(a, struct extent_map, list); |
| 4072 | em2 = list_entry(b, struct extent_map, list); |
| 4073 | |
| 4074 | if (em1->start < em2->start) |
| 4075 | return -1; |
| 4076 | else if (em1->start > em2->start) |
| 4077 | return 1; |
| 4078 | return 0; |
| 4079 | } |
| 4080 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4081 | static int log_extent_csums(struct btrfs_trans_handle *trans, |
| 4082 | struct btrfs_inode *inode, |
| 4083 | struct btrfs_root *root, |
| 4084 | const struct extent_map *em) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4085 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4086 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4087 | struct btrfs_root *log = root->log_root; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4088 | u64 csum_offset; |
| 4089 | u64 csum_len; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4090 | LIST_HEAD(ordered_sums); |
| 4091 | int ret = 0; |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4092 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4093 | if (inode->flags & BTRFS_INODE_NODATASUM || |
| 4094 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4095 | em->block_start == EXTENT_MAP_HOLE) |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4096 | return 0; |
| 4097 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4098 | /* 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] | 4099 | if (em->compress_type) { |
| 4100 | csum_offset = 0; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4101 | csum_len = max(em->block_len, em->orig_block_len); |
Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4102 | } else { |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4103 | csum_offset = em->mod_start - em->start; |
| 4104 | csum_len = em->mod_len; |
Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4105 | } |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4106 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4107 | /* block start is already adjusted for the file extent offset. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4108 | ret = btrfs_lookup_csums_range(fs_info->csum_root, |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4109 | em->block_start + csum_offset, |
| 4110 | em->block_start + csum_offset + |
| 4111 | csum_len - 1, &ordered_sums, 0); |
| 4112 | if (ret) |
| 4113 | return ret; |
| 4114 | |
| 4115 | while (!list_empty(&ordered_sums)) { |
| 4116 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, |
| 4117 | struct btrfs_ordered_sum, |
| 4118 | list); |
| 4119 | if (!ret) |
| 4120 | ret = btrfs_csum_file_blocks(trans, log, sums); |
| 4121 | list_del(&sums->list); |
| 4122 | kfree(sums); |
| 4123 | } |
| 4124 | |
| 4125 | return ret; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4126 | } |
| 4127 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4128 | static int log_one_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4129 | struct btrfs_inode *inode, struct btrfs_root *root, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4130 | const struct extent_map *em, |
| 4131 | struct btrfs_path *path, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4132 | struct btrfs_log_ctx *ctx) |
| 4133 | { |
| 4134 | struct btrfs_root *log = root->log_root; |
| 4135 | struct btrfs_file_extent_item *fi; |
| 4136 | struct extent_buffer *leaf; |
| 4137 | struct btrfs_map_token token; |
| 4138 | struct btrfs_key key; |
| 4139 | u64 extent_offset = em->start - em->orig_start; |
| 4140 | u64 block_len; |
| 4141 | int ret; |
| 4142 | int extent_inserted = 0; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4143 | |
Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4144 | ret = log_extent_csums(trans, inode, root, em); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4145 | if (ret) |
| 4146 | return ret; |
| 4147 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4148 | btrfs_init_map_token(&token); |
| 4149 | |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4150 | ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4151 | em->start + em->len, NULL, 0, 1, |
| 4152 | sizeof(*fi), &extent_inserted); |
| 4153 | if (ret) |
| 4154 | return ret; |
| 4155 | |
| 4156 | if (!extent_inserted) { |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4157 | key.objectid = btrfs_ino(inode); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4158 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4159 | key.offset = em->start; |
| 4160 | |
| 4161 | ret = btrfs_insert_empty_item(trans, log, path, &key, |
| 4162 | sizeof(*fi)); |
| 4163 | if (ret) |
| 4164 | return ret; |
| 4165 | } |
| 4166 | leaf = path->nodes[0]; |
| 4167 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4168 | struct btrfs_file_extent_item); |
| 4169 | |
Josef Bacik | 50d9aa9 | 2014-11-21 14:52:38 -0500 | [diff] [blame] | 4170 | btrfs_set_token_file_extent_generation(leaf, fi, trans->transid, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4171 | &token); |
| 4172 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 4173 | btrfs_set_token_file_extent_type(leaf, fi, |
| 4174 | BTRFS_FILE_EXTENT_PREALLOC, |
| 4175 | &token); |
| 4176 | else |
| 4177 | btrfs_set_token_file_extent_type(leaf, fi, |
| 4178 | BTRFS_FILE_EXTENT_REG, |
| 4179 | &token); |
| 4180 | |
| 4181 | block_len = max(em->block_len, em->orig_block_len); |
| 4182 | if (em->compress_type != BTRFS_COMPRESS_NONE) { |
| 4183 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, |
| 4184 | em->block_start, |
| 4185 | &token); |
| 4186 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len, |
| 4187 | &token); |
| 4188 | } else if (em->block_start < EXTENT_MAP_LAST_BYTE) { |
| 4189 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, |
| 4190 | em->block_start - |
| 4191 | extent_offset, &token); |
| 4192 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len, |
| 4193 | &token); |
| 4194 | } else { |
| 4195 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token); |
| 4196 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0, |
| 4197 | &token); |
| 4198 | } |
| 4199 | |
| 4200 | btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token); |
| 4201 | btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token); |
| 4202 | btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token); |
| 4203 | btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type, |
| 4204 | &token); |
| 4205 | btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token); |
| 4206 | btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token); |
| 4207 | btrfs_mark_buffer_dirty(leaf); |
| 4208 | |
| 4209 | btrfs_release_path(path); |
| 4210 | |
| 4211 | return ret; |
| 4212 | } |
| 4213 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4214 | /* |
| 4215 | * Log all prealloc extents beyond the inode's i_size to make sure we do not |
| 4216 | * lose them after doing a fast fsync and replaying the log. We scan the |
| 4217 | * subvolume's root instead of iterating the inode's extent map tree because |
| 4218 | * otherwise we can log incorrect extent items based on extent map conversion. |
| 4219 | * That can happen due to the fact that extent maps are merged when they |
| 4220 | * are not in the extent map tree's list of modified extents. |
| 4221 | */ |
| 4222 | static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans, |
| 4223 | struct btrfs_inode *inode, |
| 4224 | struct btrfs_path *path) |
| 4225 | { |
| 4226 | struct btrfs_root *root = inode->root; |
| 4227 | struct btrfs_key key; |
| 4228 | const u64 i_size = i_size_read(&inode->vfs_inode); |
| 4229 | const u64 ino = btrfs_ino(inode); |
| 4230 | struct btrfs_path *dst_path = NULL; |
| 4231 | u64 last_extent = (u64)-1; |
| 4232 | int ins_nr = 0; |
| 4233 | int start_slot; |
| 4234 | int ret; |
| 4235 | |
| 4236 | if (!(inode->flags & BTRFS_INODE_PREALLOC)) |
| 4237 | return 0; |
| 4238 | |
| 4239 | key.objectid = ino; |
| 4240 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4241 | key.offset = i_size; |
| 4242 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4243 | if (ret < 0) |
| 4244 | goto out; |
| 4245 | |
| 4246 | while (true) { |
| 4247 | struct extent_buffer *leaf = path->nodes[0]; |
| 4248 | int slot = path->slots[0]; |
| 4249 | |
| 4250 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4251 | if (ins_nr > 0) { |
| 4252 | ret = copy_items(trans, inode, dst_path, path, |
| 4253 | &last_extent, start_slot, |
| 4254 | ins_nr, 1, 0); |
| 4255 | if (ret < 0) |
| 4256 | goto out; |
| 4257 | ins_nr = 0; |
| 4258 | } |
| 4259 | ret = btrfs_next_leaf(root, path); |
| 4260 | if (ret < 0) |
| 4261 | goto out; |
| 4262 | if (ret > 0) { |
| 4263 | ret = 0; |
| 4264 | break; |
| 4265 | } |
| 4266 | continue; |
| 4267 | } |
| 4268 | |
| 4269 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 4270 | if (key.objectid > ino) |
| 4271 | break; |
| 4272 | if (WARN_ON_ONCE(key.objectid < ino) || |
| 4273 | key.type < BTRFS_EXTENT_DATA_KEY || |
| 4274 | key.offset < i_size) { |
| 4275 | path->slots[0]++; |
| 4276 | continue; |
| 4277 | } |
| 4278 | if (last_extent == (u64)-1) { |
| 4279 | last_extent = key.offset; |
| 4280 | /* |
| 4281 | * Avoid logging extent items logged in past fsync calls |
| 4282 | * and leading to duplicate keys in the log tree. |
| 4283 | */ |
| 4284 | do { |
| 4285 | ret = btrfs_truncate_inode_items(trans, |
| 4286 | root->log_root, |
| 4287 | &inode->vfs_inode, |
| 4288 | i_size, |
| 4289 | BTRFS_EXTENT_DATA_KEY); |
| 4290 | } while (ret == -EAGAIN); |
| 4291 | if (ret) |
| 4292 | goto out; |
| 4293 | } |
| 4294 | if (ins_nr == 0) |
| 4295 | start_slot = slot; |
| 4296 | ins_nr++; |
| 4297 | path->slots[0]++; |
| 4298 | if (!dst_path) { |
| 4299 | dst_path = btrfs_alloc_path(); |
| 4300 | if (!dst_path) { |
| 4301 | ret = -ENOMEM; |
| 4302 | goto out; |
| 4303 | } |
| 4304 | } |
| 4305 | } |
| 4306 | if (ins_nr > 0) { |
| 4307 | ret = copy_items(trans, inode, dst_path, path, &last_extent, |
| 4308 | start_slot, ins_nr, 1, 0); |
| 4309 | if (ret > 0) |
| 4310 | ret = 0; |
| 4311 | } |
| 4312 | out: |
| 4313 | btrfs_release_path(path); |
| 4314 | btrfs_free_path(dst_path); |
| 4315 | return ret; |
| 4316 | } |
| 4317 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4318 | static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, |
| 4319 | struct btrfs_root *root, |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4320 | struct btrfs_inode *inode, |
Miao Xie | 827463c | 2014-01-14 20:31:51 +0800 | [diff] [blame] | 4321 | struct btrfs_path *path, |
Filipe Manana | de0ee0e | 2016-01-21 10:17:54 +0000 | [diff] [blame] | 4322 | struct btrfs_log_ctx *ctx, |
| 4323 | const u64 start, |
| 4324 | const u64 end) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4325 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4326 | struct extent_map *em, *n; |
| 4327 | struct list_head extents; |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4328 | struct extent_map_tree *tree = &inode->extent_tree; |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4329 | u64 logged_start, logged_end; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4330 | u64 test_gen; |
| 4331 | int ret = 0; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4332 | int num = 0; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4333 | |
| 4334 | INIT_LIST_HEAD(&extents); |
| 4335 | |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4336 | down_write(&inode->dio_sem); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4337 | write_lock(&tree->lock); |
| 4338 | test_gen = root->fs_info->last_trans_committed; |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4339 | logged_start = start; |
| 4340 | logged_end = end; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4341 | |
| 4342 | list_for_each_entry_safe(em, n, &tree->modified_extents, list) { |
| 4343 | list_del_init(&em->list); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4344 | /* |
| 4345 | * Just an arbitrary number, this can be really CPU intensive |
| 4346 | * once we start getting a lot of extents, and really once we |
| 4347 | * have a bunch of extents we just want to commit since it will |
| 4348 | * be faster. |
| 4349 | */ |
| 4350 | if (++num > 32768) { |
| 4351 | list_del_init(&tree->modified_extents); |
| 4352 | ret = -EFBIG; |
| 4353 | goto process; |
| 4354 | } |
| 4355 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4356 | if (em->generation <= test_gen) |
| 4357 | continue; |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4358 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4359 | /* We log prealloc extents beyond eof later. */ |
| 4360 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && |
| 4361 | em->start >= i_size_read(&inode->vfs_inode)) |
| 4362 | continue; |
| 4363 | |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4364 | if (em->start < logged_start) |
| 4365 | logged_start = em->start; |
| 4366 | if ((em->start + em->len - 1) > logged_end) |
| 4367 | logged_end = em->start + em->len - 1; |
| 4368 | |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4369 | /* Need a ref to keep it from getting evicted from cache */ |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 4370 | refcount_inc(&em->refs); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4371 | set_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4372 | list_add_tail(&em->list, &extents); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4373 | num++; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | list_sort(NULL, &extents, extent_cmp); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4377 | process: |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4378 | while (!list_empty(&extents)) { |
| 4379 | em = list_entry(extents.next, struct extent_map, list); |
| 4380 | |
| 4381 | list_del_init(&em->list); |
| 4382 | |
| 4383 | /* |
| 4384 | * If we had an error we just need to delete everybody from our |
| 4385 | * private list. |
| 4386 | */ |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4387 | if (ret) { |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 4388 | clear_em_logging(tree, em); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4389 | free_extent_map(em); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4390 | continue; |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | write_unlock(&tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4394 | |
Josef Bacik | a2120a4 | 2018-05-23 11:58:35 -0400 | [diff] [blame^] | 4395 | ret = log_one_extent(trans, inode, root, em, path, ctx); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4396 | write_lock(&tree->lock); |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 4397 | clear_em_logging(tree, em); |
| 4398 | free_extent_map(em); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4399 | } |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4400 | WARN_ON(!list_empty(&extents)); |
| 4401 | write_unlock(&tree->lock); |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4402 | up_write(&inode->dio_sem); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4403 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4404 | btrfs_release_path(path); |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4405 | if (!ret) |
| 4406 | ret = btrfs_log_prealloc_extents(trans, inode, path); |
| 4407 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4408 | return ret; |
| 4409 | } |
| 4410 | |
Nikolay Borisov | 481b01c | 2017-01-18 00:31:34 +0200 | [diff] [blame] | 4411 | 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] | 4412 | struct btrfs_path *path, u64 *size_ret) |
| 4413 | { |
| 4414 | struct btrfs_key key; |
| 4415 | int ret; |
| 4416 | |
Nikolay Borisov | 481b01c | 2017-01-18 00:31:34 +0200 | [diff] [blame] | 4417 | key.objectid = btrfs_ino(inode); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4418 | key.type = BTRFS_INODE_ITEM_KEY; |
| 4419 | key.offset = 0; |
| 4420 | |
| 4421 | ret = btrfs_search_slot(NULL, log, &key, path, 0, 0); |
| 4422 | if (ret < 0) { |
| 4423 | return ret; |
| 4424 | } else if (ret > 0) { |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 4425 | *size_ret = 0; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4426 | } else { |
| 4427 | struct btrfs_inode_item *item; |
| 4428 | |
| 4429 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4430 | struct btrfs_inode_item); |
| 4431 | *size_ret = btrfs_inode_size(path->nodes[0], item); |
| 4432 | } |
| 4433 | |
| 4434 | btrfs_release_path(path); |
| 4435 | return 0; |
| 4436 | } |
| 4437 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4438 | /* |
| 4439 | * At the moment we always log all xattrs. This is to figure out at log replay |
| 4440 | * time which xattrs must have their deletion replayed. If a xattr is missing |
| 4441 | * in the log tree and exists in the fs/subvol tree, we delete it. This is |
| 4442 | * because if a xattr is deleted, the inode is fsynced and a power failure |
| 4443 | * happens, causing the log to be replayed the next time the fs is mounted, |
| 4444 | * we want the xattr to not exist anymore (same behaviour as other filesystems |
| 4445 | * with a journal, ext3/4, xfs, f2fs, etc). |
| 4446 | */ |
| 4447 | static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans, |
| 4448 | struct btrfs_root *root, |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4449 | struct btrfs_inode *inode, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4450 | struct btrfs_path *path, |
| 4451 | struct btrfs_path *dst_path) |
| 4452 | { |
| 4453 | int ret; |
| 4454 | struct btrfs_key key; |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4455 | const u64 ino = btrfs_ino(inode); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4456 | int ins_nr = 0; |
| 4457 | int start_slot = 0; |
| 4458 | |
| 4459 | key.objectid = ino; |
| 4460 | key.type = BTRFS_XATTR_ITEM_KEY; |
| 4461 | key.offset = 0; |
| 4462 | |
| 4463 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4464 | if (ret < 0) |
| 4465 | return ret; |
| 4466 | |
| 4467 | while (true) { |
| 4468 | int slot = path->slots[0]; |
| 4469 | struct extent_buffer *leaf = path->nodes[0]; |
| 4470 | int nritems = btrfs_header_nritems(leaf); |
| 4471 | |
| 4472 | if (slot >= nritems) { |
| 4473 | if (ins_nr > 0) { |
| 4474 | u64 last_extent = 0; |
| 4475 | |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4476 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4477 | &last_extent, start_slot, |
| 4478 | ins_nr, 1, 0); |
| 4479 | /* can't be 1, extent items aren't processed */ |
| 4480 | ASSERT(ret <= 0); |
| 4481 | if (ret < 0) |
| 4482 | return ret; |
| 4483 | ins_nr = 0; |
| 4484 | } |
| 4485 | ret = btrfs_next_leaf(root, path); |
| 4486 | if (ret < 0) |
| 4487 | return ret; |
| 4488 | else if (ret > 0) |
| 4489 | break; |
| 4490 | continue; |
| 4491 | } |
| 4492 | |
| 4493 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 4494 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) |
| 4495 | break; |
| 4496 | |
| 4497 | if (ins_nr == 0) |
| 4498 | start_slot = slot; |
| 4499 | ins_nr++; |
| 4500 | path->slots[0]++; |
| 4501 | cond_resched(); |
| 4502 | } |
| 4503 | if (ins_nr > 0) { |
| 4504 | u64 last_extent = 0; |
| 4505 | |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4506 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4507 | &last_extent, start_slot, |
| 4508 | ins_nr, 1, 0); |
| 4509 | /* can't be 1, extent items aren't processed */ |
| 4510 | ASSERT(ret <= 0); |
| 4511 | if (ret < 0) |
| 4512 | return ret; |
| 4513 | } |
| 4514 | |
| 4515 | return 0; |
| 4516 | } |
| 4517 | |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4518 | /* |
| 4519 | * If the no holes feature is enabled we need to make sure any hole between the |
| 4520 | * last extent and the i_size of our inode is explicitly marked in the log. This |
| 4521 | * is to make sure that doing something like: |
| 4522 | * |
| 4523 | * 1) create file with 128Kb of data |
| 4524 | * 2) truncate file to 64Kb |
| 4525 | * 3) truncate file to 256Kb |
| 4526 | * 4) fsync file |
| 4527 | * 5) <crash/power failure> |
| 4528 | * 6) mount fs and trigger log replay |
| 4529 | * |
| 4530 | * Will give us a file with a size of 256Kb, the first 64Kb of data match what |
| 4531 | * the file had in its first 64Kb of data at step 1 and the last 192Kb of the |
| 4532 | * file correspond to a hole. The presence of explicit holes in a log tree is |
| 4533 | * what guarantees that log replay will remove/adjust file extent items in the |
| 4534 | * fs/subvol tree. |
| 4535 | * |
| 4536 | * Here we do not need to care about holes between extents, that is already done |
| 4537 | * by copy_items(). We also only need to do this in the full sync path, where we |
| 4538 | * lookup for extents from the fs/subvol tree only. In the fast path case, we |
| 4539 | * lookup the list of modified extent maps and if any represents a hole, we |
| 4540 | * insert a corresponding extent representing a hole in the log tree. |
| 4541 | */ |
| 4542 | static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans, |
| 4543 | struct btrfs_root *root, |
Nikolay Borisov | a0308dd | 2017-01-18 00:31:38 +0200 | [diff] [blame] | 4544 | struct btrfs_inode *inode, |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4545 | struct btrfs_path *path) |
| 4546 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4547 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4548 | int ret; |
| 4549 | struct btrfs_key key; |
| 4550 | u64 hole_start; |
| 4551 | u64 hole_size; |
| 4552 | struct extent_buffer *leaf; |
| 4553 | struct btrfs_root *log = root->log_root; |
Nikolay Borisov | a0308dd | 2017-01-18 00:31:38 +0200 | [diff] [blame] | 4554 | const u64 ino = btrfs_ino(inode); |
| 4555 | const u64 i_size = i_size_read(&inode->vfs_inode); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4556 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4557 | if (!btrfs_fs_incompat(fs_info, NO_HOLES)) |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4558 | return 0; |
| 4559 | |
| 4560 | key.objectid = ino; |
| 4561 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4562 | key.offset = (u64)-1; |
| 4563 | |
| 4564 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4565 | ASSERT(ret != 0); |
| 4566 | if (ret < 0) |
| 4567 | return ret; |
| 4568 | |
| 4569 | ASSERT(path->slots[0] > 0); |
| 4570 | path->slots[0]--; |
| 4571 | leaf = path->nodes[0]; |
| 4572 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4573 | |
| 4574 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) { |
| 4575 | /* inode does not have any extents */ |
| 4576 | hole_start = 0; |
| 4577 | hole_size = i_size; |
| 4578 | } else { |
| 4579 | struct btrfs_file_extent_item *extent; |
| 4580 | u64 len; |
| 4581 | |
| 4582 | /* |
| 4583 | * If there's an extent beyond i_size, an explicit hole was |
| 4584 | * already inserted by copy_items(). |
| 4585 | */ |
| 4586 | if (key.offset >= i_size) |
| 4587 | return 0; |
| 4588 | |
| 4589 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 4590 | struct btrfs_file_extent_item); |
| 4591 | |
| 4592 | if (btrfs_file_extent_type(leaf, extent) == |
| 4593 | BTRFS_FILE_EXTENT_INLINE) { |
| 4594 | len = btrfs_file_extent_inline_len(leaf, |
| 4595 | path->slots[0], |
| 4596 | extent); |
Filipe Manana | 6399fb5 | 2017-07-28 15:22:36 +0100 | [diff] [blame] | 4597 | ASSERT(len == i_size || |
| 4598 | (len == fs_info->sectorsize && |
| 4599 | btrfs_file_extent_compression(leaf, extent) != |
| 4600 | BTRFS_COMPRESS_NONE)); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4601 | return 0; |
| 4602 | } |
| 4603 | |
| 4604 | len = btrfs_file_extent_num_bytes(leaf, extent); |
| 4605 | /* Last extent goes beyond i_size, no need to log a hole. */ |
| 4606 | if (key.offset + len > i_size) |
| 4607 | return 0; |
| 4608 | hole_start = key.offset + len; |
| 4609 | hole_size = i_size - hole_start; |
| 4610 | } |
| 4611 | btrfs_release_path(path); |
| 4612 | |
| 4613 | /* Last extent ends at i_size. */ |
| 4614 | if (hole_size == 0) |
| 4615 | return 0; |
| 4616 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4617 | hole_size = ALIGN(hole_size, fs_info->sectorsize); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4618 | ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0, |
| 4619 | hole_size, 0, hole_size, 0, 0, 0); |
| 4620 | return ret; |
| 4621 | } |
| 4622 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4623 | /* |
| 4624 | * When we are logging a new inode X, check if it doesn't have a reference that |
| 4625 | * matches the reference from some other inode Y created in a past transaction |
| 4626 | * and that was renamed in the current transaction. If we don't do this, then at |
| 4627 | * log replay time we can lose inode Y (and all its files if it's a directory): |
| 4628 | * |
| 4629 | * mkdir /mnt/x |
| 4630 | * echo "hello world" > /mnt/x/foobar |
| 4631 | * sync |
| 4632 | * mv /mnt/x /mnt/y |
| 4633 | * mkdir /mnt/x # or touch /mnt/x |
| 4634 | * xfs_io -c fsync /mnt/x |
| 4635 | * <power fail> |
| 4636 | * mount fs, trigger log replay |
| 4637 | * |
| 4638 | * After the log replay procedure, we would lose the first directory and all its |
| 4639 | * files (file foobar). |
| 4640 | * For the case where inode Y is not a directory we simply end up losing it: |
| 4641 | * |
| 4642 | * echo "123" > /mnt/foo |
| 4643 | * sync |
| 4644 | * mv /mnt/foo /mnt/bar |
| 4645 | * echo "abc" > /mnt/foo |
| 4646 | * xfs_io -c fsync /mnt/foo |
| 4647 | * <power fail> |
| 4648 | * |
| 4649 | * We also need this for cases where a snapshot entry is replaced by some other |
| 4650 | * entry (file or directory) otherwise we end up with an unreplayable log due to |
| 4651 | * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as |
| 4652 | * if it were a regular entry: |
| 4653 | * |
| 4654 | * mkdir /mnt/x |
| 4655 | * btrfs subvolume snapshot /mnt /mnt/x/snap |
| 4656 | * btrfs subvolume delete /mnt/x/snap |
| 4657 | * rmdir /mnt/x |
| 4658 | * mkdir /mnt/x |
| 4659 | * fsync /mnt/x or fsync some new file inside it |
| 4660 | * <power fail> |
| 4661 | * |
| 4662 | * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in |
| 4663 | * the same transaction. |
| 4664 | */ |
| 4665 | static int btrfs_check_ref_name_override(struct extent_buffer *eb, |
| 4666 | const int slot, |
| 4667 | const struct btrfs_key *key, |
Nikolay Borisov | 4791c8f | 2017-01-18 00:31:35 +0200 | [diff] [blame] | 4668 | struct btrfs_inode *inode, |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4669 | u64 *other_ino) |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4670 | { |
| 4671 | int ret; |
| 4672 | struct btrfs_path *search_path; |
| 4673 | char *name = NULL; |
| 4674 | u32 name_len = 0; |
| 4675 | u32 item_size = btrfs_item_size_nr(eb, slot); |
| 4676 | u32 cur_offset = 0; |
| 4677 | unsigned long ptr = btrfs_item_ptr_offset(eb, slot); |
| 4678 | |
| 4679 | search_path = btrfs_alloc_path(); |
| 4680 | if (!search_path) |
| 4681 | return -ENOMEM; |
| 4682 | search_path->search_commit_root = 1; |
| 4683 | search_path->skip_locking = 1; |
| 4684 | |
| 4685 | while (cur_offset < item_size) { |
| 4686 | u64 parent; |
| 4687 | u32 this_name_len; |
| 4688 | u32 this_len; |
| 4689 | unsigned long name_ptr; |
| 4690 | struct btrfs_dir_item *di; |
| 4691 | |
| 4692 | if (key->type == BTRFS_INODE_REF_KEY) { |
| 4693 | struct btrfs_inode_ref *iref; |
| 4694 | |
| 4695 | iref = (struct btrfs_inode_ref *)(ptr + cur_offset); |
| 4696 | parent = key->offset; |
| 4697 | this_name_len = btrfs_inode_ref_name_len(eb, iref); |
| 4698 | name_ptr = (unsigned long)(iref + 1); |
| 4699 | this_len = sizeof(*iref) + this_name_len; |
| 4700 | } else { |
| 4701 | struct btrfs_inode_extref *extref; |
| 4702 | |
| 4703 | extref = (struct btrfs_inode_extref *)(ptr + |
| 4704 | cur_offset); |
| 4705 | parent = btrfs_inode_extref_parent(eb, extref); |
| 4706 | this_name_len = btrfs_inode_extref_name_len(eb, extref); |
| 4707 | name_ptr = (unsigned long)&extref->name; |
| 4708 | this_len = sizeof(*extref) + this_name_len; |
| 4709 | } |
| 4710 | |
| 4711 | if (this_name_len > name_len) { |
| 4712 | char *new_name; |
| 4713 | |
| 4714 | new_name = krealloc(name, this_name_len, GFP_NOFS); |
| 4715 | if (!new_name) { |
| 4716 | ret = -ENOMEM; |
| 4717 | goto out; |
| 4718 | } |
| 4719 | name_len = this_name_len; |
| 4720 | name = new_name; |
| 4721 | } |
| 4722 | |
| 4723 | read_extent_buffer(eb, name, name_ptr, this_name_len); |
Nikolay Borisov | 4791c8f | 2017-01-18 00:31:35 +0200 | [diff] [blame] | 4724 | di = btrfs_lookup_dir_item(NULL, inode->root, search_path, |
| 4725 | parent, name, this_name_len, 0); |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4726 | if (di && !IS_ERR(di)) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4727 | struct btrfs_key di_key; |
| 4728 | |
| 4729 | btrfs_dir_item_key_to_cpu(search_path->nodes[0], |
| 4730 | di, &di_key); |
| 4731 | if (di_key.type == BTRFS_INODE_ITEM_KEY) { |
| 4732 | ret = 1; |
| 4733 | *other_ino = di_key.objectid; |
| 4734 | } else { |
| 4735 | ret = -EAGAIN; |
| 4736 | } |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4737 | goto out; |
| 4738 | } else if (IS_ERR(di)) { |
| 4739 | ret = PTR_ERR(di); |
| 4740 | goto out; |
| 4741 | } |
| 4742 | btrfs_release_path(search_path); |
| 4743 | |
| 4744 | cur_offset += this_len; |
| 4745 | } |
| 4746 | ret = 0; |
| 4747 | out: |
| 4748 | btrfs_free_path(search_path); |
| 4749 | kfree(name); |
| 4750 | return ret; |
| 4751 | } |
| 4752 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4753 | /* log a single inode in the tree log. |
| 4754 | * At least one parent directory for this inode must exist in the tree |
| 4755 | * or be logged already. |
| 4756 | * |
| 4757 | * Any items from this inode changed by the current transaction are copied |
| 4758 | * to the log tree. An extra reference is taken on any extents in this |
| 4759 | * file, allowing us to avoid a whole pile of corner cases around logging |
| 4760 | * blocks that have been removed from the tree. |
| 4761 | * |
| 4762 | * See LOG_INODE_ALL and related defines for a description of what inode_only |
| 4763 | * does. |
| 4764 | * |
| 4765 | * This handles both files and directories. |
| 4766 | */ |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4767 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4768 | struct btrfs_root *root, struct btrfs_inode *inode, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 4769 | int inode_only, |
| 4770 | const loff_t start, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4771 | const loff_t end, |
| 4772 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4773 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4774 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4775 | struct btrfs_path *path; |
| 4776 | struct btrfs_path *dst_path; |
| 4777 | struct btrfs_key min_key; |
| 4778 | struct btrfs_key max_key; |
| 4779 | struct btrfs_root *log = root->log_root; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4780 | u64 last_extent = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4781 | int err = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4782 | int ret; |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 4783 | int nritems; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4784 | int ins_start_slot = 0; |
| 4785 | int ins_nr; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4786 | bool fast_search = false; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4787 | u64 ino = btrfs_ino(inode); |
| 4788 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4789 | u64 logged_isize = 0; |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 4790 | bool need_log_inode_item = true; |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 4791 | bool xattrs_logged = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4792 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4793 | path = btrfs_alloc_path(); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 4794 | if (!path) |
| 4795 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4796 | dst_path = btrfs_alloc_path(); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 4797 | if (!dst_path) { |
| 4798 | btrfs_free_path(path); |
| 4799 | return -ENOMEM; |
| 4800 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4801 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4802 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4803 | min_key.type = BTRFS_INODE_ITEM_KEY; |
| 4804 | min_key.offset = 0; |
| 4805 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4806 | max_key.objectid = ino; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4807 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4808 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4809 | /* today the code can only do partial logging of directories */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4810 | if (S_ISDIR(inode->vfs_inode.i_mode) || |
Miao Xie | 5269b67 | 2012-11-01 07:35:23 +0000 | [diff] [blame] | 4811 | (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4812 | &inode->runtime_flags) && |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4813 | inode_only >= LOG_INODE_EXISTS)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4814 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
| 4815 | else |
| 4816 | max_key.type = (u8)-1; |
| 4817 | max_key.offset = (u64)-1; |
| 4818 | |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4819 | /* |
| 4820 | * Only run delayed items if we are a dir or a new file. |
| 4821 | * Otherwise commit the delayed inode only, which is needed in |
| 4822 | * order for the log replay code to mark inodes for link count |
| 4823 | * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items). |
| 4824 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4825 | if (S_ISDIR(inode->vfs_inode.i_mode) || |
| 4826 | inode->generation > fs_info->last_trans_committed) |
| 4827 | ret = btrfs_commit_inode_delayed_items(trans, inode); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4828 | else |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4829 | ret = btrfs_commit_inode_delayed_inode(inode); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4830 | |
| 4831 | if (ret) { |
| 4832 | btrfs_free_path(path); |
| 4833 | btrfs_free_path(dst_path); |
| 4834 | return ret; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4835 | } |
| 4836 | |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4837 | if (inode_only == LOG_OTHER_INODE) { |
| 4838 | inode_only = LOG_INODE_EXISTS; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4839 | mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING); |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4840 | } else { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4841 | mutex_lock(&inode->log_mutex); |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4842 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4843 | |
Filipe Manana | 5e33a2b | 2016-02-25 23:19:38 +0000 | [diff] [blame] | 4844 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4845 | * a brute force approach to making sure we get the most uptodate |
| 4846 | * copies of everything. |
| 4847 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4848 | if (S_ISDIR(inode->vfs_inode.i_mode)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4849 | int max_key_type = BTRFS_DIR_LOG_INDEX_KEY; |
| 4850 | |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4851 | if (inode_only == LOG_INODE_EXISTS) |
| 4852 | max_key_type = BTRFS_XATTR_ITEM_KEY; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4853 | ret = drop_objectid_items(trans, log, path, ino, max_key_type); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4854 | } else { |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4855 | if (inode_only == LOG_INODE_EXISTS) { |
| 4856 | /* |
| 4857 | * Make sure the new inode item we write to the log has |
| 4858 | * the same isize as the current one (if it exists). |
| 4859 | * This is necessary to prevent data loss after log |
| 4860 | * replay, and also to prevent doing a wrong expanding |
| 4861 | * truncate - for e.g. create file, write 4K into offset |
| 4862 | * 0, fsync, write 4K into offset 4096, add hard link, |
| 4863 | * fsync some other file (to sync log), power fail - if |
| 4864 | * we use the inode's current i_size, after log replay |
| 4865 | * we get a 8Kb file, with the last 4Kb extent as a hole |
| 4866 | * (zeroes), as if an expanding truncate happened, |
| 4867 | * instead of getting a file of 4Kb only. |
| 4868 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4869 | err = logged_inode_size(log, inode, path, &logged_isize); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4870 | if (err) |
| 4871 | goto out_unlock; |
| 4872 | } |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4873 | if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4874 | &inode->runtime_flags)) { |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4875 | if (inode_only == LOG_INODE_EXISTS) { |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4876 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4877 | ret = drop_objectid_items(trans, log, path, ino, |
| 4878 | max_key.type); |
| 4879 | } else { |
| 4880 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4881 | &inode->runtime_flags); |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4882 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4883 | &inode->runtime_flags); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4884 | while(1) { |
| 4885 | ret = btrfs_truncate_inode_items(trans, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4886 | log, &inode->vfs_inode, 0, 0); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4887 | if (ret != -EAGAIN) |
| 4888 | break; |
| 4889 | } |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4890 | } |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4891 | } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4892 | &inode->runtime_flags) || |
Josef Bacik | 6cfab85 | 2013-11-12 16:25:58 -0500 | [diff] [blame] | 4893 | inode_only == LOG_INODE_EXISTS) { |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4894 | if (inode_only == LOG_INODE_ALL) |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4895 | fast_search = true; |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4896 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4897 | ret = drop_objectid_items(trans, log, path, ino, |
| 4898 | max_key.type); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4899 | } else { |
Liu Bo | 183f37f | 2012-11-01 06:38:47 +0000 | [diff] [blame] | 4900 | if (inode_only == LOG_INODE_ALL) |
| 4901 | fast_search = true; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4902 | goto log_extents; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4903 | } |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4904 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4905 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4906 | if (ret) { |
| 4907 | err = ret; |
| 4908 | goto out_unlock; |
| 4909 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4910 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4911 | while (1) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4912 | ins_nr = 0; |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 4913 | ret = btrfs_search_forward(root, &min_key, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4914 | path, trans->transid); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 4915 | if (ret < 0) { |
| 4916 | err = ret; |
| 4917 | goto out_unlock; |
| 4918 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4919 | if (ret != 0) |
| 4920 | break; |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 4921 | again: |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4922 | /* note, ins_nr might be > 0 here, cleanup outside the loop */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4923 | if (min_key.objectid != ino) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4924 | break; |
| 4925 | if (min_key.type > max_key.type) |
| 4926 | break; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4927 | |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 4928 | if (min_key.type == BTRFS_INODE_ITEM_KEY) |
| 4929 | need_log_inode_item = false; |
| 4930 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4931 | if ((min_key.type == BTRFS_INODE_REF_KEY || |
| 4932 | min_key.type == BTRFS_INODE_EXTREF_KEY) && |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4933 | inode->generation == trans->transid) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4934 | u64 other_ino = 0; |
| 4935 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4936 | ret = btrfs_check_ref_name_override(path->nodes[0], |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4937 | path->slots[0], &min_key, inode, |
| 4938 | &other_ino); |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4939 | if (ret < 0) { |
| 4940 | err = ret; |
| 4941 | goto out_unlock; |
Filipe Manana | 28a2359 | 2016-08-23 21:13:51 +0100 | [diff] [blame] | 4942 | } else if (ret > 0 && ctx && |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4943 | other_ino != btrfs_ino(BTRFS_I(ctx->inode))) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4944 | struct btrfs_key inode_key; |
| 4945 | struct inode *other_inode; |
| 4946 | |
| 4947 | if (ins_nr > 0) { |
| 4948 | ins_nr++; |
| 4949 | } else { |
| 4950 | ins_nr = 1; |
| 4951 | ins_start_slot = path->slots[0]; |
| 4952 | } |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4953 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4954 | &last_extent, ins_start_slot, |
| 4955 | ins_nr, inode_only, |
| 4956 | logged_isize); |
| 4957 | if (ret < 0) { |
| 4958 | err = ret; |
| 4959 | goto out_unlock; |
| 4960 | } |
| 4961 | ins_nr = 0; |
| 4962 | btrfs_release_path(path); |
| 4963 | inode_key.objectid = other_ino; |
| 4964 | inode_key.type = BTRFS_INODE_ITEM_KEY; |
| 4965 | inode_key.offset = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4966 | other_inode = btrfs_iget(fs_info->sb, |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4967 | &inode_key, root, |
| 4968 | NULL); |
| 4969 | /* |
| 4970 | * If the other inode that had a conflicting dir |
| 4971 | * entry was deleted in the current transaction, |
| 4972 | * we don't need to do more work nor fallback to |
| 4973 | * a transaction commit. |
| 4974 | */ |
| 4975 | if (IS_ERR(other_inode) && |
| 4976 | PTR_ERR(other_inode) == -ENOENT) { |
| 4977 | goto next_key; |
| 4978 | } else if (IS_ERR(other_inode)) { |
| 4979 | err = PTR_ERR(other_inode); |
| 4980 | goto out_unlock; |
| 4981 | } |
| 4982 | /* |
| 4983 | * We are safe logging the other inode without |
| 4984 | * acquiring its i_mutex as long as we log with |
| 4985 | * the LOG_INODE_EXISTS mode. We're safe against |
| 4986 | * concurrent renames of the other inode as well |
| 4987 | * because during a rename we pin the log and |
| 4988 | * update the log with the new name before we |
| 4989 | * unpin it. |
| 4990 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4991 | err = btrfs_log_inode(trans, root, |
| 4992 | BTRFS_I(other_inode), |
| 4993 | LOG_OTHER_INODE, 0, LLONG_MAX, |
| 4994 | ctx); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4995 | iput(other_inode); |
| 4996 | if (err) |
| 4997 | goto out_unlock; |
| 4998 | else |
| 4999 | goto next_key; |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5000 | } |
| 5001 | } |
| 5002 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5003 | /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */ |
| 5004 | if (min_key.type == BTRFS_XATTR_ITEM_KEY) { |
| 5005 | if (ins_nr == 0) |
| 5006 | goto next_slot; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5007 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5008 | &last_extent, ins_start_slot, |
| 5009 | ins_nr, inode_only, logged_isize); |
| 5010 | if (ret < 0) { |
| 5011 | err = ret; |
| 5012 | goto out_unlock; |
| 5013 | } |
| 5014 | ins_nr = 0; |
| 5015 | if (ret) { |
| 5016 | btrfs_release_path(path); |
| 5017 | continue; |
| 5018 | } |
| 5019 | goto next_slot; |
| 5020 | } |
| 5021 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5022 | if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { |
| 5023 | ins_nr++; |
| 5024 | goto next_slot; |
| 5025 | } else if (!ins_nr) { |
| 5026 | ins_start_slot = path->slots[0]; |
| 5027 | ins_nr = 1; |
| 5028 | goto next_slot; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5029 | } |
| 5030 | |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5031 | ret = copy_items(trans, inode, dst_path, path, &last_extent, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5032 | ins_start_slot, ins_nr, inode_only, |
| 5033 | logged_isize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5034 | if (ret < 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5035 | err = ret; |
| 5036 | goto out_unlock; |
Rasmus Villemoes | a71db86 | 2014-06-20 21:51:43 +0200 | [diff] [blame] | 5037 | } |
| 5038 | if (ret) { |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5039 | ins_nr = 0; |
| 5040 | btrfs_release_path(path); |
| 5041 | continue; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5042 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5043 | ins_nr = 1; |
| 5044 | ins_start_slot = path->slots[0]; |
| 5045 | next_slot: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5046 | |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 5047 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 5048 | path->slots[0]++; |
| 5049 | if (path->slots[0] < nritems) { |
| 5050 | btrfs_item_key_to_cpu(path->nodes[0], &min_key, |
| 5051 | path->slots[0]); |
| 5052 | goto again; |
| 5053 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5054 | if (ins_nr) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5055 | ret = copy_items(trans, inode, dst_path, path, |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5056 | &last_extent, ins_start_slot, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5057 | ins_nr, inode_only, logged_isize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5058 | if (ret < 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5059 | err = ret; |
| 5060 | goto out_unlock; |
| 5061 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5062 | ret = 0; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5063 | ins_nr = 0; |
| 5064 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5065 | btrfs_release_path(path); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5066 | next_key: |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5067 | if (min_key.offset < (u64)-1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5068 | min_key.offset++; |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5069 | } else if (min_key.type < max_key.type) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5070 | min_key.type++; |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5071 | min_key.offset = 0; |
| 5072 | } else { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5073 | break; |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5074 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5075 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5076 | if (ins_nr) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5077 | ret = copy_items(trans, inode, dst_path, path, &last_extent, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5078 | ins_start_slot, ins_nr, inode_only, |
| 5079 | logged_isize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5080 | if (ret < 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5081 | err = ret; |
| 5082 | goto out_unlock; |
| 5083 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5084 | ret = 0; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5085 | ins_nr = 0; |
| 5086 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5087 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5088 | btrfs_release_path(path); |
| 5089 | btrfs_release_path(dst_path); |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5090 | err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5091 | if (err) |
| 5092 | goto out_unlock; |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 5093 | xattrs_logged = true; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5094 | if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) { |
| 5095 | btrfs_release_path(path); |
| 5096 | btrfs_release_path(dst_path); |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5097 | err = btrfs_log_trailing_hole(trans, root, inode, path); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5098 | if (err) |
| 5099 | goto out_unlock; |
| 5100 | } |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 5101 | log_extents: |
Josef Bacik | f3b15cc | 2013-07-22 12:54:30 -0400 | [diff] [blame] | 5102 | btrfs_release_path(path); |
| 5103 | btrfs_release_path(dst_path); |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 5104 | if (need_log_inode_item) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5105 | err = log_inode_item(trans, log, dst_path, inode); |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 5106 | if (!err && !xattrs_logged) { |
| 5107 | err = btrfs_log_all_xattrs(trans, root, inode, path, |
| 5108 | dst_path); |
| 5109 | btrfs_release_path(path); |
| 5110 | } |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 5111 | if (err) |
| 5112 | goto out_unlock; |
| 5113 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5114 | if (fast_search) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5115 | ret = btrfs_log_changed_extents(trans, root, inode, dst_path, |
Josef Bacik | a2120a4 | 2018-05-23 11:58:35 -0400 | [diff] [blame^] | 5116 | ctx, start, end); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5117 | if (ret) { |
| 5118 | err = ret; |
| 5119 | goto out_unlock; |
| 5120 | } |
Josef Bacik | d006a04 | 2013-11-12 20:54:09 -0500 | [diff] [blame] | 5121 | } else if (inode_only == LOG_INODE_ALL) { |
Liu Bo | 06d3d22 | 2012-08-27 10:52:19 -0600 | [diff] [blame] | 5122 | struct extent_map *em, *n; |
| 5123 | |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5124 | write_lock(&em_tree->lock); |
| 5125 | /* |
| 5126 | * We can't just remove every em if we're called for a ranged |
| 5127 | * fsync - that is, one that doesn't cover the whole possible |
| 5128 | * file range (0 to LLONG_MAX). This is because we can have |
| 5129 | * em's that fall outside the range we're logging and therefore |
| 5130 | * their ordered operations haven't completed yet |
| 5131 | * (btrfs_finish_ordered_io() not invoked yet). This means we |
| 5132 | * didn't get their respective file extent item in the fs/subvol |
| 5133 | * tree yet, and need to let the next fast fsync (one which |
| 5134 | * consults the list of modified extent maps) find the em so |
| 5135 | * that it logs a matching file extent item and waits for the |
| 5136 | * respective ordered operation to complete (if it's still |
| 5137 | * running). |
| 5138 | * |
| 5139 | * Removing every em outside the range we're logging would make |
| 5140 | * the next fast fsync not log their matching file extent items, |
| 5141 | * therefore making us lose data after a log replay. |
| 5142 | */ |
| 5143 | list_for_each_entry_safe(em, n, &em_tree->modified_extents, |
| 5144 | list) { |
| 5145 | const u64 mod_end = em->mod_start + em->mod_len - 1; |
| 5146 | |
| 5147 | if (em->mod_start >= start && mod_end <= end) |
| 5148 | list_del_init(&em->list); |
| 5149 | } |
| 5150 | write_unlock(&em_tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5151 | } |
| 5152 | |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5153 | if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) { |
| 5154 | ret = log_directory_changes(trans, root, inode, path, dst_path, |
| 5155 | ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5156 | if (ret) { |
| 5157 | err = ret; |
| 5158 | goto out_unlock; |
| 5159 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5160 | } |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5161 | |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5162 | spin_lock(&inode->lock); |
| 5163 | inode->logged_trans = trans->transid; |
| 5164 | inode->last_log_commit = inode->last_sub_trans; |
| 5165 | spin_unlock(&inode->lock); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5166 | out_unlock: |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5167 | mutex_unlock(&inode->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5168 | |
| 5169 | btrfs_free_path(path); |
| 5170 | btrfs_free_path(dst_path); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5171 | return err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5172 | } |
| 5173 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5174 | /* |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5175 | * Check if we must fallback to a transaction commit when logging an inode. |
| 5176 | * This must be called after logging the inode and is used only in the context |
| 5177 | * when fsyncing an inode requires the need to log some other inode - in which |
| 5178 | * case we can't lock the i_mutex of each other inode we need to log as that |
| 5179 | * can lead to deadlocks with concurrent fsync against other inodes (as we can |
| 5180 | * log inodes up or down in the hierarchy) or rename operations for example. So |
| 5181 | * we take the log_mutex of the inode after we have logged it and then check for |
| 5182 | * its last_unlink_trans value - this is safe because any task setting |
| 5183 | * last_unlink_trans must take the log_mutex and it must do this before it does |
| 5184 | * the actual unlink operation, so if we do this check before a concurrent task |
| 5185 | * sets last_unlink_trans it means we've logged a consistent version/state of |
| 5186 | * 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] | 5187 | * commit (the concurrent task might have only updated last_unlink_trans before |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5188 | * we logged the inode or it might have also done the unlink). |
| 5189 | */ |
| 5190 | static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans, |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5191 | struct btrfs_inode *inode) |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5192 | { |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5193 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5194 | bool ret = false; |
| 5195 | |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5196 | mutex_lock(&inode->log_mutex); |
| 5197 | if (inode->last_unlink_trans > fs_info->last_trans_committed) { |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5198 | /* |
| 5199 | * Make sure any commits to the log are forced to be full |
| 5200 | * commits. |
| 5201 | */ |
| 5202 | btrfs_set_log_full_commit(fs_info, trans); |
| 5203 | ret = true; |
| 5204 | } |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5205 | mutex_unlock(&inode->log_mutex); |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5206 | |
| 5207 | return ret; |
| 5208 | } |
| 5209 | |
| 5210 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5211 | * follow the dentry parent pointers up the chain and see if any |
| 5212 | * of the directories in it require a full commit before they can |
| 5213 | * be logged. Returns zero if nothing special needs to be done or 1 if |
| 5214 | * a full commit is required. |
| 5215 | */ |
| 5216 | 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] | 5217 | struct btrfs_inode *inode, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5218 | struct dentry *parent, |
| 5219 | struct super_block *sb, |
| 5220 | u64 last_committed) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5221 | { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5222 | int ret = 0; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5223 | struct dentry *old_parent = NULL; |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5224 | struct btrfs_inode *orig_inode = inode; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5225 | |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5226 | /* |
| 5227 | * for regular files, if its inode is already on disk, we don't |
| 5228 | * have to worry about the parents at all. This is because |
| 5229 | * we can use the last_unlink_trans field to record renames |
| 5230 | * and other fun in this file. |
| 5231 | */ |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5232 | if (S_ISREG(inode->vfs_inode.i_mode) && |
| 5233 | inode->generation <= last_committed && |
| 5234 | inode->last_unlink_trans <= last_committed) |
| 5235 | goto out; |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5236 | |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5237 | if (!S_ISDIR(inode->vfs_inode.i_mode)) { |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5238 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5239 | goto out; |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5240 | inode = BTRFS_I(d_inode(parent)); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5241 | } |
| 5242 | |
| 5243 | while (1) { |
Josef Bacik | de2b530 | 2013-09-11 09:36:30 -0400 | [diff] [blame] | 5244 | /* |
| 5245 | * 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] | 5246 | * not our parent's inode, so we need to skip setting the |
Josef Bacik | de2b530 | 2013-09-11 09:36:30 -0400 | [diff] [blame] | 5247 | * logged_trans so that further down in the log code we don't |
| 5248 | * think this inode has already been logged. |
| 5249 | */ |
| 5250 | if (inode != orig_inode) |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5251 | inode->logged_trans = trans->transid; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5252 | smp_mb(); |
| 5253 | |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5254 | if (btrfs_must_commit_transaction(trans, inode)) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5255 | ret = 1; |
| 5256 | break; |
| 5257 | } |
| 5258 | |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5259 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5260 | break; |
| 5261 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5262 | if (IS_ROOT(parent)) { |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5263 | inode = BTRFS_I(d_inode(parent)); |
| 5264 | if (btrfs_must_commit_transaction(trans, inode)) |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5265 | ret = 1; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5266 | break; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5267 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5268 | |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5269 | parent = dget_parent(parent); |
| 5270 | dput(old_parent); |
| 5271 | old_parent = parent; |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5272 | inode = BTRFS_I(d_inode(parent)); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5273 | |
| 5274 | } |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5275 | dput(old_parent); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5276 | out: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5277 | return ret; |
| 5278 | } |
| 5279 | |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5280 | struct btrfs_dir_list { |
| 5281 | u64 ino; |
| 5282 | struct list_head list; |
| 5283 | }; |
| 5284 | |
| 5285 | /* |
| 5286 | * Log the inodes of the new dentries of a directory. See log_dir_items() for |
| 5287 | * details about the why it is needed. |
| 5288 | * This is a recursive operation - if an existing dentry corresponds to a |
| 5289 | * directory, that directory's new entries are logged too (same behaviour as |
| 5290 | * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes |
| 5291 | * the dentries point to we do not lock their i_mutex, otherwise lockdep |
| 5292 | * complains about the following circular lock dependency / possible deadlock: |
| 5293 | * |
| 5294 | * CPU0 CPU1 |
| 5295 | * ---- ---- |
| 5296 | * lock(&type->i_mutex_dir_key#3/2); |
| 5297 | * lock(sb_internal#2); |
| 5298 | * lock(&type->i_mutex_dir_key#3/2); |
| 5299 | * lock(&sb->s_type->i_mutex_key#14); |
| 5300 | * |
| 5301 | * Where sb_internal is the lock (a counter that works as a lock) acquired by |
| 5302 | * sb_start_intwrite() in btrfs_start_transaction(). |
| 5303 | * Not locking i_mutex of the inodes is still safe because: |
| 5304 | * |
| 5305 | * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible |
| 5306 | * that while logging the inode new references (names) are added or removed |
| 5307 | * from the inode, leaving the logged inode item with a link count that does |
| 5308 | * not match the number of logged inode reference items. This is fine because |
| 5309 | * at log replay time we compute the real number of links and correct the |
| 5310 | * link count in the inode item (see replay_one_buffer() and |
| 5311 | * link_to_fixup_dir()); |
| 5312 | * |
| 5313 | * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that |
| 5314 | * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and |
| 5315 | * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item |
| 5316 | * has a size that doesn't match the sum of the lengths of all the logged |
| 5317 | * names. This does not result in a problem because if a dir_item key is |
| 5318 | * logged but its matching dir_index key is not logged, at log replay time we |
| 5319 | * don't use it to replay the respective name (see replay_one_name()). On the |
| 5320 | * other hand if only the dir_index key ends up being logged, the respective |
| 5321 | * name is added to the fs/subvol tree with both the dir_item and dir_index |
| 5322 | * keys created (see replay_one_name()). |
| 5323 | * The directory's inode item with a wrong i_size is not a problem as well, |
| 5324 | * since we don't use it at log replay time to set the i_size in the inode |
| 5325 | * item of the fs/subvol tree (see overwrite_item()). |
| 5326 | */ |
| 5327 | static int log_new_dir_dentries(struct btrfs_trans_handle *trans, |
| 5328 | struct btrfs_root *root, |
Nikolay Borisov | 51cc0d3 | 2017-01-18 00:31:43 +0200 | [diff] [blame] | 5329 | struct btrfs_inode *start_inode, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5330 | struct btrfs_log_ctx *ctx) |
| 5331 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5332 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5333 | struct btrfs_root *log = root->log_root; |
| 5334 | struct btrfs_path *path; |
| 5335 | LIST_HEAD(dir_list); |
| 5336 | struct btrfs_dir_list *dir_elem; |
| 5337 | int ret = 0; |
| 5338 | |
| 5339 | path = btrfs_alloc_path(); |
| 5340 | if (!path) |
| 5341 | return -ENOMEM; |
| 5342 | |
| 5343 | dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS); |
| 5344 | if (!dir_elem) { |
| 5345 | btrfs_free_path(path); |
| 5346 | return -ENOMEM; |
| 5347 | } |
Nikolay Borisov | 51cc0d3 | 2017-01-18 00:31:43 +0200 | [diff] [blame] | 5348 | dir_elem->ino = btrfs_ino(start_inode); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5349 | list_add_tail(&dir_elem->list, &dir_list); |
| 5350 | |
| 5351 | while (!list_empty(&dir_list)) { |
| 5352 | struct extent_buffer *leaf; |
| 5353 | struct btrfs_key min_key; |
| 5354 | int nritems; |
| 5355 | int i; |
| 5356 | |
| 5357 | dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, |
| 5358 | list); |
| 5359 | if (ret) |
| 5360 | goto next_dir_inode; |
| 5361 | |
| 5362 | min_key.objectid = dir_elem->ino; |
| 5363 | min_key.type = BTRFS_DIR_ITEM_KEY; |
| 5364 | min_key.offset = 0; |
| 5365 | again: |
| 5366 | btrfs_release_path(path); |
| 5367 | ret = btrfs_search_forward(log, &min_key, path, trans->transid); |
| 5368 | if (ret < 0) { |
| 5369 | goto next_dir_inode; |
| 5370 | } else if (ret > 0) { |
| 5371 | ret = 0; |
| 5372 | goto next_dir_inode; |
| 5373 | } |
| 5374 | |
| 5375 | process_leaf: |
| 5376 | leaf = path->nodes[0]; |
| 5377 | nritems = btrfs_header_nritems(leaf); |
| 5378 | for (i = path->slots[0]; i < nritems; i++) { |
| 5379 | struct btrfs_dir_item *di; |
| 5380 | struct btrfs_key di_key; |
| 5381 | struct inode *di_inode; |
| 5382 | struct btrfs_dir_list *new_dir_elem; |
| 5383 | int log_mode = LOG_INODE_EXISTS; |
| 5384 | int type; |
| 5385 | |
| 5386 | btrfs_item_key_to_cpu(leaf, &min_key, i); |
| 5387 | if (min_key.objectid != dir_elem->ino || |
| 5388 | min_key.type != BTRFS_DIR_ITEM_KEY) |
| 5389 | goto next_dir_inode; |
| 5390 | |
| 5391 | di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item); |
| 5392 | type = btrfs_dir_type(leaf, di); |
| 5393 | if (btrfs_dir_transid(leaf, di) < trans->transid && |
| 5394 | type != BTRFS_FT_DIR) |
| 5395 | continue; |
| 5396 | btrfs_dir_item_key_to_cpu(leaf, di, &di_key); |
| 5397 | if (di_key.type == BTRFS_ROOT_ITEM_KEY) |
| 5398 | continue; |
| 5399 | |
Robbie Ko | ec125cf | 2016-10-28 10:48:26 +0800 | [diff] [blame] | 5400 | btrfs_release_path(path); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5401 | di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5402 | if (IS_ERR(di_inode)) { |
| 5403 | ret = PTR_ERR(di_inode); |
| 5404 | goto next_dir_inode; |
| 5405 | } |
| 5406 | |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 5407 | if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) { |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5408 | iput(di_inode); |
Robbie Ko | ec125cf | 2016-10-28 10:48:26 +0800 | [diff] [blame] | 5409 | break; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
| 5412 | ctx->log_new_dentries = false; |
Filipe Manana | 3f9749f | 2016-04-25 04:45:02 +0100 | [diff] [blame] | 5413 | if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK) |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5414 | log_mode = LOG_INODE_ALL; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5415 | ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode), |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5416 | log_mode, 0, LLONG_MAX, ctx); |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5417 | if (!ret && |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5418 | btrfs_must_commit_transaction(trans, BTRFS_I(di_inode))) |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5419 | ret = 1; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5420 | iput(di_inode); |
| 5421 | if (ret) |
| 5422 | goto next_dir_inode; |
| 5423 | if (ctx->log_new_dentries) { |
| 5424 | new_dir_elem = kmalloc(sizeof(*new_dir_elem), |
| 5425 | GFP_NOFS); |
| 5426 | if (!new_dir_elem) { |
| 5427 | ret = -ENOMEM; |
| 5428 | goto next_dir_inode; |
| 5429 | } |
| 5430 | new_dir_elem->ino = di_key.objectid; |
| 5431 | list_add_tail(&new_dir_elem->list, &dir_list); |
| 5432 | } |
| 5433 | break; |
| 5434 | } |
| 5435 | if (i == nritems) { |
| 5436 | ret = btrfs_next_leaf(log, path); |
| 5437 | if (ret < 0) { |
| 5438 | goto next_dir_inode; |
| 5439 | } else if (ret > 0) { |
| 5440 | ret = 0; |
| 5441 | goto next_dir_inode; |
| 5442 | } |
| 5443 | goto process_leaf; |
| 5444 | } |
| 5445 | if (min_key.offset < (u64)-1) { |
| 5446 | min_key.offset++; |
| 5447 | goto again; |
| 5448 | } |
| 5449 | next_dir_inode: |
| 5450 | list_del(&dir_elem->list); |
| 5451 | kfree(dir_elem); |
| 5452 | } |
| 5453 | |
| 5454 | btrfs_free_path(path); |
| 5455 | return ret; |
| 5456 | } |
| 5457 | |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5458 | static int btrfs_log_all_parents(struct btrfs_trans_handle *trans, |
Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 5459 | struct btrfs_inode *inode, |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5460 | struct btrfs_log_ctx *ctx) |
| 5461 | { |
Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 5462 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5463 | int ret; |
| 5464 | struct btrfs_path *path; |
| 5465 | struct btrfs_key key; |
Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 5466 | struct btrfs_root *root = inode->root; |
| 5467 | const u64 ino = btrfs_ino(inode); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5468 | |
| 5469 | path = btrfs_alloc_path(); |
| 5470 | if (!path) |
| 5471 | return -ENOMEM; |
| 5472 | path->skip_locking = 1; |
| 5473 | path->search_commit_root = 1; |
| 5474 | |
| 5475 | key.objectid = ino; |
| 5476 | key.type = BTRFS_INODE_REF_KEY; |
| 5477 | key.offset = 0; |
| 5478 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5479 | if (ret < 0) |
| 5480 | goto out; |
| 5481 | |
| 5482 | while (true) { |
| 5483 | struct extent_buffer *leaf = path->nodes[0]; |
| 5484 | int slot = path->slots[0]; |
| 5485 | u32 cur_offset = 0; |
| 5486 | u32 item_size; |
| 5487 | unsigned long ptr; |
| 5488 | |
| 5489 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5490 | ret = btrfs_next_leaf(root, path); |
| 5491 | if (ret < 0) |
| 5492 | goto out; |
| 5493 | else if (ret > 0) |
| 5494 | break; |
| 5495 | continue; |
| 5496 | } |
| 5497 | |
| 5498 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5499 | /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */ |
| 5500 | if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY) |
| 5501 | break; |
| 5502 | |
| 5503 | item_size = btrfs_item_size_nr(leaf, slot); |
| 5504 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 5505 | while (cur_offset < item_size) { |
| 5506 | struct btrfs_key inode_key; |
| 5507 | struct inode *dir_inode; |
| 5508 | |
| 5509 | inode_key.type = BTRFS_INODE_ITEM_KEY; |
| 5510 | inode_key.offset = 0; |
| 5511 | |
| 5512 | if (key.type == BTRFS_INODE_EXTREF_KEY) { |
| 5513 | struct btrfs_inode_extref *extref; |
| 5514 | |
| 5515 | extref = (struct btrfs_inode_extref *) |
| 5516 | (ptr + cur_offset); |
| 5517 | inode_key.objectid = btrfs_inode_extref_parent( |
| 5518 | leaf, extref); |
| 5519 | cur_offset += sizeof(*extref); |
| 5520 | cur_offset += btrfs_inode_extref_name_len(leaf, |
| 5521 | extref); |
| 5522 | } else { |
| 5523 | inode_key.objectid = key.offset; |
| 5524 | cur_offset = item_size; |
| 5525 | } |
| 5526 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5527 | dir_inode = btrfs_iget(fs_info->sb, &inode_key, |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5528 | root, NULL); |
| 5529 | /* If parent inode was deleted, skip it. */ |
| 5530 | if (IS_ERR(dir_inode)) |
| 5531 | continue; |
| 5532 | |
Filipe Manana | 657ed1a | 2016-04-06 17:11:56 +0100 | [diff] [blame] | 5533 | if (ctx) |
| 5534 | ctx->log_new_dentries = false; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5535 | ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode), |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5536 | LOG_INODE_ALL, 0, LLONG_MAX, ctx); |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5537 | if (!ret && |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5538 | btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode))) |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5539 | ret = 1; |
Filipe Manana | 657ed1a | 2016-04-06 17:11:56 +0100 | [diff] [blame] | 5540 | if (!ret && ctx && ctx->log_new_dentries) |
| 5541 | ret = log_new_dir_dentries(trans, root, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5542 | BTRFS_I(dir_inode), ctx); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5543 | iput(dir_inode); |
| 5544 | if (ret) |
| 5545 | goto out; |
| 5546 | } |
| 5547 | path->slots[0]++; |
| 5548 | } |
| 5549 | ret = 0; |
| 5550 | out: |
| 5551 | btrfs_free_path(path); |
| 5552 | return ret; |
| 5553 | } |
| 5554 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5555 | /* |
| 5556 | * helper function around btrfs_log_inode to make sure newly created |
| 5557 | * parent directories also end up in the log. A minimal inode and backref |
| 5558 | * only logging is done of any parent directories that are older than |
| 5559 | * the last committed transaction |
| 5560 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5561 | static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5562 | struct btrfs_inode *inode, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5563 | struct dentry *parent, |
| 5564 | const loff_t start, |
| 5565 | const loff_t end, |
Edmund Nadolski | 41a1ead | 2017-11-20 13:24:47 -0700 | [diff] [blame] | 5566 | int inode_only, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5567 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5568 | { |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5569 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5570 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5571 | struct super_block *sb; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5572 | struct dentry *old_parent = NULL; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5573 | int ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5574 | u64 last_committed = fs_info->last_trans_committed; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5575 | bool log_dentries = false; |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5576 | struct btrfs_inode *orig_inode = inode; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5577 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5578 | sb = inode->vfs_inode.i_sb; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5579 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5580 | if (btrfs_test_opt(fs_info, NOTREELOG)) { |
Sage Weil | 3a5e140 | 2009-04-02 16:49:40 -0400 | [diff] [blame] | 5581 | ret = 1; |
| 5582 | goto end_no_trans; |
| 5583 | } |
| 5584 | |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 5585 | /* |
| 5586 | * The prev transaction commit doesn't complete, we need do |
| 5587 | * full commit by ourselves. |
| 5588 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5589 | if (fs_info->last_trans_log_full_commit > |
| 5590 | fs_info->last_trans_committed) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5591 | ret = 1; |
| 5592 | goto end_no_trans; |
| 5593 | } |
| 5594 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5595 | if (btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5596 | ret = 1; |
| 5597 | goto end_no_trans; |
| 5598 | } |
| 5599 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5600 | ret = check_parent_dirs_for_sync(trans, inode, parent, sb, |
| 5601 | last_committed); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5602 | if (ret) |
| 5603 | goto end_no_trans; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5604 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5605 | if (btrfs_inode_in_log(inode, trans->transid)) { |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5606 | ret = BTRFS_NO_LOG_SYNC; |
| 5607 | goto end_no_trans; |
| 5608 | } |
| 5609 | |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5610 | ret = start_log_trans(trans, root, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5611 | if (ret) |
Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 5612 | goto end_no_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5613 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5614 | ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5615 | if (ret) |
| 5616 | goto end_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5617 | |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5618 | /* |
| 5619 | * for regular files, if its inode is already on disk, we don't |
| 5620 | * have to worry about the parents at all. This is because |
| 5621 | * we can use the last_unlink_trans field to record renames |
| 5622 | * and other fun in this file. |
| 5623 | */ |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5624 | if (S_ISREG(inode->vfs_inode.i_mode) && |
| 5625 | inode->generation <= last_committed && |
| 5626 | inode->last_unlink_trans <= last_committed) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5627 | ret = 0; |
| 5628 | goto end_trans; |
| 5629 | } |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5630 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5631 | 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] | 5632 | log_dentries = true; |
| 5633 | |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5634 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5635 | * 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] | 5636 | * inodes are fully logged. This is to prevent leaving dangling |
| 5637 | * directory index entries in directories that were our parents but are |
| 5638 | * not anymore. Not doing this results in old parent directory being |
| 5639 | * impossible to delete after log replay (rmdir will always fail with |
| 5640 | * error -ENOTEMPTY). |
| 5641 | * |
| 5642 | * Example 1: |
| 5643 | * |
| 5644 | * mkdir testdir |
| 5645 | * touch testdir/foo |
| 5646 | * ln testdir/foo testdir/bar |
| 5647 | * sync |
| 5648 | * unlink testdir/bar |
| 5649 | * xfs_io -c fsync testdir/foo |
| 5650 | * <power failure> |
| 5651 | * mount fs, triggers log replay |
| 5652 | * |
| 5653 | * If we don't log the parent directory (testdir), after log replay the |
| 5654 | * directory still has an entry pointing to the file inode using the bar |
| 5655 | * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and |
| 5656 | * the file inode has a link count of 1. |
| 5657 | * |
| 5658 | * Example 2: |
| 5659 | * |
| 5660 | * mkdir testdir |
| 5661 | * touch foo |
| 5662 | * ln foo testdir/foo2 |
| 5663 | * ln foo testdir/foo3 |
| 5664 | * sync |
| 5665 | * unlink testdir/foo3 |
| 5666 | * xfs_io -c fsync foo |
| 5667 | * <power failure> |
| 5668 | * mount fs, triggers log replay |
| 5669 | * |
| 5670 | * Similar as the first example, after log replay the parent directory |
| 5671 | * testdir still has an entry pointing to the inode file with name foo3 |
| 5672 | * but the file inode does not have a matching BTRFS_INODE_REF_KEY item |
| 5673 | * and has a link count of 2. |
| 5674 | */ |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5675 | if (inode->last_unlink_trans > last_committed) { |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5676 | ret = btrfs_log_all_parents(trans, orig_inode, ctx); |
| 5677 | if (ret) |
| 5678 | goto end_trans; |
| 5679 | } |
| 5680 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5681 | while (1) { |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5682 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5683 | break; |
| 5684 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5685 | inode = BTRFS_I(d_inode(parent)); |
| 5686 | if (root != inode->root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5687 | break; |
| 5688 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5689 | if (inode->generation > last_committed) { |
| 5690 | ret = btrfs_log_inode(trans, root, inode, |
| 5691 | LOG_INODE_EXISTS, 0, LLONG_MAX, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5692 | if (ret) |
| 5693 | goto end_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5694 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5695 | if (IS_ROOT(parent)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5696 | break; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5697 | |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5698 | parent = dget_parent(parent); |
| 5699 | dput(old_parent); |
| 5700 | old_parent = parent; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5701 | } |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5702 | if (log_dentries) |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5703 | ret = log_new_dir_dentries(trans, root, orig_inode, ctx); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5704 | else |
| 5705 | ret = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5706 | end_trans: |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5707 | dput(old_parent); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5708 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5709 | btrfs_set_log_full_commit(fs_info, trans); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5710 | ret = 1; |
| 5711 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5712 | |
| 5713 | if (ret) |
| 5714 | btrfs_remove_log_ctx(root, ctx); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5715 | btrfs_end_log_trans(root); |
| 5716 | end_no_trans: |
| 5717 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | /* |
| 5721 | * it is not safe to log dentry if the chunk root has added new |
| 5722 | * chunks. This returns 0 if the dentry was logged, and 1 otherwise. |
| 5723 | * If this returns 1, you must commit the transaction to safely get your |
| 5724 | * data on disk. |
| 5725 | */ |
| 5726 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e5b84f7a | 2018-02-27 17:37:18 +0200 | [diff] [blame] | 5727 | struct dentry *dentry, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5728 | const loff_t start, |
| 5729 | const loff_t end, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5730 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5731 | { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5732 | struct dentry *parent = dget_parent(dentry); |
| 5733 | int ret; |
| 5734 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5735 | ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent, |
| 5736 | start, end, LOG_INODE_ALL, ctx); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5737 | dput(parent); |
| 5738 | |
| 5739 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5740 | } |
| 5741 | |
| 5742 | /* |
| 5743 | * should be called during mount to recover any replay any log trees |
| 5744 | * from the FS |
| 5745 | */ |
| 5746 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) |
| 5747 | { |
| 5748 | int ret; |
| 5749 | struct btrfs_path *path; |
| 5750 | struct btrfs_trans_handle *trans; |
| 5751 | struct btrfs_key key; |
| 5752 | struct btrfs_key found_key; |
| 5753 | struct btrfs_key tmp_key; |
| 5754 | struct btrfs_root *log; |
| 5755 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; |
| 5756 | struct walk_control wc = { |
| 5757 | .process_func = process_one_buffer, |
| 5758 | .stage = 0, |
| 5759 | }; |
| 5760 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5761 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5762 | if (!path) |
| 5763 | return -ENOMEM; |
| 5764 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 5765 | set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5766 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5767 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5768 | if (IS_ERR(trans)) { |
| 5769 | ret = PTR_ERR(trans); |
| 5770 | goto error; |
| 5771 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5772 | |
| 5773 | wc.trans = trans; |
| 5774 | wc.pin = 1; |
| 5775 | |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5776 | ret = walk_log_tree(trans, log_root_tree, &wc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5777 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5778 | btrfs_handle_fs_error(fs_info, ret, |
| 5779 | "Failed to pin buffers while recovering log root tree."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5780 | goto error; |
| 5781 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5782 | |
| 5783 | again: |
| 5784 | key.objectid = BTRFS_TREE_LOG_OBJECTID; |
| 5785 | key.offset = (u64)-1; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5786 | key.type = BTRFS_ROOT_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5787 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5788 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5789 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5790 | |
| 5791 | if (ret < 0) { |
Anand Jain | 34d9700 | 2016-03-16 16:43:06 +0800 | [diff] [blame] | 5792 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5793 | "Couldn't find tree log root."); |
| 5794 | goto error; |
| 5795 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5796 | if (ret > 0) { |
| 5797 | if (path->slots[0] == 0) |
| 5798 | break; |
| 5799 | path->slots[0]--; |
| 5800 | } |
| 5801 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 5802 | path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5803 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5804 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 5805 | break; |
| 5806 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 5807 | log = btrfs_read_fs_root(log_root_tree, &found_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5808 | if (IS_ERR(log)) { |
| 5809 | ret = PTR_ERR(log); |
Anand Jain | 34d9700 | 2016-03-16 16:43:06 +0800 | [diff] [blame] | 5810 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5811 | "Couldn't read tree log root."); |
| 5812 | goto error; |
| 5813 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5814 | |
| 5815 | tmp_key.objectid = found_key.offset; |
| 5816 | tmp_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5817 | tmp_key.offset = (u64)-1; |
| 5818 | |
| 5819 | 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] | 5820 | if (IS_ERR(wc.replay_dest)) { |
| 5821 | ret = PTR_ERR(wc.replay_dest); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5822 | free_extent_buffer(log->node); |
| 5823 | free_extent_buffer(log->commit_root); |
| 5824 | kfree(log); |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5825 | btrfs_handle_fs_error(fs_info, ret, |
| 5826 | "Couldn't read target root for tree log recovery."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5827 | goto error; |
| 5828 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5829 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 5830 | wc.replay_dest->log_root = log; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5831 | btrfs_record_root_in_trans(trans, wc.replay_dest); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5832 | ret = walk_log_tree(trans, log, &wc); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5833 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5834 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5835 | ret = fixup_inode_link_counts(trans, wc.replay_dest, |
| 5836 | path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5837 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5838 | |
Liu Bo | 900c998 | 2018-01-25 11:02:56 -0700 | [diff] [blame] | 5839 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
| 5840 | struct btrfs_root *root = wc.replay_dest; |
| 5841 | |
| 5842 | btrfs_release_path(path); |
| 5843 | |
| 5844 | /* |
| 5845 | * We have just replayed everything, and the highest |
| 5846 | * objectid of fs roots probably has changed in case |
| 5847 | * some inode_item's got replayed. |
| 5848 | * |
| 5849 | * root->objectid_mutex is not acquired as log replay |
| 5850 | * could only happen during mount. |
| 5851 | */ |
| 5852 | ret = btrfs_find_highest_objectid(root, |
| 5853 | &root->highest_objectid); |
| 5854 | } |
| 5855 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5856 | key.offset = found_key.offset - 1; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 5857 | wc.replay_dest->log_root = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5858 | free_extent_buffer(log->node); |
Chris Mason | b263c2c | 2009-06-11 11:24:47 -0400 | [diff] [blame] | 5859 | free_extent_buffer(log->commit_root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5860 | kfree(log); |
| 5861 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5862 | if (ret) |
| 5863 | goto error; |
| 5864 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5865 | if (found_key.offset == 0) |
| 5866 | break; |
| 5867 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5868 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5869 | |
| 5870 | /* step one is to pin it all, step two is to replay just inodes */ |
| 5871 | if (wc.pin) { |
| 5872 | wc.pin = 0; |
| 5873 | wc.process_func = replay_one_buffer; |
| 5874 | wc.stage = LOG_WALK_REPLAY_INODES; |
| 5875 | goto again; |
| 5876 | } |
| 5877 | /* step three is to replay everything */ |
| 5878 | if (wc.stage < LOG_WALK_REPLAY_ALL) { |
| 5879 | wc.stage++; |
| 5880 | goto again; |
| 5881 | } |
| 5882 | |
| 5883 | btrfs_free_path(path); |
| 5884 | |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 5885 | /* step 4: commit the transaction, which also unpins the blocks */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5886 | ret = btrfs_commit_transaction(trans); |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 5887 | if (ret) |
| 5888 | return ret; |
| 5889 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5890 | free_extent_buffer(log_root_tree->node); |
| 5891 | log_root_tree->log_root = NULL; |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 5892 | clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5893 | kfree(log_root_tree); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5894 | |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 5895 | return 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5896 | error: |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5897 | if (wc.trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5898 | btrfs_end_transaction(wc.trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5899 | btrfs_free_path(path); |
| 5900 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5901 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5902 | |
| 5903 | /* |
| 5904 | * there are some corner cases where we want to force a full |
| 5905 | * commit instead of allowing a directory to be logged. |
| 5906 | * |
| 5907 | * They revolve around files there were unlinked from the directory, and |
| 5908 | * this function updates the parent directory so that a full commit is |
| 5909 | * 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] | 5910 | * |
| 5911 | * Must be called before the unlink operations (updates to the subvolume tree, |
| 5912 | * inodes, etc) are done. |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5913 | */ |
| 5914 | void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5915 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5916 | int for_rename) |
| 5917 | { |
| 5918 | /* |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5919 | * when we're logging a file, if it hasn't been renamed |
| 5920 | * or unlinked, and its inode is fully committed on disk, |
| 5921 | * we don't have to worry about walking up the directory chain |
| 5922 | * to log its parents. |
| 5923 | * |
| 5924 | * So, we use the last_unlink_trans field to put this transid |
| 5925 | * into the file. When the file is logged we check it and |
| 5926 | * don't log the parents if the file is fully on disk. |
| 5927 | */ |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5928 | mutex_lock(&inode->log_mutex); |
| 5929 | inode->last_unlink_trans = trans->transid; |
| 5930 | mutex_unlock(&inode->log_mutex); |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5931 | |
| 5932 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5933 | * if this directory was already logged any new |
| 5934 | * names for this file/dir will get recorded |
| 5935 | */ |
| 5936 | smp_mb(); |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5937 | if (dir->logged_trans == trans->transid) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5938 | return; |
| 5939 | |
| 5940 | /* |
| 5941 | * if the inode we're about to unlink was logged, |
| 5942 | * the log will be properly updated for any new names |
| 5943 | */ |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5944 | if (inode->logged_trans == trans->transid) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5945 | return; |
| 5946 | |
| 5947 | /* |
| 5948 | * when renaming files across directories, if the directory |
| 5949 | * there we're unlinking from gets fsync'd later on, there's |
| 5950 | * no way to find the destination directory later and fsync it |
| 5951 | * properly. So, we have to be conservative and force commits |
| 5952 | * so the new name gets discovered. |
| 5953 | */ |
| 5954 | if (for_rename) |
| 5955 | goto record; |
| 5956 | |
| 5957 | /* we can safely do the unlink without any special recording */ |
| 5958 | return; |
| 5959 | |
| 5960 | record: |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5961 | mutex_lock(&dir->log_mutex); |
| 5962 | dir->last_unlink_trans = trans->transid; |
| 5963 | mutex_unlock(&dir->log_mutex); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5964 | } |
| 5965 | |
| 5966 | /* |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 5967 | * Make sure that if someone attempts to fsync the parent directory of a deleted |
| 5968 | * snapshot, it ends up triggering a transaction commit. This is to guarantee |
| 5969 | * that after replaying the log tree of the parent directory's root we will not |
| 5970 | * see the snapshot anymore and at log replay time we will not see any log tree |
| 5971 | * corresponding to the deleted snapshot's root, which could lead to replaying |
| 5972 | * it after replaying the log tree of the parent directory (which would replay |
| 5973 | * the snapshot delete operation). |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5974 | * |
| 5975 | * Must be called before the actual snapshot destroy operation (updates to the |
| 5976 | * parent root and tree of tree roots trees, etc) are done. |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 5977 | */ |
| 5978 | void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4366355 | 2017-01-18 00:31:29 +0200 | [diff] [blame] | 5979 | struct btrfs_inode *dir) |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 5980 | { |
Nikolay Borisov | 4366355 | 2017-01-18 00:31:29 +0200 | [diff] [blame] | 5981 | mutex_lock(&dir->log_mutex); |
| 5982 | dir->last_unlink_trans = trans->transid; |
| 5983 | mutex_unlock(&dir->log_mutex); |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 5984 | } |
| 5985 | |
| 5986 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5987 | * Call this after adding a new name for a file and it will properly |
| 5988 | * update the log to reflect the new name. |
| 5989 | * |
| 5990 | * It will return zero if all goes well, and it will return 1 if a |
| 5991 | * full transaction commit is required. |
| 5992 | */ |
| 5993 | int btrfs_log_new_name(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 5994 | struct btrfs_inode *inode, struct btrfs_inode *old_dir, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5995 | struct dentry *parent) |
| 5996 | { |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 5997 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5998 | |
| 5999 | /* |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 6000 | * this will force the logging code to walk the dentry chain |
| 6001 | * up for the file |
| 6002 | */ |
Filipe Manana | 9a6509c | 2018-02-28 15:55:40 +0000 | [diff] [blame] | 6003 | if (!S_ISDIR(inode->vfs_inode.i_mode)) |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6004 | inode->last_unlink_trans = trans->transid; |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 6005 | |
| 6006 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6007 | * if this inode hasn't been logged and directory we're renaming it |
| 6008 | * from hasn't been logged, we don't need to log it |
| 6009 | */ |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6010 | if (inode->logged_trans <= fs_info->last_trans_committed && |
| 6011 | (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed)) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6012 | return 0; |
| 6013 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 6014 | return btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX, |
| 6015 | LOG_INODE_EXISTS, NULL); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6016 | } |
| 6017 | |