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