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