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 | 779adf0 | 2015-02-16 19:39:00 +0100 | [diff] [blame] | 225 | /* |
| 226 | * Implicit memory barrier after atomic_dec_and_test |
| 227 | */ |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 228 | if (waitqueue_active(&root->log_writer_wait)) |
| 229 | wake_up(&root->log_writer_wait); |
| 230 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
| 234 | /* |
| 235 | * the walk control struct is used to pass state down the chain when |
| 236 | * processing the log tree. The stage field tells us which part |
| 237 | * of the log tree processing we are currently doing. The others |
| 238 | * are state fields used for that specific part |
| 239 | */ |
| 240 | struct walk_control { |
| 241 | /* should we free the extent on disk when done? This is used |
| 242 | * at transaction commit time while freeing a log tree |
| 243 | */ |
| 244 | int free; |
| 245 | |
| 246 | /* should we write out the extent buffer? This is used |
| 247 | * while flushing the log tree to disk during a sync |
| 248 | */ |
| 249 | int write; |
| 250 | |
| 251 | /* should we wait for the extent buffer io to finish? Also used |
| 252 | * while flushing the log tree to disk for a sync |
| 253 | */ |
| 254 | int wait; |
| 255 | |
| 256 | /* pin only walk, we record which extents on disk belong to the |
| 257 | * log trees |
| 258 | */ |
| 259 | int pin; |
| 260 | |
| 261 | /* what stage of the replay code we're currently in */ |
| 262 | int stage; |
| 263 | |
| 264 | /* the root we are currently replaying */ |
| 265 | struct btrfs_root *replay_dest; |
| 266 | |
| 267 | /* the trans handle for the current replay */ |
| 268 | struct btrfs_trans_handle *trans; |
| 269 | |
| 270 | /* the function that gets used to process blocks we find in the |
| 271 | * tree. Note the extent_buffer might not be up to date when it is |
| 272 | * passed in, and it must be checked or read if you need the data |
| 273 | * inside it |
| 274 | */ |
| 275 | int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 276 | struct walk_control *wc, u64 gen, int level); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | /* |
| 280 | * process_func used to pin down extents, write them or wait on them |
| 281 | */ |
| 282 | static int process_one_buffer(struct btrfs_root *log, |
| 283 | struct extent_buffer *eb, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 284 | struct walk_control *wc, u64 gen, int level) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 285 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 286 | struct btrfs_fs_info *fs_info = log->fs_info; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 287 | int ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 288 | |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 289 | /* |
| 290 | * If this fs is mixed then we need to be able to process the leaves to |
| 291 | * pin down any logged extents, so we have to read the block. |
| 292 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 293 | if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 294 | ret = btrfs_read_buffer(eb, gen, level, NULL); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 295 | if (ret) |
| 296 | return ret; |
| 297 | } |
| 298 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 299 | if (wc->pin) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 300 | ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start, |
| 301 | eb->len); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 302 | |
| 303 | if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) { |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 304 | if (wc->pin && btrfs_header_level(eb) == 0) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 305 | ret = btrfs_exclude_logged_extents(fs_info, eb); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 306 | if (wc->write) |
| 307 | btrfs_write_tree_block(eb); |
| 308 | if (wc->wait) |
| 309 | btrfs_wait_tree_block_writeback(eb); |
| 310 | } |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 311 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Item overwrite used by replay and tree logging. eb, slot and key all refer |
| 316 | * to the src data we are copying out. |
| 317 | * |
| 318 | * root is the tree we are copying into, and path is a scratch |
| 319 | * path for use in this function (it should be released on entry and |
| 320 | * will be released on exit). |
| 321 | * |
| 322 | * If the key is already in the destination tree the existing item is |
| 323 | * overwritten. If the existing item isn't big enough, it is extended. |
| 324 | * If it is too large, it is truncated. |
| 325 | * |
| 326 | * If the key isn't in the destination yet, a new item is inserted. |
| 327 | */ |
| 328 | static noinline int overwrite_item(struct btrfs_trans_handle *trans, |
| 329 | struct btrfs_root *root, |
| 330 | struct btrfs_path *path, |
| 331 | struct extent_buffer *eb, int slot, |
| 332 | struct btrfs_key *key) |
| 333 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 334 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 335 | int ret; |
| 336 | u32 item_size; |
| 337 | u64 saved_i_size = 0; |
| 338 | int save_old_i_size = 0; |
| 339 | unsigned long src_ptr; |
| 340 | unsigned long dst_ptr; |
| 341 | int overwrite_root = 0; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 342 | bool inode_item = key->type == BTRFS_INODE_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 343 | |
| 344 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 345 | overwrite_root = 1; |
| 346 | |
| 347 | item_size = btrfs_item_size_nr(eb, slot); |
| 348 | src_ptr = btrfs_item_ptr_offset(eb, slot); |
| 349 | |
| 350 | /* look for the key in the destination tree */ |
| 351 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 352 | if (ret < 0) |
| 353 | return ret; |
| 354 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 355 | if (ret == 0) { |
| 356 | char *src_copy; |
| 357 | char *dst_copy; |
| 358 | u32 dst_size = btrfs_item_size_nr(path->nodes[0], |
| 359 | path->slots[0]); |
| 360 | if (dst_size != item_size) |
| 361 | goto insert; |
| 362 | |
| 363 | if (item_size == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 364 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 365 | return 0; |
| 366 | } |
| 367 | dst_copy = kmalloc(item_size, GFP_NOFS); |
| 368 | src_copy = kmalloc(item_size, GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 369 | if (!dst_copy || !src_copy) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 370 | btrfs_release_path(path); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 371 | kfree(dst_copy); |
| 372 | kfree(src_copy); |
| 373 | return -ENOMEM; |
| 374 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 375 | |
| 376 | read_extent_buffer(eb, src_copy, src_ptr, item_size); |
| 377 | |
| 378 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
| 379 | read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, |
| 380 | item_size); |
| 381 | ret = memcmp(dst_copy, src_copy, item_size); |
| 382 | |
| 383 | kfree(dst_copy); |
| 384 | kfree(src_copy); |
| 385 | /* |
| 386 | * they have the same contents, just return, this saves |
| 387 | * us from cowing blocks in the destination tree and doing |
| 388 | * extra writes that may not have been done by a previous |
| 389 | * sync |
| 390 | */ |
| 391 | if (ret == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 392 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 393 | return 0; |
| 394 | } |
| 395 | |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 396 | /* |
| 397 | * We need to load the old nbytes into the inode so when we |
| 398 | * replay the extents we've logged we get the right nbytes. |
| 399 | */ |
| 400 | if (inode_item) { |
| 401 | struct btrfs_inode_item *item; |
| 402 | u64 nbytes; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 403 | u32 mode; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 404 | |
| 405 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 406 | struct btrfs_inode_item); |
| 407 | nbytes = btrfs_inode_nbytes(path->nodes[0], item); |
| 408 | item = btrfs_item_ptr(eb, slot, |
| 409 | struct btrfs_inode_item); |
| 410 | btrfs_set_inode_nbytes(eb, item, nbytes); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * If this is a directory we need to reset the i_size to |
| 414 | * 0 so that we can set it up properly when replaying |
| 415 | * the rest of the items in this log. |
| 416 | */ |
| 417 | mode = btrfs_inode_mode(eb, item); |
| 418 | if (S_ISDIR(mode)) |
| 419 | btrfs_set_inode_size(eb, item, 0); |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 420 | } |
| 421 | } else if (inode_item) { |
| 422 | struct btrfs_inode_item *item; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 423 | u32 mode; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * New inode, set nbytes to 0 so that the nbytes comes out |
| 427 | * properly when we replay the extents. |
| 428 | */ |
| 429 | item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); |
| 430 | btrfs_set_inode_nbytes(eb, item, 0); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 431 | |
| 432 | /* |
| 433 | * If this is a directory we need to reset the i_size to 0 so |
| 434 | * that we can set it up properly when replaying the rest of |
| 435 | * the items in this log. |
| 436 | */ |
| 437 | mode = btrfs_inode_mode(eb, item); |
| 438 | if (S_ISDIR(mode)) |
| 439 | btrfs_set_inode_size(eb, item, 0); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 440 | } |
| 441 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 442 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 443 | /* try to insert the key into the destination tree */ |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 444 | path->skip_release_on_error = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 445 | ret = btrfs_insert_empty_item(trans, root, path, |
| 446 | key, item_size); |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 447 | path->skip_release_on_error = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 448 | |
| 449 | /* make sure any existing item is the correct size */ |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 450 | if (ret == -EEXIST || ret == -EOVERFLOW) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 451 | u32 found_size; |
| 452 | found_size = btrfs_item_size_nr(path->nodes[0], |
| 453 | path->slots[0]); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 454 | if (found_size > item_size) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 455 | btrfs_truncate_item(fs_info, path, item_size, 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 456 | else if (found_size < item_size) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 457 | btrfs_extend_item(fs_info, path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 458 | item_size - found_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 459 | } else if (ret) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 460 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 461 | } |
| 462 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], |
| 463 | path->slots[0]); |
| 464 | |
| 465 | /* don't overwrite an existing inode if the generation number |
| 466 | * was logged as zero. This is done when the tree logging code |
| 467 | * is just logging an inode to make sure it exists after recovery. |
| 468 | * |
| 469 | * Also, don't overwrite i_size on directories during replay. |
| 470 | * log replay inserts and removes directory items based on the |
| 471 | * state of the tree found in the subvolume, and i_size is modified |
| 472 | * as it goes |
| 473 | */ |
| 474 | if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) { |
| 475 | struct btrfs_inode_item *src_item; |
| 476 | struct btrfs_inode_item *dst_item; |
| 477 | |
| 478 | src_item = (struct btrfs_inode_item *)src_ptr; |
| 479 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 480 | |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 481 | if (btrfs_inode_generation(eb, src_item) == 0) { |
| 482 | struct extent_buffer *dst_eb = path->nodes[0]; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 483 | const u64 ino_size = btrfs_inode_size(eb, src_item); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 484 | |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 485 | /* |
| 486 | * For regular files an ino_size == 0 is used only when |
| 487 | * logging that an inode exists, as part of a directory |
| 488 | * fsync, and the inode wasn't fsynced before. In this |
| 489 | * case don't set the size of the inode in the fs/subvol |
| 490 | * tree, otherwise we would be throwing valid data away. |
| 491 | */ |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 492 | if (S_ISREG(btrfs_inode_mode(eb, src_item)) && |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 493 | S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) && |
| 494 | ino_size != 0) { |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 495 | struct btrfs_map_token token; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 496 | |
| 497 | btrfs_init_map_token(&token); |
| 498 | btrfs_set_token_inode_size(dst_eb, dst_item, |
| 499 | ino_size, &token); |
| 500 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 501 | goto no_copy; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 502 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 503 | |
| 504 | if (overwrite_root && |
| 505 | S_ISDIR(btrfs_inode_mode(eb, src_item)) && |
| 506 | S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { |
| 507 | save_old_i_size = 1; |
| 508 | saved_i_size = btrfs_inode_size(path->nodes[0], |
| 509 | dst_item); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | copy_extent_buffer(path->nodes[0], eb, dst_ptr, |
| 514 | src_ptr, item_size); |
| 515 | |
| 516 | if (save_old_i_size) { |
| 517 | struct btrfs_inode_item *dst_item; |
| 518 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 519 | btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); |
| 520 | } |
| 521 | |
| 522 | /* make sure the generation is filled in */ |
| 523 | if (key->type == BTRFS_INODE_ITEM_KEY) { |
| 524 | struct btrfs_inode_item *dst_item; |
| 525 | dst_item = (struct btrfs_inode_item *)dst_ptr; |
| 526 | if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { |
| 527 | btrfs_set_inode_generation(path->nodes[0], dst_item, |
| 528 | trans->transid); |
| 529 | } |
| 530 | } |
| 531 | no_copy: |
| 532 | btrfs_mark_buffer_dirty(path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 533 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * simple helper to read an inode off the disk from a given root |
| 539 | * This can only be called for subvolume roots and not for the log |
| 540 | */ |
| 541 | static noinline struct inode *read_one_inode(struct btrfs_root *root, |
| 542 | u64 objectid) |
| 543 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 544 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 545 | struct inode *inode; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 546 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 547 | key.objectid = objectid; |
| 548 | key.type = BTRFS_INODE_ITEM_KEY; |
| 549 | key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 550 | inode = btrfs_iget(root->fs_info->sb, &key, root, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 551 | if (IS_ERR(inode)) { |
| 552 | inode = NULL; |
| 553 | } else if (is_bad_inode(inode)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 554 | iput(inode); |
| 555 | inode = NULL; |
| 556 | } |
| 557 | return inode; |
| 558 | } |
| 559 | |
| 560 | /* replays a single extent in 'eb' at 'slot' with 'key' into the |
| 561 | * subvolume 'root'. path is released on entry and should be released |
| 562 | * on exit. |
| 563 | * |
| 564 | * extents in the log tree have not been allocated out of the extent |
| 565 | * tree yet. So, this completes the allocation, taking a reference |
| 566 | * as required if the extent already exists or creating a new extent |
| 567 | * if it isn't in the extent allocation tree yet. |
| 568 | * |
| 569 | * The extent is inserted into the file, dropping any existing extents |
| 570 | * from the file that overlap the new one. |
| 571 | */ |
| 572 | static noinline int replay_one_extent(struct btrfs_trans_handle *trans, |
| 573 | struct btrfs_root *root, |
| 574 | struct btrfs_path *path, |
| 575 | struct extent_buffer *eb, int slot, |
| 576 | struct btrfs_key *key) |
| 577 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 578 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 579 | int found_type; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 580 | u64 extent_end; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 581 | u64 start = key->offset; |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 582 | u64 nbytes = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 583 | struct btrfs_file_extent_item *item; |
| 584 | struct inode *inode = NULL; |
| 585 | unsigned long size; |
| 586 | int ret = 0; |
| 587 | |
| 588 | item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 589 | found_type = btrfs_file_extent_type(eb, item); |
| 590 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 591 | if (found_type == BTRFS_FILE_EXTENT_REG || |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 592 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 593 | nbytes = btrfs_file_extent_num_bytes(eb, item); |
| 594 | extent_end = start + nbytes; |
| 595 | |
| 596 | /* |
| 597 | * We don't add to the inodes nbytes if we are prealloc or a |
| 598 | * hole. |
| 599 | */ |
| 600 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 601 | nbytes = 0; |
| 602 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 603 | size = btrfs_file_extent_inline_len(eb, slot, item); |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 604 | nbytes = btrfs_file_extent_ram_bytes(eb, item); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 605 | extent_end = ALIGN(start + size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 606 | fs_info->sectorsize); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 607 | } else { |
| 608 | ret = 0; |
| 609 | goto out; |
| 610 | } |
| 611 | |
| 612 | inode = read_one_inode(root, key->objectid); |
| 613 | if (!inode) { |
| 614 | ret = -EIO; |
| 615 | goto out; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * first check to see if we already have this extent in the |
| 620 | * file. This must be done before the btrfs_drop_extents run |
| 621 | * so we don't try to drop this extent. |
| 622 | */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 623 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 624 | btrfs_ino(BTRFS_I(inode)), start, 0); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 625 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 626 | if (ret == 0 && |
| 627 | (found_type == BTRFS_FILE_EXTENT_REG || |
| 628 | found_type == BTRFS_FILE_EXTENT_PREALLOC)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 629 | struct btrfs_file_extent_item cmp1; |
| 630 | struct btrfs_file_extent_item cmp2; |
| 631 | struct btrfs_file_extent_item *existing; |
| 632 | struct extent_buffer *leaf; |
| 633 | |
| 634 | leaf = path->nodes[0]; |
| 635 | existing = btrfs_item_ptr(leaf, path->slots[0], |
| 636 | struct btrfs_file_extent_item); |
| 637 | |
| 638 | read_extent_buffer(eb, &cmp1, (unsigned long)item, |
| 639 | sizeof(cmp1)); |
| 640 | read_extent_buffer(leaf, &cmp2, (unsigned long)existing, |
| 641 | sizeof(cmp2)); |
| 642 | |
| 643 | /* |
| 644 | * we already have a pointer to this exact extent, |
| 645 | * we don't have to do anything |
| 646 | */ |
| 647 | if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 648 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 649 | goto out; |
| 650 | } |
| 651 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 652 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 653 | |
| 654 | /* drop any overlapping extents */ |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 655 | ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 656 | if (ret) |
| 657 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 658 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 659 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 660 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 661 | u64 offset; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 662 | unsigned long dest_offset; |
| 663 | struct btrfs_key ins; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 664 | |
Filipe Manana | 3168021c | 2017-02-01 14:58:02 +0000 | [diff] [blame] | 665 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0 && |
| 666 | btrfs_fs_incompat(fs_info, NO_HOLES)) |
| 667 | goto update_inode; |
| 668 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 669 | ret = btrfs_insert_empty_item(trans, root, path, key, |
| 670 | sizeof(*item)); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 671 | if (ret) |
| 672 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 673 | dest_offset = btrfs_item_ptr_offset(path->nodes[0], |
| 674 | path->slots[0]); |
| 675 | copy_extent_buffer(path->nodes[0], eb, dest_offset, |
| 676 | (unsigned long)item, sizeof(*item)); |
| 677 | |
| 678 | ins.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 679 | ins.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
| 680 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 681 | offset = key->offset - btrfs_file_extent_offset(eb, item); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 682 | |
Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 683 | /* |
| 684 | * Manually record dirty extent, as here we did a shallow |
| 685 | * file extent item copy and skip normal backref update, |
| 686 | * but modifying extent tree all by ourselves. |
| 687 | * So need to manually record dirty extent for qgroup, |
| 688 | * as the owner of the file extent changed from log tree |
| 689 | * (doesn't affect qgroup) to fs/file tree(affects qgroup) |
| 690 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 691 | ret = btrfs_qgroup_trace_extent(trans, fs_info, |
Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 692 | btrfs_file_extent_disk_bytenr(eb, item), |
| 693 | btrfs_file_extent_disk_num_bytes(eb, item), |
| 694 | GFP_NOFS); |
| 695 | if (ret < 0) |
| 696 | goto out; |
| 697 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 698 | if (ins.objectid > 0) { |
| 699 | u64 csum_start; |
| 700 | u64 csum_end; |
| 701 | LIST_HEAD(ordered_sums); |
| 702 | /* |
| 703 | * is this extent already allocated in the extent |
| 704 | * allocation tree? If so, just add a reference |
| 705 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 706 | ret = btrfs_lookup_data_extent(fs_info, ins.objectid, |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 707 | ins.offset); |
| 708 | if (ret == 0) { |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 709 | ret = btrfs_inc_extent_ref(trans, root, |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 710 | ins.objectid, ins.offset, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 711 | 0, root->root_key.objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 712 | key->objectid, offset); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 713 | if (ret) |
| 714 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 715 | } else { |
| 716 | /* |
| 717 | * insert the extent pointer in the extent |
| 718 | * allocation tree |
| 719 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 720 | ret = btrfs_alloc_logged_file_extent(trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 721 | fs_info, |
| 722 | root->root_key.objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 723 | key->objectid, offset, &ins); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 724 | if (ret) |
| 725 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 726 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 727 | btrfs_release_path(path); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 728 | |
| 729 | if (btrfs_file_extent_compression(eb, item)) { |
| 730 | csum_start = ins.objectid; |
| 731 | csum_end = csum_start + ins.offset; |
| 732 | } else { |
| 733 | csum_start = ins.objectid + |
| 734 | btrfs_file_extent_offset(eb, item); |
| 735 | csum_end = csum_start + |
| 736 | btrfs_file_extent_num_bytes(eb, item); |
| 737 | } |
| 738 | |
| 739 | ret = btrfs_lookup_csums_range(root->log_root, |
| 740 | csum_start, csum_end - 1, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 741 | &ordered_sums, 0); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 742 | if (ret) |
| 743 | goto out; |
Filipe Manana | b84b839 | 2015-08-19 11:09:40 +0100 | [diff] [blame] | 744 | /* |
| 745 | * Now delete all existing cums in the csum root that |
| 746 | * cover our range. We do this because we can have an |
| 747 | * extent that is completely referenced by one file |
| 748 | * extent item and partially referenced by another |
| 749 | * file extent item (like after using the clone or |
| 750 | * extent_same ioctls). In this case if we end up doing |
| 751 | * the replay of the one that partially references the |
| 752 | * extent first, and we do not do the csum deletion |
| 753 | * below, we can get 2 csum items in the csum tree that |
| 754 | * overlap each other. For example, imagine our log has |
| 755 | * the two following file extent items: |
| 756 | * |
| 757 | * key (257 EXTENT_DATA 409600) |
| 758 | * extent data disk byte 12845056 nr 102400 |
| 759 | * extent data offset 20480 nr 20480 ram 102400 |
| 760 | * |
| 761 | * key (257 EXTENT_DATA 819200) |
| 762 | * extent data disk byte 12845056 nr 102400 |
| 763 | * extent data offset 0 nr 102400 ram 102400 |
| 764 | * |
| 765 | * Where the second one fully references the 100K extent |
| 766 | * that starts at disk byte 12845056, and the log tree |
| 767 | * has a single csum item that covers the entire range |
| 768 | * of the extent: |
| 769 | * |
| 770 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 |
| 771 | * |
| 772 | * After the first file extent item is replayed, the |
| 773 | * csum tree gets the following csum item: |
| 774 | * |
| 775 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 |
| 776 | * |
| 777 | * Which covers the 20K sub-range starting at offset 20K |
| 778 | * of our extent. Now when we replay the second file |
| 779 | * extent item, if we do not delete existing csum items |
| 780 | * that cover any of its blocks, we end up getting two |
| 781 | * csum items in our csum tree that overlap each other: |
| 782 | * |
| 783 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 |
| 784 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 |
| 785 | * |
| 786 | * Which is a problem, because after this anyone trying |
| 787 | * to lookup up for the checksum of any block of our |
| 788 | * extent starting at an offset of 40K or higher, will |
| 789 | * end up looking at the second csum item only, which |
| 790 | * does not contain the checksum for any block starting |
| 791 | * at offset 40K or higher of our extent. |
| 792 | */ |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 793 | while (!list_empty(&ordered_sums)) { |
| 794 | struct btrfs_ordered_sum *sums; |
| 795 | sums = list_entry(ordered_sums.next, |
| 796 | struct btrfs_ordered_sum, |
| 797 | list); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 798 | if (!ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 799 | ret = btrfs_del_csums(trans, fs_info, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 800 | sums->bytenr, |
| 801 | sums->len); |
Filipe Manana | b84b839 | 2015-08-19 11:09:40 +0100 | [diff] [blame] | 802 | if (!ret) |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 803 | ret = btrfs_csum_file_blocks(trans, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 804 | fs_info->csum_root, sums); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 805 | list_del(&sums->list); |
| 806 | kfree(sums); |
| 807 | } |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 808 | if (ret) |
| 809 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 810 | } else { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 811 | btrfs_release_path(path); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 812 | } |
| 813 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 814 | /* inline extents are easy, we just overwrite them */ |
| 815 | ret = overwrite_item(trans, root, path, eb, slot, key); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 816 | if (ret) |
| 817 | goto out; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 818 | } |
| 819 | |
Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 820 | inode_add_bytes(inode, nbytes); |
Filipe Manana | 3168021c | 2017-02-01 14:58:02 +0000 | [diff] [blame] | 821 | update_inode: |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 822 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 823 | out: |
| 824 | if (inode) |
| 825 | iput(inode); |
| 826 | return ret; |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * when cleaning up conflicts between the directory names in the |
| 831 | * subvolume, directory names in the log and directory names in the |
| 832 | * inode back references, we may have to unlink inodes from directories. |
| 833 | * |
| 834 | * This is a helper function to do the unlink of a specific directory |
| 835 | * item |
| 836 | */ |
| 837 | static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, |
| 838 | struct btrfs_root *root, |
| 839 | struct btrfs_path *path, |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 840 | struct btrfs_inode *dir, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 841 | struct btrfs_dir_item *di) |
| 842 | { |
| 843 | struct inode *inode; |
| 844 | char *name; |
| 845 | int name_len; |
| 846 | struct extent_buffer *leaf; |
| 847 | struct btrfs_key location; |
| 848 | int ret; |
| 849 | |
| 850 | leaf = path->nodes[0]; |
| 851 | |
| 852 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
| 853 | name_len = btrfs_dir_name_len(leaf, di); |
| 854 | name = kmalloc(name_len, GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 855 | if (!name) |
| 856 | return -ENOMEM; |
| 857 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 858 | read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 859 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 860 | |
| 861 | inode = read_one_inode(root, location.objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 862 | if (!inode) { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 863 | ret = -EIO; |
| 864 | goto out; |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 865 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 866 | |
Yan Zheng | ec051c0 | 2009-01-05 15:43:42 -0500 | [diff] [blame] | 867 | ret = link_to_fixup_dir(trans, root, path, location.objectid); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 868 | if (ret) |
| 869 | goto out; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 870 | |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 871 | ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name, |
| 872 | name_len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 873 | if (ret) |
| 874 | goto out; |
Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 875 | else |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 876 | ret = btrfs_run_delayed_items(trans); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 877 | out: |
| 878 | kfree(name); |
| 879 | iput(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 880 | return ret; |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * helper function to see if a given name and sequence number found |
| 885 | * in an inode back reference are already in a directory and correctly |
| 886 | * point to this inode |
| 887 | */ |
| 888 | static noinline int inode_in_dir(struct btrfs_root *root, |
| 889 | struct btrfs_path *path, |
| 890 | u64 dirid, u64 objectid, u64 index, |
| 891 | const char *name, int name_len) |
| 892 | { |
| 893 | struct btrfs_dir_item *di; |
| 894 | struct btrfs_key location; |
| 895 | int match = 0; |
| 896 | |
| 897 | di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, |
| 898 | index, name, name_len, 0); |
| 899 | if (di && !IS_ERR(di)) { |
| 900 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
| 901 | if (location.objectid != objectid) |
| 902 | goto out; |
| 903 | } else |
| 904 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 905 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 906 | |
| 907 | di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0); |
| 908 | if (di && !IS_ERR(di)) { |
| 909 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); |
| 910 | if (location.objectid != objectid) |
| 911 | goto out; |
| 912 | } else |
| 913 | goto out; |
| 914 | match = 1; |
| 915 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 916 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 917 | return match; |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * helper function to check a log tree for a named back reference in |
| 922 | * an inode. This is used to decide if a back reference that is |
| 923 | * found in the subvolume conflicts with what we find in the log. |
| 924 | * |
| 925 | * inode backreferences may have multiple refs in a single item, |
| 926 | * during replay we process one reference at a time, and we don't |
| 927 | * want to delete valid links to a file from the subvolume if that |
| 928 | * link is also in the log. |
| 929 | */ |
| 930 | static noinline int backref_in_log(struct btrfs_root *log, |
| 931 | struct btrfs_key *key, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 932 | u64 ref_objectid, |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 933 | const char *name, int namelen) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 934 | { |
| 935 | struct btrfs_path *path; |
| 936 | struct btrfs_inode_ref *ref; |
| 937 | unsigned long ptr; |
| 938 | unsigned long ptr_end; |
| 939 | unsigned long name_ptr; |
| 940 | int found_name_len; |
| 941 | int item_size; |
| 942 | int ret; |
| 943 | int match = 0; |
| 944 | |
| 945 | path = btrfs_alloc_path(); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 946 | if (!path) |
| 947 | return -ENOMEM; |
| 948 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 949 | ret = btrfs_search_slot(NULL, log, key, path, 0, 0); |
| 950 | if (ret != 0) |
| 951 | goto out; |
| 952 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 953 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 954 | |
| 955 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 956 | if (btrfs_find_name_in_ext_backref(path->nodes[0], |
| 957 | path->slots[0], |
| 958 | ref_objectid, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 959 | name, namelen, NULL)) |
| 960 | match = 1; |
| 961 | |
| 962 | goto out; |
| 963 | } |
| 964 | |
| 965 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 966 | ptr_end = ptr + item_size; |
| 967 | while (ptr < ptr_end) { |
| 968 | ref = (struct btrfs_inode_ref *)ptr; |
| 969 | found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref); |
| 970 | if (found_name_len == namelen) { |
| 971 | name_ptr = (unsigned long)(ref + 1); |
| 972 | ret = memcmp_extent_buffer(path->nodes[0], name, |
| 973 | name_ptr, namelen); |
| 974 | if (ret == 0) { |
| 975 | match = 1; |
| 976 | goto out; |
| 977 | } |
| 978 | } |
| 979 | ptr = (unsigned long)(ref + 1) + found_name_len; |
| 980 | } |
| 981 | out: |
| 982 | btrfs_free_path(path); |
| 983 | return match; |
| 984 | } |
| 985 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 986 | static inline int __add_inode_ref(struct btrfs_trans_handle *trans, |
| 987 | struct btrfs_root *root, |
| 988 | struct btrfs_path *path, |
| 989 | struct btrfs_root *log_root, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 990 | struct btrfs_inode *dir, |
| 991 | struct btrfs_inode *inode, |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 992 | u64 inode_objectid, u64 parent_objectid, |
| 993 | u64 ref_index, char *name, int namelen, |
| 994 | int *search_done) |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 995 | { |
| 996 | int ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 997 | char *victim_name; |
| 998 | int victim_name_len; |
| 999 | struct extent_buffer *leaf; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1000 | struct btrfs_dir_item *di; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1001 | struct btrfs_key search_key; |
| 1002 | struct btrfs_inode_extref *extref; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1003 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1004 | again: |
| 1005 | /* Search old style refs */ |
| 1006 | search_key.objectid = inode_objectid; |
| 1007 | search_key.type = BTRFS_INODE_REF_KEY; |
| 1008 | search_key.offset = parent_objectid; |
| 1009 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1010 | if (ret == 0) { |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1011 | struct btrfs_inode_ref *victim_ref; |
| 1012 | unsigned long ptr; |
| 1013 | unsigned long ptr_end; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1014 | |
| 1015 | leaf = path->nodes[0]; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1016 | |
| 1017 | /* are we trying to overwrite a back ref for the root directory |
| 1018 | * if so, just jump out, we're done |
| 1019 | */ |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1020 | if (search_key.objectid == search_key.offset) |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1021 | return 1; |
| 1022 | |
| 1023 | /* check all the names in this back reference to see |
| 1024 | * if they are in the log. if so, we allow them to stay |
| 1025 | * otherwise they must be unlinked as a conflict |
| 1026 | */ |
| 1027 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1028 | ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1029 | while (ptr < ptr_end) { |
| 1030 | victim_ref = (struct btrfs_inode_ref *)ptr; |
| 1031 | victim_name_len = btrfs_inode_ref_name_len(leaf, |
| 1032 | victim_ref); |
| 1033 | victim_name = kmalloc(victim_name_len, GFP_NOFS); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1034 | if (!victim_name) |
| 1035 | return -ENOMEM; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1036 | |
| 1037 | read_extent_buffer(leaf, victim_name, |
| 1038 | (unsigned long)(victim_ref + 1), |
| 1039 | victim_name_len); |
| 1040 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1041 | if (!backref_in_log(log_root, &search_key, |
| 1042 | parent_objectid, |
| 1043 | victim_name, |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1044 | victim_name_len)) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1045 | inc_nlink(&inode->vfs_inode); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1046 | btrfs_release_path(path); |
| 1047 | |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1048 | ret = btrfs_unlink_inode(trans, root, dir, inode, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1049 | victim_name, victim_name_len); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1050 | kfree(victim_name); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1051 | if (ret) |
| 1052 | return ret; |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 1053 | ret = btrfs_run_delayed_items(trans); |
Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 1054 | if (ret) |
| 1055 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1056 | *search_done = 1; |
| 1057 | goto again; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1058 | } |
| 1059 | kfree(victim_name); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1060 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1061 | ptr = (unsigned long)(victim_ref + 1) + victim_name_len; |
| 1062 | } |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1063 | |
| 1064 | /* |
| 1065 | * NOTE: we have searched root tree and checked the |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 1066 | * corresponding ref, it does not need to check again. |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1067 | */ |
| 1068 | *search_done = 1; |
| 1069 | } |
| 1070 | btrfs_release_path(path); |
| 1071 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1072 | /* Same search but for extended refs */ |
| 1073 | extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen, |
| 1074 | inode_objectid, parent_objectid, 0, |
| 1075 | 0); |
| 1076 | if (!IS_ERR_OR_NULL(extref)) { |
| 1077 | u32 item_size; |
| 1078 | u32 cur_offset = 0; |
| 1079 | unsigned long base; |
| 1080 | struct inode *victim_parent; |
| 1081 | |
| 1082 | leaf = path->nodes[0]; |
| 1083 | |
| 1084 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1085 | base = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1086 | |
| 1087 | while (cur_offset < item_size) { |
Quentin Casasnovas | dd9ef13 | 2015-03-03 16:31:38 +0100 | [diff] [blame] | 1088 | extref = (struct btrfs_inode_extref *)(base + cur_offset); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1089 | |
| 1090 | victim_name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 1091 | |
| 1092 | if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid) |
| 1093 | goto next; |
| 1094 | |
| 1095 | victim_name = kmalloc(victim_name_len, GFP_NOFS); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1096 | if (!victim_name) |
| 1097 | return -ENOMEM; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1098 | read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name, |
| 1099 | victim_name_len); |
| 1100 | |
| 1101 | search_key.objectid = inode_objectid; |
| 1102 | search_key.type = BTRFS_INODE_EXTREF_KEY; |
| 1103 | search_key.offset = btrfs_extref_hash(parent_objectid, |
| 1104 | victim_name, |
| 1105 | victim_name_len); |
| 1106 | ret = 0; |
| 1107 | if (!backref_in_log(log_root, &search_key, |
| 1108 | parent_objectid, victim_name, |
| 1109 | victim_name_len)) { |
| 1110 | ret = -ENOENT; |
| 1111 | victim_parent = read_one_inode(root, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1112 | parent_objectid); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1113 | if (victim_parent) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1114 | inc_nlink(&inode->vfs_inode); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1115 | btrfs_release_path(path); |
| 1116 | |
| 1117 | ret = btrfs_unlink_inode(trans, root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1118 | BTRFS_I(victim_parent), |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1119 | inode, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1120 | victim_name, |
| 1121 | victim_name_len); |
Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 1122 | if (!ret) |
| 1123 | ret = btrfs_run_delayed_items( |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 1124 | trans); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1125 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1126 | iput(victim_parent); |
| 1127 | kfree(victim_name); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1128 | if (ret) |
| 1129 | return ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1130 | *search_done = 1; |
| 1131 | goto again; |
| 1132 | } |
| 1133 | kfree(victim_name); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1134 | next: |
| 1135 | cur_offset += victim_name_len + sizeof(*extref); |
| 1136 | } |
| 1137 | *search_done = 1; |
| 1138 | } |
| 1139 | btrfs_release_path(path); |
| 1140 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1141 | /* look for a conflicting sequence number */ |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1142 | di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir), |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1143 | ref_index, name, namelen, 0); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1144 | if (di && !IS_ERR(di)) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1145 | ret = drop_one_dir_item(trans, root, path, dir, di); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1146 | if (ret) |
| 1147 | return ret; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1148 | } |
| 1149 | btrfs_release_path(path); |
| 1150 | |
| 1151 | /* look for a conflicing name */ |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1152 | di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1153 | name, namelen, 0); |
| 1154 | if (di && !IS_ERR(di)) { |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1155 | ret = drop_one_dir_item(trans, root, path, dir, di); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1156 | if (ret) |
| 1157 | return ret; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1158 | } |
| 1159 | btrfs_release_path(path); |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1163 | |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1164 | static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
| 1165 | u32 *namelen, char **name, u64 *index, |
| 1166 | u64 *parent_objectid) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1167 | { |
| 1168 | struct btrfs_inode_extref *extref; |
| 1169 | |
| 1170 | extref = (struct btrfs_inode_extref *)ref_ptr; |
| 1171 | |
| 1172 | *namelen = btrfs_inode_extref_name_len(eb, extref); |
| 1173 | *name = kmalloc(*namelen, GFP_NOFS); |
| 1174 | if (*name == NULL) |
| 1175 | return -ENOMEM; |
| 1176 | |
| 1177 | read_extent_buffer(eb, *name, (unsigned long)&extref->name, |
| 1178 | *namelen); |
| 1179 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1180 | if (index) |
| 1181 | *index = btrfs_inode_extref_index(eb, extref); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1182 | if (parent_objectid) |
| 1183 | *parent_objectid = btrfs_inode_extref_parent(eb, extref); |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1188 | static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
| 1189 | u32 *namelen, char **name, u64 *index) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1190 | { |
| 1191 | struct btrfs_inode_ref *ref; |
| 1192 | |
| 1193 | ref = (struct btrfs_inode_ref *)ref_ptr; |
| 1194 | |
| 1195 | *namelen = btrfs_inode_ref_name_len(eb, ref); |
| 1196 | *name = kmalloc(*namelen, GFP_NOFS); |
| 1197 | if (*name == NULL) |
| 1198 | return -ENOMEM; |
| 1199 | |
| 1200 | read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen); |
| 1201 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1202 | if (index) |
| 1203 | *index = btrfs_inode_ref_index(eb, ref); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1204 | |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1208 | /* |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1209 | * Take an inode reference item from the log tree and iterate all names from the |
| 1210 | * inode reference item in the subvolume tree with the same key (if it exists). |
| 1211 | * For any name that is not in the inode reference item from the log tree, do a |
| 1212 | * proper unlink of that name (that is, remove its entry from the inode |
| 1213 | * reference item and both dir index keys). |
| 1214 | */ |
| 1215 | static int unlink_old_inode_refs(struct btrfs_trans_handle *trans, |
| 1216 | struct btrfs_root *root, |
| 1217 | struct btrfs_path *path, |
| 1218 | struct btrfs_inode *inode, |
| 1219 | struct extent_buffer *log_eb, |
| 1220 | int log_slot, |
| 1221 | struct btrfs_key *key) |
| 1222 | { |
| 1223 | int ret; |
| 1224 | unsigned long ref_ptr; |
| 1225 | unsigned long ref_end; |
| 1226 | struct extent_buffer *eb; |
| 1227 | |
| 1228 | again: |
| 1229 | btrfs_release_path(path); |
| 1230 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 1231 | if (ret > 0) { |
| 1232 | ret = 0; |
| 1233 | goto out; |
| 1234 | } |
| 1235 | if (ret < 0) |
| 1236 | goto out; |
| 1237 | |
| 1238 | eb = path->nodes[0]; |
| 1239 | ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]); |
| 1240 | ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]); |
| 1241 | while (ref_ptr < ref_end) { |
| 1242 | char *name = NULL; |
| 1243 | int namelen; |
| 1244 | u64 parent_id; |
| 1245 | |
| 1246 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
| 1247 | ret = extref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1248 | NULL, &parent_id); |
| 1249 | } else { |
| 1250 | parent_id = key->offset; |
| 1251 | ret = ref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1252 | NULL); |
| 1253 | } |
| 1254 | if (ret) |
| 1255 | goto out; |
| 1256 | |
| 1257 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
| 1258 | ret = btrfs_find_name_in_ext_backref(log_eb, log_slot, |
| 1259 | parent_id, name, |
| 1260 | namelen, NULL); |
| 1261 | else |
| 1262 | ret = btrfs_find_name_in_backref(log_eb, log_slot, name, |
| 1263 | namelen, NULL); |
| 1264 | |
| 1265 | if (!ret) { |
| 1266 | struct inode *dir; |
| 1267 | |
| 1268 | btrfs_release_path(path); |
| 1269 | dir = read_one_inode(root, parent_id); |
| 1270 | if (!dir) { |
| 1271 | ret = -ENOENT; |
| 1272 | kfree(name); |
| 1273 | goto out; |
| 1274 | } |
| 1275 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 1276 | inode, name, namelen); |
| 1277 | kfree(name); |
| 1278 | iput(dir); |
| 1279 | if (ret) |
| 1280 | goto out; |
| 1281 | goto again; |
| 1282 | } |
| 1283 | |
| 1284 | kfree(name); |
| 1285 | ref_ptr += namelen; |
| 1286 | if (key->type == BTRFS_INODE_EXTREF_KEY) |
| 1287 | ref_ptr += sizeof(struct btrfs_inode_extref); |
| 1288 | else |
| 1289 | ref_ptr += sizeof(struct btrfs_inode_ref); |
| 1290 | } |
| 1291 | ret = 0; |
| 1292 | out: |
| 1293 | btrfs_release_path(path); |
| 1294 | return ret; |
| 1295 | } |
| 1296 | |
| 1297 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1298 | * replay one inode back reference item found in the log tree. |
| 1299 | * eb, slot and key refer to the buffer and key found in the log tree. |
| 1300 | * root is the destination we are replaying into, and path is for temp |
| 1301 | * use by this function. (it should be released on return). |
| 1302 | */ |
| 1303 | static noinline int add_inode_ref(struct btrfs_trans_handle *trans, |
| 1304 | struct btrfs_root *root, |
| 1305 | struct btrfs_root *log, |
| 1306 | struct btrfs_path *path, |
| 1307 | struct extent_buffer *eb, int slot, |
| 1308 | struct btrfs_key *key) |
| 1309 | { |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1310 | struct inode *dir = NULL; |
| 1311 | struct inode *inode = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1312 | unsigned long ref_ptr; |
| 1313 | unsigned long ref_end; |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1314 | char *name = NULL; |
liubo | 34f3e4f | 2011-08-06 08:35:23 +0000 | [diff] [blame] | 1315 | int namelen; |
| 1316 | int ret; |
liubo | c622ae6 | 2011-03-26 08:01:12 -0400 | [diff] [blame] | 1317 | int search_done = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1318 | int log_ref_ver = 0; |
| 1319 | u64 parent_objectid; |
| 1320 | u64 inode_objectid; |
Chris Mason | f46dbe3 | 2012-10-09 11:17:20 -0400 | [diff] [blame] | 1321 | u64 ref_index = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1322 | int ref_struct_size; |
| 1323 | |
| 1324 | ref_ptr = btrfs_item_ptr_offset(eb, slot); |
| 1325 | ref_end = ref_ptr + btrfs_item_size_nr(eb, slot); |
| 1326 | |
| 1327 | if (key->type == BTRFS_INODE_EXTREF_KEY) { |
| 1328 | struct btrfs_inode_extref *r; |
| 1329 | |
| 1330 | ref_struct_size = sizeof(struct btrfs_inode_extref); |
| 1331 | log_ref_ver = 1; |
| 1332 | r = (struct btrfs_inode_extref *)ref_ptr; |
| 1333 | parent_objectid = btrfs_inode_extref_parent(eb, r); |
| 1334 | } else { |
| 1335 | ref_struct_size = sizeof(struct btrfs_inode_ref); |
| 1336 | parent_objectid = key->offset; |
| 1337 | } |
| 1338 | inode_objectid = key->objectid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1339 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1340 | /* |
| 1341 | * it is possible that we didn't log all the parent directories |
| 1342 | * for a given inode. If we don't find the dir, just don't |
| 1343 | * copy the back ref in. The link count fixup code will take |
| 1344 | * care of the rest |
| 1345 | */ |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1346 | dir = read_one_inode(root, parent_objectid); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1347 | if (!dir) { |
| 1348 | ret = -ENOENT; |
| 1349 | goto out; |
| 1350 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1351 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1352 | inode = read_one_inode(root, inode_objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1353 | if (!inode) { |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1354 | ret = -EIO; |
| 1355 | goto out; |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1356 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1357 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1358 | while (ref_ptr < ref_end) { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1359 | if (log_ref_ver) { |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1360 | ret = extref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1361 | &ref_index, &parent_objectid); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1362 | /* |
| 1363 | * parent object can change from one array |
| 1364 | * item to another. |
| 1365 | */ |
| 1366 | if (!dir) |
| 1367 | dir = read_one_inode(root, parent_objectid); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1368 | if (!dir) { |
| 1369 | ret = -ENOENT; |
| 1370 | goto out; |
| 1371 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1372 | } else { |
Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1373 | ret = ref_get_fields(eb, ref_ptr, &namelen, &name, |
| 1374 | &ref_index); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1375 | } |
| 1376 | if (ret) |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1377 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1378 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1379 | /* if we already have a perfect match, we're done */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 1380 | if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)), |
| 1381 | btrfs_ino(BTRFS_I(inode)), ref_index, |
| 1382 | name, namelen)) { |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1383 | /* |
| 1384 | * look for a conflicting back reference in the |
| 1385 | * metadata. if we find one we have to unlink that name |
| 1386 | * of the file before we add our new link. Later on, we |
| 1387 | * overwrite any existing back reference, and we don't |
| 1388 | * want to create dangling pointers in the directory. |
| 1389 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1390 | |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1391 | if (!search_done) { |
| 1392 | ret = __add_inode_ref(trans, root, path, log, |
Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1393 | BTRFS_I(dir), |
David Sterba | d75eefd | 2017-02-10 20:20:19 +0100 | [diff] [blame] | 1394 | BTRFS_I(inode), |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1395 | inode_objectid, |
| 1396 | parent_objectid, |
| 1397 | ref_index, name, namelen, |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1398 | &search_done); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1399 | if (ret) { |
| 1400 | if (ret == 1) |
| 1401 | ret = 0; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1402 | goto out; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1403 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1404 | } |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1405 | |
| 1406 | /* insert our name */ |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 1407 | ret = btrfs_add_link(trans, BTRFS_I(dir), |
| 1408 | BTRFS_I(inode), |
| 1409 | name, namelen, 0, ref_index); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1410 | if (ret) |
| 1411 | goto out; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1412 | |
| 1413 | btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1414 | } |
liubo | c622ae6 | 2011-03-26 08:01:12 -0400 | [diff] [blame] | 1415 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1416 | ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen; |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1417 | kfree(name); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1418 | name = NULL; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1419 | if (log_ref_ver) { |
| 1420 | iput(dir); |
| 1421 | dir = NULL; |
| 1422 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1423 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1424 | |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1425 | /* |
| 1426 | * Before we overwrite the inode reference item in the subvolume tree |
| 1427 | * with the item from the log tree, we must unlink all names from the |
| 1428 | * parent directory that are in the subvolume's tree inode reference |
| 1429 | * item, otherwise we end up with an inconsistent subvolume tree where |
| 1430 | * dir index entries exist for a name but there is no inode reference |
| 1431 | * item with the same name. |
| 1432 | */ |
| 1433 | ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot, |
| 1434 | key); |
| 1435 | if (ret) |
| 1436 | goto out; |
| 1437 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1438 | /* finally write the back reference in the inode */ |
| 1439 | ret = overwrite_item(trans, root, path, eb, slot, key); |
Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1440 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1441 | btrfs_release_path(path); |
Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1442 | kfree(name); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1443 | iput(dir); |
| 1444 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1445 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1446 | } |
| 1447 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1448 | static int insert_orphan_item(struct btrfs_trans_handle *trans, |
David Sterba | 9c4f61f | 2015-01-02 19:12:57 +0100 | [diff] [blame] | 1449 | struct btrfs_root *root, u64 ino) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1450 | { |
| 1451 | int ret; |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 1452 | |
David Sterba | 9c4f61f | 2015-01-02 19:12:57 +0100 | [diff] [blame] | 1453 | ret = btrfs_insert_orphan_item(trans, root, ino); |
| 1454 | if (ret == -EEXIST) |
| 1455 | ret = 0; |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 1456 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1457 | return ret; |
| 1458 | } |
| 1459 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1460 | static int count_inode_extrefs(struct btrfs_root *root, |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1461 | struct btrfs_inode *inode, struct btrfs_path *path) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1462 | { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1463 | int ret = 0; |
| 1464 | int name_len; |
| 1465 | unsigned int nlink = 0; |
| 1466 | u32 item_size; |
| 1467 | u32 cur_offset = 0; |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1468 | u64 inode_objectid = btrfs_ino(inode); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1469 | u64 offset = 0; |
| 1470 | unsigned long ptr; |
| 1471 | struct btrfs_inode_extref *extref; |
| 1472 | struct extent_buffer *leaf; |
| 1473 | |
| 1474 | while (1) { |
| 1475 | ret = btrfs_find_one_extref(root, inode_objectid, offset, path, |
| 1476 | &extref, &offset); |
| 1477 | if (ret) |
| 1478 | break; |
| 1479 | |
| 1480 | leaf = path->nodes[0]; |
| 1481 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1482 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 1483 | cur_offset = 0; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1484 | |
| 1485 | while (cur_offset < item_size) { |
| 1486 | extref = (struct btrfs_inode_extref *) (ptr + cur_offset); |
| 1487 | name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 1488 | |
| 1489 | nlink++; |
| 1490 | |
| 1491 | cur_offset += name_len + sizeof(*extref); |
| 1492 | } |
| 1493 | |
| 1494 | offset++; |
| 1495 | btrfs_release_path(path); |
| 1496 | } |
| 1497 | btrfs_release_path(path); |
| 1498 | |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 1499 | if (ret < 0 && ret != -ENOENT) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1500 | return ret; |
| 1501 | return nlink; |
| 1502 | } |
| 1503 | |
| 1504 | static int count_inode_refs(struct btrfs_root *root, |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1505 | struct btrfs_inode *inode, struct btrfs_path *path) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1506 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1507 | int ret; |
| 1508 | struct btrfs_key key; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1509 | unsigned int nlink = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1510 | unsigned long ptr; |
| 1511 | unsigned long ptr_end; |
| 1512 | int name_len; |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1513 | u64 ino = btrfs_ino(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1514 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1515 | key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1516 | key.type = BTRFS_INODE_REF_KEY; |
| 1517 | key.offset = (u64)-1; |
| 1518 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1519 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1520 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1521 | if (ret < 0) |
| 1522 | break; |
| 1523 | if (ret > 0) { |
| 1524 | if (path->slots[0] == 0) |
| 1525 | break; |
| 1526 | path->slots[0]--; |
| 1527 | } |
Filipe David Borba Manana | e93ae26 | 2013-10-14 22:49:11 +0100 | [diff] [blame] | 1528 | process_slot: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1529 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1530 | path->slots[0]); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1531 | if (key.objectid != ino || |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1532 | key.type != BTRFS_INODE_REF_KEY) |
| 1533 | break; |
| 1534 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
| 1535 | ptr_end = ptr + btrfs_item_size_nr(path->nodes[0], |
| 1536 | path->slots[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1537 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1538 | struct btrfs_inode_ref *ref; |
| 1539 | |
| 1540 | ref = (struct btrfs_inode_ref *)ptr; |
| 1541 | name_len = btrfs_inode_ref_name_len(path->nodes[0], |
| 1542 | ref); |
| 1543 | ptr = (unsigned long)(ref + 1) + name_len; |
| 1544 | nlink++; |
| 1545 | } |
| 1546 | |
| 1547 | if (key.offset == 0) |
| 1548 | break; |
Filipe David Borba Manana | e93ae26 | 2013-10-14 22:49:11 +0100 | [diff] [blame] | 1549 | if (path->slots[0] > 0) { |
| 1550 | path->slots[0]--; |
| 1551 | goto process_slot; |
| 1552 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1553 | key.offset--; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1554 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1555 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1556 | btrfs_release_path(path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1557 | |
| 1558 | return nlink; |
| 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * There are a few corners where the link count of the file can't |
| 1563 | * be properly maintained during replay. So, instead of adding |
| 1564 | * lots of complexity to the log code, we just scan the backrefs |
| 1565 | * for any file that has been through replay. |
| 1566 | * |
| 1567 | * The scan will update the link count on the inode to reflect the |
| 1568 | * number of back refs found. If it goes down to zero, the iput |
| 1569 | * will free the inode. |
| 1570 | */ |
| 1571 | static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, |
| 1572 | struct btrfs_root *root, |
| 1573 | struct inode *inode) |
| 1574 | { |
| 1575 | struct btrfs_path *path; |
| 1576 | int ret; |
| 1577 | u64 nlink = 0; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1578 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1579 | |
| 1580 | path = btrfs_alloc_path(); |
| 1581 | if (!path) |
| 1582 | return -ENOMEM; |
| 1583 | |
Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1584 | ret = count_inode_refs(root, BTRFS_I(inode), path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1585 | if (ret < 0) |
| 1586 | goto out; |
| 1587 | |
| 1588 | nlink = ret; |
| 1589 | |
Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1590 | ret = count_inode_extrefs(root, BTRFS_I(inode), path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1591 | if (ret < 0) |
| 1592 | goto out; |
| 1593 | |
| 1594 | nlink += ret; |
| 1595 | |
| 1596 | ret = 0; |
| 1597 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1598 | if (nlink != inode->i_nlink) { |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 1599 | set_nlink(inode, nlink); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1600 | btrfs_update_inode(trans, root, inode); |
| 1601 | } |
Chris Mason | 8d5bf1c | 2008-09-11 15:51:21 -0400 | [diff] [blame] | 1602 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1603 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1604 | if (inode->i_nlink == 0) { |
| 1605 | if (S_ISDIR(inode->i_mode)) { |
| 1606 | ret = replay_dir_deletes(trans, root, NULL, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1607 | ino, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1608 | if (ret) |
| 1609 | goto out; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1610 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1611 | ret = insert_orphan_item(trans, root, ino); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1612 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1613 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1614 | out: |
| 1615 | btrfs_free_path(path); |
| 1616 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, |
| 1620 | struct btrfs_root *root, |
| 1621 | struct btrfs_path *path) |
| 1622 | { |
| 1623 | int ret; |
| 1624 | struct btrfs_key key; |
| 1625 | struct inode *inode; |
| 1626 | |
| 1627 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; |
| 1628 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
| 1629 | key.offset = (u64)-1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1630 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1631 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1632 | if (ret < 0) |
| 1633 | break; |
| 1634 | |
| 1635 | if (ret == 1) { |
| 1636 | if (path->slots[0] == 0) |
| 1637 | break; |
| 1638 | path->slots[0]--; |
| 1639 | } |
| 1640 | |
| 1641 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 1642 | if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || |
| 1643 | key.type != BTRFS_ORPHAN_ITEM_KEY) |
| 1644 | break; |
| 1645 | |
| 1646 | ret = btrfs_del_item(trans, root, path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1647 | if (ret) |
| 1648 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1649 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1650 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1651 | inode = read_one_inode(root, key.offset); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1652 | if (!inode) |
| 1653 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1654 | |
| 1655 | ret = fixup_inode_link_count(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1656 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1657 | if (ret) |
| 1658 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1659 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1660 | /* |
| 1661 | * fixup on a directory may create new entries, |
| 1662 | * make sure we always look for the highset possible |
| 1663 | * offset |
| 1664 | */ |
| 1665 | key.offset = (u64)-1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1666 | } |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1667 | ret = 0; |
| 1668 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1669 | btrfs_release_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1670 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | |
| 1674 | /* |
| 1675 | * record a given inode in the fixup dir so we can check its link |
| 1676 | * count when replay is done. The link count is incremented here |
| 1677 | * so the inode won't go away until we check it |
| 1678 | */ |
| 1679 | static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, |
| 1680 | struct btrfs_root *root, |
| 1681 | struct btrfs_path *path, |
| 1682 | u64 objectid) |
| 1683 | { |
| 1684 | struct btrfs_key key; |
| 1685 | int ret = 0; |
| 1686 | struct inode *inode; |
| 1687 | |
| 1688 | inode = read_one_inode(root, objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1689 | if (!inode) |
| 1690 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1691 | |
| 1692 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1693 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1694 | key.offset = objectid; |
| 1695 | |
| 1696 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
| 1697 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1698 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1699 | if (ret == 0) { |
Josef Bacik | 9bf7a48 | 2013-03-01 13:35:47 -0500 | [diff] [blame] | 1700 | if (!inode->i_nlink) |
| 1701 | set_nlink(inode, 1); |
| 1702 | else |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 1703 | inc_nlink(inode); |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 1704 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1705 | } else if (ret == -EEXIST) { |
| 1706 | ret = 0; |
| 1707 | } else { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1708 | BUG(); /* Logic Error */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1709 | } |
| 1710 | iput(inode); |
| 1711 | |
| 1712 | return ret; |
| 1713 | } |
| 1714 | |
| 1715 | /* |
| 1716 | * when replaying the log for a directory, we only insert names |
| 1717 | * for inodes that actually exist. This means an fsync on a directory |
| 1718 | * does not implicitly fsync all the new files in it |
| 1719 | */ |
| 1720 | static noinline int insert_one_name(struct btrfs_trans_handle *trans, |
| 1721 | struct btrfs_root *root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1722 | u64 dirid, u64 index, |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 1723 | char *name, int name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1724 | struct btrfs_key *location) |
| 1725 | { |
| 1726 | struct inode *inode; |
| 1727 | struct inode *dir; |
| 1728 | int ret; |
| 1729 | |
| 1730 | inode = read_one_inode(root, location->objectid); |
| 1731 | if (!inode) |
| 1732 | return -ENOENT; |
| 1733 | |
| 1734 | dir = read_one_inode(root, dirid); |
| 1735 | if (!dir) { |
| 1736 | iput(inode); |
| 1737 | return -EIO; |
| 1738 | } |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1739 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 1740 | ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name, |
| 1741 | name_len, 1, index); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1742 | |
| 1743 | /* FIXME, put inode into FIXUP list */ |
| 1744 | |
| 1745 | iput(inode); |
| 1746 | iput(dir); |
| 1747 | return ret; |
| 1748 | } |
| 1749 | |
| 1750 | /* |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1751 | * Return true if an inode reference exists in the log for the given name, |
| 1752 | * inode and parent inode. |
| 1753 | */ |
| 1754 | static bool name_in_log_ref(struct btrfs_root *log_root, |
| 1755 | const char *name, const int name_len, |
| 1756 | const u64 dirid, const u64 ino) |
| 1757 | { |
| 1758 | struct btrfs_key search_key; |
| 1759 | |
| 1760 | search_key.objectid = ino; |
| 1761 | search_key.type = BTRFS_INODE_REF_KEY; |
| 1762 | search_key.offset = dirid; |
| 1763 | if (backref_in_log(log_root, &search_key, dirid, name, name_len)) |
| 1764 | return true; |
| 1765 | |
| 1766 | search_key.type = BTRFS_INODE_EXTREF_KEY; |
| 1767 | search_key.offset = btrfs_extref_hash(dirid, name, name_len); |
| 1768 | if (backref_in_log(log_root, &search_key, dirid, name, name_len)) |
| 1769 | return true; |
| 1770 | |
| 1771 | return false; |
| 1772 | } |
| 1773 | |
| 1774 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1775 | * take a single entry in a log directory item and replay it into |
| 1776 | * the subvolume. |
| 1777 | * |
| 1778 | * if a conflicting item exists in the subdirectory already, |
| 1779 | * the inode it points to is unlinked and put into the link count |
| 1780 | * fix up tree. |
| 1781 | * |
| 1782 | * If a name from the log points to a file or directory that does |
| 1783 | * not exist in the FS, it is skipped. fsyncs on directories |
| 1784 | * do not force down inodes inside that directory, just changes to the |
| 1785 | * names or unlinks in a directory. |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1786 | * |
| 1787 | * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a |
| 1788 | * non-existing inode) and 1 if the name was replayed. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1789 | */ |
| 1790 | static noinline int replay_one_name(struct btrfs_trans_handle *trans, |
| 1791 | struct btrfs_root *root, |
| 1792 | struct btrfs_path *path, |
| 1793 | struct extent_buffer *eb, |
| 1794 | struct btrfs_dir_item *di, |
| 1795 | struct btrfs_key *key) |
| 1796 | { |
| 1797 | char *name; |
| 1798 | int name_len; |
| 1799 | struct btrfs_dir_item *dst_di; |
| 1800 | struct btrfs_key found_key; |
| 1801 | struct btrfs_key log_key; |
| 1802 | struct inode *dir; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1803 | u8 log_type; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1804 | int exists; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1805 | int ret = 0; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1806 | bool update_size = (key->type == BTRFS_DIR_INDEX_KEY); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1807 | bool name_added = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1808 | |
| 1809 | dir = read_one_inode(root, key->objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1810 | if (!dir) |
| 1811 | return -EIO; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1812 | |
| 1813 | name_len = btrfs_dir_name_len(eb, di); |
| 1814 | name = kmalloc(name_len, GFP_NOFS); |
Filipe David Borba Manana | 2bac325 | 2013-08-04 19:58:57 +0100 | [diff] [blame] | 1815 | if (!name) { |
| 1816 | ret = -ENOMEM; |
| 1817 | goto out; |
| 1818 | } |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 1819 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1820 | log_type = btrfs_dir_type(eb, di); |
| 1821 | read_extent_buffer(eb, name, (unsigned long)(di + 1), |
| 1822 | name_len); |
| 1823 | |
| 1824 | btrfs_dir_item_key_to_cpu(eb, di, &log_key); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1825 | exists = btrfs_lookup_inode(trans, root, path, &log_key, 0); |
| 1826 | if (exists == 0) |
| 1827 | exists = 1; |
| 1828 | else |
| 1829 | exists = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1830 | btrfs_release_path(path); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1831 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1832 | if (key->type == BTRFS_DIR_ITEM_KEY) { |
| 1833 | dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, |
| 1834 | name, name_len, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1835 | } else if (key->type == BTRFS_DIR_INDEX_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1836 | dst_di = btrfs_lookup_dir_index_item(trans, root, path, |
| 1837 | key->objectid, |
| 1838 | key->offset, name, |
| 1839 | name_len, 1); |
| 1840 | } else { |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1841 | /* Corruption */ |
| 1842 | ret = -EINVAL; |
| 1843 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1844 | } |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 1845 | if (IS_ERR_OR_NULL(dst_di)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1846 | /* we need a sequence number to insert, so we only |
| 1847 | * do inserts for the BTRFS_DIR_INDEX_KEY types |
| 1848 | */ |
| 1849 | if (key->type != BTRFS_DIR_INDEX_KEY) |
| 1850 | goto out; |
| 1851 | goto insert; |
| 1852 | } |
| 1853 | |
| 1854 | btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); |
| 1855 | /* the existing item matches the logged item */ |
| 1856 | if (found_key.objectid == log_key.objectid && |
| 1857 | found_key.type == log_key.type && |
| 1858 | found_key.offset == log_key.offset && |
| 1859 | btrfs_dir_type(path->nodes[0], dst_di) == log_type) { |
Filipe Manana | a2cc11d | 2014-09-08 22:53:18 +0100 | [diff] [blame] | 1860 | update_size = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1861 | goto out; |
| 1862 | } |
| 1863 | |
| 1864 | /* |
| 1865 | * don't drop the conflicting directory entry if the inode |
| 1866 | * for the new entry doesn't exist |
| 1867 | */ |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1868 | if (!exists) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1869 | goto out; |
| 1870 | |
Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 1871 | 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] | 1872 | if (ret) |
| 1873 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1874 | |
| 1875 | if (key->type == BTRFS_DIR_INDEX_KEY) |
| 1876 | goto insert; |
| 1877 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1878 | btrfs_release_path(path); |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1879 | if (!ret && update_size) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 1880 | 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] | 1881 | ret = btrfs_update_inode(trans, root, dir); |
| 1882 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1883 | kfree(name); |
| 1884 | iput(dir); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1885 | if (!ret && name_added) |
| 1886 | ret = 1; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1887 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1888 | |
| 1889 | insert: |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1890 | if (name_in_log_ref(root->log_root, name, name_len, |
| 1891 | key->objectid, log_key.objectid)) { |
| 1892 | /* The dentry will be added later. */ |
| 1893 | ret = 0; |
| 1894 | update_size = false; |
| 1895 | goto out; |
| 1896 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1897 | btrfs_release_path(path); |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 1898 | ret = insert_one_name(trans, root, key->objectid, key->offset, |
| 1899 | name, name_len, &log_key); |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1900 | if (ret && ret != -ENOENT && ret != -EEXIST) |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1901 | goto out; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1902 | if (!ret) |
| 1903 | name_added = true; |
Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1904 | update_size = false; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1905 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1906 | goto out; |
| 1907 | } |
| 1908 | |
| 1909 | /* |
| 1910 | * find all the names in a directory item and reconcile them into |
| 1911 | * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than |
| 1912 | * one name in a directory item, but the same code gets used for |
| 1913 | * both directory index types |
| 1914 | */ |
| 1915 | static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, |
| 1916 | struct btrfs_root *root, |
| 1917 | struct btrfs_path *path, |
| 1918 | struct extent_buffer *eb, int slot, |
| 1919 | struct btrfs_key *key) |
| 1920 | { |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1921 | int ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1922 | u32 item_size = btrfs_item_size_nr(eb, slot); |
| 1923 | struct btrfs_dir_item *di; |
| 1924 | int name_len; |
| 1925 | unsigned long ptr; |
| 1926 | unsigned long ptr_end; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1927 | struct btrfs_path *fixup_path = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1928 | |
| 1929 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 1930 | ptr_end = ptr + item_size; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1931 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1932 | di = (struct btrfs_dir_item *)ptr; |
| 1933 | name_len = btrfs_dir_name_len(eb, di); |
| 1934 | ret = replay_one_name(trans, root, path, eb, di, key); |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1935 | if (ret < 0) |
| 1936 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1937 | ptr = (unsigned long)(di + 1); |
| 1938 | ptr += name_len; |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1939 | |
| 1940 | /* |
| 1941 | * If this entry refers to a non-directory (directories can not |
| 1942 | * have a link count > 1) and it was added in the transaction |
| 1943 | * that was not committed, make sure we fixup the link count of |
| 1944 | * the inode it the entry points to. Otherwise something like |
| 1945 | * the following would result in a directory pointing to an |
| 1946 | * inode with a wrong link that does not account for this dir |
| 1947 | * entry: |
| 1948 | * |
| 1949 | * mkdir testdir |
| 1950 | * touch testdir/foo |
| 1951 | * touch testdir/bar |
| 1952 | * sync |
| 1953 | * |
| 1954 | * ln testdir/bar testdir/bar_link |
| 1955 | * ln testdir/foo testdir/foo_link |
| 1956 | * xfs_io -c "fsync" testdir/bar |
| 1957 | * |
| 1958 | * <power failure> |
| 1959 | * |
| 1960 | * mount fs, log replay happens |
| 1961 | * |
| 1962 | * File foo would remain with a link count of 1 when it has two |
| 1963 | * entries pointing to it in the directory testdir. This would |
| 1964 | * make it impossible to ever delete the parent directory has |
| 1965 | * it would result in stale dentries that can never be deleted. |
| 1966 | */ |
| 1967 | if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) { |
| 1968 | struct btrfs_key di_key; |
| 1969 | |
| 1970 | if (!fixup_path) { |
| 1971 | fixup_path = btrfs_alloc_path(); |
| 1972 | if (!fixup_path) { |
| 1973 | ret = -ENOMEM; |
| 1974 | break; |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 1979 | ret = link_to_fixup_dir(trans, root, fixup_path, |
| 1980 | di_key.objectid); |
| 1981 | if (ret) |
| 1982 | break; |
| 1983 | } |
| 1984 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1985 | } |
Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1986 | btrfs_free_path(fixup_path); |
| 1987 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | /* |
| 1991 | * directory replay has two parts. There are the standard directory |
| 1992 | * items in the log copied from the subvolume, and range items |
| 1993 | * created in the log while the subvolume was logged. |
| 1994 | * |
| 1995 | * The range items tell us which parts of the key space the log |
| 1996 | * is authoritative for. During replay, if a key in the subvolume |
| 1997 | * directory is in a logged range item, but not actually in the log |
| 1998 | * that means it was deleted from the directory before the fsync |
| 1999 | * and should be removed. |
| 2000 | */ |
| 2001 | static noinline int find_dir_range(struct btrfs_root *root, |
| 2002 | struct btrfs_path *path, |
| 2003 | u64 dirid, int key_type, |
| 2004 | u64 *start_ret, u64 *end_ret) |
| 2005 | { |
| 2006 | struct btrfs_key key; |
| 2007 | u64 found_end; |
| 2008 | struct btrfs_dir_log_item *item; |
| 2009 | int ret; |
| 2010 | int nritems; |
| 2011 | |
| 2012 | if (*start_ret == (u64)-1) |
| 2013 | return 1; |
| 2014 | |
| 2015 | key.objectid = dirid; |
| 2016 | key.type = key_type; |
| 2017 | key.offset = *start_ret; |
| 2018 | |
| 2019 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2020 | if (ret < 0) |
| 2021 | goto out; |
| 2022 | if (ret > 0) { |
| 2023 | if (path->slots[0] == 0) |
| 2024 | goto out; |
| 2025 | path->slots[0]--; |
| 2026 | } |
| 2027 | if (ret != 0) |
| 2028 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 2029 | |
| 2030 | if (key.type != key_type || key.objectid != dirid) { |
| 2031 | ret = 1; |
| 2032 | goto next; |
| 2033 | } |
| 2034 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2035 | struct btrfs_dir_log_item); |
| 2036 | found_end = btrfs_dir_log_end(path->nodes[0], item); |
| 2037 | |
| 2038 | if (*start_ret >= key.offset && *start_ret <= found_end) { |
| 2039 | ret = 0; |
| 2040 | *start_ret = key.offset; |
| 2041 | *end_ret = found_end; |
| 2042 | goto out; |
| 2043 | } |
| 2044 | ret = 1; |
| 2045 | next: |
| 2046 | /* check the next slot in the tree to see if it is a valid item */ |
| 2047 | nritems = btrfs_header_nritems(path->nodes[0]); |
Robbie Ko | 2a7bf53 | 2016-10-07 17:30:47 +0800 | [diff] [blame] | 2048 | path->slots[0]++; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2049 | if (path->slots[0] >= nritems) { |
| 2050 | ret = btrfs_next_leaf(root, path); |
| 2051 | if (ret) |
| 2052 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 2056 | |
| 2057 | if (key.type != key_type || key.objectid != dirid) { |
| 2058 | ret = 1; |
| 2059 | goto out; |
| 2060 | } |
| 2061 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2062 | struct btrfs_dir_log_item); |
| 2063 | found_end = btrfs_dir_log_end(path->nodes[0], item); |
| 2064 | *start_ret = key.offset; |
| 2065 | *end_ret = found_end; |
| 2066 | ret = 0; |
| 2067 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2068 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2069 | return ret; |
| 2070 | } |
| 2071 | |
| 2072 | /* |
| 2073 | * this looks for a given directory item in the log. If the directory |
| 2074 | * item is not in the log, the item is removed and the inode it points |
| 2075 | * to is unlinked |
| 2076 | */ |
| 2077 | static noinline int check_item_in_log(struct btrfs_trans_handle *trans, |
| 2078 | struct btrfs_root *root, |
| 2079 | struct btrfs_root *log, |
| 2080 | struct btrfs_path *path, |
| 2081 | struct btrfs_path *log_path, |
| 2082 | struct inode *dir, |
| 2083 | struct btrfs_key *dir_key) |
| 2084 | { |
| 2085 | int ret; |
| 2086 | struct extent_buffer *eb; |
| 2087 | int slot; |
| 2088 | u32 item_size; |
| 2089 | struct btrfs_dir_item *di; |
| 2090 | struct btrfs_dir_item *log_di; |
| 2091 | int name_len; |
| 2092 | unsigned long ptr; |
| 2093 | unsigned long ptr_end; |
| 2094 | char *name; |
| 2095 | struct inode *inode; |
| 2096 | struct btrfs_key location; |
| 2097 | |
| 2098 | again: |
| 2099 | eb = path->nodes[0]; |
| 2100 | slot = path->slots[0]; |
| 2101 | item_size = btrfs_item_size_nr(eb, slot); |
| 2102 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 2103 | ptr_end = ptr + item_size; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2104 | while (ptr < ptr_end) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2105 | di = (struct btrfs_dir_item *)ptr; |
| 2106 | name_len = btrfs_dir_name_len(eb, di); |
| 2107 | name = kmalloc(name_len, GFP_NOFS); |
| 2108 | if (!name) { |
| 2109 | ret = -ENOMEM; |
| 2110 | goto out; |
| 2111 | } |
| 2112 | read_extent_buffer(eb, name, (unsigned long)(di + 1), |
| 2113 | name_len); |
| 2114 | log_di = NULL; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2115 | if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2116 | log_di = btrfs_lookup_dir_item(trans, log, log_path, |
| 2117 | dir_key->objectid, |
| 2118 | name, name_len, 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2119 | } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2120 | log_di = btrfs_lookup_dir_index_item(trans, log, |
| 2121 | log_path, |
| 2122 | dir_key->objectid, |
| 2123 | dir_key->offset, |
| 2124 | name, name_len, 0); |
| 2125 | } |
Filipe David Borba Manana | 269d040 | 2013-10-28 17:39:21 +0000 | [diff] [blame] | 2126 | if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2127 | btrfs_dir_item_key_to_cpu(eb, di, &location); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2128 | btrfs_release_path(path); |
| 2129 | btrfs_release_path(log_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2130 | inode = read_one_inode(root, location.objectid); |
Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 2131 | if (!inode) { |
| 2132 | kfree(name); |
| 2133 | return -EIO; |
| 2134 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2135 | |
| 2136 | ret = link_to_fixup_dir(trans, root, |
| 2137 | path, location.objectid); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2138 | if (ret) { |
| 2139 | kfree(name); |
| 2140 | iput(inode); |
| 2141 | goto out; |
| 2142 | } |
| 2143 | |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 2144 | inc_nlink(inode); |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 2145 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 2146 | BTRFS_I(inode), name, name_len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2147 | if (!ret) |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 2148 | ret = btrfs_run_delayed_items(trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2149 | kfree(name); |
| 2150 | iput(inode); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2151 | if (ret) |
| 2152 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2153 | |
| 2154 | /* there might still be more names under this key |
| 2155 | * check and repeat if required |
| 2156 | */ |
| 2157 | ret = btrfs_search_slot(NULL, root, dir_key, path, |
| 2158 | 0, 0); |
| 2159 | if (ret == 0) |
| 2160 | goto again; |
| 2161 | ret = 0; |
| 2162 | goto out; |
Filipe David Borba Manana | 269d040 | 2013-10-28 17:39:21 +0000 | [diff] [blame] | 2163 | } else if (IS_ERR(log_di)) { |
| 2164 | kfree(name); |
| 2165 | return PTR_ERR(log_di); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2166 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2167 | btrfs_release_path(log_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2168 | kfree(name); |
| 2169 | |
| 2170 | ptr = (unsigned long)(di + 1); |
| 2171 | ptr += name_len; |
| 2172 | } |
| 2173 | ret = 0; |
| 2174 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2175 | btrfs_release_path(path); |
| 2176 | btrfs_release_path(log_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2177 | return ret; |
| 2178 | } |
| 2179 | |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2180 | static int replay_xattr_deletes(struct btrfs_trans_handle *trans, |
| 2181 | struct btrfs_root *root, |
| 2182 | struct btrfs_root *log, |
| 2183 | struct btrfs_path *path, |
| 2184 | const u64 ino) |
| 2185 | { |
| 2186 | struct btrfs_key search_key; |
| 2187 | struct btrfs_path *log_path; |
| 2188 | int i; |
| 2189 | int nritems; |
| 2190 | int ret; |
| 2191 | |
| 2192 | log_path = btrfs_alloc_path(); |
| 2193 | if (!log_path) |
| 2194 | return -ENOMEM; |
| 2195 | |
| 2196 | search_key.objectid = ino; |
| 2197 | search_key.type = BTRFS_XATTR_ITEM_KEY; |
| 2198 | search_key.offset = 0; |
| 2199 | again: |
| 2200 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
| 2201 | if (ret < 0) |
| 2202 | goto out; |
| 2203 | process_leaf: |
| 2204 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2205 | for (i = path->slots[0]; i < nritems; i++) { |
| 2206 | struct btrfs_key key; |
| 2207 | struct btrfs_dir_item *di; |
| 2208 | struct btrfs_dir_item *log_di; |
| 2209 | u32 total_size; |
| 2210 | u32 cur; |
| 2211 | |
| 2212 | btrfs_item_key_to_cpu(path->nodes[0], &key, i); |
| 2213 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) { |
| 2214 | ret = 0; |
| 2215 | goto out; |
| 2216 | } |
| 2217 | |
| 2218 | di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item); |
| 2219 | total_size = btrfs_item_size_nr(path->nodes[0], i); |
| 2220 | cur = 0; |
| 2221 | while (cur < total_size) { |
| 2222 | u16 name_len = btrfs_dir_name_len(path->nodes[0], di); |
| 2223 | u16 data_len = btrfs_dir_data_len(path->nodes[0], di); |
| 2224 | u32 this_len = sizeof(*di) + name_len + data_len; |
| 2225 | char *name; |
| 2226 | |
| 2227 | name = kmalloc(name_len, GFP_NOFS); |
| 2228 | if (!name) { |
| 2229 | ret = -ENOMEM; |
| 2230 | goto out; |
| 2231 | } |
| 2232 | read_extent_buffer(path->nodes[0], name, |
| 2233 | (unsigned long)(di + 1), name_len); |
| 2234 | |
| 2235 | log_di = btrfs_lookup_xattr(NULL, log, log_path, ino, |
| 2236 | name, name_len, 0); |
| 2237 | btrfs_release_path(log_path); |
| 2238 | if (!log_di) { |
| 2239 | /* Doesn't exist in log tree, so delete it. */ |
| 2240 | btrfs_release_path(path); |
| 2241 | di = btrfs_lookup_xattr(trans, root, path, ino, |
| 2242 | name, name_len, -1); |
| 2243 | kfree(name); |
| 2244 | if (IS_ERR(di)) { |
| 2245 | ret = PTR_ERR(di); |
| 2246 | goto out; |
| 2247 | } |
| 2248 | ASSERT(di); |
| 2249 | ret = btrfs_delete_one_dir_name(trans, root, |
| 2250 | path, di); |
| 2251 | if (ret) |
| 2252 | goto out; |
| 2253 | btrfs_release_path(path); |
| 2254 | search_key = key; |
| 2255 | goto again; |
| 2256 | } |
| 2257 | kfree(name); |
| 2258 | if (IS_ERR(log_di)) { |
| 2259 | ret = PTR_ERR(log_di); |
| 2260 | goto out; |
| 2261 | } |
| 2262 | cur += this_len; |
| 2263 | di = (struct btrfs_dir_item *)((char *)di + this_len); |
| 2264 | } |
| 2265 | } |
| 2266 | ret = btrfs_next_leaf(root, path); |
| 2267 | if (ret > 0) |
| 2268 | ret = 0; |
| 2269 | else if (ret == 0) |
| 2270 | goto process_leaf; |
| 2271 | out: |
| 2272 | btrfs_free_path(log_path); |
| 2273 | btrfs_release_path(path); |
| 2274 | return ret; |
| 2275 | } |
| 2276 | |
| 2277 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2278 | /* |
| 2279 | * deletion replay happens before we copy any new directory items |
| 2280 | * out of the log or out of backreferences from inodes. It |
| 2281 | * scans the log to find ranges of keys that log is authoritative for, |
| 2282 | * and then scans the directory to find items in those ranges that are |
| 2283 | * not present in the log. |
| 2284 | * |
| 2285 | * Anything we don't find in the log is unlinked and removed from the |
| 2286 | * directory. |
| 2287 | */ |
| 2288 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, |
| 2289 | struct btrfs_root *root, |
| 2290 | struct btrfs_root *log, |
| 2291 | struct btrfs_path *path, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2292 | u64 dirid, int del_all) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2293 | { |
| 2294 | u64 range_start; |
| 2295 | u64 range_end; |
| 2296 | int key_type = BTRFS_DIR_LOG_ITEM_KEY; |
| 2297 | int ret = 0; |
| 2298 | struct btrfs_key dir_key; |
| 2299 | struct btrfs_key found_key; |
| 2300 | struct btrfs_path *log_path; |
| 2301 | struct inode *dir; |
| 2302 | |
| 2303 | dir_key.objectid = dirid; |
| 2304 | dir_key.type = BTRFS_DIR_ITEM_KEY; |
| 2305 | log_path = btrfs_alloc_path(); |
| 2306 | if (!log_path) |
| 2307 | return -ENOMEM; |
| 2308 | |
| 2309 | dir = read_one_inode(root, dirid); |
| 2310 | /* it isn't an error if the inode isn't there, that can happen |
| 2311 | * because we replay the deletes before we copy in the inode item |
| 2312 | * from the log |
| 2313 | */ |
| 2314 | if (!dir) { |
| 2315 | btrfs_free_path(log_path); |
| 2316 | return 0; |
| 2317 | } |
| 2318 | again: |
| 2319 | range_start = 0; |
| 2320 | range_end = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2321 | while (1) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2322 | if (del_all) |
| 2323 | range_end = (u64)-1; |
| 2324 | else { |
| 2325 | ret = find_dir_range(log, path, dirid, key_type, |
| 2326 | &range_start, &range_end); |
| 2327 | if (ret != 0) |
| 2328 | break; |
| 2329 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2330 | |
| 2331 | dir_key.offset = range_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2332 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2333 | int nritems; |
| 2334 | ret = btrfs_search_slot(NULL, root, &dir_key, path, |
| 2335 | 0, 0); |
| 2336 | if (ret < 0) |
| 2337 | goto out; |
| 2338 | |
| 2339 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2340 | if (path->slots[0] >= nritems) { |
| 2341 | ret = btrfs_next_leaf(root, path); |
Liu Bo | b98def7 | 2018-04-03 01:59:48 +0800 | [diff] [blame] | 2342 | if (ret == 1) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2343 | break; |
Liu Bo | b98def7 | 2018-04-03 01:59:48 +0800 | [diff] [blame] | 2344 | else if (ret < 0) |
| 2345 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2346 | } |
| 2347 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2348 | path->slots[0]); |
| 2349 | if (found_key.objectid != dirid || |
| 2350 | found_key.type != dir_key.type) |
| 2351 | goto next_type; |
| 2352 | |
| 2353 | if (found_key.offset > range_end) |
| 2354 | break; |
| 2355 | |
| 2356 | ret = check_item_in_log(trans, root, log, path, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2357 | log_path, dir, |
| 2358 | &found_key); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2359 | if (ret) |
| 2360 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2361 | if (found_key.offset == (u64)-1) |
| 2362 | break; |
| 2363 | dir_key.offset = found_key.offset + 1; |
| 2364 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2365 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2366 | if (range_end == (u64)-1) |
| 2367 | break; |
| 2368 | range_start = range_end + 1; |
| 2369 | } |
| 2370 | |
| 2371 | next_type: |
| 2372 | ret = 0; |
| 2373 | if (key_type == BTRFS_DIR_LOG_ITEM_KEY) { |
| 2374 | key_type = BTRFS_DIR_LOG_INDEX_KEY; |
| 2375 | dir_key.type = BTRFS_DIR_INDEX_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2376 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2377 | goto again; |
| 2378 | } |
| 2379 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2380 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2381 | btrfs_free_path(log_path); |
| 2382 | iput(dir); |
| 2383 | return ret; |
| 2384 | } |
| 2385 | |
| 2386 | /* |
| 2387 | * the process_func used to replay items from the log tree. This |
| 2388 | * gets called in two different stages. The first stage just looks |
| 2389 | * for inodes and makes sure they are all copied into the subvolume. |
| 2390 | * |
| 2391 | * The second stage copies all the other item types from the log into |
| 2392 | * the subvolume. The two stage approach is slower, but gets rid of |
| 2393 | * lots of complexity around inodes referencing other inodes that exist |
| 2394 | * only in the log (references come from either directory items or inode |
| 2395 | * back refs). |
| 2396 | */ |
| 2397 | 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] | 2398 | struct walk_control *wc, u64 gen, int level) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2399 | { |
| 2400 | int nritems; |
| 2401 | struct btrfs_path *path; |
| 2402 | struct btrfs_root *root = wc->replay_dest; |
| 2403 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2404 | int i; |
| 2405 | int ret; |
| 2406 | |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2407 | ret = btrfs_read_buffer(eb, gen, level, NULL); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2408 | if (ret) |
| 2409 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2410 | |
| 2411 | level = btrfs_header_level(eb); |
| 2412 | |
| 2413 | if (level != 0) |
| 2414 | return 0; |
| 2415 | |
| 2416 | path = btrfs_alloc_path(); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2417 | if (!path) |
| 2418 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2419 | |
| 2420 | nritems = btrfs_header_nritems(eb); |
| 2421 | for (i = 0; i < nritems; i++) { |
| 2422 | btrfs_item_key_to_cpu(eb, &key, i); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2423 | |
| 2424 | /* inode keys are done during the first stage */ |
| 2425 | if (key.type == BTRFS_INODE_ITEM_KEY && |
| 2426 | wc->stage == LOG_WALK_REPLAY_INODES) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2427 | struct btrfs_inode_item *inode_item; |
| 2428 | u32 mode; |
| 2429 | |
| 2430 | inode_item = btrfs_item_ptr(eb, i, |
| 2431 | struct btrfs_inode_item); |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2432 | ret = replay_xattr_deletes(wc->trans, root, log, |
| 2433 | path, key.objectid); |
| 2434 | if (ret) |
| 2435 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2436 | mode = btrfs_inode_mode(eb, inode_item); |
| 2437 | if (S_ISDIR(mode)) { |
| 2438 | ret = replay_dir_deletes(wc->trans, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2439 | root, log, path, key.objectid, 0); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2440 | if (ret) |
| 2441 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2442 | } |
| 2443 | ret = overwrite_item(wc->trans, root, path, |
| 2444 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2445 | if (ret) |
| 2446 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2447 | |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2448 | /* |
| 2449 | * Before replaying extents, truncate the inode to its |
| 2450 | * size. We need to do it now and not after log replay |
| 2451 | * because before an fsync we can have prealloc extents |
| 2452 | * added beyond the inode's i_size. If we did it after, |
| 2453 | * through orphan cleanup for example, we would drop |
| 2454 | * those prealloc extents just after replaying them. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2455 | */ |
| 2456 | if (S_ISREG(mode)) { |
Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2457 | struct inode *inode; |
| 2458 | u64 from; |
| 2459 | |
| 2460 | inode = read_one_inode(root, key.objectid); |
| 2461 | if (!inode) { |
| 2462 | ret = -EIO; |
| 2463 | break; |
| 2464 | } |
| 2465 | from = ALIGN(i_size_read(inode), |
| 2466 | root->fs_info->sectorsize); |
| 2467 | ret = btrfs_drop_extents(wc->trans, root, inode, |
| 2468 | from, (u64)-1, 1); |
| 2469 | /* |
| 2470 | * If the nlink count is zero here, the iput |
| 2471 | * will free the inode. We bump it to make |
| 2472 | * sure it doesn't get freed until the link |
| 2473 | * count fixup is done. |
| 2474 | */ |
| 2475 | if (!ret) { |
| 2476 | if (inode->i_nlink == 0) |
| 2477 | inc_nlink(inode); |
| 2478 | /* Update link count and nbytes. */ |
| 2479 | ret = btrfs_update_inode(wc->trans, |
| 2480 | root, inode); |
| 2481 | } |
| 2482 | iput(inode); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2483 | if (ret) |
| 2484 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2485 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2486 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2487 | ret = link_to_fixup_dir(wc->trans, root, |
| 2488 | path, key.objectid); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2489 | if (ret) |
| 2490 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2491 | } |
Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2492 | |
| 2493 | if (key.type == BTRFS_DIR_INDEX_KEY && |
| 2494 | wc->stage == LOG_WALK_REPLAY_DIR_INDEX) { |
| 2495 | ret = replay_one_dir_item(wc->trans, root, path, |
| 2496 | eb, i, &key); |
| 2497 | if (ret) |
| 2498 | break; |
| 2499 | } |
| 2500 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2501 | if (wc->stage < LOG_WALK_REPLAY_ALL) |
| 2502 | continue; |
| 2503 | |
| 2504 | /* these keys are simply copied */ |
| 2505 | if (key.type == BTRFS_XATTR_ITEM_KEY) { |
| 2506 | ret = overwrite_item(wc->trans, root, path, |
| 2507 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2508 | if (ret) |
| 2509 | break; |
Liu Bo | 2da1c66 | 2013-05-26 13:50:29 +0000 | [diff] [blame] | 2510 | } else if (key.type == BTRFS_INODE_REF_KEY || |
| 2511 | key.type == BTRFS_INODE_EXTREF_KEY) { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 2512 | ret = add_inode_ref(wc->trans, root, log, path, |
| 2513 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2514 | if (ret && ret != -ENOENT) |
| 2515 | break; |
| 2516 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2517 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 2518 | ret = replay_one_extent(wc->trans, root, path, |
| 2519 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2520 | if (ret) |
| 2521 | break; |
Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2522 | } else if (key.type == BTRFS_DIR_ITEM_KEY) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2523 | ret = replay_one_dir_item(wc->trans, root, path, |
| 2524 | eb, i, &key); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2525 | if (ret) |
| 2526 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2527 | } |
| 2528 | } |
| 2529 | btrfs_free_path(path); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2530 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2531 | } |
| 2532 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2533 | static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2534 | struct btrfs_root *root, |
| 2535 | struct btrfs_path *path, int *level, |
| 2536 | struct walk_control *wc) |
| 2537 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2538 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2539 | u64 root_owner; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2540 | u64 bytenr; |
| 2541 | u64 ptr_gen; |
| 2542 | struct extent_buffer *next; |
| 2543 | struct extent_buffer *cur; |
| 2544 | struct extent_buffer *parent; |
| 2545 | u32 blocksize; |
| 2546 | int ret = 0; |
| 2547 | |
| 2548 | WARN_ON(*level < 0); |
| 2549 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 2550 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2551 | while (*level > 0) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2552 | struct btrfs_key first_key; |
| 2553 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2554 | WARN_ON(*level < 0); |
| 2555 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 2556 | cur = path->nodes[*level]; |
| 2557 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2558 | WARN_ON(btrfs_header_level(cur) != *level); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2559 | |
| 2560 | if (path->slots[*level] >= |
| 2561 | btrfs_header_nritems(cur)) |
| 2562 | break; |
| 2563 | |
| 2564 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 2565 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2566 | btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2567 | blocksize = fs_info->nodesize; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2568 | |
| 2569 | parent = path->nodes[*level]; |
| 2570 | root_owner = btrfs_header_owner(parent); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2571 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2572 | next = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 2573 | if (IS_ERR(next)) |
| 2574 | return PTR_ERR(next); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2575 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2576 | if (*level == 1) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2577 | ret = wc->process_func(root, next, wc, ptr_gen, |
| 2578 | *level - 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2579 | if (ret) { |
| 2580 | free_extent_buffer(next); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2581 | return ret; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2582 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2583 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2584 | path->slots[*level]++; |
| 2585 | if (wc->free) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2586 | ret = btrfs_read_buffer(next, ptr_gen, |
| 2587 | *level - 1, &first_key); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2588 | if (ret) { |
| 2589 | free_extent_buffer(next); |
| 2590 | return ret; |
| 2591 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2592 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2593 | if (trans) { |
| 2594 | btrfs_tree_lock(next); |
| 2595 | btrfs_set_lock_blocking(next); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2596 | clean_tree_block(fs_info, next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2597 | btrfs_wait_tree_block_writeback(next); |
| 2598 | btrfs_tree_unlock(next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2599 | } else { |
| 2600 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) |
| 2601 | clear_extent_buffer_dirty(next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2602 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2603 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2604 | WARN_ON(root_owner != |
| 2605 | BTRFS_TREE_LOG_OBJECTID); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2606 | ret = btrfs_free_and_pin_reserved_extent( |
| 2607 | fs_info, bytenr, |
| 2608 | blocksize); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2609 | if (ret) { |
| 2610 | free_extent_buffer(next); |
| 2611 | return ret; |
| 2612 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2613 | } |
| 2614 | free_extent_buffer(next); |
| 2615 | continue; |
| 2616 | } |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2617 | ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2618 | if (ret) { |
| 2619 | free_extent_buffer(next); |
| 2620 | return ret; |
| 2621 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2622 | |
| 2623 | WARN_ON(*level <= 0); |
| 2624 | if (path->nodes[*level-1]) |
| 2625 | free_extent_buffer(path->nodes[*level-1]); |
| 2626 | path->nodes[*level-1] = next; |
| 2627 | *level = btrfs_header_level(next); |
| 2628 | path->slots[*level] = 0; |
| 2629 | cond_resched(); |
| 2630 | } |
| 2631 | WARN_ON(*level < 0); |
| 2632 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 2633 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2634 | path->slots[*level] = btrfs_header_nritems(path->nodes[*level]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2635 | |
| 2636 | cond_resched(); |
| 2637 | return 0; |
| 2638 | } |
| 2639 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2640 | static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2641 | struct btrfs_root *root, |
| 2642 | struct btrfs_path *path, int *level, |
| 2643 | struct walk_control *wc) |
| 2644 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2645 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2646 | u64 root_owner; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2647 | int i; |
| 2648 | int slot; |
| 2649 | int ret; |
| 2650 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2651 | for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2652 | slot = path->slots[i]; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2653 | if (slot + 1 < btrfs_header_nritems(path->nodes[i])) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2654 | path->slots[i]++; |
| 2655 | *level = i; |
| 2656 | WARN_ON(*level == 0); |
| 2657 | return 0; |
| 2658 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2659 | struct extent_buffer *parent; |
| 2660 | if (path->nodes[*level] == root->node) |
| 2661 | parent = path->nodes[*level]; |
| 2662 | else |
| 2663 | parent = path->nodes[*level + 1]; |
| 2664 | |
| 2665 | root_owner = btrfs_header_owner(parent); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2666 | ret = wc->process_func(root, path->nodes[*level], wc, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2667 | btrfs_header_generation(path->nodes[*level]), |
| 2668 | *level); |
Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2669 | if (ret) |
| 2670 | return ret; |
| 2671 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2672 | if (wc->free) { |
| 2673 | struct extent_buffer *next; |
| 2674 | |
| 2675 | next = path->nodes[*level]; |
| 2676 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2677 | if (trans) { |
| 2678 | btrfs_tree_lock(next); |
| 2679 | btrfs_set_lock_blocking(next); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2680 | clean_tree_block(fs_info, next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2681 | btrfs_wait_tree_block_writeback(next); |
| 2682 | btrfs_tree_unlock(next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2683 | } else { |
| 2684 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) |
| 2685 | clear_extent_buffer_dirty(next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2686 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2687 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2688 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2689 | ret = btrfs_free_and_pin_reserved_extent( |
| 2690 | fs_info, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2691 | path->nodes[*level]->start, |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2692 | path->nodes[*level]->len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2693 | if (ret) |
| 2694 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2695 | } |
| 2696 | free_extent_buffer(path->nodes[*level]); |
| 2697 | path->nodes[*level] = NULL; |
| 2698 | *level = i + 1; |
| 2699 | } |
| 2700 | } |
| 2701 | return 1; |
| 2702 | } |
| 2703 | |
| 2704 | /* |
| 2705 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 2706 | * the tree freeing any blocks that have a ref count of zero after being |
| 2707 | * decremented. |
| 2708 | */ |
| 2709 | static int walk_log_tree(struct btrfs_trans_handle *trans, |
| 2710 | struct btrfs_root *log, struct walk_control *wc) |
| 2711 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2712 | struct btrfs_fs_info *fs_info = log->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2713 | int ret = 0; |
| 2714 | int wret; |
| 2715 | int level; |
| 2716 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2717 | int orig_level; |
| 2718 | |
| 2719 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 2720 | if (!path) |
| 2721 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2722 | |
| 2723 | level = btrfs_header_level(log->node); |
| 2724 | orig_level = level; |
| 2725 | path->nodes[level] = log->node; |
| 2726 | extent_buffer_get(log->node); |
| 2727 | path->slots[level] = 0; |
| 2728 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2729 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2730 | wret = walk_down_log_tree(trans, log, path, &level, wc); |
| 2731 | if (wret > 0) |
| 2732 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2733 | if (wret < 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2734 | ret = wret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2735 | goto out; |
| 2736 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2737 | |
| 2738 | wret = walk_up_log_tree(trans, log, path, &level, wc); |
| 2739 | if (wret > 0) |
| 2740 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2741 | if (wret < 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2742 | ret = wret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2743 | goto out; |
| 2744 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | /* was the root node processed? if not, catch it here */ |
| 2748 | if (path->nodes[orig_level]) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2749 | ret = wc->process_func(log, path->nodes[orig_level], wc, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2750 | btrfs_header_generation(path->nodes[orig_level]), |
| 2751 | orig_level); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2752 | if (ret) |
| 2753 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2754 | if (wc->free) { |
| 2755 | struct extent_buffer *next; |
| 2756 | |
| 2757 | next = path->nodes[orig_level]; |
| 2758 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2759 | if (trans) { |
| 2760 | btrfs_tree_lock(next); |
| 2761 | btrfs_set_lock_blocking(next); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2762 | clean_tree_block(fs_info, next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2763 | btrfs_wait_tree_block_writeback(next); |
| 2764 | btrfs_tree_unlock(next); |
Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2765 | } else { |
| 2766 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) |
| 2767 | clear_extent_buffer_dirty(next); |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2768 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2769 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2770 | WARN_ON(log->root_key.objectid != |
| 2771 | BTRFS_TREE_LOG_OBJECTID); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2772 | ret = btrfs_free_and_pin_reserved_extent(fs_info, |
| 2773 | next->start, next->len); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2774 | if (ret) |
| 2775 | goto out; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2779 | out: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2780 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2781 | return ret; |
| 2782 | } |
| 2783 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2784 | /* |
| 2785 | * helper function to update the item for a given subvolumes log root |
| 2786 | * in the tree of log roots |
| 2787 | */ |
| 2788 | static int update_log_root(struct btrfs_trans_handle *trans, |
| 2789 | struct btrfs_root *log) |
| 2790 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2791 | struct btrfs_fs_info *fs_info = log->fs_info; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2792 | int ret; |
| 2793 | |
| 2794 | if (log->log_transid == 1) { |
| 2795 | /* insert root item on the first sync */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2796 | ret = btrfs_insert_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 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2799 | ret = btrfs_update_root(trans, fs_info->log_root_tree, |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2800 | &log->root_key, &log->root_item); |
| 2801 | } |
| 2802 | return ret; |
| 2803 | } |
| 2804 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2805 | static void wait_log_commit(struct btrfs_root *root, int transid) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2806 | { |
| 2807 | DEFINE_WAIT(wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2808 | int index = transid % 2; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2809 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2810 | /* |
| 2811 | * we only allow two pending log transactions at a time, |
| 2812 | * so we know that if ours is more than 2 older than the |
| 2813 | * current transaction, we're done |
| 2814 | */ |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2815 | for (;;) { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2816 | prepare_to_wait(&root->log_commit_wait[index], |
| 2817 | &wait, TASK_UNINTERRUPTIBLE); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2818 | |
| 2819 | if (!(root->log_transid_committed < transid && |
| 2820 | atomic_read(&root->log_commit[index]))) |
| 2821 | break; |
| 2822 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2823 | mutex_unlock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2824 | schedule(); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2825 | mutex_lock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2826 | } |
| 2827 | finish_wait(&root->log_commit_wait[index], &wait); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2828 | } |
| 2829 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2830 | static void wait_for_writer(struct btrfs_root *root) |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2831 | { |
| 2832 | DEFINE_WAIT(wait); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2833 | |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2834 | for (;;) { |
| 2835 | prepare_to_wait(&root->log_writer_wait, &wait, |
| 2836 | TASK_UNINTERRUPTIBLE); |
| 2837 | if (!atomic_read(&root->log_writers)) |
| 2838 | break; |
| 2839 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2840 | mutex_unlock(&root->log_mutex); |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2841 | schedule(); |
Filipe Manana | 575849e | 2015-02-11 11:12:39 +0000 | [diff] [blame] | 2842 | mutex_lock(&root->log_mutex); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2843 | } |
Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2844 | finish_wait(&root->log_writer_wait, &wait); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2845 | } |
| 2846 | |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2847 | static inline void btrfs_remove_log_ctx(struct btrfs_root *root, |
| 2848 | struct btrfs_log_ctx *ctx) |
| 2849 | { |
| 2850 | if (!ctx) |
| 2851 | return; |
| 2852 | |
| 2853 | mutex_lock(&root->log_mutex); |
| 2854 | list_del_init(&ctx->list); |
| 2855 | mutex_unlock(&root->log_mutex); |
| 2856 | } |
| 2857 | |
| 2858 | /* |
| 2859 | * Invoked in log mutex context, or be sure there is no other task which |
| 2860 | * can access the list. |
| 2861 | */ |
| 2862 | static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root, |
| 2863 | int index, int error) |
| 2864 | { |
| 2865 | struct btrfs_log_ctx *ctx; |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2866 | struct btrfs_log_ctx *safe; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2867 | |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2868 | list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) { |
| 2869 | list_del_init(&ctx->list); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2870 | ctx->log_ret = error; |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2871 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2872 | |
| 2873 | INIT_LIST_HEAD(&root->log_ctxs[index]); |
| 2874 | } |
| 2875 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2876 | /* |
| 2877 | * btrfs_sync_log does sends a given tree log down to the disk and |
| 2878 | * updates the super blocks to record it. When this call is done, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2879 | * you know that any inodes previously logged are safely on disk only |
| 2880 | * if it returns 0. |
| 2881 | * |
| 2882 | * Any other return value means you need to call btrfs_commit_transaction. |
| 2883 | * Some of the edge cases for fsyncing directories that have had unlinks |
| 2884 | * or renames done in the past mean that sometimes the only safe |
| 2885 | * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN, |
| 2886 | * that has happened. |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2887 | */ |
| 2888 | int btrfs_sync_log(struct btrfs_trans_handle *trans, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2889 | struct btrfs_root *root, struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2890 | { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2891 | int index1; |
| 2892 | int index2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2893 | int mark; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2894 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2895 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2896 | struct btrfs_root *log = root->log_root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2897 | struct btrfs_root *log_root_tree = fs_info->log_root_tree; |
Miao Xie | bb14a59 | 2014-02-20 18:08:56 +0800 | [diff] [blame] | 2898 | int log_transid = 0; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2899 | struct btrfs_log_ctx root_log_ctx; |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2900 | struct blk_plug plug; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2901 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2902 | mutex_lock(&root->log_mutex); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2903 | log_transid = ctx->log_transid; |
| 2904 | if (root->log_transid_committed >= log_transid) { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2905 | mutex_unlock(&root->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2906 | return ctx->log_ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2907 | } |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2908 | |
| 2909 | index1 = log_transid % 2; |
| 2910 | if (atomic_read(&root->log_commit[index1])) { |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2911 | wait_log_commit(root, log_transid); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2912 | mutex_unlock(&root->log_mutex); |
| 2913 | return ctx->log_ret; |
| 2914 | } |
| 2915 | ASSERT(log_transid == root->log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2916 | atomic_set(&root->log_commit[index1], 1); |
| 2917 | |
| 2918 | /* wait for previous tree log sync to complete */ |
| 2919 | if (atomic_read(&root->log_commit[(index1 + 1) % 2])) |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2920 | wait_log_commit(root, log_transid - 1); |
Miao Xie | 48cab2e | 2014-02-20 18:08:52 +0800 | [diff] [blame] | 2921 | |
Yan, Zheng | 86df7eb | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2922 | while (1) { |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2923 | int batch = atomic_read(&root->log_batch); |
Chris Mason | cd354ad | 2011-10-20 15:45:37 -0400 | [diff] [blame] | 2924 | /* when we're on an ssd, just kick the log commit out */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2925 | if (!btrfs_test_opt(fs_info, SSD) && |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 2926 | test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) { |
Yan, Zheng | 86df7eb | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2927 | mutex_unlock(&root->log_mutex); |
| 2928 | schedule_timeout_uninterruptible(1); |
| 2929 | mutex_lock(&root->log_mutex); |
| 2930 | } |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2931 | wait_for_writer(root); |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2932 | if (batch == atomic_read(&root->log_batch)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2933 | break; |
| 2934 | } |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2935 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2936 | /* bail out if we need to do a full commit */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2937 | if (btrfs_need_log_full_commit(fs_info, trans)) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2938 | ret = -EAGAIN; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 2939 | btrfs_free_logged_extents(log, log_transid); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2940 | mutex_unlock(&root->log_mutex); |
| 2941 | goto out; |
| 2942 | } |
| 2943 | |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2944 | if (log_transid % 2 == 0) |
| 2945 | mark = EXTENT_DIRTY; |
| 2946 | else |
| 2947 | mark = EXTENT_NEW; |
| 2948 | |
Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 2949 | /* we start IO on all the marked extents here, but we don't actually |
| 2950 | * wait for them until later. |
| 2951 | */ |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2952 | blk_start_plug(&plug); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2953 | ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2954 | if (ret) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2955 | blk_finish_plug(&plug); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2956 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 2957 | btrfs_free_logged_extents(log, log_transid); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2958 | btrfs_set_log_full_commit(fs_info, trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2959 | mutex_unlock(&root->log_mutex); |
| 2960 | goto out; |
| 2961 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2962 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2963 | btrfs_set_root_node(&log->root_item, log->node); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2964 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2965 | root->log_transid++; |
| 2966 | log->log_transid = root->log_transid; |
Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 2967 | root->log_start_pid = 0; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2968 | /* |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2969 | * IO has been started, blocks of the log tree have WRITTEN flag set |
| 2970 | * in their headers. new modifications of the log will be written to |
| 2971 | * 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] | 2972 | */ |
| 2973 | mutex_unlock(&root->log_mutex); |
| 2974 | |
Filipe Manana | 28a2359 | 2016-08-23 21:13:51 +0100 | [diff] [blame] | 2975 | btrfs_init_log_ctx(&root_log_ctx, NULL); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2976 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2977 | mutex_lock(&log_root_tree->log_mutex); |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2978 | atomic_inc(&log_root_tree->log_batch); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2979 | atomic_inc(&log_root_tree->log_writers); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2980 | |
| 2981 | index2 = log_root_tree->log_transid % 2; |
| 2982 | list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]); |
| 2983 | root_log_ctx.log_transid = log_root_tree->log_transid; |
| 2984 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2985 | mutex_unlock(&log_root_tree->log_mutex); |
| 2986 | |
| 2987 | ret = update_log_root(trans, log); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2988 | |
| 2989 | mutex_lock(&log_root_tree->log_mutex); |
| 2990 | if (atomic_dec_and_test(&log_root_tree->log_writers)) { |
David Sterba | 779adf0 | 2015-02-16 19:39:00 +0100 | [diff] [blame] | 2991 | /* |
| 2992 | * Implicit memory barrier after atomic_dec_and_test |
| 2993 | */ |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2994 | if (waitqueue_active(&log_root_tree->log_writer_wait)) |
| 2995 | wake_up(&log_root_tree->log_writer_wait); |
| 2996 | } |
| 2997 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2998 | if (ret) { |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2999 | if (!list_empty(&root_log_ctx.list)) |
| 3000 | list_del_init(&root_log_ctx.list); |
| 3001 | |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3002 | blk_finish_plug(&plug); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3003 | btrfs_set_log_full_commit(fs_info, trans); |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 3004 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3005 | if (ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3006 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3007 | mutex_unlock(&log_root_tree->log_mutex); |
| 3008 | goto out; |
| 3009 | } |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3010 | btrfs_wait_tree_log_extents(log, mark); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 3011 | btrfs_free_logged_extents(log, log_transid); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3012 | mutex_unlock(&log_root_tree->log_mutex); |
| 3013 | ret = -EAGAIN; |
| 3014 | goto out; |
| 3015 | } |
| 3016 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3017 | if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) { |
Forrest Liu | 3da5ab5 | 2015-01-30 19:42:12 +0800 | [diff] [blame] | 3018 | blk_finish_plug(&plug); |
Chris Mason | cbd60aa | 2016-09-06 05:37:40 -0700 | [diff] [blame] | 3019 | list_del_init(&root_log_ctx.list); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3020 | mutex_unlock(&log_root_tree->log_mutex); |
| 3021 | ret = root_log_ctx.log_ret; |
| 3022 | goto out; |
| 3023 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3024 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3025 | index2 = root_log_ctx.log_transid % 2; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3026 | if (atomic_read(&log_root_tree->log_commit[index2])) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3027 | blk_finish_plug(&plug); |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3028 | ret = btrfs_wait_tree_log_extents(log, mark); |
Josef Bacik | 50d9aa9 | 2014-11-21 14:52:38 -0500 | [diff] [blame] | 3029 | btrfs_wait_logged_extents(trans, log, log_transid); |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3030 | wait_log_commit(log_root_tree, |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3031 | root_log_ctx.log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3032 | mutex_unlock(&log_root_tree->log_mutex); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3033 | if (!ret) |
| 3034 | ret = root_log_ctx.log_ret; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3035 | goto out; |
| 3036 | } |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3037 | ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3038 | atomic_set(&log_root_tree->log_commit[index2], 1); |
| 3039 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3040 | if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) { |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3041 | wait_log_commit(log_root_tree, |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3042 | root_log_ctx.log_transid - 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3043 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3044 | |
Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3045 | wait_for_writer(log_root_tree); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3046 | |
| 3047 | /* |
| 3048 | * now that we've moved on to the tree of log tree roots, |
| 3049 | * check the full commit flag again |
| 3050 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3051 | if (btrfs_need_log_full_commit(fs_info, trans)) { |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3052 | blk_finish_plug(&plug); |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3053 | btrfs_wait_tree_log_extents(log, mark); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 3054 | btrfs_free_logged_extents(log, log_transid); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3055 | mutex_unlock(&log_root_tree->log_mutex); |
| 3056 | ret = -EAGAIN; |
| 3057 | goto out_wake_log_root; |
| 3058 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3059 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3060 | ret = btrfs_write_marked_extents(fs_info, |
Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3061 | &log_root_tree->dirty_log_pages, |
| 3062 | EXTENT_DIRTY | EXTENT_NEW); |
| 3063 | blk_finish_plug(&plug); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3064 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3065 | btrfs_set_log_full_commit(fs_info, trans); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3066 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 3067 | btrfs_free_logged_extents(log, log_transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3068 | mutex_unlock(&log_root_tree->log_mutex); |
| 3069 | goto out_wake_log_root; |
| 3070 | } |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3071 | ret = btrfs_wait_tree_log_extents(log, mark); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3072 | if (!ret) |
Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3073 | ret = btrfs_wait_tree_log_extents(log_root_tree, |
| 3074 | EXTENT_NEW | EXTENT_DIRTY); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3075 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3076 | btrfs_set_log_full_commit(fs_info, trans); |
Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3077 | btrfs_free_logged_extents(log, log_transid); |
| 3078 | mutex_unlock(&log_root_tree->log_mutex); |
| 3079 | goto out_wake_log_root; |
| 3080 | } |
Josef Bacik | 50d9aa9 | 2014-11-21 14:52:38 -0500 | [diff] [blame] | 3081 | btrfs_wait_logged_extents(trans, log, log_transid); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3082 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3083 | btrfs_set_super_log_root(fs_info->super_for_commit, |
| 3084 | log_root_tree->node->start); |
| 3085 | btrfs_set_super_log_root_level(fs_info->super_for_commit, |
| 3086 | btrfs_header_level(log_root_tree->node)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3087 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3088 | log_root_tree->log_transid++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3089 | mutex_unlock(&log_root_tree->log_mutex); |
| 3090 | |
| 3091 | /* |
| 3092 | * nobody else is going to jump in and write the the ctree |
| 3093 | * super here because the log_commit atomic below is protecting |
| 3094 | * us. We must be called with a transaction handle pinning |
| 3095 | * the running transaction open, so a full commit can't hop |
| 3096 | * in and cause problems either. |
| 3097 | */ |
David Sterba | eece6a9 | 2017-02-10 19:04:32 +0100 | [diff] [blame] | 3098 | ret = write_all_supers(fs_info, 1); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3099 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3100 | btrfs_set_log_full_commit(fs_info, trans); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3101 | btrfs_abort_transaction(trans, ret); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3102 | goto out_wake_log_root; |
| 3103 | } |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3104 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 3105 | mutex_lock(&root->log_mutex); |
| 3106 | if (root->last_log_commit < log_transid) |
| 3107 | root->last_log_commit = log_transid; |
| 3108 | mutex_unlock(&root->log_mutex); |
| 3109 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3110 | out_wake_log_root: |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 3111 | mutex_lock(&log_root_tree->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3112 | btrfs_remove_all_log_ctxs(log_root_tree, index2, ret); |
| 3113 | |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3114 | log_root_tree->log_transid_committed++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3115 | atomic_set(&log_root_tree->log_commit[index2], 0); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3116 | mutex_unlock(&log_root_tree->log_mutex); |
| 3117 | |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3118 | /* |
David Sterba | 3d3a2e6 | 2018-04-24 14:53:56 +0200 | [diff] [blame^] | 3119 | * The barrier before waitqueue_active is needed so all the updates |
| 3120 | * above are seen by the woken threads. It might not be necessary, but |
| 3121 | * proving that seems to be hard. |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3122 | */ |
David Sterba | 3d3a2e6 | 2018-04-24 14:53:56 +0200 | [diff] [blame^] | 3123 | smp_mb(); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3124 | if (waitqueue_active(&log_root_tree->log_commit_wait[index2])) |
| 3125 | wake_up(&log_root_tree->log_commit_wait[index2]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3126 | out: |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3127 | mutex_lock(&root->log_mutex); |
Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 3128 | btrfs_remove_all_log_ctxs(root, index1, ret); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3129 | root->log_transid_committed++; |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3130 | atomic_set(&root->log_commit[index1], 0); |
Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3131 | mutex_unlock(&root->log_mutex); |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3132 | |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3133 | /* |
David Sterba | 3d3a2e6 | 2018-04-24 14:53:56 +0200 | [diff] [blame^] | 3134 | * The barrier before waitqueue_active is needed so all the updates |
| 3135 | * above are seen by the woken threads. It might not be necessary, but |
| 3136 | * proving that seems to be hard. |
David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3137 | */ |
David Sterba | 3d3a2e6 | 2018-04-24 14:53:56 +0200 | [diff] [blame^] | 3138 | smp_mb(); |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3139 | if (waitqueue_active(&root->log_commit_wait[index1])) |
| 3140 | wake_up(&root->log_commit_wait[index1]); |
Chris Mason | b31eabd | 2011-01-31 16:48:24 -0500 | [diff] [blame] | 3141 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3142 | } |
| 3143 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3144 | static void free_log_tree(struct btrfs_trans_handle *trans, |
| 3145 | struct btrfs_root *log) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3146 | { |
| 3147 | int ret; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3148 | u64 start; |
| 3149 | u64 end; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3150 | struct walk_control wc = { |
| 3151 | .free = 1, |
| 3152 | .process_func = process_one_buffer |
| 3153 | }; |
| 3154 | |
Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 3155 | ret = walk_log_tree(trans, log, &wc); |
| 3156 | /* I don't think this can happen but just in case */ |
| 3157 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3158 | btrfs_abort_transaction(trans, ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3159 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3160 | while (1) { |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3161 | ret = find_first_extent_bit(&log->dirty_log_pages, |
Liu Bo | 55237a5 | 2018-01-25 11:02:52 -0700 | [diff] [blame] | 3162 | 0, &start, &end, |
| 3163 | EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 3164 | NULL); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3165 | if (ret) |
| 3166 | break; |
| 3167 | |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 3168 | clear_extent_bits(&log->dirty_log_pages, start, end, |
Liu Bo | 55237a5 | 2018-01-25 11:02:52 -0700 | [diff] [blame] | 3169 | EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3170 | } |
| 3171 | |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 3172 | /* |
| 3173 | * We may have short-circuited the log tree with the full commit logic |
| 3174 | * and left ordered extents on our list, so clear these out to keep us |
| 3175 | * from leaking inodes and memory. |
| 3176 | */ |
| 3177 | btrfs_free_logged_extents(log, 0); |
| 3178 | btrfs_free_logged_extents(log, 1); |
| 3179 | |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3180 | free_extent_buffer(log->node); |
| 3181 | kfree(log); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | /* |
| 3185 | * free all the extents used by the tree log. This should be called |
| 3186 | * at commit time of the full transaction |
| 3187 | */ |
| 3188 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) |
| 3189 | { |
| 3190 | if (root->log_root) { |
| 3191 | free_log_tree(trans, root->log_root); |
| 3192 | root->log_root = NULL; |
| 3193 | } |
| 3194 | return 0; |
| 3195 | } |
| 3196 | |
| 3197 | int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, |
| 3198 | struct btrfs_fs_info *fs_info) |
| 3199 | { |
| 3200 | if (fs_info->log_root_tree) { |
| 3201 | free_log_tree(trans, fs_info->log_root_tree); |
| 3202 | fs_info->log_root_tree = NULL; |
| 3203 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3204 | return 0; |
| 3205 | } |
| 3206 | |
| 3207 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3208 | * If both a file and directory are logged, and unlinks or renames are |
| 3209 | * mixed in, we have a few interesting corners: |
| 3210 | * |
| 3211 | * create file X in dir Y |
| 3212 | * link file X to X.link in dir Y |
| 3213 | * fsync file X |
| 3214 | * unlink file X but leave X.link |
| 3215 | * fsync dir Y |
| 3216 | * |
| 3217 | * After a crash we would expect only X.link to exist. But file X |
| 3218 | * didn't get fsync'd again so the log has back refs for X and X.link. |
| 3219 | * |
| 3220 | * We solve this by removing directory entries and inode backrefs from the |
| 3221 | * log when a file that was logged in the current transaction is |
| 3222 | * unlinked. Any later fsync will include the updated log entries, and |
| 3223 | * we'll be able to reconstruct the proper directory items from backrefs. |
| 3224 | * |
| 3225 | * This optimizations allows us to avoid relogging the entire inode |
| 3226 | * or the entire directory. |
| 3227 | */ |
| 3228 | int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, |
| 3229 | struct btrfs_root *root, |
| 3230 | const char *name, int name_len, |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3231 | struct btrfs_inode *dir, u64 index) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3232 | { |
| 3233 | struct btrfs_root *log; |
| 3234 | struct btrfs_dir_item *di; |
| 3235 | struct btrfs_path *path; |
| 3236 | int ret; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3237 | int err = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3238 | int bytes_del = 0; |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3239 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3240 | |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3241 | if (dir->logged_trans < trans->transid) |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 3242 | return 0; |
| 3243 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3244 | ret = join_running_log_trans(root); |
| 3245 | if (ret) |
| 3246 | return 0; |
| 3247 | |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3248 | mutex_lock(&dir->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3249 | |
| 3250 | log = root->log_root; |
| 3251 | path = btrfs_alloc_path(); |
Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3252 | if (!path) { |
| 3253 | err = -ENOMEM; |
| 3254 | goto out_unlock; |
| 3255 | } |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 3256 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3257 | di = btrfs_lookup_dir_item(trans, log, path, dir_ino, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3258 | name, name_len, -1); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3259 | if (IS_ERR(di)) { |
| 3260 | err = PTR_ERR(di); |
| 3261 | goto fail; |
| 3262 | } |
| 3263 | if (di) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3264 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
| 3265 | bytes_del += name_len; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3266 | if (ret) { |
| 3267 | err = ret; |
| 3268 | goto fail; |
| 3269 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3270 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3271 | btrfs_release_path(path); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3272 | di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3273 | index, name, name_len, -1); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3274 | if (IS_ERR(di)) { |
| 3275 | err = PTR_ERR(di); |
| 3276 | goto fail; |
| 3277 | } |
| 3278 | if (di) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3279 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
| 3280 | bytes_del += name_len; |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3281 | if (ret) { |
| 3282 | err = ret; |
| 3283 | goto fail; |
| 3284 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | /* update the directory size in the log to reflect the names |
| 3288 | * we have removed |
| 3289 | */ |
| 3290 | if (bytes_del) { |
| 3291 | struct btrfs_key key; |
| 3292 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3293 | key.objectid = dir_ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3294 | key.offset = 0; |
| 3295 | key.type = BTRFS_INODE_ITEM_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3296 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3297 | |
| 3298 | ret = btrfs_search_slot(trans, log, &key, path, 0, 1); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3299 | if (ret < 0) { |
| 3300 | err = ret; |
| 3301 | goto fail; |
| 3302 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3303 | if (ret == 0) { |
| 3304 | struct btrfs_inode_item *item; |
| 3305 | u64 i_size; |
| 3306 | |
| 3307 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3308 | struct btrfs_inode_item); |
| 3309 | i_size = btrfs_inode_size(path->nodes[0], item); |
| 3310 | if (i_size > bytes_del) |
| 3311 | i_size -= bytes_del; |
| 3312 | else |
| 3313 | i_size = 0; |
| 3314 | btrfs_set_inode_size(path->nodes[0], item, i_size); |
| 3315 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 3316 | } else |
| 3317 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3318 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3319 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3320 | fail: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3321 | btrfs_free_path(path); |
Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3322 | out_unlock: |
Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3323 | mutex_unlock(&dir->log_mutex); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3324 | if (ret == -ENOSPC) { |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 3325 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3326 | ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3327 | } else if (ret < 0) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3328 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3329 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3330 | btrfs_end_log_trans(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3331 | |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 3332 | return err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | /* see comments for btrfs_del_dir_entries_in_log */ |
| 3336 | int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, |
| 3337 | struct btrfs_root *root, |
| 3338 | const char *name, int name_len, |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3339 | struct btrfs_inode *inode, u64 dirid) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3340 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3341 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3342 | struct btrfs_root *log; |
| 3343 | u64 index; |
| 3344 | int ret; |
| 3345 | |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3346 | if (inode->logged_trans < trans->transid) |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 3347 | return 0; |
| 3348 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3349 | ret = join_running_log_trans(root); |
| 3350 | if (ret) |
| 3351 | return 0; |
| 3352 | log = root->log_root; |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3353 | mutex_lock(&inode->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3354 | |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3355 | 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] | 3356 | dirid, &index); |
Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3357 | mutex_unlock(&inode->log_mutex); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3358 | if (ret == -ENOSPC) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3359 | btrfs_set_log_full_commit(fs_info, trans); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3360 | ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3361 | } else if (ret < 0 && ret != -ENOENT) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3362 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3363 | btrfs_end_log_trans(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3364 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3365 | return ret; |
| 3366 | } |
| 3367 | |
| 3368 | /* |
| 3369 | * creates a range item in the log for 'dirid'. first_offset and |
| 3370 | * last_offset tell us which parts of the key space the log should |
| 3371 | * be considered authoritative for. |
| 3372 | */ |
| 3373 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, |
| 3374 | struct btrfs_root *log, |
| 3375 | struct btrfs_path *path, |
| 3376 | int key_type, u64 dirid, |
| 3377 | u64 first_offset, u64 last_offset) |
| 3378 | { |
| 3379 | int ret; |
| 3380 | struct btrfs_key key; |
| 3381 | struct btrfs_dir_log_item *item; |
| 3382 | |
| 3383 | key.objectid = dirid; |
| 3384 | key.offset = first_offset; |
| 3385 | if (key_type == BTRFS_DIR_ITEM_KEY) |
| 3386 | key.type = BTRFS_DIR_LOG_ITEM_KEY; |
| 3387 | else |
| 3388 | key.type = BTRFS_DIR_LOG_INDEX_KEY; |
| 3389 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3390 | if (ret) |
| 3391 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3392 | |
| 3393 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3394 | struct btrfs_dir_log_item); |
| 3395 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); |
| 3396 | btrfs_mark_buffer_dirty(path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3397 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3398 | return 0; |
| 3399 | } |
| 3400 | |
| 3401 | /* |
| 3402 | * log all the items included in the current transaction for a given |
| 3403 | * directory. This also creates the range items in the log tree required |
| 3404 | * to replay anything deleted before the fsync |
| 3405 | */ |
| 3406 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 684a577 | 2017-01-18 00:31:41 +0200 | [diff] [blame] | 3407 | struct btrfs_root *root, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3408 | struct btrfs_path *path, |
| 3409 | struct btrfs_path *dst_path, int key_type, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3410 | struct btrfs_log_ctx *ctx, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3411 | u64 min_offset, u64 *last_offset_ret) |
| 3412 | { |
| 3413 | struct btrfs_key min_key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3414 | struct btrfs_root *log = root->log_root; |
| 3415 | struct extent_buffer *src; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3416 | int err = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3417 | int ret; |
| 3418 | int i; |
| 3419 | int nritems; |
| 3420 | u64 first_offset = min_offset; |
| 3421 | u64 last_offset = (u64)-1; |
Nikolay Borisov | 684a577 | 2017-01-18 00:31:41 +0200 | [diff] [blame] | 3422 | u64 ino = btrfs_ino(inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3423 | |
| 3424 | log = root->log_root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3425 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3426 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3427 | min_key.type = key_type; |
| 3428 | min_key.offset = min_offset; |
| 3429 | |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3430 | ret = btrfs_search_forward(root, &min_key, path, trans->transid); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3431 | |
| 3432 | /* |
| 3433 | * we didn't find anything from this transaction, see if there |
| 3434 | * is anything at all |
| 3435 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3436 | if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) { |
| 3437 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3438 | min_key.type = key_type; |
| 3439 | min_key.offset = (u64)-1; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3440 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3441 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
| 3442 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3443 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3444 | return ret; |
| 3445 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3446 | ret = btrfs_previous_item(root, path, ino, key_type); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3447 | |
| 3448 | /* if ret == 0 there are items for this type, |
| 3449 | * create a range to tell us the last key of this type. |
| 3450 | * otherwise, there are no items in this directory after |
| 3451 | * *min_offset, and we create a range to indicate that. |
| 3452 | */ |
| 3453 | if (ret == 0) { |
| 3454 | struct btrfs_key tmp; |
| 3455 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, |
| 3456 | path->slots[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3457 | if (key_type == tmp.type) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3458 | first_offset = max(min_offset, tmp.offset) + 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3459 | } |
| 3460 | goto done; |
| 3461 | } |
| 3462 | |
| 3463 | /* go backward to find any previous key */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3464 | ret = btrfs_previous_item(root, path, ino, key_type); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3465 | if (ret == 0) { |
| 3466 | struct btrfs_key tmp; |
| 3467 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
| 3468 | if (key_type == tmp.type) { |
| 3469 | first_offset = tmp.offset; |
| 3470 | ret = overwrite_item(trans, log, dst_path, |
| 3471 | path->nodes[0], path->slots[0], |
| 3472 | &tmp); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3473 | if (ret) { |
| 3474 | err = ret; |
| 3475 | goto done; |
| 3476 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3477 | } |
| 3478 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3479 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3480 | |
| 3481 | /* find the first key from this transaction again */ |
| 3482 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3483 | if (WARN_ON(ret != 0)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3484 | goto done; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3485 | |
| 3486 | /* |
| 3487 | * we have a block from this transaction, log every item in it |
| 3488 | * from our directory |
| 3489 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3490 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3491 | struct btrfs_key tmp; |
| 3492 | src = path->nodes[0]; |
| 3493 | nritems = btrfs_header_nritems(src); |
| 3494 | for (i = path->slots[0]; i < nritems; i++) { |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3495 | struct btrfs_dir_item *di; |
| 3496 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3497 | btrfs_item_key_to_cpu(src, &min_key, i); |
| 3498 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3499 | if (min_key.objectid != ino || min_key.type != key_type) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3500 | goto done; |
| 3501 | ret = overwrite_item(trans, log, dst_path, src, i, |
| 3502 | &min_key); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3503 | if (ret) { |
| 3504 | err = ret; |
| 3505 | goto done; |
| 3506 | } |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3507 | |
| 3508 | /* |
| 3509 | * We must make sure that when we log a directory entry, |
| 3510 | * the corresponding inode, after log replay, has a |
| 3511 | * matching link count. For example: |
| 3512 | * |
| 3513 | * touch foo |
| 3514 | * mkdir mydir |
| 3515 | * sync |
| 3516 | * ln foo mydir/bar |
| 3517 | * xfs_io -c "fsync" mydir |
| 3518 | * <crash> |
| 3519 | * <mount fs and log replay> |
| 3520 | * |
| 3521 | * Would result in a fsync log that when replayed, our |
| 3522 | * file inode would have a link count of 1, but we get |
| 3523 | * two directory entries pointing to the same inode. |
| 3524 | * After removing one of the names, it would not be |
| 3525 | * possible to remove the other name, which resulted |
| 3526 | * always in stale file handle errors, and would not |
| 3527 | * be possible to rmdir the parent directory, since |
| 3528 | * its i_size could never decrement to the value |
| 3529 | * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors. |
| 3530 | */ |
| 3531 | di = btrfs_item_ptr(src, i, struct btrfs_dir_item); |
| 3532 | btrfs_dir_item_key_to_cpu(src, di, &tmp); |
| 3533 | if (ctx && |
| 3534 | (btrfs_dir_transid(src, di) == trans->transid || |
| 3535 | btrfs_dir_type(src, di) == BTRFS_FT_DIR) && |
| 3536 | tmp.type != BTRFS_ROOT_ITEM_KEY) |
| 3537 | ctx->log_new_dentries = true; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3538 | } |
| 3539 | path->slots[0] = nritems; |
| 3540 | |
| 3541 | /* |
| 3542 | * look ahead to the next item and see if it is also |
| 3543 | * from this directory and from this transaction |
| 3544 | */ |
| 3545 | ret = btrfs_next_leaf(root, path); |
Liu Bo | 80c0b42 | 2018-04-03 01:59:47 +0800 | [diff] [blame] | 3546 | if (ret) { |
| 3547 | if (ret == 1) |
| 3548 | last_offset = (u64)-1; |
| 3549 | else |
| 3550 | err = ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3551 | goto done; |
| 3552 | } |
| 3553 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3554 | if (tmp.objectid != ino || tmp.type != key_type) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3555 | last_offset = (u64)-1; |
| 3556 | goto done; |
| 3557 | } |
| 3558 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { |
| 3559 | ret = overwrite_item(trans, log, dst_path, |
| 3560 | path->nodes[0], path->slots[0], |
| 3561 | &tmp); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3562 | if (ret) |
| 3563 | err = ret; |
| 3564 | else |
| 3565 | last_offset = tmp.offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3566 | goto done; |
| 3567 | } |
| 3568 | } |
| 3569 | done: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3570 | btrfs_release_path(path); |
| 3571 | btrfs_release_path(dst_path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3572 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3573 | if (err == 0) { |
| 3574 | *last_offset_ret = last_offset; |
| 3575 | /* |
| 3576 | * insert the log range keys to indicate where the log |
| 3577 | * is valid |
| 3578 | */ |
| 3579 | ret = insert_dir_log_key(trans, log, path, key_type, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3580 | ino, first_offset, last_offset); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3581 | if (ret) |
| 3582 | err = ret; |
| 3583 | } |
| 3584 | return err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | /* |
| 3588 | * logging directories is very similar to logging inodes, We find all the items |
| 3589 | * from the current transaction and write them to the log. |
| 3590 | * |
| 3591 | * The recovery code scans the directory in the subvolume, and if it finds a |
| 3592 | * key in the range logged that is not present in the log tree, then it means |
| 3593 | * that dir entry was unlinked during the transaction. |
| 3594 | * |
| 3595 | * In order for that scan to work, we must include one key smaller than |
| 3596 | * the smallest logged by this transaction and one key larger than the largest |
| 3597 | * key logged by this transaction. |
| 3598 | */ |
| 3599 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, |
Nikolay Borisov | dbf39ea | 2017-01-18 00:31:42 +0200 | [diff] [blame] | 3600 | struct btrfs_root *root, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3601 | struct btrfs_path *path, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3602 | struct btrfs_path *dst_path, |
| 3603 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3604 | { |
| 3605 | u64 min_key; |
| 3606 | u64 max_key; |
| 3607 | int ret; |
| 3608 | int key_type = BTRFS_DIR_ITEM_KEY; |
| 3609 | |
| 3610 | again: |
| 3611 | min_key = 0; |
| 3612 | max_key = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3613 | while (1) { |
Nikolay Borisov | dbf39ea | 2017-01-18 00:31:42 +0200 | [diff] [blame] | 3614 | ret = log_dir_items(trans, root, inode, path, dst_path, key_type, |
| 3615 | ctx, min_key, &max_key); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3616 | if (ret) |
| 3617 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3618 | if (max_key == (u64)-1) |
| 3619 | break; |
| 3620 | min_key = max_key + 1; |
| 3621 | } |
| 3622 | |
| 3623 | if (key_type == BTRFS_DIR_ITEM_KEY) { |
| 3624 | key_type = BTRFS_DIR_INDEX_KEY; |
| 3625 | goto again; |
| 3626 | } |
| 3627 | return 0; |
| 3628 | } |
| 3629 | |
| 3630 | /* |
| 3631 | * a helper function to drop items from the log before we relog an |
| 3632 | * inode. max_key_type indicates the highest item type to remove. |
| 3633 | * This cannot be run for file data extents because it does not |
| 3634 | * free the extents they point to. |
| 3635 | */ |
| 3636 | static int drop_objectid_items(struct btrfs_trans_handle *trans, |
| 3637 | struct btrfs_root *log, |
| 3638 | struct btrfs_path *path, |
| 3639 | u64 objectid, int max_key_type) |
| 3640 | { |
| 3641 | int ret; |
| 3642 | struct btrfs_key key; |
| 3643 | struct btrfs_key found_key; |
Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 3644 | int start_slot; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3645 | |
| 3646 | key.objectid = objectid; |
| 3647 | key.type = max_key_type; |
| 3648 | key.offset = (u64)-1; |
| 3649 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3650 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3651 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3652 | BUG_ON(ret == 0); /* Logic error */ |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3653 | if (ret < 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3654 | break; |
| 3655 | |
| 3656 | if (path->slots[0] == 0) |
| 3657 | break; |
| 3658 | |
| 3659 | path->slots[0]--; |
| 3660 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 3661 | path->slots[0]); |
| 3662 | |
| 3663 | if (found_key.objectid != objectid) |
| 3664 | break; |
| 3665 | |
Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 3666 | found_key.offset = 0; |
| 3667 | found_key.type = 0; |
| 3668 | ret = btrfs_bin_search(path->nodes[0], &found_key, 0, |
| 3669 | &start_slot); |
| 3670 | |
| 3671 | ret = btrfs_del_items(trans, log, path, start_slot, |
| 3672 | path->slots[0] - start_slot + 1); |
| 3673 | /* |
| 3674 | * If start slot isn't 0 then we don't need to re-search, we've |
| 3675 | * found the last guy with the objectid in this tree. |
| 3676 | */ |
| 3677 | if (ret || start_slot != 0) |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 3678 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3679 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3680 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3681 | btrfs_release_path(path); |
Josef Bacik | 5bdbeb2 | 2012-05-29 16:59:49 -0400 | [diff] [blame] | 3682 | if (ret > 0) |
| 3683 | ret = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3684 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3685 | } |
| 3686 | |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3687 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3688 | struct extent_buffer *leaf, |
| 3689 | struct btrfs_inode_item *item, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3690 | struct inode *inode, int log_inode_only, |
| 3691 | u64 logged_isize) |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3692 | { |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3693 | struct btrfs_map_token token; |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3694 | |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3695 | btrfs_init_map_token(&token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3696 | |
| 3697 | if (log_inode_only) { |
| 3698 | /* set the generation to zero so the recover code |
| 3699 | * can tell the difference between an logging |
| 3700 | * just to say 'this inode exists' and a logging |
| 3701 | * to say 'update this inode with these values' |
| 3702 | */ |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3703 | btrfs_set_token_inode_generation(leaf, item, 0, &token); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3704 | btrfs_set_token_inode_size(leaf, item, logged_isize, &token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3705 | } else { |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3706 | btrfs_set_token_inode_generation(leaf, item, |
| 3707 | BTRFS_I(inode)->generation, |
| 3708 | &token); |
| 3709 | btrfs_set_token_inode_size(leaf, item, inode->i_size, &token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3710 | } |
| 3711 | |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3712 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3713 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3714 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3715 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
| 3716 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3717 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3718 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3719 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3720 | inode->i_atime.tv_nsec, &token); |
| 3721 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3722 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3723 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3724 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3725 | inode->i_mtime.tv_nsec, &token); |
| 3726 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3727 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3728 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3729 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3730 | inode->i_ctime.tv_nsec, &token); |
| 3731 | |
| 3732 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3733 | &token); |
| 3734 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3735 | btrfs_set_token_inode_sequence(leaf, item, |
| 3736 | inode_peek_iversion(inode), &token); |
Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3737 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3738 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3739 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3740 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3741 | } |
| 3742 | |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3743 | static int log_inode_item(struct btrfs_trans_handle *trans, |
| 3744 | struct btrfs_root *log, struct btrfs_path *path, |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3745 | struct btrfs_inode *inode) |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3746 | { |
| 3747 | struct btrfs_inode_item *inode_item; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3748 | int ret; |
| 3749 | |
Filipe David Borba Manana | efd0c40 | 2013-10-07 21:20:44 +0100 | [diff] [blame] | 3750 | ret = btrfs_insert_empty_item(trans, log, path, |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3751 | &inode->location, sizeof(*inode_item)); |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3752 | if (ret && ret != -EEXIST) |
| 3753 | return ret; |
| 3754 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3755 | struct btrfs_inode_item); |
Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3756 | fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode, |
| 3757 | 0, 0); |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3758 | btrfs_release_path(path); |
| 3759 | return 0; |
| 3760 | } |
| 3761 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3762 | static noinline int copy_items(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3763 | struct btrfs_inode *inode, |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3764 | struct btrfs_path *dst_path, |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3765 | struct btrfs_path *src_path, u64 *last_extent, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3766 | int start_slot, int nr, int inode_only, |
| 3767 | u64 logged_isize) |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3768 | { |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3769 | 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] | 3770 | unsigned long src_offset; |
| 3771 | unsigned long dst_offset; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3772 | struct btrfs_root *log = inode->root->log_root; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3773 | struct btrfs_file_extent_item *extent; |
| 3774 | struct btrfs_inode_item *inode_item; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3775 | struct extent_buffer *src = src_path->nodes[0]; |
| 3776 | struct btrfs_key first_key, last_key, key; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3777 | int ret; |
| 3778 | struct btrfs_key *ins_keys; |
| 3779 | u32 *ins_sizes; |
| 3780 | char *ins_data; |
| 3781 | int i; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3782 | struct list_head ordered_sums; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3783 | int skip_csum = inode->flags & BTRFS_INODE_NODATASUM; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3784 | bool has_extents = false; |
Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 3785 | bool need_find_last_extent = true; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3786 | bool done = false; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3787 | |
| 3788 | INIT_LIST_HEAD(&ordered_sums); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3789 | |
| 3790 | ins_data = kmalloc(nr * sizeof(struct btrfs_key) + |
| 3791 | nr * sizeof(u32), GFP_NOFS); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 3792 | if (!ins_data) |
| 3793 | return -ENOMEM; |
| 3794 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3795 | first_key.objectid = (u64)-1; |
| 3796 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3797 | ins_sizes = (u32 *)ins_data; |
| 3798 | ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); |
| 3799 | |
| 3800 | for (i = 0; i < nr; i++) { |
| 3801 | ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot); |
| 3802 | btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot); |
| 3803 | } |
| 3804 | ret = btrfs_insert_empty_items(trans, log, dst_path, |
| 3805 | ins_keys, ins_sizes, nr); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3806 | if (ret) { |
| 3807 | kfree(ins_data); |
| 3808 | return ret; |
| 3809 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3810 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3811 | for (i = 0; i < nr; i++, dst_path->slots[0]++) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3812 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], |
| 3813 | dst_path->slots[0]); |
| 3814 | |
| 3815 | src_offset = btrfs_item_ptr_offset(src, start_slot + i); |
| 3816 | |
Matthias Kaehlcke | 0dde10b | 2017-07-27 14:30:23 -0700 | [diff] [blame] | 3817 | if (i == nr - 1) |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3818 | last_key = ins_keys[i]; |
| 3819 | |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3820 | if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3821 | inode_item = btrfs_item_ptr(dst_path->nodes[0], |
| 3822 | dst_path->slots[0], |
| 3823 | struct btrfs_inode_item); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3824 | fill_inode_item(trans, dst_path->nodes[0], inode_item, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3825 | &inode->vfs_inode, |
| 3826 | inode_only == LOG_INODE_EXISTS, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3827 | logged_isize); |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3828 | } else { |
| 3829 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, |
| 3830 | src_offset, ins_sizes[i]); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3831 | } |
Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3832 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3833 | /* |
| 3834 | * We set need_find_last_extent here in case we know we were |
| 3835 | * processing other items and then walk into the first extent in |
| 3836 | * the inode. If we don't hit an extent then nothing changes, |
| 3837 | * we'll do the last search the next time around. |
| 3838 | */ |
| 3839 | if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) { |
| 3840 | has_extents = true; |
Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 3841 | if (first_key.objectid == (u64)-1) |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3842 | first_key = ins_keys[i]; |
| 3843 | } else { |
| 3844 | need_find_last_extent = false; |
| 3845 | } |
| 3846 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3847 | /* take a reference on file data extents so that truncates |
| 3848 | * or deletes of this inode don't have to relog the inode |
| 3849 | * again |
| 3850 | */ |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3851 | if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY && |
Liu Bo | d279440 | 2012-08-29 01:07:56 -0600 | [diff] [blame] | 3852 | !skip_csum) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3853 | int found_type; |
| 3854 | extent = btrfs_item_ptr(src, start_slot + i, |
| 3855 | struct btrfs_file_extent_item); |
| 3856 | |
liubo | 8e531cd | 2011-05-06 10:36:09 +0800 | [diff] [blame] | 3857 | if (btrfs_file_extent_generation(src, extent) < trans->transid) |
| 3858 | continue; |
| 3859 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3860 | found_type = btrfs_file_extent_type(src, extent); |
Josef Bacik | 6f1fed7 | 2012-09-26 11:07:06 -0400 | [diff] [blame] | 3861 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3862 | u64 ds, dl, cs, cl; |
| 3863 | ds = btrfs_file_extent_disk_bytenr(src, |
| 3864 | extent); |
| 3865 | /* ds == 0 is a hole */ |
| 3866 | if (ds == 0) |
| 3867 | continue; |
| 3868 | |
| 3869 | dl = btrfs_file_extent_disk_num_bytes(src, |
| 3870 | extent); |
| 3871 | cs = btrfs_file_extent_offset(src, extent); |
| 3872 | cl = btrfs_file_extent_num_bytes(src, |
Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 3873 | extent); |
Chris Mason | 580afd7 | 2008-12-08 19:15:39 -0500 | [diff] [blame] | 3874 | if (btrfs_file_extent_compression(src, |
| 3875 | extent)) { |
| 3876 | cs = 0; |
| 3877 | cl = dl; |
| 3878 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3879 | |
| 3880 | ret = btrfs_lookup_csums_range( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3881 | fs_info->csum_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3882 | ds + cs, ds + cs + cl - 1, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3883 | &ordered_sums, 0); |
Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3884 | if (ret) { |
| 3885 | btrfs_release_path(dst_path); |
| 3886 | kfree(ins_data); |
| 3887 | return ret; |
| 3888 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3889 | } |
| 3890 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | btrfs_mark_buffer_dirty(dst_path->nodes[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3894 | btrfs_release_path(dst_path); |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3895 | kfree(ins_data); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3896 | |
| 3897 | /* |
| 3898 | * we have to do this after the loop above to avoid changing the |
| 3899 | * log tree while trying to change the log tree. |
| 3900 | */ |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3901 | ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3902 | while (!list_empty(&ordered_sums)) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3903 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, |
| 3904 | struct btrfs_ordered_sum, |
| 3905 | list); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3906 | if (!ret) |
| 3907 | ret = btrfs_csum_file_blocks(trans, log, sums); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3908 | list_del(&sums->list); |
| 3909 | kfree(sums); |
| 3910 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3911 | |
| 3912 | if (!has_extents) |
| 3913 | return ret; |
| 3914 | |
Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 3915 | if (need_find_last_extent && *last_extent == first_key.offset) { |
| 3916 | /* |
| 3917 | * We don't have any leafs between our current one and the one |
| 3918 | * we processed before that can have file extent items for our |
| 3919 | * inode (and have a generation number smaller than our current |
| 3920 | * transaction id). |
| 3921 | */ |
| 3922 | need_find_last_extent = false; |
| 3923 | } |
| 3924 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3925 | /* |
| 3926 | * Because we use btrfs_search_forward we could skip leaves that were |
| 3927 | * not modified and then assume *last_extent is valid when it really |
| 3928 | * isn't. So back up to the previous leaf and read the end of the last |
| 3929 | * extent before we go and fill in holes. |
| 3930 | */ |
| 3931 | if (need_find_last_extent) { |
| 3932 | u64 len; |
| 3933 | |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3934 | ret = btrfs_prev_leaf(inode->root, src_path); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3935 | if (ret < 0) |
| 3936 | return ret; |
| 3937 | if (ret) |
| 3938 | goto fill_holes; |
| 3939 | if (src_path->slots[0]) |
| 3940 | src_path->slots[0]--; |
| 3941 | src = src_path->nodes[0]; |
| 3942 | btrfs_item_key_to_cpu(src, &key, src_path->slots[0]); |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3943 | if (key.objectid != btrfs_ino(inode) || |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3944 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 3945 | goto fill_holes; |
| 3946 | extent = btrfs_item_ptr(src, src_path->slots[0], |
| 3947 | struct btrfs_file_extent_item); |
| 3948 | if (btrfs_file_extent_type(src, extent) == |
| 3949 | BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 3950 | len = btrfs_file_extent_inline_len(src, |
| 3951 | src_path->slots[0], |
| 3952 | extent); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3953 | *last_extent = ALIGN(key.offset + len, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3954 | fs_info->sectorsize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3955 | } else { |
| 3956 | len = btrfs_file_extent_num_bytes(src, extent); |
| 3957 | *last_extent = key.offset + len; |
| 3958 | } |
| 3959 | } |
| 3960 | fill_holes: |
| 3961 | /* So we did prev_leaf, now we need to move to the next leaf, but a few |
| 3962 | * things could have happened |
| 3963 | * |
| 3964 | * 1) A merge could have happened, so we could currently be on a leaf |
| 3965 | * that holds what we were copying in the first place. |
| 3966 | * 2) A split could have happened, and now not all of the items we want |
| 3967 | * are on the same leaf. |
| 3968 | * |
| 3969 | * So we need to adjust how we search for holes, we need to drop the |
| 3970 | * path and re-search for the first extent key we found, and then walk |
| 3971 | * forward until we hit the last one we copied. |
| 3972 | */ |
| 3973 | if (need_find_last_extent) { |
| 3974 | /* btrfs_prev_leaf could return 1 without releasing the path */ |
| 3975 | btrfs_release_path(src_path); |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3976 | ret = btrfs_search_slot(NULL, inode->root, &first_key, |
| 3977 | src_path, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3978 | if (ret < 0) |
| 3979 | return ret; |
| 3980 | ASSERT(ret == 0); |
| 3981 | src = src_path->nodes[0]; |
| 3982 | i = src_path->slots[0]; |
| 3983 | } else { |
| 3984 | i = start_slot; |
| 3985 | } |
| 3986 | |
| 3987 | /* |
| 3988 | * Ok so here we need to go through and fill in any holes we may have |
| 3989 | * to make sure that holes are punched for those areas in case they had |
| 3990 | * extents previously. |
| 3991 | */ |
| 3992 | while (!done) { |
| 3993 | u64 offset, len; |
| 3994 | u64 extent_end; |
| 3995 | |
| 3996 | if (i >= btrfs_header_nritems(src_path->nodes[0])) { |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3997 | ret = btrfs_next_leaf(inode->root, src_path); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3998 | if (ret < 0) |
| 3999 | return ret; |
| 4000 | ASSERT(ret == 0); |
| 4001 | src = src_path->nodes[0]; |
| 4002 | i = 0; |
Filipe Manana | 8434ec4 | 2018-03-26 23:59:12 +0100 | [diff] [blame] | 4003 | need_find_last_extent = true; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | btrfs_item_key_to_cpu(src, &key, i); |
| 4007 | if (!btrfs_comp_cpu_keys(&key, &last_key)) |
| 4008 | done = true; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 4009 | if (key.objectid != btrfs_ino(inode) || |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4010 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 4011 | i++; |
| 4012 | continue; |
| 4013 | } |
| 4014 | extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item); |
| 4015 | if (btrfs_file_extent_type(src, extent) == |
| 4016 | BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4017 | len = btrfs_file_extent_inline_len(src, i, extent); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4018 | extent_end = ALIGN(key.offset + len, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4019 | fs_info->sectorsize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4020 | } else { |
| 4021 | len = btrfs_file_extent_num_bytes(src, extent); |
| 4022 | extent_end = key.offset + len; |
| 4023 | } |
| 4024 | i++; |
| 4025 | |
| 4026 | if (*last_extent == key.offset) { |
| 4027 | *last_extent = extent_end; |
| 4028 | continue; |
| 4029 | } |
| 4030 | offset = *last_extent; |
| 4031 | len = key.offset - *last_extent; |
Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 4032 | ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode), |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4033 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4034 | if (ret) |
| 4035 | break; |
Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 4036 | *last_extent = extent_end; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4037 | } |
Filipe Manana | 4ee3fad | 2018-03-26 23:59:00 +0100 | [diff] [blame] | 4038 | |
| 4039 | /* |
| 4040 | * Check if there is a hole between the last extent found in our leaf |
| 4041 | * and the first extent in the next leaf. If there is one, we need to |
| 4042 | * log an explicit hole so that at replay time we can punch the hole. |
| 4043 | */ |
| 4044 | if (ret == 0 && |
| 4045 | key.objectid == btrfs_ino(inode) && |
| 4046 | key.type == BTRFS_EXTENT_DATA_KEY && |
| 4047 | i == btrfs_header_nritems(src_path->nodes[0])) { |
| 4048 | ret = btrfs_next_leaf(inode->root, src_path); |
| 4049 | need_find_last_extent = true; |
| 4050 | if (ret > 0) { |
| 4051 | ret = 0; |
| 4052 | } else if (ret == 0) { |
| 4053 | btrfs_item_key_to_cpu(src_path->nodes[0], &key, |
| 4054 | src_path->slots[0]); |
| 4055 | if (key.objectid == btrfs_ino(inode) && |
| 4056 | key.type == BTRFS_EXTENT_DATA_KEY && |
| 4057 | *last_extent < key.offset) { |
| 4058 | const u64 len = key.offset - *last_extent; |
| 4059 | |
| 4060 | ret = btrfs_insert_file_extent(trans, log, |
| 4061 | btrfs_ino(inode), |
| 4062 | *last_extent, 0, |
| 4063 | 0, len, 0, len, |
| 4064 | 0, 0, 0); |
| 4065 | } |
| 4066 | } |
| 4067 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4068 | /* |
| 4069 | * Need to let the callers know we dropped the path so they should |
| 4070 | * re-search. |
| 4071 | */ |
| 4072 | if (!ret && need_find_last_extent) |
| 4073 | ret = 1; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4074 | return ret; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4075 | } |
| 4076 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4077 | static int extent_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 4078 | { |
| 4079 | struct extent_map *em1, *em2; |
| 4080 | |
| 4081 | em1 = list_entry(a, struct extent_map, list); |
| 4082 | em2 = list_entry(b, struct extent_map, list); |
| 4083 | |
| 4084 | if (em1->start < em2->start) |
| 4085 | return -1; |
| 4086 | else if (em1->start > em2->start) |
| 4087 | return 1; |
| 4088 | return 0; |
| 4089 | } |
| 4090 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4091 | static int wait_ordered_extents(struct btrfs_trans_handle *trans, |
| 4092 | struct inode *inode, |
| 4093 | struct btrfs_root *root, |
| 4094 | const struct extent_map *em, |
| 4095 | const struct list_head *logged_list, |
| 4096 | bool *ordered_io_error) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4097 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4098 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4099 | struct btrfs_ordered_extent *ordered; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4100 | struct btrfs_root *log = root->log_root; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4101 | u64 mod_start = em->mod_start; |
| 4102 | u64 mod_len = em->mod_len; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4103 | const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4104 | u64 csum_offset; |
| 4105 | u64 csum_len; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4106 | LIST_HEAD(ordered_sums); |
| 4107 | int ret = 0; |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4108 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4109 | *ordered_io_error = false; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4110 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4111 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 4112 | em->block_start == EXTENT_MAP_HOLE) |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4113 | return 0; |
| 4114 | |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4115 | /* |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4116 | * Wait far any ordered extent that covers our extent map. If it |
| 4117 | * finishes without an error, first check and see if our csums are on |
| 4118 | * our outstanding ordered extents. |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4119 | */ |
Miao Xie | 827463c | 2014-01-14 20:31:51 +0800 | [diff] [blame] | 4120 | list_for_each_entry(ordered, logged_list, log_list) { |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4121 | struct btrfs_ordered_sum *sum; |
| 4122 | |
| 4123 | if (!mod_len) |
| 4124 | break; |
| 4125 | |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4126 | if (ordered->file_offset + ordered->len <= mod_start || |
| 4127 | mod_start + mod_len <= ordered->file_offset) |
| 4128 | continue; |
| 4129 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4130 | if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) && |
| 4131 | !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) && |
| 4132 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) { |
| 4133 | const u64 start = ordered->file_offset; |
| 4134 | const u64 end = ordered->file_offset + ordered->len - 1; |
| 4135 | |
| 4136 | WARN_ON(ordered->inode != inode); |
| 4137 | filemap_fdatawrite_range(inode->i_mapping, start, end); |
| 4138 | } |
| 4139 | |
| 4140 | wait_event(ordered->wait, |
| 4141 | (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) || |
| 4142 | test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))); |
| 4143 | |
| 4144 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) { |
Filipe Manana | b38ef71 | 2014-11-13 17:01:45 +0000 | [diff] [blame] | 4145 | /* |
| 4146 | * Clear the AS_EIO/AS_ENOSPC flags from the inode's |
| 4147 | * i_mapping flags, so that the next fsync won't get |
| 4148 | * an outdated io error too. |
| 4149 | */ |
Miklos Szeredi | f031221 | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 4150 | filemap_check_errors(inode->i_mapping); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4151 | *ordered_io_error = true; |
| 4152 | break; |
| 4153 | } |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4154 | /* |
| 4155 | * We are going to copy all the csums on this ordered extent, so |
| 4156 | * go ahead and adjust mod_start and mod_len in case this |
| 4157 | * ordered extent has already been logged. |
| 4158 | */ |
| 4159 | if (ordered->file_offset > mod_start) { |
| 4160 | if (ordered->file_offset + ordered->len >= |
| 4161 | mod_start + mod_len) |
| 4162 | mod_len = ordered->file_offset - mod_start; |
| 4163 | /* |
| 4164 | * If we have this case |
| 4165 | * |
| 4166 | * |--------- logged extent ---------| |
| 4167 | * |----- ordered extent ----| |
| 4168 | * |
| 4169 | * Just don't mess with mod_start and mod_len, we'll |
| 4170 | * just end up logging more csums than we need and it |
| 4171 | * will be ok. |
| 4172 | */ |
| 4173 | } else { |
| 4174 | if (ordered->file_offset + ordered->len < |
| 4175 | mod_start + mod_len) { |
| 4176 | mod_len = (mod_start + mod_len) - |
| 4177 | (ordered->file_offset + ordered->len); |
| 4178 | mod_start = ordered->file_offset + |
| 4179 | ordered->len; |
| 4180 | } else { |
| 4181 | mod_len = 0; |
| 4182 | } |
| 4183 | } |
| 4184 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4185 | if (skip_csum) |
| 4186 | continue; |
| 4187 | |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4188 | /* |
| 4189 | * To keep us from looping for the above case of an ordered |
| 4190 | * extent that falls inside of the logged extent. |
| 4191 | */ |
| 4192 | if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, |
| 4193 | &ordered->flags)) |
| 4194 | continue; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4195 | |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4196 | list_for_each_entry(sum, &ordered->list, list) { |
| 4197 | ret = btrfs_csum_file_blocks(trans, log, sum); |
Miao Xie | 827463c | 2014-01-14 20:31:51 +0800 | [diff] [blame] | 4198 | if (ret) |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4199 | break; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4200 | } |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4201 | } |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4202 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4203 | if (*ordered_io_error || !mod_len || ret || skip_csum) |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4204 | return ret; |
| 4205 | |
Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4206 | if (em->compress_type) { |
| 4207 | csum_offset = 0; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4208 | csum_len = max(em->block_len, em->orig_block_len); |
Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4209 | } else { |
| 4210 | csum_offset = mod_start - em->start; |
| 4211 | csum_len = mod_len; |
| 4212 | } |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4213 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4214 | /* block start is already adjusted for the file extent offset. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4215 | ret = btrfs_lookup_csums_range(fs_info->csum_root, |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4216 | em->block_start + csum_offset, |
| 4217 | em->block_start + csum_offset + |
| 4218 | csum_len - 1, &ordered_sums, 0); |
| 4219 | if (ret) |
| 4220 | return ret; |
| 4221 | |
| 4222 | while (!list_empty(&ordered_sums)) { |
| 4223 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, |
| 4224 | struct btrfs_ordered_sum, |
| 4225 | list); |
| 4226 | if (!ret) |
| 4227 | ret = btrfs_csum_file_blocks(trans, log, sums); |
| 4228 | list_del(&sums->list); |
| 4229 | kfree(sums); |
| 4230 | } |
| 4231 | |
| 4232 | return ret; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4233 | } |
| 4234 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4235 | static int log_one_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4236 | struct btrfs_inode *inode, struct btrfs_root *root, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4237 | const struct extent_map *em, |
| 4238 | struct btrfs_path *path, |
| 4239 | const struct list_head *logged_list, |
| 4240 | struct btrfs_log_ctx *ctx) |
| 4241 | { |
| 4242 | struct btrfs_root *log = root->log_root; |
| 4243 | struct btrfs_file_extent_item *fi; |
| 4244 | struct extent_buffer *leaf; |
| 4245 | struct btrfs_map_token token; |
| 4246 | struct btrfs_key key; |
| 4247 | u64 extent_offset = em->start - em->orig_start; |
| 4248 | u64 block_len; |
| 4249 | int ret; |
| 4250 | int extent_inserted = 0; |
| 4251 | bool ordered_io_err = false; |
| 4252 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4253 | ret = wait_ordered_extents(trans, &inode->vfs_inode, root, em, |
| 4254 | logged_list, &ordered_io_err); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4255 | if (ret) |
| 4256 | return ret; |
| 4257 | |
| 4258 | if (ordered_io_err) { |
| 4259 | ctx->io_err = -EIO; |
Liu Bo | ebb7044 | 2017-11-21 14:35:40 -0700 | [diff] [blame] | 4260 | return ctx->io_err; |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4261 | } |
| 4262 | |
| 4263 | btrfs_init_map_token(&token); |
| 4264 | |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4265 | ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4266 | em->start + em->len, NULL, 0, 1, |
| 4267 | sizeof(*fi), &extent_inserted); |
| 4268 | if (ret) |
| 4269 | return ret; |
| 4270 | |
| 4271 | if (!extent_inserted) { |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4272 | key.objectid = btrfs_ino(inode); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4273 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4274 | key.offset = em->start; |
| 4275 | |
| 4276 | ret = btrfs_insert_empty_item(trans, log, path, &key, |
| 4277 | sizeof(*fi)); |
| 4278 | if (ret) |
| 4279 | return ret; |
| 4280 | } |
| 4281 | leaf = path->nodes[0]; |
| 4282 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4283 | struct btrfs_file_extent_item); |
| 4284 | |
Josef Bacik | 50d9aa9 | 2014-11-21 14:52:38 -0500 | [diff] [blame] | 4285 | btrfs_set_token_file_extent_generation(leaf, fi, trans->transid, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4286 | &token); |
| 4287 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 4288 | btrfs_set_token_file_extent_type(leaf, fi, |
| 4289 | BTRFS_FILE_EXTENT_PREALLOC, |
| 4290 | &token); |
| 4291 | else |
| 4292 | btrfs_set_token_file_extent_type(leaf, fi, |
| 4293 | BTRFS_FILE_EXTENT_REG, |
| 4294 | &token); |
| 4295 | |
| 4296 | block_len = max(em->block_len, em->orig_block_len); |
| 4297 | if (em->compress_type != BTRFS_COMPRESS_NONE) { |
| 4298 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, |
| 4299 | em->block_start, |
| 4300 | &token); |
| 4301 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len, |
| 4302 | &token); |
| 4303 | } else if (em->block_start < EXTENT_MAP_LAST_BYTE) { |
| 4304 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, |
| 4305 | em->block_start - |
| 4306 | extent_offset, &token); |
| 4307 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len, |
| 4308 | &token); |
| 4309 | } else { |
| 4310 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token); |
| 4311 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0, |
| 4312 | &token); |
| 4313 | } |
| 4314 | |
| 4315 | btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token); |
| 4316 | btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token); |
| 4317 | btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token); |
| 4318 | btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type, |
| 4319 | &token); |
| 4320 | btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token); |
| 4321 | btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token); |
| 4322 | btrfs_mark_buffer_dirty(leaf); |
| 4323 | |
| 4324 | btrfs_release_path(path); |
| 4325 | |
| 4326 | return ret; |
| 4327 | } |
| 4328 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4329 | /* |
| 4330 | * Log all prealloc extents beyond the inode's i_size to make sure we do not |
| 4331 | * lose them after doing a fast fsync and replaying the log. We scan the |
| 4332 | * subvolume's root instead of iterating the inode's extent map tree because |
| 4333 | * otherwise we can log incorrect extent items based on extent map conversion. |
| 4334 | * That can happen due to the fact that extent maps are merged when they |
| 4335 | * are not in the extent map tree's list of modified extents. |
| 4336 | */ |
| 4337 | static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans, |
| 4338 | struct btrfs_inode *inode, |
| 4339 | struct btrfs_path *path) |
| 4340 | { |
| 4341 | struct btrfs_root *root = inode->root; |
| 4342 | struct btrfs_key key; |
| 4343 | const u64 i_size = i_size_read(&inode->vfs_inode); |
| 4344 | const u64 ino = btrfs_ino(inode); |
| 4345 | struct btrfs_path *dst_path = NULL; |
| 4346 | u64 last_extent = (u64)-1; |
| 4347 | int ins_nr = 0; |
| 4348 | int start_slot; |
| 4349 | int ret; |
| 4350 | |
| 4351 | if (!(inode->flags & BTRFS_INODE_PREALLOC)) |
| 4352 | return 0; |
| 4353 | |
| 4354 | key.objectid = ino; |
| 4355 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4356 | key.offset = i_size; |
| 4357 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4358 | if (ret < 0) |
| 4359 | goto out; |
| 4360 | |
| 4361 | while (true) { |
| 4362 | struct extent_buffer *leaf = path->nodes[0]; |
| 4363 | int slot = path->slots[0]; |
| 4364 | |
| 4365 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4366 | if (ins_nr > 0) { |
| 4367 | ret = copy_items(trans, inode, dst_path, path, |
| 4368 | &last_extent, start_slot, |
| 4369 | ins_nr, 1, 0); |
| 4370 | if (ret < 0) |
| 4371 | goto out; |
| 4372 | ins_nr = 0; |
| 4373 | } |
| 4374 | ret = btrfs_next_leaf(root, path); |
| 4375 | if (ret < 0) |
| 4376 | goto out; |
| 4377 | if (ret > 0) { |
| 4378 | ret = 0; |
| 4379 | break; |
| 4380 | } |
| 4381 | continue; |
| 4382 | } |
| 4383 | |
| 4384 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 4385 | if (key.objectid > ino) |
| 4386 | break; |
| 4387 | if (WARN_ON_ONCE(key.objectid < ino) || |
| 4388 | key.type < BTRFS_EXTENT_DATA_KEY || |
| 4389 | key.offset < i_size) { |
| 4390 | path->slots[0]++; |
| 4391 | continue; |
| 4392 | } |
| 4393 | if (last_extent == (u64)-1) { |
| 4394 | last_extent = key.offset; |
| 4395 | /* |
| 4396 | * Avoid logging extent items logged in past fsync calls |
| 4397 | * and leading to duplicate keys in the log tree. |
| 4398 | */ |
| 4399 | do { |
| 4400 | ret = btrfs_truncate_inode_items(trans, |
| 4401 | root->log_root, |
| 4402 | &inode->vfs_inode, |
| 4403 | i_size, |
| 4404 | BTRFS_EXTENT_DATA_KEY); |
| 4405 | } while (ret == -EAGAIN); |
| 4406 | if (ret) |
| 4407 | goto out; |
| 4408 | } |
| 4409 | if (ins_nr == 0) |
| 4410 | start_slot = slot; |
| 4411 | ins_nr++; |
| 4412 | path->slots[0]++; |
| 4413 | if (!dst_path) { |
| 4414 | dst_path = btrfs_alloc_path(); |
| 4415 | if (!dst_path) { |
| 4416 | ret = -ENOMEM; |
| 4417 | goto out; |
| 4418 | } |
| 4419 | } |
| 4420 | } |
| 4421 | if (ins_nr > 0) { |
| 4422 | ret = copy_items(trans, inode, dst_path, path, &last_extent, |
| 4423 | start_slot, ins_nr, 1, 0); |
| 4424 | if (ret > 0) |
| 4425 | ret = 0; |
| 4426 | } |
| 4427 | out: |
| 4428 | btrfs_release_path(path); |
| 4429 | btrfs_free_path(dst_path); |
| 4430 | return ret; |
| 4431 | } |
| 4432 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4433 | static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, |
| 4434 | struct btrfs_root *root, |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4435 | struct btrfs_inode *inode, |
Miao Xie | 827463c | 2014-01-14 20:31:51 +0800 | [diff] [blame] | 4436 | struct btrfs_path *path, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4437 | struct list_head *logged_list, |
Filipe Manana | de0ee0e | 2016-01-21 10:17:54 +0000 | [diff] [blame] | 4438 | struct btrfs_log_ctx *ctx, |
| 4439 | const u64 start, |
| 4440 | const u64 end) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4441 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4442 | struct extent_map *em, *n; |
| 4443 | struct list_head extents; |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4444 | struct extent_map_tree *tree = &inode->extent_tree; |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4445 | u64 logged_start, logged_end; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4446 | u64 test_gen; |
| 4447 | int ret = 0; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4448 | int num = 0; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4449 | |
| 4450 | INIT_LIST_HEAD(&extents); |
| 4451 | |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4452 | down_write(&inode->dio_sem); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4453 | write_lock(&tree->lock); |
| 4454 | test_gen = root->fs_info->last_trans_committed; |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4455 | logged_start = start; |
| 4456 | logged_end = end; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4457 | |
| 4458 | list_for_each_entry_safe(em, n, &tree->modified_extents, list) { |
| 4459 | list_del_init(&em->list); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4460 | /* |
| 4461 | * Just an arbitrary number, this can be really CPU intensive |
| 4462 | * once we start getting a lot of extents, and really once we |
| 4463 | * have a bunch of extents we just want to commit since it will |
| 4464 | * be faster. |
| 4465 | */ |
| 4466 | if (++num > 32768) { |
| 4467 | list_del_init(&tree->modified_extents); |
| 4468 | ret = -EFBIG; |
| 4469 | goto process; |
| 4470 | } |
| 4471 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4472 | if (em->generation <= test_gen) |
| 4473 | continue; |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4474 | |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4475 | /* We log prealloc extents beyond eof later. */ |
| 4476 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && |
| 4477 | em->start >= i_size_read(&inode->vfs_inode)) |
| 4478 | continue; |
| 4479 | |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4480 | if (em->start < logged_start) |
| 4481 | logged_start = em->start; |
| 4482 | if ((em->start + em->len - 1) > logged_end) |
| 4483 | logged_end = em->start + em->len - 1; |
| 4484 | |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4485 | /* Need a ref to keep it from getting evicted from cache */ |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 4486 | refcount_inc(&em->refs); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4487 | set_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4488 | list_add_tail(&em->list, &extents); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4489 | num++; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | list_sort(NULL, &extents, extent_cmp); |
Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4493 | btrfs_get_logged_extents(inode, logged_list, logged_start, logged_end); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 4494 | /* |
| 4495 | * Some ordered extents started by fsync might have completed |
| 4496 | * before we could collect them into the list logged_list, which |
| 4497 | * means they're gone, not in our logged_list nor in the inode's |
| 4498 | * ordered tree. We want the application/user space to know an |
| 4499 | * error happened while attempting to persist file data so that |
| 4500 | * it can take proper action. If such error happened, we leave |
| 4501 | * without writing to the log tree and the fsync must report the |
| 4502 | * file data write error and not commit the current transaction. |
| 4503 | */ |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4504 | ret = filemap_check_errors(inode->vfs_inode.i_mapping); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 4505 | if (ret) |
| 4506 | ctx->io_err = ret; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4507 | process: |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4508 | while (!list_empty(&extents)) { |
| 4509 | em = list_entry(extents.next, struct extent_map, list); |
| 4510 | |
| 4511 | list_del_init(&em->list); |
| 4512 | |
| 4513 | /* |
| 4514 | * If we had an error we just need to delete everybody from our |
| 4515 | * private list. |
| 4516 | */ |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4517 | if (ret) { |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 4518 | clear_em_logging(tree, em); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4519 | free_extent_map(em); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4520 | continue; |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | write_unlock(&tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4524 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4525 | ret = log_one_extent(trans, inode, root, em, path, logged_list, |
| 4526 | ctx); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4527 | write_lock(&tree->lock); |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 4528 | clear_em_logging(tree, em); |
| 4529 | free_extent_map(em); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4530 | } |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4531 | WARN_ON(!list_empty(&extents)); |
| 4532 | write_unlock(&tree->lock); |
Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4533 | up_write(&inode->dio_sem); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4534 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4535 | btrfs_release_path(path); |
Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4536 | if (!ret) |
| 4537 | ret = btrfs_log_prealloc_extents(trans, inode, path); |
| 4538 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4539 | return ret; |
| 4540 | } |
| 4541 | |
Nikolay Borisov | 481b01c | 2017-01-18 00:31:34 +0200 | [diff] [blame] | 4542 | 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] | 4543 | struct btrfs_path *path, u64 *size_ret) |
| 4544 | { |
| 4545 | struct btrfs_key key; |
| 4546 | int ret; |
| 4547 | |
Nikolay Borisov | 481b01c | 2017-01-18 00:31:34 +0200 | [diff] [blame] | 4548 | key.objectid = btrfs_ino(inode); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4549 | key.type = BTRFS_INODE_ITEM_KEY; |
| 4550 | key.offset = 0; |
| 4551 | |
| 4552 | ret = btrfs_search_slot(NULL, log, &key, path, 0, 0); |
| 4553 | if (ret < 0) { |
| 4554 | return ret; |
| 4555 | } else if (ret > 0) { |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 4556 | *size_ret = 0; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4557 | } else { |
| 4558 | struct btrfs_inode_item *item; |
| 4559 | |
| 4560 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4561 | struct btrfs_inode_item); |
| 4562 | *size_ret = btrfs_inode_size(path->nodes[0], item); |
| 4563 | } |
| 4564 | |
| 4565 | btrfs_release_path(path); |
| 4566 | return 0; |
| 4567 | } |
| 4568 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4569 | /* |
| 4570 | * At the moment we always log all xattrs. This is to figure out at log replay |
| 4571 | * time which xattrs must have their deletion replayed. If a xattr is missing |
| 4572 | * in the log tree and exists in the fs/subvol tree, we delete it. This is |
| 4573 | * because if a xattr is deleted, the inode is fsynced and a power failure |
| 4574 | * happens, causing the log to be replayed the next time the fs is mounted, |
| 4575 | * we want the xattr to not exist anymore (same behaviour as other filesystems |
| 4576 | * with a journal, ext3/4, xfs, f2fs, etc). |
| 4577 | */ |
| 4578 | static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans, |
| 4579 | struct btrfs_root *root, |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4580 | struct btrfs_inode *inode, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4581 | struct btrfs_path *path, |
| 4582 | struct btrfs_path *dst_path) |
| 4583 | { |
| 4584 | int ret; |
| 4585 | struct btrfs_key key; |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4586 | const u64 ino = btrfs_ino(inode); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4587 | int ins_nr = 0; |
| 4588 | int start_slot = 0; |
| 4589 | |
| 4590 | key.objectid = ino; |
| 4591 | key.type = BTRFS_XATTR_ITEM_KEY; |
| 4592 | key.offset = 0; |
| 4593 | |
| 4594 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4595 | if (ret < 0) |
| 4596 | return ret; |
| 4597 | |
| 4598 | while (true) { |
| 4599 | int slot = path->slots[0]; |
| 4600 | struct extent_buffer *leaf = path->nodes[0]; |
| 4601 | int nritems = btrfs_header_nritems(leaf); |
| 4602 | |
| 4603 | if (slot >= nritems) { |
| 4604 | if (ins_nr > 0) { |
| 4605 | u64 last_extent = 0; |
| 4606 | |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4607 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4608 | &last_extent, start_slot, |
| 4609 | ins_nr, 1, 0); |
| 4610 | /* can't be 1, extent items aren't processed */ |
| 4611 | ASSERT(ret <= 0); |
| 4612 | if (ret < 0) |
| 4613 | return ret; |
| 4614 | ins_nr = 0; |
| 4615 | } |
| 4616 | ret = btrfs_next_leaf(root, path); |
| 4617 | if (ret < 0) |
| 4618 | return ret; |
| 4619 | else if (ret > 0) |
| 4620 | break; |
| 4621 | continue; |
| 4622 | } |
| 4623 | |
| 4624 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 4625 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) |
| 4626 | break; |
| 4627 | |
| 4628 | if (ins_nr == 0) |
| 4629 | start_slot = slot; |
| 4630 | ins_nr++; |
| 4631 | path->slots[0]++; |
| 4632 | cond_resched(); |
| 4633 | } |
| 4634 | if (ins_nr > 0) { |
| 4635 | u64 last_extent = 0; |
| 4636 | |
Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4637 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4638 | &last_extent, start_slot, |
| 4639 | ins_nr, 1, 0); |
| 4640 | /* can't be 1, extent items aren't processed */ |
| 4641 | ASSERT(ret <= 0); |
| 4642 | if (ret < 0) |
| 4643 | return ret; |
| 4644 | } |
| 4645 | |
| 4646 | return 0; |
| 4647 | } |
| 4648 | |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4649 | /* |
| 4650 | * If the no holes feature is enabled we need to make sure any hole between the |
| 4651 | * last extent and the i_size of our inode is explicitly marked in the log. This |
| 4652 | * is to make sure that doing something like: |
| 4653 | * |
| 4654 | * 1) create file with 128Kb of data |
| 4655 | * 2) truncate file to 64Kb |
| 4656 | * 3) truncate file to 256Kb |
| 4657 | * 4) fsync file |
| 4658 | * 5) <crash/power failure> |
| 4659 | * 6) mount fs and trigger log replay |
| 4660 | * |
| 4661 | * Will give us a file with a size of 256Kb, the first 64Kb of data match what |
| 4662 | * the file had in its first 64Kb of data at step 1 and the last 192Kb of the |
| 4663 | * file correspond to a hole. The presence of explicit holes in a log tree is |
| 4664 | * what guarantees that log replay will remove/adjust file extent items in the |
| 4665 | * fs/subvol tree. |
| 4666 | * |
| 4667 | * Here we do not need to care about holes between extents, that is already done |
| 4668 | * by copy_items(). We also only need to do this in the full sync path, where we |
| 4669 | * lookup for extents from the fs/subvol tree only. In the fast path case, we |
| 4670 | * lookup the list of modified extent maps and if any represents a hole, we |
| 4671 | * insert a corresponding extent representing a hole in the log tree. |
| 4672 | */ |
| 4673 | static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans, |
| 4674 | struct btrfs_root *root, |
Nikolay Borisov | a0308dd | 2017-01-18 00:31:38 +0200 | [diff] [blame] | 4675 | struct btrfs_inode *inode, |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4676 | struct btrfs_path *path) |
| 4677 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4678 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4679 | int ret; |
| 4680 | struct btrfs_key key; |
| 4681 | u64 hole_start; |
| 4682 | u64 hole_size; |
| 4683 | struct extent_buffer *leaf; |
| 4684 | struct btrfs_root *log = root->log_root; |
Nikolay Borisov | a0308dd | 2017-01-18 00:31:38 +0200 | [diff] [blame] | 4685 | const u64 ino = btrfs_ino(inode); |
| 4686 | const u64 i_size = i_size_read(&inode->vfs_inode); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4687 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4688 | if (!btrfs_fs_incompat(fs_info, NO_HOLES)) |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4689 | return 0; |
| 4690 | |
| 4691 | key.objectid = ino; |
| 4692 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4693 | key.offset = (u64)-1; |
| 4694 | |
| 4695 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4696 | ASSERT(ret != 0); |
| 4697 | if (ret < 0) |
| 4698 | return ret; |
| 4699 | |
| 4700 | ASSERT(path->slots[0] > 0); |
| 4701 | path->slots[0]--; |
| 4702 | leaf = path->nodes[0]; |
| 4703 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4704 | |
| 4705 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) { |
| 4706 | /* inode does not have any extents */ |
| 4707 | hole_start = 0; |
| 4708 | hole_size = i_size; |
| 4709 | } else { |
| 4710 | struct btrfs_file_extent_item *extent; |
| 4711 | u64 len; |
| 4712 | |
| 4713 | /* |
| 4714 | * If there's an extent beyond i_size, an explicit hole was |
| 4715 | * already inserted by copy_items(). |
| 4716 | */ |
| 4717 | if (key.offset >= i_size) |
| 4718 | return 0; |
| 4719 | |
| 4720 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 4721 | struct btrfs_file_extent_item); |
| 4722 | |
| 4723 | if (btrfs_file_extent_type(leaf, extent) == |
| 4724 | BTRFS_FILE_EXTENT_INLINE) { |
| 4725 | len = btrfs_file_extent_inline_len(leaf, |
| 4726 | path->slots[0], |
| 4727 | extent); |
Filipe Manana | 6399fb5 | 2017-07-28 15:22:36 +0100 | [diff] [blame] | 4728 | ASSERT(len == i_size || |
| 4729 | (len == fs_info->sectorsize && |
| 4730 | btrfs_file_extent_compression(leaf, extent) != |
| 4731 | BTRFS_COMPRESS_NONE)); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4732 | return 0; |
| 4733 | } |
| 4734 | |
| 4735 | len = btrfs_file_extent_num_bytes(leaf, extent); |
| 4736 | /* Last extent goes beyond i_size, no need to log a hole. */ |
| 4737 | if (key.offset + len > i_size) |
| 4738 | return 0; |
| 4739 | hole_start = key.offset + len; |
| 4740 | hole_size = i_size - hole_start; |
| 4741 | } |
| 4742 | btrfs_release_path(path); |
| 4743 | |
| 4744 | /* Last extent ends at i_size. */ |
| 4745 | if (hole_size == 0) |
| 4746 | return 0; |
| 4747 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4748 | hole_size = ALIGN(hole_size, fs_info->sectorsize); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4749 | ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0, |
| 4750 | hole_size, 0, hole_size, 0, 0, 0); |
| 4751 | return ret; |
| 4752 | } |
| 4753 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4754 | /* |
| 4755 | * When we are logging a new inode X, check if it doesn't have a reference that |
| 4756 | * matches the reference from some other inode Y created in a past transaction |
| 4757 | * and that was renamed in the current transaction. If we don't do this, then at |
| 4758 | * log replay time we can lose inode Y (and all its files if it's a directory): |
| 4759 | * |
| 4760 | * mkdir /mnt/x |
| 4761 | * echo "hello world" > /mnt/x/foobar |
| 4762 | * sync |
| 4763 | * mv /mnt/x /mnt/y |
| 4764 | * mkdir /mnt/x # or touch /mnt/x |
| 4765 | * xfs_io -c fsync /mnt/x |
| 4766 | * <power fail> |
| 4767 | * mount fs, trigger log replay |
| 4768 | * |
| 4769 | * After the log replay procedure, we would lose the first directory and all its |
| 4770 | * files (file foobar). |
| 4771 | * For the case where inode Y is not a directory we simply end up losing it: |
| 4772 | * |
| 4773 | * echo "123" > /mnt/foo |
| 4774 | * sync |
| 4775 | * mv /mnt/foo /mnt/bar |
| 4776 | * echo "abc" > /mnt/foo |
| 4777 | * xfs_io -c fsync /mnt/foo |
| 4778 | * <power fail> |
| 4779 | * |
| 4780 | * We also need this for cases where a snapshot entry is replaced by some other |
| 4781 | * entry (file or directory) otherwise we end up with an unreplayable log due to |
| 4782 | * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as |
| 4783 | * if it were a regular entry: |
| 4784 | * |
| 4785 | * mkdir /mnt/x |
| 4786 | * btrfs subvolume snapshot /mnt /mnt/x/snap |
| 4787 | * btrfs subvolume delete /mnt/x/snap |
| 4788 | * rmdir /mnt/x |
| 4789 | * mkdir /mnt/x |
| 4790 | * fsync /mnt/x or fsync some new file inside it |
| 4791 | * <power fail> |
| 4792 | * |
| 4793 | * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in |
| 4794 | * the same transaction. |
| 4795 | */ |
| 4796 | static int btrfs_check_ref_name_override(struct extent_buffer *eb, |
| 4797 | const int slot, |
| 4798 | const struct btrfs_key *key, |
Nikolay Borisov | 4791c8f | 2017-01-18 00:31:35 +0200 | [diff] [blame] | 4799 | struct btrfs_inode *inode, |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4800 | u64 *other_ino) |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4801 | { |
| 4802 | int ret; |
| 4803 | struct btrfs_path *search_path; |
| 4804 | char *name = NULL; |
| 4805 | u32 name_len = 0; |
| 4806 | u32 item_size = btrfs_item_size_nr(eb, slot); |
| 4807 | u32 cur_offset = 0; |
| 4808 | unsigned long ptr = btrfs_item_ptr_offset(eb, slot); |
| 4809 | |
| 4810 | search_path = btrfs_alloc_path(); |
| 4811 | if (!search_path) |
| 4812 | return -ENOMEM; |
| 4813 | search_path->search_commit_root = 1; |
| 4814 | search_path->skip_locking = 1; |
| 4815 | |
| 4816 | while (cur_offset < item_size) { |
| 4817 | u64 parent; |
| 4818 | u32 this_name_len; |
| 4819 | u32 this_len; |
| 4820 | unsigned long name_ptr; |
| 4821 | struct btrfs_dir_item *di; |
| 4822 | |
| 4823 | if (key->type == BTRFS_INODE_REF_KEY) { |
| 4824 | struct btrfs_inode_ref *iref; |
| 4825 | |
| 4826 | iref = (struct btrfs_inode_ref *)(ptr + cur_offset); |
| 4827 | parent = key->offset; |
| 4828 | this_name_len = btrfs_inode_ref_name_len(eb, iref); |
| 4829 | name_ptr = (unsigned long)(iref + 1); |
| 4830 | this_len = sizeof(*iref) + this_name_len; |
| 4831 | } else { |
| 4832 | struct btrfs_inode_extref *extref; |
| 4833 | |
| 4834 | extref = (struct btrfs_inode_extref *)(ptr + |
| 4835 | cur_offset); |
| 4836 | parent = btrfs_inode_extref_parent(eb, extref); |
| 4837 | this_name_len = btrfs_inode_extref_name_len(eb, extref); |
| 4838 | name_ptr = (unsigned long)&extref->name; |
| 4839 | this_len = sizeof(*extref) + this_name_len; |
| 4840 | } |
| 4841 | |
| 4842 | if (this_name_len > name_len) { |
| 4843 | char *new_name; |
| 4844 | |
| 4845 | new_name = krealloc(name, this_name_len, GFP_NOFS); |
| 4846 | if (!new_name) { |
| 4847 | ret = -ENOMEM; |
| 4848 | goto out; |
| 4849 | } |
| 4850 | name_len = this_name_len; |
| 4851 | name = new_name; |
| 4852 | } |
| 4853 | |
| 4854 | read_extent_buffer(eb, name, name_ptr, this_name_len); |
Nikolay Borisov | 4791c8f | 2017-01-18 00:31:35 +0200 | [diff] [blame] | 4855 | di = btrfs_lookup_dir_item(NULL, inode->root, search_path, |
| 4856 | parent, name, this_name_len, 0); |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4857 | if (di && !IS_ERR(di)) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4858 | struct btrfs_key di_key; |
| 4859 | |
| 4860 | btrfs_dir_item_key_to_cpu(search_path->nodes[0], |
| 4861 | di, &di_key); |
| 4862 | if (di_key.type == BTRFS_INODE_ITEM_KEY) { |
| 4863 | ret = 1; |
| 4864 | *other_ino = di_key.objectid; |
| 4865 | } else { |
| 4866 | ret = -EAGAIN; |
| 4867 | } |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4868 | goto out; |
| 4869 | } else if (IS_ERR(di)) { |
| 4870 | ret = PTR_ERR(di); |
| 4871 | goto out; |
| 4872 | } |
| 4873 | btrfs_release_path(search_path); |
| 4874 | |
| 4875 | cur_offset += this_len; |
| 4876 | } |
| 4877 | ret = 0; |
| 4878 | out: |
| 4879 | btrfs_free_path(search_path); |
| 4880 | kfree(name); |
| 4881 | return ret; |
| 4882 | } |
| 4883 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4884 | /* log a single inode in the tree log. |
| 4885 | * At least one parent directory for this inode must exist in the tree |
| 4886 | * or be logged already. |
| 4887 | * |
| 4888 | * Any items from this inode changed by the current transaction are copied |
| 4889 | * to the log tree. An extra reference is taken on any extents in this |
| 4890 | * file, allowing us to avoid a whole pile of corner cases around logging |
| 4891 | * blocks that have been removed from the tree. |
| 4892 | * |
| 4893 | * See LOG_INODE_ALL and related defines for a description of what inode_only |
| 4894 | * does. |
| 4895 | * |
| 4896 | * This handles both files and directories. |
| 4897 | */ |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4898 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4899 | struct btrfs_root *root, struct btrfs_inode *inode, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 4900 | int inode_only, |
| 4901 | const loff_t start, |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4902 | const loff_t end, |
| 4903 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4904 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4905 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4906 | struct btrfs_path *path; |
| 4907 | struct btrfs_path *dst_path; |
| 4908 | struct btrfs_key min_key; |
| 4909 | struct btrfs_key max_key; |
| 4910 | struct btrfs_root *log = root->log_root; |
Miao Xie | 827463c | 2014-01-14 20:31:51 +0800 | [diff] [blame] | 4911 | LIST_HEAD(logged_list); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4912 | u64 last_extent = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4913 | int err = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4914 | int ret; |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 4915 | int nritems; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4916 | int ins_start_slot = 0; |
| 4917 | int ins_nr; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4918 | bool fast_search = false; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4919 | u64 ino = btrfs_ino(inode); |
| 4920 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4921 | u64 logged_isize = 0; |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 4922 | bool need_log_inode_item = true; |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 4923 | bool xattrs_logged = false; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4924 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4925 | path = btrfs_alloc_path(); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 4926 | if (!path) |
| 4927 | return -ENOMEM; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4928 | dst_path = btrfs_alloc_path(); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 4929 | if (!dst_path) { |
| 4930 | btrfs_free_path(path); |
| 4931 | return -ENOMEM; |
| 4932 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4933 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4934 | min_key.objectid = ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4935 | min_key.type = BTRFS_INODE_ITEM_KEY; |
| 4936 | min_key.offset = 0; |
| 4937 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4938 | max_key.objectid = ino; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4939 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4940 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4941 | /* today the code can only do partial logging of directories */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4942 | if (S_ISDIR(inode->vfs_inode.i_mode) || |
Miao Xie | 5269b67 | 2012-11-01 07:35:23 +0000 | [diff] [blame] | 4943 | (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4944 | &inode->runtime_flags) && |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4945 | inode_only >= LOG_INODE_EXISTS)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4946 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
| 4947 | else |
| 4948 | max_key.type = (u8)-1; |
| 4949 | max_key.offset = (u64)-1; |
| 4950 | |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4951 | /* |
| 4952 | * Only run delayed items if we are a dir or a new file. |
| 4953 | * Otherwise commit the delayed inode only, which is needed in |
| 4954 | * order for the log replay code to mark inodes for link count |
| 4955 | * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items). |
| 4956 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4957 | if (S_ISDIR(inode->vfs_inode.i_mode) || |
| 4958 | inode->generation > fs_info->last_trans_committed) |
| 4959 | ret = btrfs_commit_inode_delayed_items(trans, inode); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4960 | else |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4961 | ret = btrfs_commit_inode_delayed_inode(inode); |
Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4962 | |
| 4963 | if (ret) { |
| 4964 | btrfs_free_path(path); |
| 4965 | btrfs_free_path(dst_path); |
| 4966 | return ret; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4967 | } |
| 4968 | |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4969 | if (inode_only == LOG_OTHER_INODE) { |
| 4970 | inode_only = LOG_INODE_EXISTS; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4971 | mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING); |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4972 | } else { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4973 | mutex_lock(&inode->log_mutex); |
Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4974 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4975 | |
Filipe Manana | 5e33a2b | 2016-02-25 23:19:38 +0000 | [diff] [blame] | 4976 | /* |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4977 | * a brute force approach to making sure we get the most uptodate |
| 4978 | * copies of everything. |
| 4979 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4980 | if (S_ISDIR(inode->vfs_inode.i_mode)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4981 | int max_key_type = BTRFS_DIR_LOG_INDEX_KEY; |
| 4982 | |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4983 | if (inode_only == LOG_INODE_EXISTS) |
| 4984 | max_key_type = BTRFS_XATTR_ITEM_KEY; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4985 | ret = drop_objectid_items(trans, log, path, ino, max_key_type); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4986 | } else { |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4987 | if (inode_only == LOG_INODE_EXISTS) { |
| 4988 | /* |
| 4989 | * Make sure the new inode item we write to the log has |
| 4990 | * the same isize as the current one (if it exists). |
| 4991 | * This is necessary to prevent data loss after log |
| 4992 | * replay, and also to prevent doing a wrong expanding |
| 4993 | * truncate - for e.g. create file, write 4K into offset |
| 4994 | * 0, fsync, write 4K into offset 4096, add hard link, |
| 4995 | * fsync some other file (to sync log), power fail - if |
| 4996 | * we use the inode's current i_size, after log replay |
| 4997 | * we get a 8Kb file, with the last 4Kb extent as a hole |
| 4998 | * (zeroes), as if an expanding truncate happened, |
| 4999 | * instead of getting a file of 4Kb only. |
| 5000 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5001 | err = logged_inode_size(log, inode, path, &logged_isize); |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5002 | if (err) |
| 5003 | goto out_unlock; |
| 5004 | } |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 5005 | if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5006 | &inode->runtime_flags)) { |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 5007 | if (inode_only == LOG_INODE_EXISTS) { |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 5008 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 5009 | ret = drop_objectid_items(trans, log, path, ino, |
| 5010 | max_key.type); |
| 5011 | } else { |
| 5012 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5013 | &inode->runtime_flags); |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 5014 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5015 | &inode->runtime_flags); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 5016 | while(1) { |
| 5017 | ret = btrfs_truncate_inode_items(trans, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5018 | log, &inode->vfs_inode, 0, 0); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 5019 | if (ret != -EAGAIN) |
| 5020 | break; |
| 5021 | } |
Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 5022 | } |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 5023 | } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING, |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5024 | &inode->runtime_flags) || |
Josef Bacik | 6cfab85 | 2013-11-12 16:25:58 -0500 | [diff] [blame] | 5025 | inode_only == LOG_INODE_EXISTS) { |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 5026 | if (inode_only == LOG_INODE_ALL) |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 5027 | fast_search = true; |
Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 5028 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 5029 | ret = drop_objectid_items(trans, log, path, ino, |
| 5030 | max_key.type); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5031 | } else { |
Liu Bo | 183f37f | 2012-11-01 06:38:47 +0000 | [diff] [blame] | 5032 | if (inode_only == LOG_INODE_ALL) |
| 5033 | fast_search = true; |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 5034 | goto log_extents; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5035 | } |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 5036 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5037 | } |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5038 | if (ret) { |
| 5039 | err = ret; |
| 5040 | goto out_unlock; |
| 5041 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5042 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5043 | while (1) { |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5044 | ins_nr = 0; |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 5045 | ret = btrfs_search_forward(root, &min_key, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5046 | path, trans->transid); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 5047 | if (ret < 0) { |
| 5048 | err = ret; |
| 5049 | goto out_unlock; |
| 5050 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5051 | if (ret != 0) |
| 5052 | break; |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 5053 | again: |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5054 | /* note, ins_nr might be > 0 here, cleanup outside the loop */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5055 | if (min_key.objectid != ino) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5056 | break; |
| 5057 | if (min_key.type > max_key.type) |
| 5058 | break; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5059 | |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 5060 | if (min_key.type == BTRFS_INODE_ITEM_KEY) |
| 5061 | need_log_inode_item = false; |
| 5062 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5063 | if ((min_key.type == BTRFS_INODE_REF_KEY || |
| 5064 | min_key.type == BTRFS_INODE_EXTREF_KEY) && |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5065 | inode->generation == trans->transid) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5066 | u64 other_ino = 0; |
| 5067 | |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5068 | ret = btrfs_check_ref_name_override(path->nodes[0], |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5069 | path->slots[0], &min_key, inode, |
| 5070 | &other_ino); |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5071 | if (ret < 0) { |
| 5072 | err = ret; |
| 5073 | goto out_unlock; |
Filipe Manana | 28a2359 | 2016-08-23 21:13:51 +0100 | [diff] [blame] | 5074 | } else if (ret > 0 && ctx && |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5075 | other_ino != btrfs_ino(BTRFS_I(ctx->inode))) { |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5076 | struct btrfs_key inode_key; |
| 5077 | struct inode *other_inode; |
| 5078 | |
| 5079 | if (ins_nr > 0) { |
| 5080 | ins_nr++; |
| 5081 | } else { |
| 5082 | ins_nr = 1; |
| 5083 | ins_start_slot = path->slots[0]; |
| 5084 | } |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5085 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5086 | &last_extent, ins_start_slot, |
| 5087 | ins_nr, inode_only, |
| 5088 | logged_isize); |
| 5089 | if (ret < 0) { |
| 5090 | err = ret; |
| 5091 | goto out_unlock; |
| 5092 | } |
| 5093 | ins_nr = 0; |
| 5094 | btrfs_release_path(path); |
| 5095 | inode_key.objectid = other_ino; |
| 5096 | inode_key.type = BTRFS_INODE_ITEM_KEY; |
| 5097 | inode_key.offset = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5098 | other_inode = btrfs_iget(fs_info->sb, |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5099 | &inode_key, root, |
| 5100 | NULL); |
| 5101 | /* |
| 5102 | * If the other inode that had a conflicting dir |
| 5103 | * entry was deleted in the current transaction, |
| 5104 | * we don't need to do more work nor fallback to |
| 5105 | * a transaction commit. |
| 5106 | */ |
| 5107 | if (IS_ERR(other_inode) && |
| 5108 | PTR_ERR(other_inode) == -ENOENT) { |
| 5109 | goto next_key; |
| 5110 | } else if (IS_ERR(other_inode)) { |
| 5111 | err = PTR_ERR(other_inode); |
| 5112 | goto out_unlock; |
| 5113 | } |
| 5114 | /* |
| 5115 | * We are safe logging the other inode without |
| 5116 | * acquiring its i_mutex as long as we log with |
| 5117 | * the LOG_INODE_EXISTS mode. We're safe against |
| 5118 | * concurrent renames of the other inode as well |
| 5119 | * because during a rename we pin the log and |
| 5120 | * update the log with the new name before we |
| 5121 | * unpin it. |
| 5122 | */ |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5123 | err = btrfs_log_inode(trans, root, |
| 5124 | BTRFS_I(other_inode), |
| 5125 | LOG_OTHER_INODE, 0, LLONG_MAX, |
| 5126 | ctx); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5127 | iput(other_inode); |
| 5128 | if (err) |
| 5129 | goto out_unlock; |
| 5130 | else |
| 5131 | goto next_key; |
Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5132 | } |
| 5133 | } |
| 5134 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5135 | /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */ |
| 5136 | if (min_key.type == BTRFS_XATTR_ITEM_KEY) { |
| 5137 | if (ins_nr == 0) |
| 5138 | goto next_slot; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5139 | ret = copy_items(trans, inode, dst_path, path, |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5140 | &last_extent, ins_start_slot, |
| 5141 | ins_nr, inode_only, logged_isize); |
| 5142 | if (ret < 0) { |
| 5143 | err = ret; |
| 5144 | goto out_unlock; |
| 5145 | } |
| 5146 | ins_nr = 0; |
| 5147 | if (ret) { |
| 5148 | btrfs_release_path(path); |
| 5149 | continue; |
| 5150 | } |
| 5151 | goto next_slot; |
| 5152 | } |
| 5153 | |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5154 | if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { |
| 5155 | ins_nr++; |
| 5156 | goto next_slot; |
| 5157 | } else if (!ins_nr) { |
| 5158 | ins_start_slot = path->slots[0]; |
| 5159 | ins_nr = 1; |
| 5160 | goto next_slot; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5161 | } |
| 5162 | |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5163 | ret = copy_items(trans, inode, dst_path, path, &last_extent, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5164 | ins_start_slot, ins_nr, inode_only, |
| 5165 | logged_isize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5166 | if (ret < 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5167 | err = ret; |
| 5168 | goto out_unlock; |
Rasmus Villemoes | a71db86 | 2014-06-20 21:51:43 +0200 | [diff] [blame] | 5169 | } |
| 5170 | if (ret) { |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5171 | ins_nr = 0; |
| 5172 | btrfs_release_path(path); |
| 5173 | continue; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5174 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5175 | ins_nr = 1; |
| 5176 | ins_start_slot = path->slots[0]; |
| 5177 | next_slot: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5178 | |
Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 5179 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 5180 | path->slots[0]++; |
| 5181 | if (path->slots[0] < nritems) { |
| 5182 | btrfs_item_key_to_cpu(path->nodes[0], &min_key, |
| 5183 | path->slots[0]); |
| 5184 | goto again; |
| 5185 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5186 | if (ins_nr) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5187 | ret = copy_items(trans, inode, dst_path, path, |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5188 | &last_extent, ins_start_slot, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5189 | ins_nr, inode_only, logged_isize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5190 | if (ret < 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5191 | err = ret; |
| 5192 | goto out_unlock; |
| 5193 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5194 | ret = 0; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5195 | ins_nr = 0; |
| 5196 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5197 | btrfs_release_path(path); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5198 | next_key: |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5199 | if (min_key.offset < (u64)-1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5200 | min_key.offset++; |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5201 | } else if (min_key.type < max_key.type) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5202 | min_key.type++; |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5203 | min_key.offset = 0; |
| 5204 | } else { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5205 | break; |
Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5206 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5207 | } |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5208 | if (ins_nr) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5209 | ret = copy_items(trans, inode, dst_path, path, &last_extent, |
Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5210 | ins_start_slot, ins_nr, inode_only, |
| 5211 | logged_isize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5212 | if (ret < 0) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5213 | err = ret; |
| 5214 | goto out_unlock; |
| 5215 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5216 | ret = 0; |
Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5217 | ins_nr = 0; |
| 5218 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5219 | |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5220 | btrfs_release_path(path); |
| 5221 | btrfs_release_path(dst_path); |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5222 | err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path); |
Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5223 | if (err) |
| 5224 | goto out_unlock; |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 5225 | xattrs_logged = true; |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5226 | if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) { |
| 5227 | btrfs_release_path(path); |
| 5228 | btrfs_release_path(dst_path); |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5229 | err = btrfs_log_trailing_hole(trans, root, inode, path); |
Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5230 | if (err) |
| 5231 | goto out_unlock; |
| 5232 | } |
Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 5233 | log_extents: |
Josef Bacik | f3b15cc | 2013-07-22 12:54:30 -0400 | [diff] [blame] | 5234 | btrfs_release_path(path); |
| 5235 | btrfs_release_path(dst_path); |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 5236 | if (need_log_inode_item) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5237 | err = log_inode_item(trans, log, dst_path, inode); |
Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 5238 | if (!err && !xattrs_logged) { |
| 5239 | err = btrfs_log_all_xattrs(trans, root, inode, path, |
| 5240 | dst_path); |
| 5241 | btrfs_release_path(path); |
| 5242 | } |
Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 5243 | if (err) |
| 5244 | goto out_unlock; |
| 5245 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5246 | if (fast_search) { |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5247 | ret = btrfs_log_changed_extents(trans, root, inode, dst_path, |
Filipe Manana | de0ee0e | 2016-01-21 10:17:54 +0000 | [diff] [blame] | 5248 | &logged_list, ctx, start, end); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5249 | if (ret) { |
| 5250 | err = ret; |
| 5251 | goto out_unlock; |
| 5252 | } |
Josef Bacik | d006a04 | 2013-11-12 20:54:09 -0500 | [diff] [blame] | 5253 | } else if (inode_only == LOG_INODE_ALL) { |
Liu Bo | 06d3d22 | 2012-08-27 10:52:19 -0600 | [diff] [blame] | 5254 | struct extent_map *em, *n; |
| 5255 | |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5256 | write_lock(&em_tree->lock); |
| 5257 | /* |
| 5258 | * We can't just remove every em if we're called for a ranged |
| 5259 | * fsync - that is, one that doesn't cover the whole possible |
| 5260 | * file range (0 to LLONG_MAX). This is because we can have |
| 5261 | * em's that fall outside the range we're logging and therefore |
| 5262 | * their ordered operations haven't completed yet |
| 5263 | * (btrfs_finish_ordered_io() not invoked yet). This means we |
| 5264 | * didn't get their respective file extent item in the fs/subvol |
| 5265 | * tree yet, and need to let the next fast fsync (one which |
| 5266 | * consults the list of modified extent maps) find the em so |
| 5267 | * that it logs a matching file extent item and waits for the |
| 5268 | * respective ordered operation to complete (if it's still |
| 5269 | * running). |
| 5270 | * |
| 5271 | * Removing every em outside the range we're logging would make |
| 5272 | * the next fast fsync not log their matching file extent items, |
| 5273 | * therefore making us lose data after a log replay. |
| 5274 | */ |
| 5275 | list_for_each_entry_safe(em, n, &em_tree->modified_extents, |
| 5276 | list) { |
| 5277 | const u64 mod_end = em->mod_start + em->mod_len - 1; |
| 5278 | |
| 5279 | if (em->mod_start >= start && mod_end <= end) |
| 5280 | list_del_init(&em->list); |
| 5281 | } |
| 5282 | write_unlock(&em_tree->lock); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5283 | } |
| 5284 | |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5285 | if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) { |
| 5286 | ret = log_directory_changes(trans, root, inode, path, dst_path, |
| 5287 | ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5288 | if (ret) { |
| 5289 | err = ret; |
| 5290 | goto out_unlock; |
| 5291 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5292 | } |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5293 | |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5294 | spin_lock(&inode->lock); |
| 5295 | inode->logged_trans = trans->transid; |
| 5296 | inode->last_log_commit = inode->last_sub_trans; |
| 5297 | spin_unlock(&inode->lock); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5298 | out_unlock: |
Miao Xie | 827463c | 2014-01-14 20:31:51 +0800 | [diff] [blame] | 5299 | if (unlikely(err)) |
| 5300 | btrfs_put_logged_extents(&logged_list); |
| 5301 | else |
| 5302 | btrfs_submit_logged_extents(&logged_list, log); |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5303 | mutex_unlock(&inode->log_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5304 | |
| 5305 | btrfs_free_path(path); |
| 5306 | btrfs_free_path(dst_path); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5307 | return err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5308 | } |
| 5309 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5310 | /* |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5311 | * Check if we must fallback to a transaction commit when logging an inode. |
| 5312 | * This must be called after logging the inode and is used only in the context |
| 5313 | * when fsyncing an inode requires the need to log some other inode - in which |
| 5314 | * case we can't lock the i_mutex of each other inode we need to log as that |
| 5315 | * can lead to deadlocks with concurrent fsync against other inodes (as we can |
| 5316 | * log inodes up or down in the hierarchy) or rename operations for example. So |
| 5317 | * we take the log_mutex of the inode after we have logged it and then check for |
| 5318 | * its last_unlink_trans value - this is safe because any task setting |
| 5319 | * last_unlink_trans must take the log_mutex and it must do this before it does |
| 5320 | * the actual unlink operation, so if we do this check before a concurrent task |
| 5321 | * sets last_unlink_trans it means we've logged a consistent version/state of |
| 5322 | * 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] | 5323 | * commit (the concurrent task might have only updated last_unlink_trans before |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5324 | * we logged the inode or it might have also done the unlink). |
| 5325 | */ |
| 5326 | static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans, |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5327 | struct btrfs_inode *inode) |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5328 | { |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5329 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5330 | bool ret = false; |
| 5331 | |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5332 | mutex_lock(&inode->log_mutex); |
| 5333 | if (inode->last_unlink_trans > fs_info->last_trans_committed) { |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5334 | /* |
| 5335 | * Make sure any commits to the log are forced to be full |
| 5336 | * commits. |
| 5337 | */ |
| 5338 | btrfs_set_log_full_commit(fs_info, trans); |
| 5339 | ret = true; |
| 5340 | } |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5341 | mutex_unlock(&inode->log_mutex); |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5342 | |
| 5343 | return ret; |
| 5344 | } |
| 5345 | |
| 5346 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5347 | * follow the dentry parent pointers up the chain and see if any |
| 5348 | * of the directories in it require a full commit before they can |
| 5349 | * be logged. Returns zero if nothing special needs to be done or 1 if |
| 5350 | * a full commit is required. |
| 5351 | */ |
| 5352 | 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] | 5353 | struct btrfs_inode *inode, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5354 | struct dentry *parent, |
| 5355 | struct super_block *sb, |
| 5356 | u64 last_committed) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5357 | { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5358 | int ret = 0; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5359 | struct dentry *old_parent = NULL; |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5360 | struct btrfs_inode *orig_inode = inode; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5361 | |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5362 | /* |
| 5363 | * for regular files, if its inode is already on disk, we don't |
| 5364 | * have to worry about the parents at all. This is because |
| 5365 | * we can use the last_unlink_trans field to record renames |
| 5366 | * and other fun in this file. |
| 5367 | */ |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5368 | if (S_ISREG(inode->vfs_inode.i_mode) && |
| 5369 | inode->generation <= last_committed && |
| 5370 | inode->last_unlink_trans <= last_committed) |
| 5371 | goto out; |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5372 | |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5373 | if (!S_ISDIR(inode->vfs_inode.i_mode)) { |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5374 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5375 | goto out; |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5376 | inode = BTRFS_I(d_inode(parent)); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5377 | } |
| 5378 | |
| 5379 | while (1) { |
Josef Bacik | de2b530 | 2013-09-11 09:36:30 -0400 | [diff] [blame] | 5380 | /* |
| 5381 | * 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] | 5382 | * not our parent's inode, so we need to skip setting the |
Josef Bacik | de2b530 | 2013-09-11 09:36:30 -0400 | [diff] [blame] | 5383 | * logged_trans so that further down in the log code we don't |
| 5384 | * think this inode has already been logged. |
| 5385 | */ |
| 5386 | if (inode != orig_inode) |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5387 | inode->logged_trans = trans->transid; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5388 | smp_mb(); |
| 5389 | |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5390 | if (btrfs_must_commit_transaction(trans, inode)) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5391 | ret = 1; |
| 5392 | break; |
| 5393 | } |
| 5394 | |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5395 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5396 | break; |
| 5397 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5398 | if (IS_ROOT(parent)) { |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5399 | inode = BTRFS_I(d_inode(parent)); |
| 5400 | if (btrfs_must_commit_transaction(trans, inode)) |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5401 | ret = 1; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5402 | break; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5403 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5404 | |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5405 | parent = dget_parent(parent); |
| 5406 | dput(old_parent); |
| 5407 | old_parent = parent; |
Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5408 | inode = BTRFS_I(d_inode(parent)); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5409 | |
| 5410 | } |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5411 | dput(old_parent); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5412 | out: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5413 | return ret; |
| 5414 | } |
| 5415 | |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5416 | struct btrfs_dir_list { |
| 5417 | u64 ino; |
| 5418 | struct list_head list; |
| 5419 | }; |
| 5420 | |
| 5421 | /* |
| 5422 | * Log the inodes of the new dentries of a directory. See log_dir_items() for |
| 5423 | * details about the why it is needed. |
| 5424 | * This is a recursive operation - if an existing dentry corresponds to a |
| 5425 | * directory, that directory's new entries are logged too (same behaviour as |
| 5426 | * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes |
| 5427 | * the dentries point to we do not lock their i_mutex, otherwise lockdep |
| 5428 | * complains about the following circular lock dependency / possible deadlock: |
| 5429 | * |
| 5430 | * CPU0 CPU1 |
| 5431 | * ---- ---- |
| 5432 | * lock(&type->i_mutex_dir_key#3/2); |
| 5433 | * lock(sb_internal#2); |
| 5434 | * lock(&type->i_mutex_dir_key#3/2); |
| 5435 | * lock(&sb->s_type->i_mutex_key#14); |
| 5436 | * |
| 5437 | * Where sb_internal is the lock (a counter that works as a lock) acquired by |
| 5438 | * sb_start_intwrite() in btrfs_start_transaction(). |
| 5439 | * Not locking i_mutex of the inodes is still safe because: |
| 5440 | * |
| 5441 | * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible |
| 5442 | * that while logging the inode new references (names) are added or removed |
| 5443 | * from the inode, leaving the logged inode item with a link count that does |
| 5444 | * not match the number of logged inode reference items. This is fine because |
| 5445 | * at log replay time we compute the real number of links and correct the |
| 5446 | * link count in the inode item (see replay_one_buffer() and |
| 5447 | * link_to_fixup_dir()); |
| 5448 | * |
| 5449 | * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that |
| 5450 | * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and |
| 5451 | * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item |
| 5452 | * has a size that doesn't match the sum of the lengths of all the logged |
| 5453 | * names. This does not result in a problem because if a dir_item key is |
| 5454 | * logged but its matching dir_index key is not logged, at log replay time we |
| 5455 | * don't use it to replay the respective name (see replay_one_name()). On the |
| 5456 | * other hand if only the dir_index key ends up being logged, the respective |
| 5457 | * name is added to the fs/subvol tree with both the dir_item and dir_index |
| 5458 | * keys created (see replay_one_name()). |
| 5459 | * The directory's inode item with a wrong i_size is not a problem as well, |
| 5460 | * since we don't use it at log replay time to set the i_size in the inode |
| 5461 | * item of the fs/subvol tree (see overwrite_item()). |
| 5462 | */ |
| 5463 | static int log_new_dir_dentries(struct btrfs_trans_handle *trans, |
| 5464 | struct btrfs_root *root, |
Nikolay Borisov | 51cc0d3 | 2017-01-18 00:31:43 +0200 | [diff] [blame] | 5465 | struct btrfs_inode *start_inode, |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5466 | struct btrfs_log_ctx *ctx) |
| 5467 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5468 | struct btrfs_fs_info *fs_info = root->fs_info; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5469 | struct btrfs_root *log = root->log_root; |
| 5470 | struct btrfs_path *path; |
| 5471 | LIST_HEAD(dir_list); |
| 5472 | struct btrfs_dir_list *dir_elem; |
| 5473 | int ret = 0; |
| 5474 | |
| 5475 | path = btrfs_alloc_path(); |
| 5476 | if (!path) |
| 5477 | return -ENOMEM; |
| 5478 | |
| 5479 | dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS); |
| 5480 | if (!dir_elem) { |
| 5481 | btrfs_free_path(path); |
| 5482 | return -ENOMEM; |
| 5483 | } |
Nikolay Borisov | 51cc0d3 | 2017-01-18 00:31:43 +0200 | [diff] [blame] | 5484 | dir_elem->ino = btrfs_ino(start_inode); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5485 | list_add_tail(&dir_elem->list, &dir_list); |
| 5486 | |
| 5487 | while (!list_empty(&dir_list)) { |
| 5488 | struct extent_buffer *leaf; |
| 5489 | struct btrfs_key min_key; |
| 5490 | int nritems; |
| 5491 | int i; |
| 5492 | |
| 5493 | dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, |
| 5494 | list); |
| 5495 | if (ret) |
| 5496 | goto next_dir_inode; |
| 5497 | |
| 5498 | min_key.objectid = dir_elem->ino; |
| 5499 | min_key.type = BTRFS_DIR_ITEM_KEY; |
| 5500 | min_key.offset = 0; |
| 5501 | again: |
| 5502 | btrfs_release_path(path); |
| 5503 | ret = btrfs_search_forward(log, &min_key, path, trans->transid); |
| 5504 | if (ret < 0) { |
| 5505 | goto next_dir_inode; |
| 5506 | } else if (ret > 0) { |
| 5507 | ret = 0; |
| 5508 | goto next_dir_inode; |
| 5509 | } |
| 5510 | |
| 5511 | process_leaf: |
| 5512 | leaf = path->nodes[0]; |
| 5513 | nritems = btrfs_header_nritems(leaf); |
| 5514 | for (i = path->slots[0]; i < nritems; i++) { |
| 5515 | struct btrfs_dir_item *di; |
| 5516 | struct btrfs_key di_key; |
| 5517 | struct inode *di_inode; |
| 5518 | struct btrfs_dir_list *new_dir_elem; |
| 5519 | int log_mode = LOG_INODE_EXISTS; |
| 5520 | int type; |
| 5521 | |
| 5522 | btrfs_item_key_to_cpu(leaf, &min_key, i); |
| 5523 | if (min_key.objectid != dir_elem->ino || |
| 5524 | min_key.type != BTRFS_DIR_ITEM_KEY) |
| 5525 | goto next_dir_inode; |
| 5526 | |
| 5527 | di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item); |
| 5528 | type = btrfs_dir_type(leaf, di); |
| 5529 | if (btrfs_dir_transid(leaf, di) < trans->transid && |
| 5530 | type != BTRFS_FT_DIR) |
| 5531 | continue; |
| 5532 | btrfs_dir_item_key_to_cpu(leaf, di, &di_key); |
| 5533 | if (di_key.type == BTRFS_ROOT_ITEM_KEY) |
| 5534 | continue; |
| 5535 | |
Robbie Ko | ec125cf | 2016-10-28 10:48:26 +0800 | [diff] [blame] | 5536 | btrfs_release_path(path); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5537 | di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5538 | if (IS_ERR(di_inode)) { |
| 5539 | ret = PTR_ERR(di_inode); |
| 5540 | goto next_dir_inode; |
| 5541 | } |
| 5542 | |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 5543 | if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) { |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5544 | iput(di_inode); |
Robbie Ko | ec125cf | 2016-10-28 10:48:26 +0800 | [diff] [blame] | 5545 | break; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5546 | } |
| 5547 | |
| 5548 | ctx->log_new_dentries = false; |
Filipe Manana | 3f9749f | 2016-04-25 04:45:02 +0100 | [diff] [blame] | 5549 | if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK) |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5550 | log_mode = LOG_INODE_ALL; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5551 | ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode), |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5552 | log_mode, 0, LLONG_MAX, ctx); |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5553 | if (!ret && |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5554 | btrfs_must_commit_transaction(trans, BTRFS_I(di_inode))) |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5555 | ret = 1; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5556 | iput(di_inode); |
| 5557 | if (ret) |
| 5558 | goto next_dir_inode; |
| 5559 | if (ctx->log_new_dentries) { |
| 5560 | new_dir_elem = kmalloc(sizeof(*new_dir_elem), |
| 5561 | GFP_NOFS); |
| 5562 | if (!new_dir_elem) { |
| 5563 | ret = -ENOMEM; |
| 5564 | goto next_dir_inode; |
| 5565 | } |
| 5566 | new_dir_elem->ino = di_key.objectid; |
| 5567 | list_add_tail(&new_dir_elem->list, &dir_list); |
| 5568 | } |
| 5569 | break; |
| 5570 | } |
| 5571 | if (i == nritems) { |
| 5572 | ret = btrfs_next_leaf(log, path); |
| 5573 | if (ret < 0) { |
| 5574 | goto next_dir_inode; |
| 5575 | } else if (ret > 0) { |
| 5576 | ret = 0; |
| 5577 | goto next_dir_inode; |
| 5578 | } |
| 5579 | goto process_leaf; |
| 5580 | } |
| 5581 | if (min_key.offset < (u64)-1) { |
| 5582 | min_key.offset++; |
| 5583 | goto again; |
| 5584 | } |
| 5585 | next_dir_inode: |
| 5586 | list_del(&dir_elem->list); |
| 5587 | kfree(dir_elem); |
| 5588 | } |
| 5589 | |
| 5590 | btrfs_free_path(path); |
| 5591 | return ret; |
| 5592 | } |
| 5593 | |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5594 | static int btrfs_log_all_parents(struct btrfs_trans_handle *trans, |
Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 5595 | struct btrfs_inode *inode, |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5596 | struct btrfs_log_ctx *ctx) |
| 5597 | { |
Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 5598 | 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] | 5599 | int ret; |
| 5600 | struct btrfs_path *path; |
| 5601 | struct btrfs_key key; |
Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 5602 | struct btrfs_root *root = inode->root; |
| 5603 | const u64 ino = btrfs_ino(inode); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5604 | |
| 5605 | path = btrfs_alloc_path(); |
| 5606 | if (!path) |
| 5607 | return -ENOMEM; |
| 5608 | path->skip_locking = 1; |
| 5609 | path->search_commit_root = 1; |
| 5610 | |
| 5611 | key.objectid = ino; |
| 5612 | key.type = BTRFS_INODE_REF_KEY; |
| 5613 | key.offset = 0; |
| 5614 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5615 | if (ret < 0) |
| 5616 | goto out; |
| 5617 | |
| 5618 | while (true) { |
| 5619 | struct extent_buffer *leaf = path->nodes[0]; |
| 5620 | int slot = path->slots[0]; |
| 5621 | u32 cur_offset = 0; |
| 5622 | u32 item_size; |
| 5623 | unsigned long ptr; |
| 5624 | |
| 5625 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5626 | ret = btrfs_next_leaf(root, path); |
| 5627 | if (ret < 0) |
| 5628 | goto out; |
| 5629 | else if (ret > 0) |
| 5630 | break; |
| 5631 | continue; |
| 5632 | } |
| 5633 | |
| 5634 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5635 | /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */ |
| 5636 | if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY) |
| 5637 | break; |
| 5638 | |
| 5639 | item_size = btrfs_item_size_nr(leaf, slot); |
| 5640 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 5641 | while (cur_offset < item_size) { |
| 5642 | struct btrfs_key inode_key; |
| 5643 | struct inode *dir_inode; |
| 5644 | |
| 5645 | inode_key.type = BTRFS_INODE_ITEM_KEY; |
| 5646 | inode_key.offset = 0; |
| 5647 | |
| 5648 | if (key.type == BTRFS_INODE_EXTREF_KEY) { |
| 5649 | struct btrfs_inode_extref *extref; |
| 5650 | |
| 5651 | extref = (struct btrfs_inode_extref *) |
| 5652 | (ptr + cur_offset); |
| 5653 | inode_key.objectid = btrfs_inode_extref_parent( |
| 5654 | leaf, extref); |
| 5655 | cur_offset += sizeof(*extref); |
| 5656 | cur_offset += btrfs_inode_extref_name_len(leaf, |
| 5657 | extref); |
| 5658 | } else { |
| 5659 | inode_key.objectid = key.offset; |
| 5660 | cur_offset = item_size; |
| 5661 | } |
| 5662 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5663 | dir_inode = btrfs_iget(fs_info->sb, &inode_key, |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5664 | root, NULL); |
| 5665 | /* If parent inode was deleted, skip it. */ |
| 5666 | if (IS_ERR(dir_inode)) |
| 5667 | continue; |
| 5668 | |
Filipe Manana | 657ed1a | 2016-04-06 17:11:56 +0100 | [diff] [blame] | 5669 | if (ctx) |
| 5670 | ctx->log_new_dentries = false; |
Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5671 | ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode), |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5672 | LOG_INODE_ALL, 0, LLONG_MAX, ctx); |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5673 | if (!ret && |
Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5674 | btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode))) |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5675 | ret = 1; |
Filipe Manana | 657ed1a | 2016-04-06 17:11:56 +0100 | [diff] [blame] | 5676 | if (!ret && ctx && ctx->log_new_dentries) |
| 5677 | ret = log_new_dir_dentries(trans, root, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5678 | BTRFS_I(dir_inode), ctx); |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5679 | iput(dir_inode); |
| 5680 | if (ret) |
| 5681 | goto out; |
| 5682 | } |
| 5683 | path->slots[0]++; |
| 5684 | } |
| 5685 | ret = 0; |
| 5686 | out: |
| 5687 | btrfs_free_path(path); |
| 5688 | return ret; |
| 5689 | } |
| 5690 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5691 | /* |
| 5692 | * helper function around btrfs_log_inode to make sure newly created |
| 5693 | * parent directories also end up in the log. A minimal inode and backref |
| 5694 | * only logging is done of any parent directories that are older than |
| 5695 | * the last committed transaction |
| 5696 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5697 | static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5698 | struct btrfs_inode *inode, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5699 | struct dentry *parent, |
| 5700 | const loff_t start, |
| 5701 | const loff_t end, |
Edmund Nadolski | 41a1ead | 2017-11-20 13:24:47 -0700 | [diff] [blame] | 5702 | int inode_only, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5703 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5704 | { |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5705 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5706 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5707 | struct super_block *sb; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5708 | struct dentry *old_parent = NULL; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5709 | int ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5710 | u64 last_committed = fs_info->last_trans_committed; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5711 | bool log_dentries = false; |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5712 | struct btrfs_inode *orig_inode = inode; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5713 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5714 | sb = inode->vfs_inode.i_sb; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5715 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5716 | if (btrfs_test_opt(fs_info, NOTREELOG)) { |
Sage Weil | 3a5e140 | 2009-04-02 16:49:40 -0400 | [diff] [blame] | 5717 | ret = 1; |
| 5718 | goto end_no_trans; |
| 5719 | } |
| 5720 | |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 5721 | /* |
| 5722 | * The prev transaction commit doesn't complete, we need do |
| 5723 | * full commit by ourselves. |
| 5724 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5725 | if (fs_info->last_trans_log_full_commit > |
| 5726 | fs_info->last_trans_committed) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5727 | ret = 1; |
| 5728 | goto end_no_trans; |
| 5729 | } |
| 5730 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5731 | if (btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5732 | ret = 1; |
| 5733 | goto end_no_trans; |
| 5734 | } |
| 5735 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5736 | ret = check_parent_dirs_for_sync(trans, inode, parent, sb, |
| 5737 | last_committed); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5738 | if (ret) |
| 5739 | goto end_no_trans; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5740 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5741 | if (btrfs_inode_in_log(inode, trans->transid)) { |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5742 | ret = BTRFS_NO_LOG_SYNC; |
| 5743 | goto end_no_trans; |
| 5744 | } |
| 5745 | |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5746 | ret = start_log_trans(trans, root, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5747 | if (ret) |
Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 5748 | goto end_no_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5749 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5750 | ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5751 | if (ret) |
| 5752 | goto end_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5753 | |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5754 | /* |
| 5755 | * for regular files, if its inode is already on disk, we don't |
| 5756 | * have to worry about the parents at all. This is because |
| 5757 | * we can use the last_unlink_trans field to record renames |
| 5758 | * and other fun in this file. |
| 5759 | */ |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5760 | if (S_ISREG(inode->vfs_inode.i_mode) && |
| 5761 | inode->generation <= last_committed && |
| 5762 | inode->last_unlink_trans <= last_committed) { |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5763 | ret = 0; |
| 5764 | goto end_trans; |
| 5765 | } |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5766 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5767 | 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] | 5768 | log_dentries = true; |
| 5769 | |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5770 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5771 | * 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] | 5772 | * inodes are fully logged. This is to prevent leaving dangling |
| 5773 | * directory index entries in directories that were our parents but are |
| 5774 | * not anymore. Not doing this results in old parent directory being |
| 5775 | * impossible to delete after log replay (rmdir will always fail with |
| 5776 | * error -ENOTEMPTY). |
| 5777 | * |
| 5778 | * Example 1: |
| 5779 | * |
| 5780 | * mkdir testdir |
| 5781 | * touch testdir/foo |
| 5782 | * ln testdir/foo testdir/bar |
| 5783 | * sync |
| 5784 | * unlink testdir/bar |
| 5785 | * xfs_io -c fsync testdir/foo |
| 5786 | * <power failure> |
| 5787 | * mount fs, triggers log replay |
| 5788 | * |
| 5789 | * If we don't log the parent directory (testdir), after log replay the |
| 5790 | * directory still has an entry pointing to the file inode using the bar |
| 5791 | * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and |
| 5792 | * the file inode has a link count of 1. |
| 5793 | * |
| 5794 | * Example 2: |
| 5795 | * |
| 5796 | * mkdir testdir |
| 5797 | * touch foo |
| 5798 | * ln foo testdir/foo2 |
| 5799 | * ln foo testdir/foo3 |
| 5800 | * sync |
| 5801 | * unlink testdir/foo3 |
| 5802 | * xfs_io -c fsync foo |
| 5803 | * <power failure> |
| 5804 | * mount fs, triggers log replay |
| 5805 | * |
| 5806 | * Similar as the first example, after log replay the parent directory |
| 5807 | * testdir still has an entry pointing to the inode file with name foo3 |
| 5808 | * but the file inode does not have a matching BTRFS_INODE_REF_KEY item |
| 5809 | * and has a link count of 2. |
| 5810 | */ |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5811 | if (inode->last_unlink_trans > last_committed) { |
Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5812 | ret = btrfs_log_all_parents(trans, orig_inode, ctx); |
| 5813 | if (ret) |
| 5814 | goto end_trans; |
| 5815 | } |
| 5816 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5817 | while (1) { |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5818 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5819 | break; |
| 5820 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5821 | inode = BTRFS_I(d_inode(parent)); |
| 5822 | if (root != inode->root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5823 | break; |
| 5824 | |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5825 | if (inode->generation > last_committed) { |
| 5826 | ret = btrfs_log_inode(trans, root, inode, |
| 5827 | LOG_INODE_EXISTS, 0, LLONG_MAX, ctx); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5828 | if (ret) |
| 5829 | goto end_trans; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5830 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5831 | if (IS_ROOT(parent)) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5832 | break; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5833 | |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5834 | parent = dget_parent(parent); |
| 5835 | dput(old_parent); |
| 5836 | old_parent = parent; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5837 | } |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5838 | if (log_dentries) |
Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5839 | ret = log_new_dir_dentries(trans, root, orig_inode, ctx); |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5840 | else |
| 5841 | ret = 0; |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5842 | end_trans: |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5843 | dput(old_parent); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5844 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5845 | btrfs_set_log_full_commit(fs_info, trans); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5846 | ret = 1; |
| 5847 | } |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5848 | |
| 5849 | if (ret) |
| 5850 | btrfs_remove_log_ctx(root, ctx); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5851 | btrfs_end_log_trans(root); |
| 5852 | end_no_trans: |
| 5853 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5854 | } |
| 5855 | |
| 5856 | /* |
| 5857 | * it is not safe to log dentry if the chunk root has added new |
| 5858 | * chunks. This returns 0 if the dentry was logged, and 1 otherwise. |
| 5859 | * If this returns 1, you must commit the transaction to safely get your |
| 5860 | * data on disk. |
| 5861 | */ |
| 5862 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e5b84f7a | 2018-02-27 17:37:18 +0200 | [diff] [blame] | 5863 | struct dentry *dentry, |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5864 | const loff_t start, |
| 5865 | const loff_t end, |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5866 | struct btrfs_log_ctx *ctx) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5867 | { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5868 | struct dentry *parent = dget_parent(dentry); |
| 5869 | int ret; |
| 5870 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5871 | ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent, |
| 5872 | start, end, LOG_INODE_ALL, ctx); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5873 | dput(parent); |
| 5874 | |
| 5875 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5876 | } |
| 5877 | |
| 5878 | /* |
| 5879 | * should be called during mount to recover any replay any log trees |
| 5880 | * from the FS |
| 5881 | */ |
| 5882 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) |
| 5883 | { |
| 5884 | int ret; |
| 5885 | struct btrfs_path *path; |
| 5886 | struct btrfs_trans_handle *trans; |
| 5887 | struct btrfs_key key; |
| 5888 | struct btrfs_key found_key; |
| 5889 | struct btrfs_key tmp_key; |
| 5890 | struct btrfs_root *log; |
| 5891 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; |
| 5892 | struct walk_control wc = { |
| 5893 | .process_func = process_one_buffer, |
| 5894 | .stage = 0, |
| 5895 | }; |
| 5896 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5897 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5898 | if (!path) |
| 5899 | return -ENOMEM; |
| 5900 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 5901 | set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5902 | |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5903 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5904 | if (IS_ERR(trans)) { |
| 5905 | ret = PTR_ERR(trans); |
| 5906 | goto error; |
| 5907 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5908 | |
| 5909 | wc.trans = trans; |
| 5910 | wc.pin = 1; |
| 5911 | |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5912 | ret = walk_log_tree(trans, log_root_tree, &wc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5913 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5914 | btrfs_handle_fs_error(fs_info, ret, |
| 5915 | "Failed to pin buffers while recovering log root tree."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5916 | goto error; |
| 5917 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5918 | |
| 5919 | again: |
| 5920 | key.objectid = BTRFS_TREE_LOG_OBJECTID; |
| 5921 | key.offset = (u64)-1; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5922 | key.type = BTRFS_ROOT_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5923 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5924 | while (1) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5925 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5926 | |
| 5927 | if (ret < 0) { |
Anand Jain | 34d9700 | 2016-03-16 16:43:06 +0800 | [diff] [blame] | 5928 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5929 | "Couldn't find tree log root."); |
| 5930 | goto error; |
| 5931 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5932 | if (ret > 0) { |
| 5933 | if (path->slots[0] == 0) |
| 5934 | break; |
| 5935 | path->slots[0]--; |
| 5936 | } |
| 5937 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 5938 | path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5939 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5940 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 5941 | break; |
| 5942 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 5943 | log = btrfs_read_fs_root(log_root_tree, &found_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5944 | if (IS_ERR(log)) { |
| 5945 | ret = PTR_ERR(log); |
Anand Jain | 34d9700 | 2016-03-16 16:43:06 +0800 | [diff] [blame] | 5946 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5947 | "Couldn't read tree log root."); |
| 5948 | goto error; |
| 5949 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5950 | |
| 5951 | tmp_key.objectid = found_key.offset; |
| 5952 | tmp_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5953 | tmp_key.offset = (u64)-1; |
| 5954 | |
| 5955 | 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] | 5956 | if (IS_ERR(wc.replay_dest)) { |
| 5957 | ret = PTR_ERR(wc.replay_dest); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5958 | free_extent_buffer(log->node); |
| 5959 | free_extent_buffer(log->commit_root); |
| 5960 | kfree(log); |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5961 | btrfs_handle_fs_error(fs_info, ret, |
| 5962 | "Couldn't read target root for tree log recovery."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5963 | goto error; |
| 5964 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5965 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 5966 | wc.replay_dest->log_root = log; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5967 | btrfs_record_root_in_trans(trans, wc.replay_dest); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5968 | ret = walk_log_tree(trans, log, &wc); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5969 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5970 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5971 | ret = fixup_inode_link_counts(trans, wc.replay_dest, |
| 5972 | path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5973 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5974 | |
Liu Bo | 900c998 | 2018-01-25 11:02:56 -0700 | [diff] [blame] | 5975 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
| 5976 | struct btrfs_root *root = wc.replay_dest; |
| 5977 | |
| 5978 | btrfs_release_path(path); |
| 5979 | |
| 5980 | /* |
| 5981 | * We have just replayed everything, and the highest |
| 5982 | * objectid of fs roots probably has changed in case |
| 5983 | * some inode_item's got replayed. |
| 5984 | * |
| 5985 | * root->objectid_mutex is not acquired as log replay |
| 5986 | * could only happen during mount. |
| 5987 | */ |
| 5988 | ret = btrfs_find_highest_objectid(root, |
| 5989 | &root->highest_objectid); |
| 5990 | } |
| 5991 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5992 | key.offset = found_key.offset - 1; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 5993 | wc.replay_dest->log_root = NULL; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5994 | free_extent_buffer(log->node); |
Chris Mason | b263c2c | 2009-06-11 11:24:47 -0400 | [diff] [blame] | 5995 | free_extent_buffer(log->commit_root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5996 | kfree(log); |
| 5997 | |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5998 | if (ret) |
| 5999 | goto error; |
| 6000 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6001 | if (found_key.offset == 0) |
| 6002 | break; |
| 6003 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6004 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6005 | |
| 6006 | /* step one is to pin it all, step two is to replay just inodes */ |
| 6007 | if (wc.pin) { |
| 6008 | wc.pin = 0; |
| 6009 | wc.process_func = replay_one_buffer; |
| 6010 | wc.stage = LOG_WALK_REPLAY_INODES; |
| 6011 | goto again; |
| 6012 | } |
| 6013 | /* step three is to replay everything */ |
| 6014 | if (wc.stage < LOG_WALK_REPLAY_ALL) { |
| 6015 | wc.stage++; |
| 6016 | goto again; |
| 6017 | } |
| 6018 | |
| 6019 | btrfs_free_path(path); |
| 6020 | |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 6021 | /* step 4: commit the transaction, which also unpins the blocks */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6022 | ret = btrfs_commit_transaction(trans); |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 6023 | if (ret) |
| 6024 | return ret; |
| 6025 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6026 | free_extent_buffer(log_root_tree->node); |
| 6027 | log_root_tree->log_root = NULL; |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 6028 | clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6029 | kfree(log_root_tree); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6030 | |
Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 6031 | return 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6032 | error: |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6033 | if (wc.trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6034 | btrfs_end_transaction(wc.trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6035 | btrfs_free_path(path); |
| 6036 | return ret; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6037 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6038 | |
| 6039 | /* |
| 6040 | * there are some corner cases where we want to force a full |
| 6041 | * commit instead of allowing a directory to be logged. |
| 6042 | * |
| 6043 | * They revolve around files there were unlinked from the directory, and |
| 6044 | * this function updates the parent directory so that a full commit is |
| 6045 | * 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] | 6046 | * |
| 6047 | * Must be called before the unlink operations (updates to the subvolume tree, |
| 6048 | * inodes, etc) are done. |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6049 | */ |
| 6050 | void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 6051 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6052 | int for_rename) |
| 6053 | { |
| 6054 | /* |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 6055 | * when we're logging a file, if it hasn't been renamed |
| 6056 | * or unlinked, and its inode is fully committed on disk, |
| 6057 | * we don't have to worry about walking up the directory chain |
| 6058 | * to log its parents. |
| 6059 | * |
| 6060 | * So, we use the last_unlink_trans field to put this transid |
| 6061 | * into the file. When the file is logged we check it and |
| 6062 | * don't log the parents if the file is fully on disk. |
| 6063 | */ |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 6064 | mutex_lock(&inode->log_mutex); |
| 6065 | inode->last_unlink_trans = trans->transid; |
| 6066 | mutex_unlock(&inode->log_mutex); |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 6067 | |
| 6068 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6069 | * if this directory was already logged any new |
| 6070 | * names for this file/dir will get recorded |
| 6071 | */ |
| 6072 | smp_mb(); |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 6073 | if (dir->logged_trans == trans->transid) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6074 | return; |
| 6075 | |
| 6076 | /* |
| 6077 | * if the inode we're about to unlink was logged, |
| 6078 | * the log will be properly updated for any new names |
| 6079 | */ |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 6080 | if (inode->logged_trans == trans->transid) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6081 | return; |
| 6082 | |
| 6083 | /* |
| 6084 | * when renaming files across directories, if the directory |
| 6085 | * there we're unlinking from gets fsync'd later on, there's |
| 6086 | * no way to find the destination directory later and fsync it |
| 6087 | * properly. So, we have to be conservative and force commits |
| 6088 | * so the new name gets discovered. |
| 6089 | */ |
| 6090 | if (for_rename) |
| 6091 | goto record; |
| 6092 | |
| 6093 | /* we can safely do the unlink without any special recording */ |
| 6094 | return; |
| 6095 | |
| 6096 | record: |
Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 6097 | mutex_lock(&dir->log_mutex); |
| 6098 | dir->last_unlink_trans = trans->transid; |
| 6099 | mutex_unlock(&dir->log_mutex); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6100 | } |
| 6101 | |
| 6102 | /* |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 6103 | * Make sure that if someone attempts to fsync the parent directory of a deleted |
| 6104 | * snapshot, it ends up triggering a transaction commit. This is to guarantee |
| 6105 | * that after replaying the log tree of the parent directory's root we will not |
| 6106 | * see the snapshot anymore and at log replay time we will not see any log tree |
| 6107 | * corresponding to the deleted snapshot's root, which could lead to replaying |
| 6108 | * it after replaying the log tree of the parent directory (which would replay |
| 6109 | * the snapshot delete operation). |
Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 6110 | * |
| 6111 | * Must be called before the actual snapshot destroy operation (updates to the |
| 6112 | * parent root and tree of tree roots trees, etc) are done. |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 6113 | */ |
| 6114 | void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4366355 | 2017-01-18 00:31:29 +0200 | [diff] [blame] | 6115 | struct btrfs_inode *dir) |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 6116 | { |
Nikolay Borisov | 4366355 | 2017-01-18 00:31:29 +0200 | [diff] [blame] | 6117 | mutex_lock(&dir->log_mutex); |
| 6118 | dir->last_unlink_trans = trans->transid; |
| 6119 | mutex_unlock(&dir->log_mutex); |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 6120 | } |
| 6121 | |
| 6122 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6123 | * Call this after adding a new name for a file and it will properly |
| 6124 | * update the log to reflect the new name. |
| 6125 | * |
| 6126 | * It will return zero if all goes well, and it will return 1 if a |
| 6127 | * full transaction commit is required. |
| 6128 | */ |
| 6129 | int btrfs_log_new_name(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6130 | struct btrfs_inode *inode, struct btrfs_inode *old_dir, |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6131 | struct dentry *parent) |
| 6132 | { |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6133 | 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] | 6134 | |
| 6135 | /* |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 6136 | * this will force the logging code to walk the dentry chain |
| 6137 | * up for the file |
| 6138 | */ |
Filipe Manana | 9a6509c | 2018-02-28 15:55:40 +0000 | [diff] [blame] | 6139 | if (!S_ISDIR(inode->vfs_inode.i_mode)) |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6140 | inode->last_unlink_trans = trans->transid; |
Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 6141 | |
| 6142 | /* |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6143 | * if this inode hasn't been logged and directory we're renaming it |
| 6144 | * from hasn't been logged, we don't need to log it |
| 6145 | */ |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6146 | if (inode->logged_trans <= fs_info->last_trans_committed && |
| 6147 | (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed)) |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6148 | return 0; |
| 6149 | |
Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 6150 | return btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX, |
| 6151 | LOG_INODE_EXISTS, NULL); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6152 | } |
| 6153 | |