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