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