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