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