| David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2 | /* | 
|  | 3 | * Copyright (C) 2008 Oracle.  All rights reserved. | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4 | */ | 
|  | 5 |  | 
|  | 6 | #include <linux/sched.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> | 
| Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 8 | #include <linux/blkdev.h> | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9 | #include <linux/list_sort.h> | 
| Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 10 | #include <linux/iversion.h> | 
| Nikolay Borisov | 9678c54 | 2018-01-08 11:45:05 +0200 | [diff] [blame] | 11 | #include "ctree.h" | 
| Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 12 | #include "tree-log.h" | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 13 | #include "disk-io.h" | 
|  | 14 | #include "locking.h" | 
|  | 15 | #include "print-tree.h" | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 16 | #include "backref.h" | 
| Anand Jain | ebb8765 | 2016-03-10 17:26:59 +0800 | [diff] [blame] | 17 | #include "compression.h" | 
| Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 18 | #include "qgroup.h" | 
| Liu Bo | 900c998 | 2018-01-25 11:02:56 -0700 | [diff] [blame] | 19 | #include "inode-map.h" | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 20 |  | 
|  | 21 | /* magic values for the inode_only field in btrfs_log_inode: | 
|  | 22 | * | 
|  | 23 | * LOG_INODE_ALL means to log everything | 
|  | 24 | * LOG_INODE_EXISTS means to log just enough to recreate the inode | 
|  | 25 | * during log replay | 
|  | 26 | */ | 
|  | 27 | #define LOG_INODE_ALL 0 | 
|  | 28 | #define LOG_INODE_EXISTS 1 | 
| Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 29 | #define LOG_OTHER_INODE 2 | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 30 |  | 
|  | 31 | /* | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 32 | * directory trouble cases | 
|  | 33 | * | 
|  | 34 | * 1) on rename or unlink, if the inode being unlinked isn't in the fsync | 
|  | 35 | * log, we must force a full commit before doing an fsync of the directory | 
|  | 36 | * where the unlink was done. | 
|  | 37 | * ---> record transid of last unlink/rename per directory | 
|  | 38 | * | 
|  | 39 | * mkdir foo/some_dir | 
|  | 40 | * normal commit | 
|  | 41 | * rename foo/some_dir foo2/some_dir | 
|  | 42 | * mkdir foo/some_dir | 
|  | 43 | * fsync foo/some_dir/some_file | 
|  | 44 | * | 
|  | 45 | * The fsync above will unlink the original some_dir without recording | 
|  | 46 | * it in its new location (foo2).  After a crash, some_dir will be gone | 
|  | 47 | * unless the fsync of some_file forces a full commit | 
|  | 48 | * | 
|  | 49 | * 2) we must log any new names for any file or dir that is in the fsync | 
|  | 50 | * log. ---> check inode while renaming/linking. | 
|  | 51 | * | 
|  | 52 | * 2a) we must log any new names for any file or dir during rename | 
|  | 53 | * when the directory they are being removed from was logged. | 
|  | 54 | * ---> check inode and old parent dir during rename | 
|  | 55 | * | 
|  | 56 | *  2a is actually the more important variant.  With the extra logging | 
|  | 57 | *  a crash might unlink the old name without recreating the new one | 
|  | 58 | * | 
|  | 59 | * 3) after a crash, we must go through any directories with a link count | 
|  | 60 | * of zero and redo the rm -rf | 
|  | 61 | * | 
|  | 62 | * mkdir f1/foo | 
|  | 63 | * normal commit | 
|  | 64 | * rm -rf f1/foo | 
|  | 65 | * fsync(f1) | 
|  | 66 | * | 
|  | 67 | * The directory f1 was fully removed from the FS, but fsync was never | 
|  | 68 | * called on f1, only its parent dir.  After a crash the rm -rf must | 
|  | 69 | * be replayed.  This must be able to recurse down the entire | 
|  | 70 | * directory tree.  The inode link count fixup code takes care of the | 
|  | 71 | * ugly details. | 
|  | 72 | */ | 
|  | 73 |  | 
|  | 74 | /* | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 75 | * stages for the tree walking.  The first | 
|  | 76 | * stage (0) is to only pin down the blocks we find | 
|  | 77 | * the second stage (1) is to make sure that all the inodes | 
|  | 78 | * we find in the log are created in the subvolume. | 
|  | 79 | * | 
|  | 80 | * The last stage is to deal with directories and links and extents | 
|  | 81 | * and all the other fun semantics | 
|  | 82 | */ | 
|  | 83 | #define LOG_WALK_PIN_ONLY 0 | 
|  | 84 | #define LOG_WALK_REPLAY_INODES 1 | 
| Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 85 | #define LOG_WALK_REPLAY_DIR_INDEX 2 | 
|  | 86 | #define LOG_WALK_REPLAY_ALL 3 | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 87 |  | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 88 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 89 | struct btrfs_root *root, struct btrfs_inode *inode, | 
| Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 90 | int inode_only, | 
|  | 91 | const loff_t start, | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 92 | const loff_t end, | 
|  | 93 | struct btrfs_log_ctx *ctx); | 
| Yan Zheng | ec051c0 | 2009-01-05 15:43:42 -0500 | [diff] [blame] | 94 | static int link_to_fixup_dir(struct btrfs_trans_handle *trans, | 
|  | 95 | struct btrfs_root *root, | 
|  | 96 | struct btrfs_path *path, u64 objectid); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 97 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, | 
|  | 98 | struct btrfs_root *root, | 
|  | 99 | struct btrfs_root *log, | 
|  | 100 | struct btrfs_path *path, | 
|  | 101 | u64 dirid, int del_all); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 102 |  | 
|  | 103 | /* | 
|  | 104 | * tree logging is a special write ahead log used to make sure that | 
|  | 105 | * fsyncs and O_SYNCs can happen without doing full tree commits. | 
|  | 106 | * | 
|  | 107 | * Full tree commits are expensive because they require commonly | 
|  | 108 | * modified blocks to be recowed, creating many dirty pages in the | 
|  | 109 | * extent tree an 4x-6x higher write load than ext3. | 
|  | 110 | * | 
|  | 111 | * Instead of doing a tree commit on every fsync, we use the | 
|  | 112 | * key ranges and transaction ids to find items for a given file or directory | 
|  | 113 | * that have changed in this transaction.  Those items are copied into | 
|  | 114 | * a special tree (one per subvolume root), that tree is written to disk | 
|  | 115 | * and then the fsync is considered complete. | 
|  | 116 | * | 
|  | 117 | * After a crash, items are copied out of the log-tree back into the | 
|  | 118 | * subvolume tree.  Any file data extents found are recorded in the extent | 
|  | 119 | * allocation tree, and the log-tree freed. | 
|  | 120 | * | 
|  | 121 | * The log tree is read three times, once to pin down all the extents it is | 
|  | 122 | * using in ram and once, once to create all the inodes logged in the tree | 
|  | 123 | * and once to do all the other items. | 
|  | 124 | */ | 
|  | 125 |  | 
|  | 126 | /* | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 127 | * start a sub transaction and setup the log tree | 
|  | 128 | * this increments the log tree writer count to make the people | 
|  | 129 | * syncing the tree wait for us to finish | 
|  | 130 | */ | 
|  | 131 | static int start_log_trans(struct btrfs_trans_handle *trans, | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 132 | struct btrfs_root *root, | 
|  | 133 | struct btrfs_log_ctx *ctx) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 134 | { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 135 | struct btrfs_fs_info *fs_info = root->fs_info; | 
| Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 136 | int ret = 0; | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 137 |  | 
|  | 138 | mutex_lock(&root->log_mutex); | 
| Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 139 |  | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 140 | if (root->log_root) { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 141 | if (btrfs_need_log_full_commit(fs_info, trans)) { | 
| Miao Xie | 50471a3 | 2014-02-20 18:08:57 +0800 | [diff] [blame] | 142 | ret = -EAGAIN; | 
|  | 143 | goto out; | 
|  | 144 | } | 
| Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 145 |  | 
| Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 146 | if (!root->log_start_pid) { | 
| Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 147 | clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); | 
| Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 148 | root->log_start_pid = current->pid; | 
| Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 149 | } else if (root->log_start_pid != current->pid) { | 
| Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 150 | set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); | 
| Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 151 | } | 
| Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 152 | } else { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 153 | mutex_lock(&fs_info->tree_log_mutex); | 
|  | 154 | if (!fs_info->log_root_tree) | 
|  | 155 | ret = btrfs_init_log_root_tree(trans, fs_info); | 
|  | 156 | mutex_unlock(&fs_info->tree_log_mutex); | 
| Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 157 | if (ret) | 
|  | 158 | goto out; | 
| Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 159 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 160 | ret = btrfs_add_log_tree(trans, root); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 161 | if (ret) | 
| Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 162 | goto out; | 
| Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 163 |  | 
|  | 164 | clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); | 
|  | 165 | root->log_start_pid = current->pid; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 166 | } | 
| Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 167 |  | 
| Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 168 | atomic_inc(&root->log_batch); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 169 | atomic_inc(&root->log_writers); | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 170 | if (ctx) { | 
| Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 171 | int index = root->log_transid % 2; | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 172 | list_add_tail(&ctx->list, &root->log_ctxs[index]); | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 173 | ctx->log_transid = root->log_transid; | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 174 | } | 
| Zhaolei | 34eb2a5 | 2015-08-17 18:44:45 +0800 | [diff] [blame] | 175 |  | 
| Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 176 | out: | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 177 | mutex_unlock(&root->log_mutex); | 
| Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 178 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 179 | } | 
|  | 180 |  | 
|  | 181 | /* | 
|  | 182 | * returns 0 if there was a log transaction running and we were able | 
|  | 183 | * to join, or returns -ENOENT if there were not transactions | 
|  | 184 | * in progress | 
|  | 185 | */ | 
|  | 186 | static int join_running_log_trans(struct btrfs_root *root) | 
|  | 187 | { | 
|  | 188 | int ret = -ENOENT; | 
|  | 189 |  | 
|  | 190 | smp_mb(); | 
|  | 191 | if (!root->log_root) | 
|  | 192 | return -ENOENT; | 
|  | 193 |  | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 194 | mutex_lock(&root->log_mutex); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 195 | if (root->log_root) { | 
|  | 196 | ret = 0; | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 197 | atomic_inc(&root->log_writers); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 198 | } | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 199 | mutex_unlock(&root->log_mutex); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 200 | return ret; | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | /* | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 204 | * This either makes the current running log transaction wait | 
|  | 205 | * until you call btrfs_end_log_trans() or it makes any future | 
|  | 206 | * log transactions wait until you call btrfs_end_log_trans() | 
|  | 207 | */ | 
| zhong jiang | 45128b0 | 2018-08-17 00:37:15 +0800 | [diff] [blame^] | 208 | void btrfs_pin_log_trans(struct btrfs_root *root) | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 209 | { | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 210 | mutex_lock(&root->log_mutex); | 
|  | 211 | atomic_inc(&root->log_writers); | 
|  | 212 | mutex_unlock(&root->log_mutex); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 213 | } | 
|  | 214 |  | 
|  | 215 | /* | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 216 | * indicate we're done making changes to the log tree | 
|  | 217 | * and wake up anyone waiting to do a sync | 
|  | 218 | */ | 
| Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 219 | void btrfs_end_log_trans(struct btrfs_root *root) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 220 | { | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 221 | if (atomic_dec_and_test(&root->log_writers)) { | 
| David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 222 | /* atomic_dec_and_test implies a barrier */ | 
|  | 223 | cond_wake_up_nomb(&root->log_writer_wait); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 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, | 
| Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 270 | struct walk_control *wc, u64 gen, int level); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 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, | 
| Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 278 | struct walk_control *wc, u64 gen, int level) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 279 | { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 280 | struct btrfs_fs_info *fs_info = log->fs_info; | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 281 | int ret = 0; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 282 |  | 
| Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 283 | /* | 
|  | 284 | * If this fs is mixed then we need to be able to process the leaves to | 
|  | 285 | * pin down any logged extents, so we have to read the block. | 
|  | 286 | */ | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 287 | if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { | 
| Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 288 | ret = btrfs_read_buffer(eb, gen, level, NULL); | 
| Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 289 | if (ret) | 
|  | 290 | return ret; | 
|  | 291 | } | 
|  | 292 |  | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 293 | if (wc->pin) | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 294 | ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start, | 
|  | 295 | eb->len); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 296 |  | 
|  | 297 | if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) { | 
| Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 298 | if (wc->pin && btrfs_header_level(eb) == 0) | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 299 | ret = btrfs_exclude_logged_extents(fs_info, eb); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 300 | if (wc->write) | 
|  | 301 | btrfs_write_tree_block(eb); | 
|  | 302 | if (wc->wait) | 
|  | 303 | btrfs_wait_tree_block_writeback(eb); | 
|  | 304 | } | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 305 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 306 | } | 
|  | 307 |  | 
|  | 308 | /* | 
|  | 309 | * Item overwrite used by replay and tree logging.  eb, slot and key all refer | 
|  | 310 | * to the src data we are copying out. | 
|  | 311 | * | 
|  | 312 | * root is the tree we are copying into, and path is a scratch | 
|  | 313 | * path for use in this function (it should be released on entry and | 
|  | 314 | * will be released on exit). | 
|  | 315 | * | 
|  | 316 | * If the key is already in the destination tree the existing item is | 
|  | 317 | * overwritten.  If the existing item isn't big enough, it is extended. | 
|  | 318 | * If it is too large, it is truncated. | 
|  | 319 | * | 
|  | 320 | * If the key isn't in the destination yet, a new item is inserted. | 
|  | 321 | */ | 
|  | 322 | static noinline int overwrite_item(struct btrfs_trans_handle *trans, | 
|  | 323 | struct btrfs_root *root, | 
|  | 324 | struct btrfs_path *path, | 
|  | 325 | struct extent_buffer *eb, int slot, | 
|  | 326 | struct btrfs_key *key) | 
|  | 327 | { | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 328 | struct btrfs_fs_info *fs_info = root->fs_info; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 329 | int ret; | 
|  | 330 | u32 item_size; | 
|  | 331 | u64 saved_i_size = 0; | 
|  | 332 | int save_old_i_size = 0; | 
|  | 333 | unsigned long src_ptr; | 
|  | 334 | unsigned long dst_ptr; | 
|  | 335 | int overwrite_root = 0; | 
| Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 336 | bool inode_item = key->type == BTRFS_INODE_ITEM_KEY; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 337 |  | 
|  | 338 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | 
|  | 339 | overwrite_root = 1; | 
|  | 340 |  | 
|  | 341 | item_size = btrfs_item_size_nr(eb, slot); | 
|  | 342 | src_ptr = btrfs_item_ptr_offset(eb, slot); | 
|  | 343 |  | 
|  | 344 | /* look for the key in the destination tree */ | 
|  | 345 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | 
| Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 346 | if (ret < 0) | 
|  | 347 | return ret; | 
|  | 348 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 349 | if (ret == 0) { | 
|  | 350 | char *src_copy; | 
|  | 351 | char *dst_copy; | 
|  | 352 | u32 dst_size = btrfs_item_size_nr(path->nodes[0], | 
|  | 353 | path->slots[0]); | 
|  | 354 | if (dst_size != item_size) | 
|  | 355 | goto insert; | 
|  | 356 |  | 
|  | 357 | if (item_size == 0) { | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 358 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 359 | return 0; | 
|  | 360 | } | 
|  | 361 | dst_copy = kmalloc(item_size, GFP_NOFS); | 
|  | 362 | src_copy = kmalloc(item_size, GFP_NOFS); | 
| liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 363 | if (!dst_copy || !src_copy) { | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 364 | btrfs_release_path(path); | 
| liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 365 | kfree(dst_copy); | 
|  | 366 | kfree(src_copy); | 
|  | 367 | return -ENOMEM; | 
|  | 368 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 369 |  | 
|  | 370 | read_extent_buffer(eb, src_copy, src_ptr, item_size); | 
|  | 371 |  | 
|  | 372 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | 
|  | 373 | read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, | 
|  | 374 | item_size); | 
|  | 375 | ret = memcmp(dst_copy, src_copy, item_size); | 
|  | 376 |  | 
|  | 377 | kfree(dst_copy); | 
|  | 378 | kfree(src_copy); | 
|  | 379 | /* | 
|  | 380 | * they have the same contents, just return, this saves | 
|  | 381 | * us from cowing blocks in the destination tree and doing | 
|  | 382 | * extra writes that may not have been done by a previous | 
|  | 383 | * sync | 
|  | 384 | */ | 
|  | 385 | if (ret == 0) { | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 386 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 387 | return 0; | 
|  | 388 | } | 
|  | 389 |  | 
| Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 390 | /* | 
|  | 391 | * We need to load the old nbytes into the inode so when we | 
|  | 392 | * replay the extents we've logged we get the right nbytes. | 
|  | 393 | */ | 
|  | 394 | if (inode_item) { | 
|  | 395 | struct btrfs_inode_item *item; | 
|  | 396 | u64 nbytes; | 
| Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 397 | u32 mode; | 
| Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 398 |  | 
|  | 399 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | 
|  | 400 | struct btrfs_inode_item); | 
|  | 401 | nbytes = btrfs_inode_nbytes(path->nodes[0], item); | 
|  | 402 | item = btrfs_item_ptr(eb, slot, | 
|  | 403 | struct btrfs_inode_item); | 
|  | 404 | btrfs_set_inode_nbytes(eb, item, nbytes); | 
| Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 405 |  | 
|  | 406 | /* | 
|  | 407 | * If this is a directory we need to reset the i_size to | 
|  | 408 | * 0 so that we can set it up properly when replaying | 
|  | 409 | * the rest of the items in this log. | 
|  | 410 | */ | 
|  | 411 | mode = btrfs_inode_mode(eb, item); | 
|  | 412 | if (S_ISDIR(mode)) | 
|  | 413 | btrfs_set_inode_size(eb, item, 0); | 
| Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 414 | } | 
|  | 415 | } else if (inode_item) { | 
|  | 416 | struct btrfs_inode_item *item; | 
| Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 417 | u32 mode; | 
| Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 418 |  | 
|  | 419 | /* | 
|  | 420 | * New inode, set nbytes to 0 so that the nbytes comes out | 
|  | 421 | * properly when we replay the extents. | 
|  | 422 | */ | 
|  | 423 | item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); | 
|  | 424 | btrfs_set_inode_nbytes(eb, item, 0); | 
| Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 425 |  | 
|  | 426 | /* | 
|  | 427 | * If this is a directory we need to reset the i_size to 0 so | 
|  | 428 | * that we can set it up properly when replaying the rest of | 
|  | 429 | * the items in this log. | 
|  | 430 | */ | 
|  | 431 | mode = btrfs_inode_mode(eb, item); | 
|  | 432 | if (S_ISDIR(mode)) | 
|  | 433 | btrfs_set_inode_size(eb, item, 0); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 434 | } | 
|  | 435 | insert: | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 436 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 437 | /* try to insert the key into the destination tree */ | 
| Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 438 | path->skip_release_on_error = 1; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 439 | ret = btrfs_insert_empty_item(trans, root, path, | 
|  | 440 | key, item_size); | 
| Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 441 | path->skip_release_on_error = 0; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 442 |  | 
|  | 443 | /* make sure any existing item is the correct size */ | 
| Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 444 | if (ret == -EEXIST || ret == -EOVERFLOW) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 445 | u32 found_size; | 
|  | 446 | found_size = btrfs_item_size_nr(path->nodes[0], | 
|  | 447 | path->slots[0]); | 
| Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 448 | if (found_size > item_size) | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 449 | btrfs_truncate_item(fs_info, path, item_size, 1); | 
| Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 450 | else if (found_size < item_size) | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 451 | btrfs_extend_item(fs_info, path, | 
| Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 452 | item_size - found_size); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 453 | } else if (ret) { | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 454 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 455 | } | 
|  | 456 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], | 
|  | 457 | path->slots[0]); | 
|  | 458 |  | 
|  | 459 | /* don't overwrite an existing inode if the generation number | 
|  | 460 | * was logged as zero.  This is done when the tree logging code | 
|  | 461 | * is just logging an inode to make sure it exists after recovery. | 
|  | 462 | * | 
|  | 463 | * Also, don't overwrite i_size on directories during replay. | 
|  | 464 | * log replay inserts and removes directory items based on the | 
|  | 465 | * state of the tree found in the subvolume, and i_size is modified | 
|  | 466 | * as it goes | 
|  | 467 | */ | 
|  | 468 | if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) { | 
|  | 469 | struct btrfs_inode_item *src_item; | 
|  | 470 | struct btrfs_inode_item *dst_item; | 
|  | 471 |  | 
|  | 472 | src_item = (struct btrfs_inode_item *)src_ptr; | 
|  | 473 | dst_item = (struct btrfs_inode_item *)dst_ptr; | 
|  | 474 |  | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 475 | if (btrfs_inode_generation(eb, src_item) == 0) { | 
|  | 476 | struct extent_buffer *dst_eb = path->nodes[0]; | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 477 | const u64 ino_size = btrfs_inode_size(eb, src_item); | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 478 |  | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 479 | /* | 
|  | 480 | * For regular files an ino_size == 0 is used only when | 
|  | 481 | * logging that an inode exists, as part of a directory | 
|  | 482 | * fsync, and the inode wasn't fsynced before. In this | 
|  | 483 | * case don't set the size of the inode in the fs/subvol | 
|  | 484 | * tree, otherwise we would be throwing valid data away. | 
|  | 485 | */ | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 486 | if (S_ISREG(btrfs_inode_mode(eb, src_item)) && | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 487 | S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) && | 
|  | 488 | ino_size != 0) { | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 489 | struct btrfs_map_token token; | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 490 |  | 
|  | 491 | btrfs_init_map_token(&token); | 
|  | 492 | btrfs_set_token_inode_size(dst_eb, dst_item, | 
|  | 493 | ino_size, &token); | 
|  | 494 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 495 | goto no_copy; | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 496 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 497 |  | 
|  | 498 | if (overwrite_root && | 
|  | 499 | S_ISDIR(btrfs_inode_mode(eb, src_item)) && | 
|  | 500 | S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { | 
|  | 501 | save_old_i_size = 1; | 
|  | 502 | saved_i_size = btrfs_inode_size(path->nodes[0], | 
|  | 503 | dst_item); | 
|  | 504 | } | 
|  | 505 | } | 
|  | 506 |  | 
|  | 507 | copy_extent_buffer(path->nodes[0], eb, dst_ptr, | 
|  | 508 | src_ptr, item_size); | 
|  | 509 |  | 
|  | 510 | if (save_old_i_size) { | 
|  | 511 | struct btrfs_inode_item *dst_item; | 
|  | 512 | dst_item = (struct btrfs_inode_item *)dst_ptr; | 
|  | 513 | btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); | 
|  | 514 | } | 
|  | 515 |  | 
|  | 516 | /* make sure the generation is filled in */ | 
|  | 517 | if (key->type == BTRFS_INODE_ITEM_KEY) { | 
|  | 518 | struct btrfs_inode_item *dst_item; | 
|  | 519 | dst_item = (struct btrfs_inode_item *)dst_ptr; | 
|  | 520 | if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { | 
|  | 521 | btrfs_set_inode_generation(path->nodes[0], dst_item, | 
|  | 522 | trans->transid); | 
|  | 523 | } | 
|  | 524 | } | 
|  | 525 | no_copy: | 
|  | 526 | btrfs_mark_buffer_dirty(path->nodes[0]); | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 527 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 528 | return 0; | 
|  | 529 | } | 
|  | 530 |  | 
|  | 531 | /* | 
|  | 532 | * simple helper to read an inode off the disk from a given root | 
|  | 533 | * This can only be called for subvolume roots and not for the log | 
|  | 534 | */ | 
|  | 535 | static noinline struct inode *read_one_inode(struct btrfs_root *root, | 
|  | 536 | u64 objectid) | 
|  | 537 | { | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 538 | struct btrfs_key key; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 539 | struct inode *inode; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 540 |  | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 541 | key.objectid = objectid; | 
|  | 542 | key.type = BTRFS_INODE_ITEM_KEY; | 
|  | 543 | key.offset = 0; | 
| Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 544 | inode = btrfs_iget(root->fs_info->sb, &key, root, NULL); | 
| Al Viro | 2e19f1f | 2018-07-29 23:04:45 +0100 | [diff] [blame] | 545 | if (IS_ERR(inode)) | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 546 | inode = NULL; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 547 | return inode; | 
|  | 548 | } | 
|  | 549 |  | 
|  | 550 | /* replays a single extent in 'eb' at 'slot' with 'key' into the | 
|  | 551 | * subvolume 'root'.  path is released on entry and should be released | 
|  | 552 | * on exit. | 
|  | 553 | * | 
|  | 554 | * extents in the log tree have not been allocated out of the extent | 
|  | 555 | * tree yet.  So, this completes the allocation, taking a reference | 
|  | 556 | * as required if the extent already exists or creating a new extent | 
|  | 557 | * if it isn't in the extent allocation tree yet. | 
|  | 558 | * | 
|  | 559 | * The extent is inserted into the file, dropping any existing extents | 
|  | 560 | * from the file that overlap the new one. | 
|  | 561 | */ | 
|  | 562 | static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | 
|  | 563 | struct btrfs_root *root, | 
|  | 564 | struct btrfs_path *path, | 
|  | 565 | struct extent_buffer *eb, int slot, | 
|  | 566 | struct btrfs_key *key) | 
|  | 567 | { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 568 | struct btrfs_fs_info *fs_info = root->fs_info; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 569 | int found_type; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 570 | u64 extent_end; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 571 | u64 start = key->offset; | 
| Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 572 | u64 nbytes = 0; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 573 | struct btrfs_file_extent_item *item; | 
|  | 574 | struct inode *inode = NULL; | 
|  | 575 | unsigned long size; | 
|  | 576 | int ret = 0; | 
|  | 577 |  | 
|  | 578 | item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); | 
|  | 579 | found_type = btrfs_file_extent_type(eb, item); | 
|  | 580 |  | 
| Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 581 | if (found_type == BTRFS_FILE_EXTENT_REG || | 
| Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 582 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | 
|  | 583 | nbytes = btrfs_file_extent_num_bytes(eb, item); | 
|  | 584 | extent_end = start + nbytes; | 
|  | 585 |  | 
|  | 586 | /* | 
|  | 587 | * We don't add to the inodes nbytes if we are prealloc or a | 
|  | 588 | * hole. | 
|  | 589 | */ | 
|  | 590 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) | 
|  | 591 | nbytes = 0; | 
|  | 592 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | 
| Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 593 | size = btrfs_file_extent_ram_bytes(eb, item); | 
| Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 594 | nbytes = btrfs_file_extent_ram_bytes(eb, item); | 
| Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 595 | extent_end = ALIGN(start + size, | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 596 | fs_info->sectorsize); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 597 | } else { | 
|  | 598 | ret = 0; | 
|  | 599 | goto out; | 
|  | 600 | } | 
|  | 601 |  | 
|  | 602 | inode = read_one_inode(root, key->objectid); | 
|  | 603 | if (!inode) { | 
|  | 604 | ret = -EIO; | 
|  | 605 | goto out; | 
|  | 606 | } | 
|  | 607 |  | 
|  | 608 | /* | 
|  | 609 | * first check to see if we already have this extent in the | 
|  | 610 | * file.  This must be done before the btrfs_drop_extents run | 
|  | 611 | * so we don't try to drop this extent. | 
|  | 612 | */ | 
| David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 613 | ret = btrfs_lookup_file_extent(trans, root, path, | 
|  | 614 | btrfs_ino(BTRFS_I(inode)), start, 0); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 615 |  | 
| Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 616 | if (ret == 0 && | 
|  | 617 | (found_type == BTRFS_FILE_EXTENT_REG || | 
|  | 618 | found_type == BTRFS_FILE_EXTENT_PREALLOC)) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 619 | struct btrfs_file_extent_item cmp1; | 
|  | 620 | struct btrfs_file_extent_item cmp2; | 
|  | 621 | struct btrfs_file_extent_item *existing; | 
|  | 622 | struct extent_buffer *leaf; | 
|  | 623 |  | 
|  | 624 | leaf = path->nodes[0]; | 
|  | 625 | existing = btrfs_item_ptr(leaf, path->slots[0], | 
|  | 626 | struct btrfs_file_extent_item); | 
|  | 627 |  | 
|  | 628 | read_extent_buffer(eb, &cmp1, (unsigned long)item, | 
|  | 629 | sizeof(cmp1)); | 
|  | 630 | read_extent_buffer(leaf, &cmp2, (unsigned long)existing, | 
|  | 631 | sizeof(cmp2)); | 
|  | 632 |  | 
|  | 633 | /* | 
|  | 634 | * we already have a pointer to this exact extent, | 
|  | 635 | * we don't have to do anything | 
|  | 636 | */ | 
|  | 637 | if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 638 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 639 | goto out; | 
|  | 640 | } | 
|  | 641 | } | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 642 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 643 |  | 
|  | 644 | /* drop any overlapping extents */ | 
| Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 645 | ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 646 | if (ret) | 
|  | 647 | goto out; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 648 |  | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 649 | if (found_type == BTRFS_FILE_EXTENT_REG || | 
|  | 650 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 651 | u64 offset; | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 652 | unsigned long dest_offset; | 
|  | 653 | struct btrfs_key ins; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 654 |  | 
| Filipe Manana | 3168021c | 2017-02-01 14:58:02 +0000 | [diff] [blame] | 655 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0 && | 
|  | 656 | btrfs_fs_incompat(fs_info, NO_HOLES)) | 
|  | 657 | goto update_inode; | 
|  | 658 |  | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 659 | ret = btrfs_insert_empty_item(trans, root, path, key, | 
|  | 660 | sizeof(*item)); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 661 | if (ret) | 
|  | 662 | goto out; | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 663 | dest_offset = btrfs_item_ptr_offset(path->nodes[0], | 
|  | 664 | path->slots[0]); | 
|  | 665 | copy_extent_buffer(path->nodes[0], eb, dest_offset, | 
|  | 666 | (unsigned long)item,  sizeof(*item)); | 
|  | 667 |  | 
|  | 668 | ins.objectid = btrfs_file_extent_disk_bytenr(eb, item); | 
|  | 669 | ins.offset = btrfs_file_extent_disk_num_bytes(eb, item); | 
|  | 670 | ins.type = BTRFS_EXTENT_ITEM_KEY; | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 671 | offset = key->offset - btrfs_file_extent_offset(eb, item); | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 672 |  | 
| Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 673 | /* | 
|  | 674 | * Manually record dirty extent, as here we did a shallow | 
|  | 675 | * file extent item copy and skip normal backref update, | 
|  | 676 | * but modifying extent tree all by ourselves. | 
|  | 677 | * So need to manually record dirty extent for qgroup, | 
|  | 678 | * as the owner of the file extent changed from log tree | 
|  | 679 | * (doesn't affect qgroup) to fs/file tree(affects qgroup) | 
|  | 680 | */ | 
| Lu Fengqi | a95f3aa | 2018-07-18 16:28:03 +0800 | [diff] [blame] | 681 | ret = btrfs_qgroup_trace_extent(trans, | 
| Qu Wenruo | df2c95f | 2016-08-15 10:36:52 +0800 | [diff] [blame] | 682 | btrfs_file_extent_disk_bytenr(eb, item), | 
|  | 683 | btrfs_file_extent_disk_num_bytes(eb, item), | 
|  | 684 | GFP_NOFS); | 
|  | 685 | if (ret < 0) | 
|  | 686 | goto out; | 
|  | 687 |  | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 688 | if (ins.objectid > 0) { | 
|  | 689 | u64 csum_start; | 
|  | 690 | u64 csum_end; | 
|  | 691 | LIST_HEAD(ordered_sums); | 
|  | 692 | /* | 
|  | 693 | * is this extent already allocated in the extent | 
|  | 694 | * allocation tree?  If so, just add a reference | 
|  | 695 | */ | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 696 | ret = btrfs_lookup_data_extent(fs_info, ins.objectid, | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 697 | ins.offset); | 
|  | 698 | if (ret == 0) { | 
| Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 699 | ret = btrfs_inc_extent_ref(trans, root, | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 700 | ins.objectid, ins.offset, | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 701 | 0, root->root_key.objectid, | 
| Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 702 | key->objectid, offset); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 703 | if (ret) | 
|  | 704 | goto out; | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 705 | } else { | 
|  | 706 | /* | 
|  | 707 | * insert the extent pointer in the extent | 
|  | 708 | * allocation tree | 
|  | 709 | */ | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 710 | ret = btrfs_alloc_logged_file_extent(trans, | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 711 | root->root_key.objectid, | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 712 | key->objectid, offset, &ins); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 713 | if (ret) | 
|  | 714 | goto out; | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 715 | } | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 716 | btrfs_release_path(path); | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 717 |  | 
|  | 718 | if (btrfs_file_extent_compression(eb, item)) { | 
|  | 719 | csum_start = ins.objectid; | 
|  | 720 | csum_end = csum_start + ins.offset; | 
|  | 721 | } else { | 
|  | 722 | csum_start = ins.objectid + | 
|  | 723 | btrfs_file_extent_offset(eb, item); | 
|  | 724 | csum_end = csum_start + | 
|  | 725 | btrfs_file_extent_num_bytes(eb, item); | 
|  | 726 | } | 
|  | 727 |  | 
|  | 728 | ret = btrfs_lookup_csums_range(root->log_root, | 
|  | 729 | csum_start, csum_end - 1, | 
| Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 730 | &ordered_sums, 0); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 731 | if (ret) | 
|  | 732 | goto out; | 
| Filipe Manana | b84b839 | 2015-08-19 11:09:40 +0100 | [diff] [blame] | 733 | /* | 
|  | 734 | * Now delete all existing cums in the csum root that | 
|  | 735 | * cover our range. We do this because we can have an | 
|  | 736 | * extent that is completely referenced by one file | 
|  | 737 | * extent item and partially referenced by another | 
|  | 738 | * file extent item (like after using the clone or | 
|  | 739 | * extent_same ioctls). In this case if we end up doing | 
|  | 740 | * the replay of the one that partially references the | 
|  | 741 | * extent first, and we do not do the csum deletion | 
|  | 742 | * below, we can get 2 csum items in the csum tree that | 
|  | 743 | * overlap each other. For example, imagine our log has | 
|  | 744 | * the two following file extent items: | 
|  | 745 | * | 
|  | 746 | * key (257 EXTENT_DATA 409600) | 
|  | 747 | *     extent data disk byte 12845056 nr 102400 | 
|  | 748 | *     extent data offset 20480 nr 20480 ram 102400 | 
|  | 749 | * | 
|  | 750 | * key (257 EXTENT_DATA 819200) | 
|  | 751 | *     extent data disk byte 12845056 nr 102400 | 
|  | 752 | *     extent data offset 0 nr 102400 ram 102400 | 
|  | 753 | * | 
|  | 754 | * Where the second one fully references the 100K extent | 
|  | 755 | * that starts at disk byte 12845056, and the log tree | 
|  | 756 | * has a single csum item that covers the entire range | 
|  | 757 | * of the extent: | 
|  | 758 | * | 
|  | 759 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 | 
|  | 760 | * | 
|  | 761 | * After the first file extent item is replayed, the | 
|  | 762 | * csum tree gets the following csum item: | 
|  | 763 | * | 
|  | 764 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 | 
|  | 765 | * | 
|  | 766 | * Which covers the 20K sub-range starting at offset 20K | 
|  | 767 | * of our extent. Now when we replay the second file | 
|  | 768 | * extent item, if we do not delete existing csum items | 
|  | 769 | * that cover any of its blocks, we end up getting two | 
|  | 770 | * csum items in our csum tree that overlap each other: | 
|  | 771 | * | 
|  | 772 | * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100 | 
|  | 773 | * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20 | 
|  | 774 | * | 
|  | 775 | * Which is a problem, because after this anyone trying | 
|  | 776 | * to lookup up for the checksum of any block of our | 
|  | 777 | * extent starting at an offset of 40K or higher, will | 
|  | 778 | * end up looking at the second csum item only, which | 
|  | 779 | * does not contain the checksum for any block starting | 
|  | 780 | * at offset 40K or higher of our extent. | 
|  | 781 | */ | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 782 | while (!list_empty(&ordered_sums)) { | 
|  | 783 | struct btrfs_ordered_sum *sums; | 
|  | 784 | sums = list_entry(ordered_sums.next, | 
|  | 785 | struct btrfs_ordered_sum, | 
|  | 786 | list); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 787 | if (!ret) | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 788 | ret = btrfs_del_csums(trans, fs_info, | 
| Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 789 | sums->bytenr, | 
|  | 790 | sums->len); | 
| Filipe Manana | b84b839 | 2015-08-19 11:09:40 +0100 | [diff] [blame] | 791 | if (!ret) | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 792 | ret = btrfs_csum_file_blocks(trans, | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 793 | fs_info->csum_root, sums); | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 794 | list_del(&sums->list); | 
|  | 795 | kfree(sums); | 
|  | 796 | } | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 797 | if (ret) | 
|  | 798 | goto out; | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 799 | } else { | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 800 | btrfs_release_path(path); | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 801 | } | 
|  | 802 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | 
|  | 803 | /* inline extents are easy, we just overwrite them */ | 
|  | 804 | ret = overwrite_item(trans, root, path, eb, slot, key); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 805 | if (ret) | 
|  | 806 | goto out; | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 807 | } | 
|  | 808 |  | 
| Josef Bacik | 4bc4bee | 2013-04-05 20:50:09 +0000 | [diff] [blame] | 809 | inode_add_bytes(inode, nbytes); | 
| Filipe Manana | 3168021c | 2017-02-01 14:58:02 +0000 | [diff] [blame] | 810 | update_inode: | 
| Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 811 | ret = btrfs_update_inode(trans, root, inode); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 812 | out: | 
|  | 813 | if (inode) | 
|  | 814 | iput(inode); | 
|  | 815 | return ret; | 
|  | 816 | } | 
|  | 817 |  | 
|  | 818 | /* | 
|  | 819 | * when cleaning up conflicts between the directory names in the | 
|  | 820 | * subvolume, directory names in the log and directory names in the | 
|  | 821 | * inode back references, we may have to unlink inodes from directories. | 
|  | 822 | * | 
|  | 823 | * This is a helper function to do the unlink of a specific directory | 
|  | 824 | * item | 
|  | 825 | */ | 
|  | 826 | static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, | 
|  | 827 | struct btrfs_root *root, | 
|  | 828 | struct btrfs_path *path, | 
| Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 829 | struct btrfs_inode *dir, | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 830 | struct btrfs_dir_item *di) | 
|  | 831 | { | 
|  | 832 | struct inode *inode; | 
|  | 833 | char *name; | 
|  | 834 | int name_len; | 
|  | 835 | struct extent_buffer *leaf; | 
|  | 836 | struct btrfs_key location; | 
|  | 837 | int ret; | 
|  | 838 |  | 
|  | 839 | leaf = path->nodes[0]; | 
|  | 840 |  | 
|  | 841 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | 
|  | 842 | name_len = btrfs_dir_name_len(leaf, di); | 
|  | 843 | name = kmalloc(name_len, GFP_NOFS); | 
| liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 844 | if (!name) | 
|  | 845 | return -ENOMEM; | 
|  | 846 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 847 | read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len); | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 848 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 849 |  | 
|  | 850 | inode = read_one_inode(root, location.objectid); | 
| Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 851 | if (!inode) { | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 852 | ret = -EIO; | 
|  | 853 | goto out; | 
| Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 854 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 855 |  | 
| Yan Zheng | ec051c0 | 2009-01-05 15:43:42 -0500 | [diff] [blame] | 856 | ret = link_to_fixup_dir(trans, root, path, location.objectid); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 857 | if (ret) | 
|  | 858 | goto out; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 859 |  | 
| Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 860 | ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name, | 
|  | 861 | name_len); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 862 | if (ret) | 
|  | 863 | goto out; | 
| Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 864 | else | 
| Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 865 | ret = btrfs_run_delayed_items(trans); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 866 | out: | 
|  | 867 | kfree(name); | 
|  | 868 | iput(inode); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 869 | return ret; | 
|  | 870 | } | 
|  | 871 |  | 
|  | 872 | /* | 
|  | 873 | * helper function to see if a given name and sequence number found | 
|  | 874 | * in an inode back reference are already in a directory and correctly | 
|  | 875 | * point to this inode | 
|  | 876 | */ | 
|  | 877 | static noinline int inode_in_dir(struct btrfs_root *root, | 
|  | 878 | struct btrfs_path *path, | 
|  | 879 | u64 dirid, u64 objectid, u64 index, | 
|  | 880 | const char *name, int name_len) | 
|  | 881 | { | 
|  | 882 | struct btrfs_dir_item *di; | 
|  | 883 | struct btrfs_key location; | 
|  | 884 | int match = 0; | 
|  | 885 |  | 
|  | 886 | di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, | 
|  | 887 | index, name, name_len, 0); | 
|  | 888 | if (di && !IS_ERR(di)) { | 
|  | 889 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); | 
|  | 890 | if (location.objectid != objectid) | 
|  | 891 | goto out; | 
|  | 892 | } else | 
|  | 893 | goto out; | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 894 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 895 |  | 
|  | 896 | di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0); | 
|  | 897 | if (di && !IS_ERR(di)) { | 
|  | 898 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); | 
|  | 899 | if (location.objectid != objectid) | 
|  | 900 | goto out; | 
|  | 901 | } else | 
|  | 902 | goto out; | 
|  | 903 | match = 1; | 
|  | 904 | out: | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 905 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 906 | return match; | 
|  | 907 | } | 
|  | 908 |  | 
|  | 909 | /* | 
|  | 910 | * helper function to check a log tree for a named back reference in | 
|  | 911 | * an inode.  This is used to decide if a back reference that is | 
|  | 912 | * found in the subvolume conflicts with what we find in the log. | 
|  | 913 | * | 
|  | 914 | * inode backreferences may have multiple refs in a single item, | 
|  | 915 | * during replay we process one reference at a time, and we don't | 
|  | 916 | * want to delete valid links to a file from the subvolume if that | 
|  | 917 | * link is also in the log. | 
|  | 918 | */ | 
|  | 919 | static noinline int backref_in_log(struct btrfs_root *log, | 
|  | 920 | struct btrfs_key *key, | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 921 | u64 ref_objectid, | 
| Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 922 | const char *name, int namelen) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 923 | { | 
|  | 924 | struct btrfs_path *path; | 
|  | 925 | struct btrfs_inode_ref *ref; | 
|  | 926 | unsigned long ptr; | 
|  | 927 | unsigned long ptr_end; | 
|  | 928 | unsigned long name_ptr; | 
|  | 929 | int found_name_len; | 
|  | 930 | int item_size; | 
|  | 931 | int ret; | 
|  | 932 | int match = 0; | 
|  | 933 |  | 
|  | 934 | path = btrfs_alloc_path(); | 
| liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 935 | if (!path) | 
|  | 936 | return -ENOMEM; | 
|  | 937 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 938 | ret = btrfs_search_slot(NULL, log, key, path, 0, 0); | 
|  | 939 | if (ret != 0) | 
|  | 940 | goto out; | 
|  | 941 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 942 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 943 |  | 
|  | 944 | if (key->type == BTRFS_INODE_EXTREF_KEY) { | 
| Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 945 | if (btrfs_find_name_in_ext_backref(path->nodes[0], | 
|  | 946 | path->slots[0], | 
|  | 947 | ref_objectid, | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 948 | name, namelen, NULL)) | 
|  | 949 | match = 1; | 
|  | 950 |  | 
|  | 951 | goto out; | 
|  | 952 | } | 
|  | 953 |  | 
|  | 954 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 955 | ptr_end = ptr + item_size; | 
|  | 956 | while (ptr < ptr_end) { | 
|  | 957 | ref = (struct btrfs_inode_ref *)ptr; | 
|  | 958 | found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref); | 
|  | 959 | if (found_name_len == namelen) { | 
|  | 960 | name_ptr = (unsigned long)(ref + 1); | 
|  | 961 | ret = memcmp_extent_buffer(path->nodes[0], name, | 
|  | 962 | name_ptr, namelen); | 
|  | 963 | if (ret == 0) { | 
|  | 964 | match = 1; | 
|  | 965 | goto out; | 
|  | 966 | } | 
|  | 967 | } | 
|  | 968 | ptr = (unsigned long)(ref + 1) + found_name_len; | 
|  | 969 | } | 
|  | 970 | out: | 
|  | 971 | btrfs_free_path(path); | 
|  | 972 | return match; | 
|  | 973 | } | 
|  | 974 |  | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 975 | static inline int __add_inode_ref(struct btrfs_trans_handle *trans, | 
|  | 976 | struct btrfs_root *root, | 
|  | 977 | struct btrfs_path *path, | 
|  | 978 | struct btrfs_root *log_root, | 
| Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 979 | struct btrfs_inode *dir, | 
|  | 980 | struct btrfs_inode *inode, | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 981 | u64 inode_objectid, u64 parent_objectid, | 
|  | 982 | u64 ref_index, char *name, int namelen, | 
|  | 983 | int *search_done) | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 984 | { | 
|  | 985 | int ret; | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 986 | char *victim_name; | 
|  | 987 | int victim_name_len; | 
|  | 988 | struct extent_buffer *leaf; | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 989 | struct btrfs_dir_item *di; | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 990 | struct btrfs_key search_key; | 
|  | 991 | struct btrfs_inode_extref *extref; | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 992 |  | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 993 | again: | 
|  | 994 | /* Search old style refs */ | 
|  | 995 | search_key.objectid = inode_objectid; | 
|  | 996 | search_key.type = BTRFS_INODE_REF_KEY; | 
|  | 997 | search_key.offset = parent_objectid; | 
|  | 998 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 999 | if (ret == 0) { | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1000 | struct btrfs_inode_ref *victim_ref; | 
|  | 1001 | unsigned long ptr; | 
|  | 1002 | unsigned long ptr_end; | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1003 |  | 
|  | 1004 | leaf = path->nodes[0]; | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1005 |  | 
|  | 1006 | /* are we trying to overwrite a back ref for the root directory | 
|  | 1007 | * if so, just jump out, we're done | 
|  | 1008 | */ | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1009 | if (search_key.objectid == search_key.offset) | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1010 | return 1; | 
|  | 1011 |  | 
|  | 1012 | /* check all the names in this back reference to see | 
|  | 1013 | * if they are in the log.  if so, we allow them to stay | 
|  | 1014 | * otherwise they must be unlinked as a conflict | 
|  | 1015 | */ | 
|  | 1016 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | 
|  | 1017 | ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]); | 
|  | 1018 | while (ptr < ptr_end) { | 
|  | 1019 | victim_ref = (struct btrfs_inode_ref *)ptr; | 
|  | 1020 | victim_name_len = btrfs_inode_ref_name_len(leaf, | 
|  | 1021 | victim_ref); | 
|  | 1022 | victim_name = kmalloc(victim_name_len, GFP_NOFS); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1023 | if (!victim_name) | 
|  | 1024 | return -ENOMEM; | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1025 |  | 
|  | 1026 | read_extent_buffer(leaf, victim_name, | 
|  | 1027 | (unsigned long)(victim_ref + 1), | 
|  | 1028 | victim_name_len); | 
|  | 1029 |  | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1030 | if (!backref_in_log(log_root, &search_key, | 
|  | 1031 | parent_objectid, | 
|  | 1032 | victim_name, | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1033 | victim_name_len)) { | 
| Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1034 | inc_nlink(&inode->vfs_inode); | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1035 | btrfs_release_path(path); | 
|  | 1036 |  | 
| Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1037 | ret = btrfs_unlink_inode(trans, root, dir, inode, | 
| Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1038 | victim_name, victim_name_len); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1039 | kfree(victim_name); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1040 | if (ret) | 
|  | 1041 | return ret; | 
| Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 1042 | ret = btrfs_run_delayed_items(trans); | 
| Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 1043 | if (ret) | 
|  | 1044 | return ret; | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1045 | *search_done = 1; | 
|  | 1046 | goto again; | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1047 | } | 
|  | 1048 | kfree(victim_name); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1049 |  | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1050 | ptr = (unsigned long)(victim_ref + 1) + victim_name_len; | 
|  | 1051 | } | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1052 |  | 
|  | 1053 | /* | 
|  | 1054 | * NOTE: we have searched root tree and checked the | 
| Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 1055 | * corresponding ref, it does not need to check again. | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1056 | */ | 
|  | 1057 | *search_done = 1; | 
|  | 1058 | } | 
|  | 1059 | btrfs_release_path(path); | 
|  | 1060 |  | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1061 | /* Same search but for extended refs */ | 
|  | 1062 | extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen, | 
|  | 1063 | inode_objectid, parent_objectid, 0, | 
|  | 1064 | 0); | 
|  | 1065 | if (!IS_ERR_OR_NULL(extref)) { | 
|  | 1066 | u32 item_size; | 
|  | 1067 | u32 cur_offset = 0; | 
|  | 1068 | unsigned long base; | 
|  | 1069 | struct inode *victim_parent; | 
|  | 1070 |  | 
|  | 1071 | leaf = path->nodes[0]; | 
|  | 1072 |  | 
|  | 1073 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | 
|  | 1074 | base = btrfs_item_ptr_offset(leaf, path->slots[0]); | 
|  | 1075 |  | 
|  | 1076 | while (cur_offset < item_size) { | 
| Quentin Casasnovas | dd9ef13 | 2015-03-03 16:31:38 +0100 | [diff] [blame] | 1077 | extref = (struct btrfs_inode_extref *)(base + cur_offset); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1078 |  | 
|  | 1079 | victim_name_len = btrfs_inode_extref_name_len(leaf, extref); | 
|  | 1080 |  | 
|  | 1081 | if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid) | 
|  | 1082 | goto next; | 
|  | 1083 |  | 
|  | 1084 | victim_name = kmalloc(victim_name_len, GFP_NOFS); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1085 | if (!victim_name) | 
|  | 1086 | return -ENOMEM; | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1087 | read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name, | 
|  | 1088 | victim_name_len); | 
|  | 1089 |  | 
|  | 1090 | search_key.objectid = inode_objectid; | 
|  | 1091 | search_key.type = BTRFS_INODE_EXTREF_KEY; | 
|  | 1092 | search_key.offset = btrfs_extref_hash(parent_objectid, | 
|  | 1093 | victim_name, | 
|  | 1094 | victim_name_len); | 
|  | 1095 | ret = 0; | 
|  | 1096 | if (!backref_in_log(log_root, &search_key, | 
|  | 1097 | parent_objectid, victim_name, | 
|  | 1098 | victim_name_len)) { | 
|  | 1099 | ret = -ENOENT; | 
|  | 1100 | victim_parent = read_one_inode(root, | 
| Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1101 | parent_objectid); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1102 | if (victim_parent) { | 
| Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1103 | inc_nlink(&inode->vfs_inode); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1104 | btrfs_release_path(path); | 
|  | 1105 |  | 
|  | 1106 | ret = btrfs_unlink_inode(trans, root, | 
| Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1107 | BTRFS_I(victim_parent), | 
| Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1108 | inode, | 
| Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 1109 | victim_name, | 
|  | 1110 | victim_name_len); | 
| Filipe David Borba Manana | ada9af2 | 2013-08-05 09:25:47 +0100 | [diff] [blame] | 1111 | if (!ret) | 
|  | 1112 | ret = btrfs_run_delayed_items( | 
| Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 1113 | trans); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1114 | } | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1115 | iput(victim_parent); | 
|  | 1116 | kfree(victim_name); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1117 | if (ret) | 
|  | 1118 | return ret; | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1119 | *search_done = 1; | 
|  | 1120 | goto again; | 
|  | 1121 | } | 
|  | 1122 | kfree(victim_name); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1123 | next: | 
|  | 1124 | cur_offset += victim_name_len + sizeof(*extref); | 
|  | 1125 | } | 
|  | 1126 | *search_done = 1; | 
|  | 1127 | } | 
|  | 1128 | btrfs_release_path(path); | 
|  | 1129 |  | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1130 | /* look for a conflicting sequence number */ | 
| Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1131 | di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir), | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1132 | ref_index, name, namelen, 0); | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1133 | if (di && !IS_ERR(di)) { | 
| Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1134 | ret = drop_one_dir_item(trans, root, path, dir, di); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1135 | if (ret) | 
|  | 1136 | return ret; | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1137 | } | 
|  | 1138 | btrfs_release_path(path); | 
|  | 1139 |  | 
|  | 1140 | /* look for a conflicing name */ | 
| Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1141 | di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1142 | name, namelen, 0); | 
|  | 1143 | if (di && !IS_ERR(di)) { | 
| Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1144 | ret = drop_one_dir_item(trans, root, path, dir, di); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1145 | if (ret) | 
|  | 1146 | return ret; | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1147 | } | 
|  | 1148 | btrfs_release_path(path); | 
|  | 1149 |  | 
|  | 1150 | return 0; | 
|  | 1151 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1152 |  | 
| Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1153 | static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, | 
|  | 1154 | u32 *namelen, char **name, u64 *index, | 
|  | 1155 | u64 *parent_objectid) | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1156 | { | 
|  | 1157 | struct btrfs_inode_extref *extref; | 
|  | 1158 |  | 
|  | 1159 | extref = (struct btrfs_inode_extref *)ref_ptr; | 
|  | 1160 |  | 
|  | 1161 | *namelen = btrfs_inode_extref_name_len(eb, extref); | 
|  | 1162 | *name = kmalloc(*namelen, GFP_NOFS); | 
|  | 1163 | if (*name == NULL) | 
|  | 1164 | return -ENOMEM; | 
|  | 1165 |  | 
|  | 1166 | read_extent_buffer(eb, *name, (unsigned long)&extref->name, | 
|  | 1167 | *namelen); | 
|  | 1168 |  | 
| Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1169 | if (index) | 
|  | 1170 | *index = btrfs_inode_extref_index(eb, extref); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1171 | if (parent_objectid) | 
|  | 1172 | *parent_objectid = btrfs_inode_extref_parent(eb, extref); | 
|  | 1173 |  | 
|  | 1174 | return 0; | 
|  | 1175 | } | 
|  | 1176 |  | 
| Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1177 | static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, | 
|  | 1178 | u32 *namelen, char **name, u64 *index) | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1179 | { | 
|  | 1180 | struct btrfs_inode_ref *ref; | 
|  | 1181 |  | 
|  | 1182 | ref = (struct btrfs_inode_ref *)ref_ptr; | 
|  | 1183 |  | 
|  | 1184 | *namelen = btrfs_inode_ref_name_len(eb, ref); | 
|  | 1185 | *name = kmalloc(*namelen, GFP_NOFS); | 
|  | 1186 | if (*name == NULL) | 
|  | 1187 | return -ENOMEM; | 
|  | 1188 |  | 
|  | 1189 | read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen); | 
|  | 1190 |  | 
| Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1191 | if (index) | 
|  | 1192 | *index = btrfs_inode_ref_index(eb, ref); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1193 |  | 
|  | 1194 | return 0; | 
|  | 1195 | } | 
|  | 1196 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1197 | /* | 
| Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1198 | * Take an inode reference item from the log tree and iterate all names from the | 
|  | 1199 | * inode reference item in the subvolume tree with the same key (if it exists). | 
|  | 1200 | * For any name that is not in the inode reference item from the log tree, do a | 
|  | 1201 | * proper unlink of that name (that is, remove its entry from the inode | 
|  | 1202 | * reference item and both dir index keys). | 
|  | 1203 | */ | 
|  | 1204 | static int unlink_old_inode_refs(struct btrfs_trans_handle *trans, | 
|  | 1205 | struct btrfs_root *root, | 
|  | 1206 | struct btrfs_path *path, | 
|  | 1207 | struct btrfs_inode *inode, | 
|  | 1208 | struct extent_buffer *log_eb, | 
|  | 1209 | int log_slot, | 
|  | 1210 | struct btrfs_key *key) | 
|  | 1211 | { | 
|  | 1212 | int ret; | 
|  | 1213 | unsigned long ref_ptr; | 
|  | 1214 | unsigned long ref_end; | 
|  | 1215 | struct extent_buffer *eb; | 
|  | 1216 |  | 
|  | 1217 | again: | 
|  | 1218 | btrfs_release_path(path); | 
|  | 1219 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | 
|  | 1220 | if (ret > 0) { | 
|  | 1221 | ret = 0; | 
|  | 1222 | goto out; | 
|  | 1223 | } | 
|  | 1224 | if (ret < 0) | 
|  | 1225 | goto out; | 
|  | 1226 |  | 
|  | 1227 | eb = path->nodes[0]; | 
|  | 1228 | ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]); | 
|  | 1229 | ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]); | 
|  | 1230 | while (ref_ptr < ref_end) { | 
|  | 1231 | char *name = NULL; | 
|  | 1232 | int namelen; | 
|  | 1233 | u64 parent_id; | 
|  | 1234 |  | 
|  | 1235 | if (key->type == BTRFS_INODE_EXTREF_KEY) { | 
|  | 1236 | ret = extref_get_fields(eb, ref_ptr, &namelen, &name, | 
|  | 1237 | NULL, &parent_id); | 
|  | 1238 | } else { | 
|  | 1239 | parent_id = key->offset; | 
|  | 1240 | ret = ref_get_fields(eb, ref_ptr, &namelen, &name, | 
|  | 1241 | NULL); | 
|  | 1242 | } | 
|  | 1243 | if (ret) | 
|  | 1244 | goto out; | 
|  | 1245 |  | 
|  | 1246 | if (key->type == BTRFS_INODE_EXTREF_KEY) | 
|  | 1247 | ret = btrfs_find_name_in_ext_backref(log_eb, log_slot, | 
|  | 1248 | parent_id, name, | 
|  | 1249 | namelen, NULL); | 
|  | 1250 | else | 
|  | 1251 | ret = btrfs_find_name_in_backref(log_eb, log_slot, name, | 
|  | 1252 | namelen, NULL); | 
|  | 1253 |  | 
|  | 1254 | if (!ret) { | 
|  | 1255 | struct inode *dir; | 
|  | 1256 |  | 
|  | 1257 | btrfs_release_path(path); | 
|  | 1258 | dir = read_one_inode(root, parent_id); | 
|  | 1259 | if (!dir) { | 
|  | 1260 | ret = -ENOENT; | 
|  | 1261 | kfree(name); | 
|  | 1262 | goto out; | 
|  | 1263 | } | 
|  | 1264 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), | 
|  | 1265 | inode, name, namelen); | 
|  | 1266 | kfree(name); | 
|  | 1267 | iput(dir); | 
|  | 1268 | if (ret) | 
|  | 1269 | goto out; | 
|  | 1270 | goto again; | 
|  | 1271 | } | 
|  | 1272 |  | 
|  | 1273 | kfree(name); | 
|  | 1274 | ref_ptr += namelen; | 
|  | 1275 | if (key->type == BTRFS_INODE_EXTREF_KEY) | 
|  | 1276 | ref_ptr += sizeof(struct btrfs_inode_extref); | 
|  | 1277 | else | 
|  | 1278 | ref_ptr += sizeof(struct btrfs_inode_ref); | 
|  | 1279 | } | 
|  | 1280 | ret = 0; | 
|  | 1281 | out: | 
|  | 1282 | btrfs_release_path(path); | 
|  | 1283 | return ret; | 
|  | 1284 | } | 
|  | 1285 |  | 
| Filipe Manana | 0d83639 | 2018-07-20 10:59:06 +0100 | [diff] [blame] | 1286 | static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir, | 
|  | 1287 | const u8 ref_type, const char *name, | 
|  | 1288 | const int namelen) | 
|  | 1289 | { | 
|  | 1290 | struct btrfs_key key; | 
|  | 1291 | struct btrfs_path *path; | 
|  | 1292 | const u64 parent_id = btrfs_ino(BTRFS_I(dir)); | 
|  | 1293 | int ret; | 
|  | 1294 |  | 
|  | 1295 | path = btrfs_alloc_path(); | 
|  | 1296 | if (!path) | 
|  | 1297 | return -ENOMEM; | 
|  | 1298 |  | 
|  | 1299 | key.objectid = btrfs_ino(BTRFS_I(inode)); | 
|  | 1300 | key.type = ref_type; | 
|  | 1301 | if (key.type == BTRFS_INODE_REF_KEY) | 
|  | 1302 | key.offset = parent_id; | 
|  | 1303 | else | 
|  | 1304 | key.offset = btrfs_extref_hash(parent_id, name, namelen); | 
|  | 1305 |  | 
|  | 1306 | ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0); | 
|  | 1307 | if (ret < 0) | 
|  | 1308 | goto out; | 
|  | 1309 | if (ret > 0) { | 
|  | 1310 | ret = 0; | 
|  | 1311 | goto out; | 
|  | 1312 | } | 
|  | 1313 | if (key.type == BTRFS_INODE_EXTREF_KEY) | 
|  | 1314 | ret = btrfs_find_name_in_ext_backref(path->nodes[0], | 
|  | 1315 | path->slots[0], parent_id, | 
|  | 1316 | name, namelen, NULL); | 
|  | 1317 | else | 
|  | 1318 | ret = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], | 
|  | 1319 | name, namelen, NULL); | 
|  | 1320 |  | 
|  | 1321 | out: | 
|  | 1322 | btrfs_free_path(path); | 
|  | 1323 | return ret; | 
|  | 1324 | } | 
|  | 1325 |  | 
| Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1326 | /* | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1327 | * replay one inode back reference item found in the log tree. | 
|  | 1328 | * eb, slot and key refer to the buffer and key found in the log tree. | 
|  | 1329 | * root is the destination we are replaying into, and path is for temp | 
|  | 1330 | * use by this function.  (it should be released on return). | 
|  | 1331 | */ | 
|  | 1332 | static noinline int add_inode_ref(struct btrfs_trans_handle *trans, | 
|  | 1333 | struct btrfs_root *root, | 
|  | 1334 | struct btrfs_root *log, | 
|  | 1335 | struct btrfs_path *path, | 
|  | 1336 | struct extent_buffer *eb, int slot, | 
|  | 1337 | struct btrfs_key *key) | 
|  | 1338 | { | 
| Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1339 | struct inode *dir = NULL; | 
|  | 1340 | struct inode *inode = NULL; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1341 | unsigned long ref_ptr; | 
|  | 1342 | unsigned long ref_end; | 
| Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1343 | char *name = NULL; | 
| liubo | 34f3e4f | 2011-08-06 08:35:23 +0000 | [diff] [blame] | 1344 | int namelen; | 
|  | 1345 | int ret; | 
| liubo | c622ae6 | 2011-03-26 08:01:12 -0400 | [diff] [blame] | 1346 | int search_done = 0; | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1347 | int log_ref_ver = 0; | 
|  | 1348 | u64 parent_objectid; | 
|  | 1349 | u64 inode_objectid; | 
| Chris Mason | f46dbe3 | 2012-10-09 11:17:20 -0400 | [diff] [blame] | 1350 | u64 ref_index = 0; | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1351 | int ref_struct_size; | 
|  | 1352 |  | 
|  | 1353 | ref_ptr = btrfs_item_ptr_offset(eb, slot); | 
|  | 1354 | ref_end = ref_ptr + btrfs_item_size_nr(eb, slot); | 
|  | 1355 |  | 
|  | 1356 | if (key->type == BTRFS_INODE_EXTREF_KEY) { | 
|  | 1357 | struct btrfs_inode_extref *r; | 
|  | 1358 |  | 
|  | 1359 | ref_struct_size = sizeof(struct btrfs_inode_extref); | 
|  | 1360 | log_ref_ver = 1; | 
|  | 1361 | r = (struct btrfs_inode_extref *)ref_ptr; | 
|  | 1362 | parent_objectid = btrfs_inode_extref_parent(eb, r); | 
|  | 1363 | } else { | 
|  | 1364 | ref_struct_size = sizeof(struct btrfs_inode_ref); | 
|  | 1365 | parent_objectid = key->offset; | 
|  | 1366 | } | 
|  | 1367 | inode_objectid = key->objectid; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1368 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1369 | /* | 
|  | 1370 | * it is possible that we didn't log all the parent directories | 
|  | 1371 | * for a given inode.  If we don't find the dir, just don't | 
|  | 1372 | * copy the back ref in.  The link count fixup code will take | 
|  | 1373 | * care of the rest | 
|  | 1374 | */ | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1375 | dir = read_one_inode(root, parent_objectid); | 
| Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1376 | if (!dir) { | 
|  | 1377 | ret = -ENOENT; | 
|  | 1378 | goto out; | 
|  | 1379 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1380 |  | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1381 | inode = read_one_inode(root, inode_objectid); | 
| Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1382 | if (!inode) { | 
| Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1383 | ret = -EIO; | 
|  | 1384 | goto out; | 
| Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1385 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1386 |  | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1387 | while (ref_ptr < ref_end) { | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1388 | if (log_ref_ver) { | 
| Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1389 | ret = extref_get_fields(eb, ref_ptr, &namelen, &name, | 
|  | 1390 | &ref_index, &parent_objectid); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1391 | /* | 
|  | 1392 | * parent object can change from one array | 
|  | 1393 | * item to another. | 
|  | 1394 | */ | 
|  | 1395 | if (!dir) | 
|  | 1396 | dir = read_one_inode(root, parent_objectid); | 
| Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1397 | if (!dir) { | 
|  | 1398 | ret = -ENOENT; | 
|  | 1399 | goto out; | 
|  | 1400 | } | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1401 | } else { | 
| Qu Wenruo | bae15d9 | 2017-11-08 08:54:26 +0800 | [diff] [blame] | 1402 | ret = ref_get_fields(eb, ref_ptr, &namelen, &name, | 
|  | 1403 | &ref_index); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1404 | } | 
|  | 1405 | if (ret) | 
| Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1406 | goto out; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1407 |  | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1408 | /* if we already have a perfect match, we're done */ | 
| David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 1409 | if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)), | 
|  | 1410 | btrfs_ino(BTRFS_I(inode)), ref_index, | 
|  | 1411 | name, namelen)) { | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1412 | /* | 
|  | 1413 | * look for a conflicting back reference in the | 
|  | 1414 | * metadata. if we find one we have to unlink that name | 
|  | 1415 | * of the file before we add our new link.  Later on, we | 
|  | 1416 | * overwrite any existing back reference, and we don't | 
|  | 1417 | * want to create dangling pointers in the directory. | 
|  | 1418 | */ | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1419 |  | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1420 | if (!search_done) { | 
|  | 1421 | ret = __add_inode_ref(trans, root, path, log, | 
| Nikolay Borisov | 94c91a1 | 2017-01-18 00:31:46 +0200 | [diff] [blame] | 1422 | BTRFS_I(dir), | 
| David Sterba | d75eefd | 2017-02-10 20:20:19 +0100 | [diff] [blame] | 1423 | BTRFS_I(inode), | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1424 | inode_objectid, | 
|  | 1425 | parent_objectid, | 
|  | 1426 | ref_index, name, namelen, | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1427 | &search_done); | 
| Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1428 | if (ret) { | 
|  | 1429 | if (ret == 1) | 
|  | 1430 | ret = 0; | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1431 | goto out; | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1432 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1433 | } | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1434 |  | 
| Filipe Manana | 0d83639 | 2018-07-20 10:59:06 +0100 | [diff] [blame] | 1435 | /* | 
|  | 1436 | * If a reference item already exists for this inode | 
|  | 1437 | * with the same parent and name, but different index, | 
|  | 1438 | * drop it and the corresponding directory index entries | 
|  | 1439 | * from the parent before adding the new reference item | 
|  | 1440 | * and dir index entries, otherwise we would fail with | 
|  | 1441 | * -EEXIST returned from btrfs_add_link() below. | 
|  | 1442 | */ | 
|  | 1443 | ret = btrfs_inode_ref_exists(inode, dir, key->type, | 
|  | 1444 | name, namelen); | 
|  | 1445 | if (ret > 0) { | 
|  | 1446 | ret = btrfs_unlink_inode(trans, root, | 
|  | 1447 | BTRFS_I(dir), | 
|  | 1448 | BTRFS_I(inode), | 
|  | 1449 | name, namelen); | 
|  | 1450 | /* | 
|  | 1451 | * If we dropped the link count to 0, bump it so | 
|  | 1452 | * that later the iput() on the inode will not | 
|  | 1453 | * free it. We will fixup the link count later. | 
|  | 1454 | */ | 
|  | 1455 | if (!ret && inode->i_nlink == 0) | 
|  | 1456 | inc_nlink(inode); | 
|  | 1457 | } | 
|  | 1458 | if (ret < 0) | 
|  | 1459 | goto out; | 
|  | 1460 |  | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1461 | /* insert our name */ | 
| Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 1462 | ret = btrfs_add_link(trans, BTRFS_I(dir), | 
|  | 1463 | BTRFS_I(inode), | 
|  | 1464 | name, namelen, 0, ref_index); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1465 | if (ret) | 
|  | 1466 | goto out; | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1467 |  | 
|  | 1468 | btrfs_update_inode(trans, root, inode); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1469 | } | 
| liubo | c622ae6 | 2011-03-26 08:01:12 -0400 | [diff] [blame] | 1470 |  | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1471 | ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen; | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1472 | kfree(name); | 
| Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1473 | name = NULL; | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1474 | if (log_ref_ver) { | 
|  | 1475 | iput(dir); | 
|  | 1476 | dir = NULL; | 
|  | 1477 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1478 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1479 |  | 
| Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 1480 | /* | 
|  | 1481 | * Before we overwrite the inode reference item in the subvolume tree | 
|  | 1482 | * with the item from the log tree, we must unlink all names from the | 
|  | 1483 | * parent directory that are in the subvolume's tree inode reference | 
|  | 1484 | * item, otherwise we end up with an inconsistent subvolume tree where | 
|  | 1485 | * dir index entries exist for a name but there is no inode reference | 
|  | 1486 | * item with the same name. | 
|  | 1487 | */ | 
|  | 1488 | ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot, | 
|  | 1489 | key); | 
|  | 1490 | if (ret) | 
|  | 1491 | goto out; | 
|  | 1492 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1493 | /* finally write the back reference in the inode */ | 
|  | 1494 | ret = overwrite_item(trans, root, path, eb, slot, key); | 
| Jan Schmidt | 5a1d784 | 2012-08-17 14:04:41 -0700 | [diff] [blame] | 1495 | out: | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1496 | btrfs_release_path(path); | 
| Geyslan G. Bem | 03b2f08 | 2013-10-11 15:35:45 -0300 | [diff] [blame] | 1497 | kfree(name); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1498 | iput(dir); | 
|  | 1499 | iput(inode); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1500 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1501 | } | 
|  | 1502 |  | 
| Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1503 | static int insert_orphan_item(struct btrfs_trans_handle *trans, | 
| David Sterba | 9c4f61f | 2015-01-02 19:12:57 +0100 | [diff] [blame] | 1504 | struct btrfs_root *root, u64 ino) | 
| Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1505 | { | 
|  | 1506 | int ret; | 
| David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 1507 |  | 
| David Sterba | 9c4f61f | 2015-01-02 19:12:57 +0100 | [diff] [blame] | 1508 | ret = btrfs_insert_orphan_item(trans, root, ino); | 
|  | 1509 | if (ret == -EEXIST) | 
|  | 1510 | ret = 0; | 
| David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 1511 |  | 
| Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1512 | return ret; | 
|  | 1513 | } | 
|  | 1514 |  | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1515 | static int count_inode_extrefs(struct btrfs_root *root, | 
| Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1516 | struct btrfs_inode *inode, struct btrfs_path *path) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1517 | { | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1518 | int ret = 0; | 
|  | 1519 | int name_len; | 
|  | 1520 | unsigned int nlink = 0; | 
|  | 1521 | u32 item_size; | 
|  | 1522 | u32 cur_offset = 0; | 
| Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1523 | u64 inode_objectid = btrfs_ino(inode); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1524 | u64 offset = 0; | 
|  | 1525 | unsigned long ptr; | 
|  | 1526 | struct btrfs_inode_extref *extref; | 
|  | 1527 | struct extent_buffer *leaf; | 
|  | 1528 |  | 
|  | 1529 | while (1) { | 
|  | 1530 | ret = btrfs_find_one_extref(root, inode_objectid, offset, path, | 
|  | 1531 | &extref, &offset); | 
|  | 1532 | if (ret) | 
|  | 1533 | break; | 
|  | 1534 |  | 
|  | 1535 | leaf = path->nodes[0]; | 
|  | 1536 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | 
|  | 1537 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | 
| Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 1538 | cur_offset = 0; | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1539 |  | 
|  | 1540 | while (cur_offset < item_size) { | 
|  | 1541 | extref = (struct btrfs_inode_extref *) (ptr + cur_offset); | 
|  | 1542 | name_len = btrfs_inode_extref_name_len(leaf, extref); | 
|  | 1543 |  | 
|  | 1544 | nlink++; | 
|  | 1545 |  | 
|  | 1546 | cur_offset += name_len + sizeof(*extref); | 
|  | 1547 | } | 
|  | 1548 |  | 
|  | 1549 | offset++; | 
|  | 1550 | btrfs_release_path(path); | 
|  | 1551 | } | 
|  | 1552 | btrfs_release_path(path); | 
|  | 1553 |  | 
| Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 1554 | if (ret < 0 && ret != -ENOENT) | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1555 | return ret; | 
|  | 1556 | return nlink; | 
|  | 1557 | } | 
|  | 1558 |  | 
|  | 1559 | static int count_inode_refs(struct btrfs_root *root, | 
| Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1560 | struct btrfs_inode *inode, struct btrfs_path *path) | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1561 | { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1562 | int ret; | 
|  | 1563 | struct btrfs_key key; | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1564 | unsigned int nlink = 0; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1565 | unsigned long ptr; | 
|  | 1566 | unsigned long ptr_end; | 
|  | 1567 | int name_len; | 
| Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1568 | u64 ino = btrfs_ino(inode); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1569 |  | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1570 | key.objectid = ino; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1571 | key.type = BTRFS_INODE_REF_KEY; | 
|  | 1572 | key.offset = (u64)-1; | 
|  | 1573 |  | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1574 | while (1) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1575 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | 
|  | 1576 | if (ret < 0) | 
|  | 1577 | break; | 
|  | 1578 | if (ret > 0) { | 
|  | 1579 | if (path->slots[0] == 0) | 
|  | 1580 | break; | 
|  | 1581 | path->slots[0]--; | 
|  | 1582 | } | 
| Filipe David Borba Manana | e93ae26 | 2013-10-14 22:49:11 +0100 | [diff] [blame] | 1583 | process_slot: | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1584 | btrfs_item_key_to_cpu(path->nodes[0], &key, | 
|  | 1585 | path->slots[0]); | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1586 | if (key.objectid != ino || | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1587 | key.type != BTRFS_INODE_REF_KEY) | 
|  | 1588 | break; | 
|  | 1589 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | 
|  | 1590 | ptr_end = ptr + btrfs_item_size_nr(path->nodes[0], | 
|  | 1591 | path->slots[0]); | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1592 | while (ptr < ptr_end) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1593 | struct btrfs_inode_ref *ref; | 
|  | 1594 |  | 
|  | 1595 | ref = (struct btrfs_inode_ref *)ptr; | 
|  | 1596 | name_len = btrfs_inode_ref_name_len(path->nodes[0], | 
|  | 1597 | ref); | 
|  | 1598 | ptr = (unsigned long)(ref + 1) + name_len; | 
|  | 1599 | nlink++; | 
|  | 1600 | } | 
|  | 1601 |  | 
|  | 1602 | if (key.offset == 0) | 
|  | 1603 | break; | 
| Filipe David Borba Manana | e93ae26 | 2013-10-14 22:49:11 +0100 | [diff] [blame] | 1604 | if (path->slots[0] > 0) { | 
|  | 1605 | path->slots[0]--; | 
|  | 1606 | goto process_slot; | 
|  | 1607 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1608 | key.offset--; | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1609 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1610 | } | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1611 | btrfs_release_path(path); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1612 |  | 
|  | 1613 | return nlink; | 
|  | 1614 | } | 
|  | 1615 |  | 
|  | 1616 | /* | 
|  | 1617 | * There are a few corners where the link count of the file can't | 
|  | 1618 | * be properly maintained during replay.  So, instead of adding | 
|  | 1619 | * lots of complexity to the log code, we just scan the backrefs | 
|  | 1620 | * for any file that has been through replay. | 
|  | 1621 | * | 
|  | 1622 | * The scan will update the link count on the inode to reflect the | 
|  | 1623 | * number of back refs found.  If it goes down to zero, the iput | 
|  | 1624 | * will free the inode. | 
|  | 1625 | */ | 
|  | 1626 | static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, | 
|  | 1627 | struct btrfs_root *root, | 
|  | 1628 | struct inode *inode) | 
|  | 1629 | { | 
|  | 1630 | struct btrfs_path *path; | 
|  | 1631 | int ret; | 
|  | 1632 | u64 nlink = 0; | 
| Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1633 | u64 ino = btrfs_ino(BTRFS_I(inode)); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1634 |  | 
|  | 1635 | path = btrfs_alloc_path(); | 
|  | 1636 | if (!path) | 
|  | 1637 | return -ENOMEM; | 
|  | 1638 |  | 
| Nikolay Borisov | f329e31 | 2017-01-18 00:31:50 +0200 | [diff] [blame] | 1639 | ret = count_inode_refs(root, BTRFS_I(inode), path); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1640 | if (ret < 0) | 
|  | 1641 | goto out; | 
|  | 1642 |  | 
|  | 1643 | nlink = ret; | 
|  | 1644 |  | 
| Nikolay Borisov | 3628365 | 2017-01-18 00:31:49 +0200 | [diff] [blame] | 1645 | ret = count_inode_extrefs(root, BTRFS_I(inode), path); | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1646 | if (ret < 0) | 
|  | 1647 | goto out; | 
|  | 1648 |  | 
|  | 1649 | nlink += ret; | 
|  | 1650 |  | 
|  | 1651 | ret = 0; | 
|  | 1652 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1653 | if (nlink != inode->i_nlink) { | 
| Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 1654 | set_nlink(inode, nlink); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1655 | btrfs_update_inode(trans, root, inode); | 
|  | 1656 | } | 
| Chris Mason | 8d5bf1c | 2008-09-11 15:51:21 -0400 | [diff] [blame] | 1657 | BTRFS_I(inode)->index_cnt = (u64)-1; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1658 |  | 
| Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1659 | if (inode->i_nlink == 0) { | 
|  | 1660 | if (S_ISDIR(inode->i_mode)) { | 
|  | 1661 | ret = replay_dir_deletes(trans, root, NULL, path, | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1662 | ino, 1); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1663 | if (ret) | 
|  | 1664 | goto out; | 
| Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 1665 | } | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1666 | ret = insert_orphan_item(trans, root, ino); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1667 | } | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1668 |  | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1669 | out: | 
|  | 1670 | btrfs_free_path(path); | 
|  | 1671 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1672 | } | 
|  | 1673 |  | 
|  | 1674 | static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, | 
|  | 1675 | struct btrfs_root *root, | 
|  | 1676 | struct btrfs_path *path) | 
|  | 1677 | { | 
|  | 1678 | int ret; | 
|  | 1679 | struct btrfs_key key; | 
|  | 1680 | struct inode *inode; | 
|  | 1681 |  | 
|  | 1682 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; | 
|  | 1683 | key.type = BTRFS_ORPHAN_ITEM_KEY; | 
|  | 1684 | key.offset = (u64)-1; | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1685 | while (1) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1686 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | 
|  | 1687 | if (ret < 0) | 
|  | 1688 | break; | 
|  | 1689 |  | 
|  | 1690 | if (ret == 1) { | 
|  | 1691 | if (path->slots[0] == 0) | 
|  | 1692 | break; | 
|  | 1693 | path->slots[0]--; | 
|  | 1694 | } | 
|  | 1695 |  | 
|  | 1696 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | 
|  | 1697 | if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || | 
|  | 1698 | key.type != BTRFS_ORPHAN_ITEM_KEY) | 
|  | 1699 | break; | 
|  | 1700 |  | 
|  | 1701 | ret = btrfs_del_item(trans, root, path); | 
| Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1702 | if (ret) | 
|  | 1703 | goto out; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1704 |  | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1705 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1706 | inode = read_one_inode(root, key.offset); | 
| Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1707 | if (!inode) | 
|  | 1708 | return -EIO; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1709 |  | 
|  | 1710 | ret = fixup_inode_link_count(trans, root, inode); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1711 | iput(inode); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1712 | if (ret) | 
|  | 1713 | goto out; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1714 |  | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1715 | /* | 
|  | 1716 | * fixup on a directory may create new entries, | 
|  | 1717 | * make sure we always look for the highset possible | 
|  | 1718 | * offset | 
|  | 1719 | */ | 
|  | 1720 | key.offset = (u64)-1; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1721 | } | 
| Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1722 | ret = 0; | 
|  | 1723 | out: | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1724 | btrfs_release_path(path); | 
| Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 1725 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1726 | } | 
|  | 1727 |  | 
|  | 1728 |  | 
|  | 1729 | /* | 
|  | 1730 | * record a given inode in the fixup dir so we can check its link | 
|  | 1731 | * count when replay is done.  The link count is incremented here | 
|  | 1732 | * so the inode won't go away until we check it | 
|  | 1733 | */ | 
|  | 1734 | static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, | 
|  | 1735 | struct btrfs_root *root, | 
|  | 1736 | struct btrfs_path *path, | 
|  | 1737 | u64 objectid) | 
|  | 1738 | { | 
|  | 1739 | struct btrfs_key key; | 
|  | 1740 | int ret = 0; | 
|  | 1741 | struct inode *inode; | 
|  | 1742 |  | 
|  | 1743 | inode = read_one_inode(root, objectid); | 
| Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1744 | if (!inode) | 
|  | 1745 | return -EIO; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1746 |  | 
|  | 1747 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; | 
| David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1748 | key.type = BTRFS_ORPHAN_ITEM_KEY; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1749 | key.offset = objectid; | 
|  | 1750 |  | 
|  | 1751 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); | 
|  | 1752 |  | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1753 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1754 | if (ret == 0) { | 
| Josef Bacik | 9bf7a48 | 2013-03-01 13:35:47 -0500 | [diff] [blame] | 1755 | if (!inode->i_nlink) | 
|  | 1756 | set_nlink(inode, 1); | 
|  | 1757 | else | 
| Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 1758 | inc_nlink(inode); | 
| Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 1759 | ret = btrfs_update_inode(trans, root, inode); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1760 | } else if (ret == -EEXIST) { | 
|  | 1761 | ret = 0; | 
|  | 1762 | } else { | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1763 | BUG(); /* Logic Error */ | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1764 | } | 
|  | 1765 | iput(inode); | 
|  | 1766 |  | 
|  | 1767 | return ret; | 
|  | 1768 | } | 
|  | 1769 |  | 
|  | 1770 | /* | 
|  | 1771 | * when replaying the log for a directory, we only insert names | 
|  | 1772 | * for inodes that actually exist.  This means an fsync on a directory | 
|  | 1773 | * does not implicitly fsync all the new files in it | 
|  | 1774 | */ | 
|  | 1775 | static noinline int insert_one_name(struct btrfs_trans_handle *trans, | 
|  | 1776 | struct btrfs_root *root, | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1777 | u64 dirid, u64 index, | 
| Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 1778 | char *name, int name_len, | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1779 | struct btrfs_key *location) | 
|  | 1780 | { | 
|  | 1781 | struct inode *inode; | 
|  | 1782 | struct inode *dir; | 
|  | 1783 | int ret; | 
|  | 1784 |  | 
|  | 1785 | inode = read_one_inode(root, location->objectid); | 
|  | 1786 | if (!inode) | 
|  | 1787 | return -ENOENT; | 
|  | 1788 |  | 
|  | 1789 | dir = read_one_inode(root, dirid); | 
|  | 1790 | if (!dir) { | 
|  | 1791 | iput(inode); | 
|  | 1792 | return -EIO; | 
|  | 1793 | } | 
| Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1794 |  | 
| Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 1795 | ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name, | 
|  | 1796 | name_len, 1, index); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1797 |  | 
|  | 1798 | /* FIXME, put inode into FIXUP list */ | 
|  | 1799 |  | 
|  | 1800 | iput(inode); | 
|  | 1801 | iput(dir); | 
|  | 1802 | return ret; | 
|  | 1803 | } | 
|  | 1804 |  | 
|  | 1805 | /* | 
| Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1806 | * Return true if an inode reference exists in the log for the given name, | 
|  | 1807 | * inode and parent inode. | 
|  | 1808 | */ | 
|  | 1809 | static bool name_in_log_ref(struct btrfs_root *log_root, | 
|  | 1810 | const char *name, const int name_len, | 
|  | 1811 | const u64 dirid, const u64 ino) | 
|  | 1812 | { | 
|  | 1813 | struct btrfs_key search_key; | 
|  | 1814 |  | 
|  | 1815 | search_key.objectid = ino; | 
|  | 1816 | search_key.type = BTRFS_INODE_REF_KEY; | 
|  | 1817 | search_key.offset = dirid; | 
|  | 1818 | if (backref_in_log(log_root, &search_key, dirid, name, name_len)) | 
|  | 1819 | return true; | 
|  | 1820 |  | 
|  | 1821 | search_key.type = BTRFS_INODE_EXTREF_KEY; | 
|  | 1822 | search_key.offset = btrfs_extref_hash(dirid, name, name_len); | 
|  | 1823 | if (backref_in_log(log_root, &search_key, dirid, name, name_len)) | 
|  | 1824 | return true; | 
|  | 1825 |  | 
|  | 1826 | return false; | 
|  | 1827 | } | 
|  | 1828 |  | 
|  | 1829 | /* | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1830 | * take a single entry in a log directory item and replay it into | 
|  | 1831 | * the subvolume. | 
|  | 1832 | * | 
|  | 1833 | * if a conflicting item exists in the subdirectory already, | 
|  | 1834 | * the inode it points to is unlinked and put into the link count | 
|  | 1835 | * fix up tree. | 
|  | 1836 | * | 
|  | 1837 | * If a name from the log points to a file or directory that does | 
|  | 1838 | * not exist in the FS, it is skipped.  fsyncs on directories | 
|  | 1839 | * do not force down inodes inside that directory, just changes to the | 
|  | 1840 | * names or unlinks in a directory. | 
| Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1841 | * | 
|  | 1842 | * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a | 
|  | 1843 | * non-existing inode) and 1 if the name was replayed. | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1844 | */ | 
|  | 1845 | static noinline int replay_one_name(struct btrfs_trans_handle *trans, | 
|  | 1846 | struct btrfs_root *root, | 
|  | 1847 | struct btrfs_path *path, | 
|  | 1848 | struct extent_buffer *eb, | 
|  | 1849 | struct btrfs_dir_item *di, | 
|  | 1850 | struct btrfs_key *key) | 
|  | 1851 | { | 
|  | 1852 | char *name; | 
|  | 1853 | int name_len; | 
|  | 1854 | struct btrfs_dir_item *dst_di; | 
|  | 1855 | struct btrfs_key found_key; | 
|  | 1856 | struct btrfs_key log_key; | 
|  | 1857 | struct inode *dir; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1858 | u8 log_type; | 
| Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1859 | int exists; | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1860 | int ret = 0; | 
| Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1861 | bool update_size = (key->type == BTRFS_DIR_INDEX_KEY); | 
| Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1862 | bool name_added = false; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1863 |  | 
|  | 1864 | dir = read_one_inode(root, key->objectid); | 
| Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 1865 | if (!dir) | 
|  | 1866 | return -EIO; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1867 |  | 
|  | 1868 | name_len = btrfs_dir_name_len(eb, di); | 
|  | 1869 | name = kmalloc(name_len, GFP_NOFS); | 
| Filipe David Borba Manana | 2bac325 | 2013-08-04 19:58:57 +0100 | [diff] [blame] | 1870 | if (!name) { | 
|  | 1871 | ret = -ENOMEM; | 
|  | 1872 | goto out; | 
|  | 1873 | } | 
| liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 1874 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1875 | log_type = btrfs_dir_type(eb, di); | 
|  | 1876 | read_extent_buffer(eb, name, (unsigned long)(di + 1), | 
|  | 1877 | name_len); | 
|  | 1878 |  | 
|  | 1879 | btrfs_dir_item_key_to_cpu(eb, di, &log_key); | 
| Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1880 | exists = btrfs_lookup_inode(trans, root, path, &log_key, 0); | 
|  | 1881 | if (exists == 0) | 
|  | 1882 | exists = 1; | 
|  | 1883 | else | 
|  | 1884 | exists = 0; | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1885 | btrfs_release_path(path); | 
| Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1886 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1887 | if (key->type == BTRFS_DIR_ITEM_KEY) { | 
|  | 1888 | dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, | 
|  | 1889 | name, name_len, 1); | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1890 | } else if (key->type == BTRFS_DIR_INDEX_KEY) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1891 | dst_di = btrfs_lookup_dir_index_item(trans, root, path, | 
|  | 1892 | key->objectid, | 
|  | 1893 | key->offset, name, | 
|  | 1894 | name_len, 1); | 
|  | 1895 | } else { | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1896 | /* Corruption */ | 
|  | 1897 | ret = -EINVAL; | 
|  | 1898 | goto out; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1899 | } | 
| David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 1900 | if (IS_ERR_OR_NULL(dst_di)) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1901 | /* we need a sequence number to insert, so we only | 
|  | 1902 | * do inserts for the BTRFS_DIR_INDEX_KEY types | 
|  | 1903 | */ | 
|  | 1904 | if (key->type != BTRFS_DIR_INDEX_KEY) | 
|  | 1905 | goto out; | 
|  | 1906 | goto insert; | 
|  | 1907 | } | 
|  | 1908 |  | 
|  | 1909 | btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); | 
|  | 1910 | /* the existing item matches the logged item */ | 
|  | 1911 | if (found_key.objectid == log_key.objectid && | 
|  | 1912 | found_key.type == log_key.type && | 
|  | 1913 | found_key.offset == log_key.offset && | 
|  | 1914 | btrfs_dir_type(path->nodes[0], dst_di) == log_type) { | 
| Filipe Manana | a2cc11d | 2014-09-08 22:53:18 +0100 | [diff] [blame] | 1915 | update_size = false; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1916 | goto out; | 
|  | 1917 | } | 
|  | 1918 |  | 
|  | 1919 | /* | 
|  | 1920 | * don't drop the conflicting directory entry if the inode | 
|  | 1921 | * for the new entry doesn't exist | 
|  | 1922 | */ | 
| Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1923 | if (!exists) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1924 | goto out; | 
|  | 1925 |  | 
| Nikolay Borisov | 207e7d9 | 2017-01-18 00:31:45 +0200 | [diff] [blame] | 1926 | ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1927 | if (ret) | 
|  | 1928 | goto out; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1929 |  | 
|  | 1930 | if (key->type == BTRFS_DIR_INDEX_KEY) | 
|  | 1931 | goto insert; | 
|  | 1932 | out: | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1933 | btrfs_release_path(path); | 
| Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1934 | if (!ret && update_size) { | 
| Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 1935 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2); | 
| Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1936 | ret = btrfs_update_inode(trans, root, dir); | 
|  | 1937 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1938 | kfree(name); | 
|  | 1939 | iput(dir); | 
| Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1940 | if (!ret && name_added) | 
|  | 1941 | ret = 1; | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1942 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1943 |  | 
|  | 1944 | insert: | 
| Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1945 | if (name_in_log_ref(root->log_root, name, name_len, | 
|  | 1946 | key->objectid, log_key.objectid)) { | 
|  | 1947 | /* The dentry will be added later. */ | 
|  | 1948 | ret = 0; | 
|  | 1949 | update_size = false; | 
|  | 1950 | goto out; | 
|  | 1951 | } | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1952 | btrfs_release_path(path); | 
| Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 1953 | ret = insert_one_name(trans, root, key->objectid, key->offset, | 
|  | 1954 | name, name_len, &log_key); | 
| Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 1955 | if (ret && ret != -ENOENT && ret != -EEXIST) | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1956 | goto out; | 
| Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1957 | if (!ret) | 
|  | 1958 | name_added = true; | 
| Josef Bacik | d555438 | 2013-09-11 14:17:00 -0400 | [diff] [blame] | 1959 | update_size = false; | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 1960 | ret = 0; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1961 | goto out; | 
|  | 1962 | } | 
|  | 1963 |  | 
|  | 1964 | /* | 
|  | 1965 | * find all the names in a directory item and reconcile them into | 
|  | 1966 | * the subvolume.  Only BTRFS_DIR_ITEM_KEY types will have more than | 
|  | 1967 | * one name in a directory item, but the same code gets used for | 
|  | 1968 | * both directory index types | 
|  | 1969 | */ | 
|  | 1970 | static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, | 
|  | 1971 | struct btrfs_root *root, | 
|  | 1972 | struct btrfs_path *path, | 
|  | 1973 | struct extent_buffer *eb, int slot, | 
|  | 1974 | struct btrfs_key *key) | 
|  | 1975 | { | 
| Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1976 | int ret = 0; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1977 | u32 item_size = btrfs_item_size_nr(eb, slot); | 
|  | 1978 | struct btrfs_dir_item *di; | 
|  | 1979 | int name_len; | 
|  | 1980 | unsigned long ptr; | 
|  | 1981 | unsigned long ptr_end; | 
| Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1982 | struct btrfs_path *fixup_path = NULL; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1983 |  | 
|  | 1984 | ptr = btrfs_item_ptr_offset(eb, slot); | 
|  | 1985 | ptr_end = ptr + item_size; | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1986 | while (ptr < ptr_end) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1987 | di = (struct btrfs_dir_item *)ptr; | 
|  | 1988 | name_len = btrfs_dir_name_len(eb, di); | 
|  | 1989 | ret = replay_one_name(trans, root, path, eb, di, key); | 
| Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1990 | if (ret < 0) | 
|  | 1991 | break; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1992 | ptr = (unsigned long)(di + 1); | 
|  | 1993 | ptr += name_len; | 
| Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 1994 |  | 
|  | 1995 | /* | 
|  | 1996 | * If this entry refers to a non-directory (directories can not | 
|  | 1997 | * have a link count > 1) and it was added in the transaction | 
|  | 1998 | * that was not committed, make sure we fixup the link count of | 
|  | 1999 | * the inode it the entry points to. Otherwise something like | 
|  | 2000 | * the following would result in a directory pointing to an | 
|  | 2001 | * inode with a wrong link that does not account for this dir | 
|  | 2002 | * entry: | 
|  | 2003 | * | 
|  | 2004 | * mkdir testdir | 
|  | 2005 | * touch testdir/foo | 
|  | 2006 | * touch testdir/bar | 
|  | 2007 | * sync | 
|  | 2008 | * | 
|  | 2009 | * ln testdir/bar testdir/bar_link | 
|  | 2010 | * ln testdir/foo testdir/foo_link | 
|  | 2011 | * xfs_io -c "fsync" testdir/bar | 
|  | 2012 | * | 
|  | 2013 | * <power failure> | 
|  | 2014 | * | 
|  | 2015 | * mount fs, log replay happens | 
|  | 2016 | * | 
|  | 2017 | * File foo would remain with a link count of 1 when it has two | 
|  | 2018 | * entries pointing to it in the directory testdir. This would | 
|  | 2019 | * make it impossible to ever delete the parent directory has | 
|  | 2020 | * it would result in stale dentries that can never be deleted. | 
|  | 2021 | */ | 
|  | 2022 | if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) { | 
|  | 2023 | struct btrfs_key di_key; | 
|  | 2024 |  | 
|  | 2025 | if (!fixup_path) { | 
|  | 2026 | fixup_path = btrfs_alloc_path(); | 
|  | 2027 | if (!fixup_path) { | 
|  | 2028 | ret = -ENOMEM; | 
|  | 2029 | break; | 
|  | 2030 | } | 
|  | 2031 | } | 
|  | 2032 |  | 
|  | 2033 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); | 
|  | 2034 | ret = link_to_fixup_dir(trans, root, fixup_path, | 
|  | 2035 | di_key.objectid); | 
|  | 2036 | if (ret) | 
|  | 2037 | break; | 
|  | 2038 | } | 
|  | 2039 | ret = 0; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2040 | } | 
| Filipe Manana | bb53eda | 2015-07-15 23:26:43 +0100 | [diff] [blame] | 2041 | btrfs_free_path(fixup_path); | 
|  | 2042 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2043 | } | 
|  | 2044 |  | 
|  | 2045 | /* | 
|  | 2046 | * directory replay has two parts.  There are the standard directory | 
|  | 2047 | * items in the log copied from the subvolume, and range items | 
|  | 2048 | * created in the log while the subvolume was logged. | 
|  | 2049 | * | 
|  | 2050 | * The range items tell us which parts of the key space the log | 
|  | 2051 | * is authoritative for.  During replay, if a key in the subvolume | 
|  | 2052 | * directory is in a logged range item, but not actually in the log | 
|  | 2053 | * that means it was deleted from the directory before the fsync | 
|  | 2054 | * and should be removed. | 
|  | 2055 | */ | 
|  | 2056 | static noinline int find_dir_range(struct btrfs_root *root, | 
|  | 2057 | struct btrfs_path *path, | 
|  | 2058 | u64 dirid, int key_type, | 
|  | 2059 | u64 *start_ret, u64 *end_ret) | 
|  | 2060 | { | 
|  | 2061 | struct btrfs_key key; | 
|  | 2062 | u64 found_end; | 
|  | 2063 | struct btrfs_dir_log_item *item; | 
|  | 2064 | int ret; | 
|  | 2065 | int nritems; | 
|  | 2066 |  | 
|  | 2067 | if (*start_ret == (u64)-1) | 
|  | 2068 | return 1; | 
|  | 2069 |  | 
|  | 2070 | key.objectid = dirid; | 
|  | 2071 | key.type = key_type; | 
|  | 2072 | key.offset = *start_ret; | 
|  | 2073 |  | 
|  | 2074 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | 
|  | 2075 | if (ret < 0) | 
|  | 2076 | goto out; | 
|  | 2077 | if (ret > 0) { | 
|  | 2078 | if (path->slots[0] == 0) | 
|  | 2079 | goto out; | 
|  | 2080 | path->slots[0]--; | 
|  | 2081 | } | 
|  | 2082 | if (ret != 0) | 
|  | 2083 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | 
|  | 2084 |  | 
|  | 2085 | if (key.type != key_type || key.objectid != dirid) { | 
|  | 2086 | ret = 1; | 
|  | 2087 | goto next; | 
|  | 2088 | } | 
|  | 2089 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | 
|  | 2090 | struct btrfs_dir_log_item); | 
|  | 2091 | found_end = btrfs_dir_log_end(path->nodes[0], item); | 
|  | 2092 |  | 
|  | 2093 | if (*start_ret >= key.offset && *start_ret <= found_end) { | 
|  | 2094 | ret = 0; | 
|  | 2095 | *start_ret = key.offset; | 
|  | 2096 | *end_ret = found_end; | 
|  | 2097 | goto out; | 
|  | 2098 | } | 
|  | 2099 | ret = 1; | 
|  | 2100 | next: | 
|  | 2101 | /* check the next slot in the tree to see if it is a valid item */ | 
|  | 2102 | nritems = btrfs_header_nritems(path->nodes[0]); | 
| Robbie Ko | 2a7bf53 | 2016-10-07 17:30:47 +0800 | [diff] [blame] | 2103 | path->slots[0]++; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2104 | if (path->slots[0] >= nritems) { | 
|  | 2105 | ret = btrfs_next_leaf(root, path); | 
|  | 2106 | if (ret) | 
|  | 2107 | goto out; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2108 | } | 
|  | 2109 |  | 
|  | 2110 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | 
|  | 2111 |  | 
|  | 2112 | if (key.type != key_type || key.objectid != dirid) { | 
|  | 2113 | ret = 1; | 
|  | 2114 | goto out; | 
|  | 2115 | } | 
|  | 2116 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | 
|  | 2117 | struct btrfs_dir_log_item); | 
|  | 2118 | found_end = btrfs_dir_log_end(path->nodes[0], item); | 
|  | 2119 | *start_ret = key.offset; | 
|  | 2120 | *end_ret = found_end; | 
|  | 2121 | ret = 0; | 
|  | 2122 | out: | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2123 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2124 | return ret; | 
|  | 2125 | } | 
|  | 2126 |  | 
|  | 2127 | /* | 
|  | 2128 | * this looks for a given directory item in the log.  If the directory | 
|  | 2129 | * item is not in the log, the item is removed and the inode it points | 
|  | 2130 | * to is unlinked | 
|  | 2131 | */ | 
|  | 2132 | static noinline int check_item_in_log(struct btrfs_trans_handle *trans, | 
|  | 2133 | struct btrfs_root *root, | 
|  | 2134 | struct btrfs_root *log, | 
|  | 2135 | struct btrfs_path *path, | 
|  | 2136 | struct btrfs_path *log_path, | 
|  | 2137 | struct inode *dir, | 
|  | 2138 | struct btrfs_key *dir_key) | 
|  | 2139 | { | 
|  | 2140 | int ret; | 
|  | 2141 | struct extent_buffer *eb; | 
|  | 2142 | int slot; | 
|  | 2143 | u32 item_size; | 
|  | 2144 | struct btrfs_dir_item *di; | 
|  | 2145 | struct btrfs_dir_item *log_di; | 
|  | 2146 | int name_len; | 
|  | 2147 | unsigned long ptr; | 
|  | 2148 | unsigned long ptr_end; | 
|  | 2149 | char *name; | 
|  | 2150 | struct inode *inode; | 
|  | 2151 | struct btrfs_key location; | 
|  | 2152 |  | 
|  | 2153 | again: | 
|  | 2154 | eb = path->nodes[0]; | 
|  | 2155 | slot = path->slots[0]; | 
|  | 2156 | item_size = btrfs_item_size_nr(eb, slot); | 
|  | 2157 | ptr = btrfs_item_ptr_offset(eb, slot); | 
|  | 2158 | ptr_end = ptr + item_size; | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2159 | while (ptr < ptr_end) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2160 | di = (struct btrfs_dir_item *)ptr; | 
|  | 2161 | name_len = btrfs_dir_name_len(eb, di); | 
|  | 2162 | name = kmalloc(name_len, GFP_NOFS); | 
|  | 2163 | if (!name) { | 
|  | 2164 | ret = -ENOMEM; | 
|  | 2165 | goto out; | 
|  | 2166 | } | 
|  | 2167 | read_extent_buffer(eb, name, (unsigned long)(di + 1), | 
|  | 2168 | name_len); | 
|  | 2169 | log_di = NULL; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2170 | if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2171 | log_di = btrfs_lookup_dir_item(trans, log, log_path, | 
|  | 2172 | dir_key->objectid, | 
|  | 2173 | name, name_len, 0); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2174 | } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2175 | log_di = btrfs_lookup_dir_index_item(trans, log, | 
|  | 2176 | log_path, | 
|  | 2177 | dir_key->objectid, | 
|  | 2178 | dir_key->offset, | 
|  | 2179 | name, name_len, 0); | 
|  | 2180 | } | 
| Al Viro | 8d9e220 | 2018-07-29 23:04:46 +0100 | [diff] [blame] | 2181 | if (!log_di || log_di == ERR_PTR(-ENOENT)) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2182 | btrfs_dir_item_key_to_cpu(eb, di, &location); | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2183 | btrfs_release_path(path); | 
|  | 2184 | btrfs_release_path(log_path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2185 | inode = read_one_inode(root, location.objectid); | 
| Tsutomu Itoh | c00e949 | 2011-04-28 09:10:23 +0000 | [diff] [blame] | 2186 | if (!inode) { | 
|  | 2187 | kfree(name); | 
|  | 2188 | return -EIO; | 
|  | 2189 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2190 |  | 
|  | 2191 | ret = link_to_fixup_dir(trans, root, | 
|  | 2192 | path, location.objectid); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2193 | if (ret) { | 
|  | 2194 | kfree(name); | 
|  | 2195 | iput(inode); | 
|  | 2196 | goto out; | 
|  | 2197 | } | 
|  | 2198 |  | 
| Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 2199 | inc_nlink(inode); | 
| Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 2200 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), | 
|  | 2201 | BTRFS_I(inode), name, name_len); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2202 | if (!ret) | 
| Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 2203 | ret = btrfs_run_delayed_items(trans); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2204 | kfree(name); | 
|  | 2205 | iput(inode); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2206 | if (ret) | 
|  | 2207 | goto out; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2208 |  | 
|  | 2209 | /* there might still be more names under this key | 
|  | 2210 | * check and repeat if required | 
|  | 2211 | */ | 
|  | 2212 | ret = btrfs_search_slot(NULL, root, dir_key, path, | 
|  | 2213 | 0, 0); | 
|  | 2214 | if (ret == 0) | 
|  | 2215 | goto again; | 
|  | 2216 | ret = 0; | 
|  | 2217 | goto out; | 
| Filipe David Borba Manana | 269d040 | 2013-10-28 17:39:21 +0000 | [diff] [blame] | 2218 | } else if (IS_ERR(log_di)) { | 
|  | 2219 | kfree(name); | 
|  | 2220 | return PTR_ERR(log_di); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2221 | } | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2222 | btrfs_release_path(log_path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2223 | kfree(name); | 
|  | 2224 |  | 
|  | 2225 | ptr = (unsigned long)(di + 1); | 
|  | 2226 | ptr += name_len; | 
|  | 2227 | } | 
|  | 2228 | ret = 0; | 
|  | 2229 | out: | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2230 | btrfs_release_path(path); | 
|  | 2231 | btrfs_release_path(log_path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2232 | return ret; | 
|  | 2233 | } | 
|  | 2234 |  | 
| Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2235 | static int replay_xattr_deletes(struct btrfs_trans_handle *trans, | 
|  | 2236 | struct btrfs_root *root, | 
|  | 2237 | struct btrfs_root *log, | 
|  | 2238 | struct btrfs_path *path, | 
|  | 2239 | const u64 ino) | 
|  | 2240 | { | 
|  | 2241 | struct btrfs_key search_key; | 
|  | 2242 | struct btrfs_path *log_path; | 
|  | 2243 | int i; | 
|  | 2244 | int nritems; | 
|  | 2245 | int ret; | 
|  | 2246 |  | 
|  | 2247 | log_path = btrfs_alloc_path(); | 
|  | 2248 | if (!log_path) | 
|  | 2249 | return -ENOMEM; | 
|  | 2250 |  | 
|  | 2251 | search_key.objectid = ino; | 
|  | 2252 | search_key.type = BTRFS_XATTR_ITEM_KEY; | 
|  | 2253 | search_key.offset = 0; | 
|  | 2254 | again: | 
|  | 2255 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | 
|  | 2256 | if (ret < 0) | 
|  | 2257 | goto out; | 
|  | 2258 | process_leaf: | 
|  | 2259 | nritems = btrfs_header_nritems(path->nodes[0]); | 
|  | 2260 | for (i = path->slots[0]; i < nritems; i++) { | 
|  | 2261 | struct btrfs_key key; | 
|  | 2262 | struct btrfs_dir_item *di; | 
|  | 2263 | struct btrfs_dir_item *log_di; | 
|  | 2264 | u32 total_size; | 
|  | 2265 | u32 cur; | 
|  | 2266 |  | 
|  | 2267 | btrfs_item_key_to_cpu(path->nodes[0], &key, i); | 
|  | 2268 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) { | 
|  | 2269 | ret = 0; | 
|  | 2270 | goto out; | 
|  | 2271 | } | 
|  | 2272 |  | 
|  | 2273 | di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item); | 
|  | 2274 | total_size = btrfs_item_size_nr(path->nodes[0], i); | 
|  | 2275 | cur = 0; | 
|  | 2276 | while (cur < total_size) { | 
|  | 2277 | u16 name_len = btrfs_dir_name_len(path->nodes[0], di); | 
|  | 2278 | u16 data_len = btrfs_dir_data_len(path->nodes[0], di); | 
|  | 2279 | u32 this_len = sizeof(*di) + name_len + data_len; | 
|  | 2280 | char *name; | 
|  | 2281 |  | 
|  | 2282 | name = kmalloc(name_len, GFP_NOFS); | 
|  | 2283 | if (!name) { | 
|  | 2284 | ret = -ENOMEM; | 
|  | 2285 | goto out; | 
|  | 2286 | } | 
|  | 2287 | read_extent_buffer(path->nodes[0], name, | 
|  | 2288 | (unsigned long)(di + 1), name_len); | 
|  | 2289 |  | 
|  | 2290 | log_di = btrfs_lookup_xattr(NULL, log, log_path, ino, | 
|  | 2291 | name, name_len, 0); | 
|  | 2292 | btrfs_release_path(log_path); | 
|  | 2293 | if (!log_di) { | 
|  | 2294 | /* Doesn't exist in log tree, so delete it. */ | 
|  | 2295 | btrfs_release_path(path); | 
|  | 2296 | di = btrfs_lookup_xattr(trans, root, path, ino, | 
|  | 2297 | name, name_len, -1); | 
|  | 2298 | kfree(name); | 
|  | 2299 | if (IS_ERR(di)) { | 
|  | 2300 | ret = PTR_ERR(di); | 
|  | 2301 | goto out; | 
|  | 2302 | } | 
|  | 2303 | ASSERT(di); | 
|  | 2304 | ret = btrfs_delete_one_dir_name(trans, root, | 
|  | 2305 | path, di); | 
|  | 2306 | if (ret) | 
|  | 2307 | goto out; | 
|  | 2308 | btrfs_release_path(path); | 
|  | 2309 | search_key = key; | 
|  | 2310 | goto again; | 
|  | 2311 | } | 
|  | 2312 | kfree(name); | 
|  | 2313 | if (IS_ERR(log_di)) { | 
|  | 2314 | ret = PTR_ERR(log_di); | 
|  | 2315 | goto out; | 
|  | 2316 | } | 
|  | 2317 | cur += this_len; | 
|  | 2318 | di = (struct btrfs_dir_item *)((char *)di + this_len); | 
|  | 2319 | } | 
|  | 2320 | } | 
|  | 2321 | ret = btrfs_next_leaf(root, path); | 
|  | 2322 | if (ret > 0) | 
|  | 2323 | ret = 0; | 
|  | 2324 | else if (ret == 0) | 
|  | 2325 | goto process_leaf; | 
|  | 2326 | out: | 
|  | 2327 | btrfs_free_path(log_path); | 
|  | 2328 | btrfs_release_path(path); | 
|  | 2329 | return ret; | 
|  | 2330 | } | 
|  | 2331 |  | 
|  | 2332 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2333 | /* | 
|  | 2334 | * deletion replay happens before we copy any new directory items | 
|  | 2335 | * out of the log or out of backreferences from inodes.  It | 
|  | 2336 | * scans the log to find ranges of keys that log is authoritative for, | 
|  | 2337 | * and then scans the directory to find items in those ranges that are | 
|  | 2338 | * not present in the log. | 
|  | 2339 | * | 
|  | 2340 | * Anything we don't find in the log is unlinked and removed from the | 
|  | 2341 | * directory. | 
|  | 2342 | */ | 
|  | 2343 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, | 
|  | 2344 | struct btrfs_root *root, | 
|  | 2345 | struct btrfs_root *log, | 
|  | 2346 | struct btrfs_path *path, | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2347 | u64 dirid, int del_all) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2348 | { | 
|  | 2349 | u64 range_start; | 
|  | 2350 | u64 range_end; | 
|  | 2351 | int key_type = BTRFS_DIR_LOG_ITEM_KEY; | 
|  | 2352 | int ret = 0; | 
|  | 2353 | struct btrfs_key dir_key; | 
|  | 2354 | struct btrfs_key found_key; | 
|  | 2355 | struct btrfs_path *log_path; | 
|  | 2356 | struct inode *dir; | 
|  | 2357 |  | 
|  | 2358 | dir_key.objectid = dirid; | 
|  | 2359 | dir_key.type = BTRFS_DIR_ITEM_KEY; | 
|  | 2360 | log_path = btrfs_alloc_path(); | 
|  | 2361 | if (!log_path) | 
|  | 2362 | return -ENOMEM; | 
|  | 2363 |  | 
|  | 2364 | dir = read_one_inode(root, dirid); | 
|  | 2365 | /* it isn't an error if the inode isn't there, that can happen | 
|  | 2366 | * because we replay the deletes before we copy in the inode item | 
|  | 2367 | * from the log | 
|  | 2368 | */ | 
|  | 2369 | if (!dir) { | 
|  | 2370 | btrfs_free_path(log_path); | 
|  | 2371 | return 0; | 
|  | 2372 | } | 
|  | 2373 | again: | 
|  | 2374 | range_start = 0; | 
|  | 2375 | range_end = 0; | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2376 | while (1) { | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2377 | if (del_all) | 
|  | 2378 | range_end = (u64)-1; | 
|  | 2379 | else { | 
|  | 2380 | ret = find_dir_range(log, path, dirid, key_type, | 
|  | 2381 | &range_start, &range_end); | 
|  | 2382 | if (ret != 0) | 
|  | 2383 | break; | 
|  | 2384 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2385 |  | 
|  | 2386 | dir_key.offset = range_start; | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2387 | while (1) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2388 | int nritems; | 
|  | 2389 | ret = btrfs_search_slot(NULL, root, &dir_key, path, | 
|  | 2390 | 0, 0); | 
|  | 2391 | if (ret < 0) | 
|  | 2392 | goto out; | 
|  | 2393 |  | 
|  | 2394 | nritems = btrfs_header_nritems(path->nodes[0]); | 
|  | 2395 | if (path->slots[0] >= nritems) { | 
|  | 2396 | ret = btrfs_next_leaf(root, path); | 
| Liu Bo | b98def7 | 2018-04-03 01:59:48 +0800 | [diff] [blame] | 2397 | if (ret == 1) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2398 | break; | 
| Liu Bo | b98def7 | 2018-04-03 01:59:48 +0800 | [diff] [blame] | 2399 | else if (ret < 0) | 
|  | 2400 | goto out; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2401 | } | 
|  | 2402 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | 
|  | 2403 | path->slots[0]); | 
|  | 2404 | if (found_key.objectid != dirid || | 
|  | 2405 | found_key.type != dir_key.type) | 
|  | 2406 | goto next_type; | 
|  | 2407 |  | 
|  | 2408 | if (found_key.offset > range_end) | 
|  | 2409 | break; | 
|  | 2410 |  | 
|  | 2411 | ret = check_item_in_log(trans, root, log, path, | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2412 | log_path, dir, | 
|  | 2413 | &found_key); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2414 | if (ret) | 
|  | 2415 | goto out; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2416 | if (found_key.offset == (u64)-1) | 
|  | 2417 | break; | 
|  | 2418 | dir_key.offset = found_key.offset + 1; | 
|  | 2419 | } | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2420 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2421 | if (range_end == (u64)-1) | 
|  | 2422 | break; | 
|  | 2423 | range_start = range_end + 1; | 
|  | 2424 | } | 
|  | 2425 |  | 
|  | 2426 | next_type: | 
|  | 2427 | ret = 0; | 
|  | 2428 | if (key_type == BTRFS_DIR_LOG_ITEM_KEY) { | 
|  | 2429 | key_type = BTRFS_DIR_LOG_INDEX_KEY; | 
|  | 2430 | dir_key.type = BTRFS_DIR_INDEX_KEY; | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2431 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2432 | goto again; | 
|  | 2433 | } | 
|  | 2434 | out: | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2435 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2436 | btrfs_free_path(log_path); | 
|  | 2437 | iput(dir); | 
|  | 2438 | return ret; | 
|  | 2439 | } | 
|  | 2440 |  | 
|  | 2441 | /* | 
|  | 2442 | * the process_func used to replay items from the log tree.  This | 
|  | 2443 | * gets called in two different stages.  The first stage just looks | 
|  | 2444 | * for inodes and makes sure they are all copied into the subvolume. | 
|  | 2445 | * | 
|  | 2446 | * The second stage copies all the other item types from the log into | 
|  | 2447 | * the subvolume.  The two stage approach is slower, but gets rid of | 
|  | 2448 | * lots of complexity around inodes referencing other inodes that exist | 
|  | 2449 | * only in the log (references come from either directory items or inode | 
|  | 2450 | * back refs). | 
|  | 2451 | */ | 
|  | 2452 | static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, | 
| Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2453 | struct walk_control *wc, u64 gen, int level) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2454 | { | 
|  | 2455 | int nritems; | 
|  | 2456 | struct btrfs_path *path; | 
|  | 2457 | struct btrfs_root *root = wc->replay_dest; | 
|  | 2458 | struct btrfs_key key; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2459 | int i; | 
|  | 2460 | int ret; | 
|  | 2461 |  | 
| Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2462 | ret = btrfs_read_buffer(eb, gen, level, NULL); | 
| Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2463 | if (ret) | 
|  | 2464 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2465 |  | 
|  | 2466 | level = btrfs_header_level(eb); | 
|  | 2467 |  | 
|  | 2468 | if (level != 0) | 
|  | 2469 | return 0; | 
|  | 2470 |  | 
|  | 2471 | path = btrfs_alloc_path(); | 
| Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2472 | if (!path) | 
|  | 2473 | return -ENOMEM; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2474 |  | 
|  | 2475 | nritems = btrfs_header_nritems(eb); | 
|  | 2476 | for (i = 0; i < nritems; i++) { | 
|  | 2477 | btrfs_item_key_to_cpu(eb, &key, i); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2478 |  | 
|  | 2479 | /* inode keys are done during the first stage */ | 
|  | 2480 | if (key.type == BTRFS_INODE_ITEM_KEY && | 
|  | 2481 | wc->stage == LOG_WALK_REPLAY_INODES) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2482 | struct btrfs_inode_item *inode_item; | 
|  | 2483 | u32 mode; | 
|  | 2484 |  | 
|  | 2485 | inode_item = btrfs_item_ptr(eb, i, | 
|  | 2486 | struct btrfs_inode_item); | 
| Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 2487 | ret = replay_xattr_deletes(wc->trans, root, log, | 
|  | 2488 | path, key.objectid); | 
|  | 2489 | if (ret) | 
|  | 2490 | break; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2491 | mode = btrfs_inode_mode(eb, inode_item); | 
|  | 2492 | if (S_ISDIR(mode)) { | 
|  | 2493 | ret = replay_dir_deletes(wc->trans, | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2494 | root, log, path, key.objectid, 0); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2495 | if (ret) | 
|  | 2496 | break; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2497 | } | 
|  | 2498 | ret = overwrite_item(wc->trans, root, path, | 
|  | 2499 | eb, i, &key); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2500 | if (ret) | 
|  | 2501 | break; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2502 |  | 
| Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2503 | /* | 
|  | 2504 | * Before replaying extents, truncate the inode to its | 
|  | 2505 | * size. We need to do it now and not after log replay | 
|  | 2506 | * because before an fsync we can have prealloc extents | 
|  | 2507 | * added beyond the inode's i_size. If we did it after, | 
|  | 2508 | * through orphan cleanup for example, we would drop | 
|  | 2509 | * those prealloc extents just after replaying them. | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2510 | */ | 
|  | 2511 | if (S_ISREG(mode)) { | 
| Filipe Manana | 471d557 | 2018-04-05 22:55:12 +0100 | [diff] [blame] | 2512 | struct inode *inode; | 
|  | 2513 | u64 from; | 
|  | 2514 |  | 
|  | 2515 | inode = read_one_inode(root, key.objectid); | 
|  | 2516 | if (!inode) { | 
|  | 2517 | ret = -EIO; | 
|  | 2518 | break; | 
|  | 2519 | } | 
|  | 2520 | from = ALIGN(i_size_read(inode), | 
|  | 2521 | root->fs_info->sectorsize); | 
|  | 2522 | ret = btrfs_drop_extents(wc->trans, root, inode, | 
|  | 2523 | from, (u64)-1, 1); | 
|  | 2524 | /* | 
|  | 2525 | * If the nlink count is zero here, the iput | 
|  | 2526 | * will free the inode.  We bump it to make | 
|  | 2527 | * sure it doesn't get freed until the link | 
|  | 2528 | * count fixup is done. | 
|  | 2529 | */ | 
|  | 2530 | if (!ret) { | 
|  | 2531 | if (inode->i_nlink == 0) | 
|  | 2532 | inc_nlink(inode); | 
|  | 2533 | /* Update link count and nbytes. */ | 
|  | 2534 | ret = btrfs_update_inode(wc->trans, | 
|  | 2535 | root, inode); | 
|  | 2536 | } | 
|  | 2537 | iput(inode); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2538 | if (ret) | 
|  | 2539 | break; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2540 | } | 
| Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2541 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2542 | ret = link_to_fixup_dir(wc->trans, root, | 
|  | 2543 | path, key.objectid); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2544 | if (ret) | 
|  | 2545 | break; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2546 | } | 
| Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2547 |  | 
|  | 2548 | if (key.type == BTRFS_DIR_INDEX_KEY && | 
|  | 2549 | wc->stage == LOG_WALK_REPLAY_DIR_INDEX) { | 
|  | 2550 | ret = replay_one_dir_item(wc->trans, root, path, | 
|  | 2551 | eb, i, &key); | 
|  | 2552 | if (ret) | 
|  | 2553 | break; | 
|  | 2554 | } | 
|  | 2555 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2556 | if (wc->stage < LOG_WALK_REPLAY_ALL) | 
|  | 2557 | continue; | 
|  | 2558 |  | 
|  | 2559 | /* these keys are simply copied */ | 
|  | 2560 | if (key.type == BTRFS_XATTR_ITEM_KEY) { | 
|  | 2561 | ret = overwrite_item(wc->trans, root, path, | 
|  | 2562 | eb, i, &key); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2563 | if (ret) | 
|  | 2564 | break; | 
| Liu Bo | 2da1c66 | 2013-05-26 13:50:29 +0000 | [diff] [blame] | 2565 | } else if (key.type == BTRFS_INODE_REF_KEY || | 
|  | 2566 | key.type == BTRFS_INODE_EXTREF_KEY) { | 
| Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 2567 | ret = add_inode_ref(wc->trans, root, log, path, | 
|  | 2568 | eb, i, &key); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2569 | if (ret && ret != -ENOENT) | 
|  | 2570 | break; | 
|  | 2571 | ret = 0; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2572 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { | 
|  | 2573 | ret = replay_one_extent(wc->trans, root, path, | 
|  | 2574 | eb, i, &key); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2575 | if (ret) | 
|  | 2576 | break; | 
| Josef Bacik | dd8e721 | 2013-09-11 11:57:23 -0400 | [diff] [blame] | 2577 | } else if (key.type == BTRFS_DIR_ITEM_KEY) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2578 | ret = replay_one_dir_item(wc->trans, root, path, | 
|  | 2579 | eb, i, &key); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2580 | if (ret) | 
|  | 2581 | break; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2582 | } | 
|  | 2583 | } | 
|  | 2584 | btrfs_free_path(path); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2585 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2586 | } | 
|  | 2587 |  | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2588 | static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2589 | struct btrfs_root *root, | 
|  | 2590 | struct btrfs_path *path, int *level, | 
|  | 2591 | struct walk_control *wc) | 
|  | 2592 | { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2593 | struct btrfs_fs_info *fs_info = root->fs_info; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2594 | u64 root_owner; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2595 | u64 bytenr; | 
|  | 2596 | u64 ptr_gen; | 
|  | 2597 | struct extent_buffer *next; | 
|  | 2598 | struct extent_buffer *cur; | 
|  | 2599 | struct extent_buffer *parent; | 
|  | 2600 | u32 blocksize; | 
|  | 2601 | int ret = 0; | 
|  | 2602 |  | 
|  | 2603 | WARN_ON(*level < 0); | 
|  | 2604 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | 
|  | 2605 |  | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2606 | while (*level > 0) { | 
| Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2607 | struct btrfs_key first_key; | 
|  | 2608 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2609 | WARN_ON(*level < 0); | 
|  | 2610 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | 
|  | 2611 | cur = path->nodes[*level]; | 
|  | 2612 |  | 
| Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2613 | WARN_ON(btrfs_header_level(cur) != *level); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2614 |  | 
|  | 2615 | if (path->slots[*level] >= | 
|  | 2616 | btrfs_header_nritems(cur)) | 
|  | 2617 | break; | 
|  | 2618 |  | 
|  | 2619 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); | 
|  | 2620 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); | 
| Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2621 | btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]); | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2622 | blocksize = fs_info->nodesize; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2623 |  | 
|  | 2624 | parent = path->nodes[*level]; | 
|  | 2625 | root_owner = btrfs_header_owner(parent); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2626 |  | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2627 | next = btrfs_find_create_tree_block(fs_info, bytenr); | 
| Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 2628 | if (IS_ERR(next)) | 
|  | 2629 | return PTR_ERR(next); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2630 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2631 | if (*level == 1) { | 
| Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2632 | ret = wc->process_func(root, next, wc, ptr_gen, | 
|  | 2633 | *level - 1); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2634 | if (ret) { | 
|  | 2635 | free_extent_buffer(next); | 
| Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2636 | return ret; | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2637 | } | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2638 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2639 | path->slots[*level]++; | 
|  | 2640 | if (wc->free) { | 
| Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2641 | ret = btrfs_read_buffer(next, ptr_gen, | 
|  | 2642 | *level - 1, &first_key); | 
| Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2643 | if (ret) { | 
|  | 2644 | free_extent_buffer(next); | 
|  | 2645 | return ret; | 
|  | 2646 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2647 |  | 
| Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2648 | if (trans) { | 
|  | 2649 | btrfs_tree_lock(next); | 
|  | 2650 | btrfs_set_lock_blocking(next); | 
| David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2651 | clean_tree_block(fs_info, next); | 
| Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2652 | btrfs_wait_tree_block_writeback(next); | 
|  | 2653 | btrfs_tree_unlock(next); | 
| Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2654 | } else { | 
|  | 2655 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) | 
|  | 2656 | clear_extent_buffer_dirty(next); | 
| Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2657 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2658 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2659 | WARN_ON(root_owner != | 
|  | 2660 | BTRFS_TREE_LOG_OBJECTID); | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2661 | ret = btrfs_free_and_pin_reserved_extent( | 
|  | 2662 | fs_info, bytenr, | 
|  | 2663 | blocksize); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2664 | if (ret) { | 
|  | 2665 | free_extent_buffer(next); | 
|  | 2666 | return ret; | 
|  | 2667 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2668 | } | 
|  | 2669 | free_extent_buffer(next); | 
|  | 2670 | continue; | 
|  | 2671 | } | 
| Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2672 | ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key); | 
| Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 2673 | if (ret) { | 
|  | 2674 | free_extent_buffer(next); | 
|  | 2675 | return ret; | 
|  | 2676 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2677 |  | 
|  | 2678 | WARN_ON(*level <= 0); | 
|  | 2679 | if (path->nodes[*level-1]) | 
|  | 2680 | free_extent_buffer(path->nodes[*level-1]); | 
|  | 2681 | path->nodes[*level-1] = next; | 
|  | 2682 | *level = btrfs_header_level(next); | 
|  | 2683 | path->slots[*level] = 0; | 
|  | 2684 | cond_resched(); | 
|  | 2685 | } | 
|  | 2686 | WARN_ON(*level < 0); | 
|  | 2687 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | 
|  | 2688 |  | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2689 | path->slots[*level] = btrfs_header_nritems(path->nodes[*level]); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2690 |  | 
|  | 2691 | cond_resched(); | 
|  | 2692 | return 0; | 
|  | 2693 | } | 
|  | 2694 |  | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2695 | static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans, | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2696 | struct btrfs_root *root, | 
|  | 2697 | struct btrfs_path *path, int *level, | 
|  | 2698 | struct walk_control *wc) | 
|  | 2699 | { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2700 | struct btrfs_fs_info *fs_info = root->fs_info; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2701 | u64 root_owner; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2702 | int i; | 
|  | 2703 | int slot; | 
|  | 2704 | int ret; | 
|  | 2705 |  | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2706 | for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2707 | slot = path->slots[i]; | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 2708 | if (slot + 1 < btrfs_header_nritems(path->nodes[i])) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2709 | path->slots[i]++; | 
|  | 2710 | *level = i; | 
|  | 2711 | WARN_ON(*level == 0); | 
|  | 2712 | return 0; | 
|  | 2713 | } else { | 
| Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2714 | struct extent_buffer *parent; | 
|  | 2715 | if (path->nodes[*level] == root->node) | 
|  | 2716 | parent = path->nodes[*level]; | 
|  | 2717 | else | 
|  | 2718 | parent = path->nodes[*level + 1]; | 
|  | 2719 |  | 
|  | 2720 | root_owner = btrfs_header_owner(parent); | 
| Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2721 | ret = wc->process_func(root, path->nodes[*level], wc, | 
| Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2722 | btrfs_header_generation(path->nodes[*level]), | 
|  | 2723 | *level); | 
| Mark Fasheh | 1e5063d | 2011-07-12 10:46:06 -0700 | [diff] [blame] | 2724 | if (ret) | 
|  | 2725 | return ret; | 
|  | 2726 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2727 | if (wc->free) { | 
|  | 2728 | struct extent_buffer *next; | 
|  | 2729 |  | 
|  | 2730 | next = path->nodes[*level]; | 
|  | 2731 |  | 
| Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2732 | if (trans) { | 
|  | 2733 | btrfs_tree_lock(next); | 
|  | 2734 | btrfs_set_lock_blocking(next); | 
| David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2735 | clean_tree_block(fs_info, next); | 
| Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2736 | btrfs_wait_tree_block_writeback(next); | 
|  | 2737 | btrfs_tree_unlock(next); | 
| Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2738 | } else { | 
|  | 2739 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) | 
|  | 2740 | clear_extent_buffer_dirty(next); | 
| Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2741 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2742 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2743 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2744 | ret = btrfs_free_and_pin_reserved_extent( | 
|  | 2745 | fs_info, | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2746 | path->nodes[*level]->start, | 
| Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2747 | path->nodes[*level]->len); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2748 | if (ret) | 
|  | 2749 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2750 | } | 
|  | 2751 | free_extent_buffer(path->nodes[*level]); | 
|  | 2752 | path->nodes[*level] = NULL; | 
|  | 2753 | *level = i + 1; | 
|  | 2754 | } | 
|  | 2755 | } | 
|  | 2756 | return 1; | 
|  | 2757 | } | 
|  | 2758 |  | 
|  | 2759 | /* | 
|  | 2760 | * drop the reference count on the tree rooted at 'snap'.  This traverses | 
|  | 2761 | * the tree freeing any blocks that have a ref count of zero after being | 
|  | 2762 | * decremented. | 
|  | 2763 | */ | 
|  | 2764 | static int walk_log_tree(struct btrfs_trans_handle *trans, | 
|  | 2765 | struct btrfs_root *log, struct walk_control *wc) | 
|  | 2766 | { | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2767 | struct btrfs_fs_info *fs_info = log->fs_info; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2768 | int ret = 0; | 
|  | 2769 | int wret; | 
|  | 2770 | int level; | 
|  | 2771 | struct btrfs_path *path; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2772 | int orig_level; | 
|  | 2773 |  | 
|  | 2774 | path = btrfs_alloc_path(); | 
| Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 2775 | if (!path) | 
|  | 2776 | return -ENOMEM; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2777 |  | 
|  | 2778 | level = btrfs_header_level(log->node); | 
|  | 2779 | orig_level = level; | 
|  | 2780 | path->nodes[level] = log->node; | 
|  | 2781 | extent_buffer_get(log->node); | 
|  | 2782 | path->slots[level] = 0; | 
|  | 2783 |  | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2784 | while (1) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2785 | wret = walk_down_log_tree(trans, log, path, &level, wc); | 
|  | 2786 | if (wret > 0) | 
|  | 2787 | break; | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2788 | if (wret < 0) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2789 | ret = wret; | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2790 | goto out; | 
|  | 2791 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2792 |  | 
|  | 2793 | wret = walk_up_log_tree(trans, log, path, &level, wc); | 
|  | 2794 | if (wret > 0) | 
|  | 2795 | break; | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2796 | if (wret < 0) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2797 | ret = wret; | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2798 | goto out; | 
|  | 2799 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2800 | } | 
|  | 2801 |  | 
|  | 2802 | /* was the root node processed? if not, catch it here */ | 
|  | 2803 | if (path->nodes[orig_level]) { | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2804 | ret = wc->process_func(log, path->nodes[orig_level], wc, | 
| Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2805 | btrfs_header_generation(path->nodes[orig_level]), | 
|  | 2806 | orig_level); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2807 | if (ret) | 
|  | 2808 | goto out; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2809 | if (wc->free) { | 
|  | 2810 | struct extent_buffer *next; | 
|  | 2811 |  | 
|  | 2812 | next = path->nodes[orig_level]; | 
|  | 2813 |  | 
| Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2814 | if (trans) { | 
|  | 2815 | btrfs_tree_lock(next); | 
|  | 2816 | btrfs_set_lock_blocking(next); | 
| David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 2817 | clean_tree_block(fs_info, next); | 
| Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2818 | btrfs_wait_tree_block_writeback(next); | 
|  | 2819 | btrfs_tree_unlock(next); | 
| Liu Bo | 1846430 | 2018-01-25 11:02:51 -0700 | [diff] [blame] | 2820 | } else { | 
|  | 2821 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags)) | 
|  | 2822 | clear_extent_buffer_dirty(next); | 
| Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 2823 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2824 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2825 | WARN_ON(log->root_key.objectid != | 
|  | 2826 | BTRFS_TREE_LOG_OBJECTID); | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2827 | ret = btrfs_free_and_pin_reserved_extent(fs_info, | 
|  | 2828 | next->start, next->len); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 2829 | if (ret) | 
|  | 2830 | goto out; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2831 | } | 
|  | 2832 | } | 
|  | 2833 |  | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2834 | out: | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2835 | btrfs_free_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2836 | return ret; | 
|  | 2837 | } | 
|  | 2838 |  | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2839 | /* | 
|  | 2840 | * helper function to update the item for a given subvolumes log root | 
|  | 2841 | * in the tree of log roots | 
|  | 2842 | */ | 
|  | 2843 | static int update_log_root(struct btrfs_trans_handle *trans, | 
|  | 2844 | struct btrfs_root *log) | 
|  | 2845 | { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2846 | struct btrfs_fs_info *fs_info = log->fs_info; | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2847 | int ret; | 
|  | 2848 |  | 
|  | 2849 | if (log->log_transid == 1) { | 
|  | 2850 | /* insert root item on the first sync */ | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2851 | ret = btrfs_insert_root(trans, fs_info->log_root_tree, | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2852 | &log->root_key, &log->root_item); | 
|  | 2853 | } else { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2854 | ret = btrfs_update_root(trans, fs_info->log_root_tree, | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2855 | &log->root_key, &log->root_item); | 
|  | 2856 | } | 
|  | 2857 | return ret; | 
|  | 2858 | } | 
|  | 2859 |  | 
| Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2860 | static void wait_log_commit(struct btrfs_root *root, int transid) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2861 | { | 
|  | 2862 | DEFINE_WAIT(wait); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2863 | int index = transid % 2; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2864 |  | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2865 | /* | 
|  | 2866 | * we only allow two pending log transactions at a time, | 
|  | 2867 | * so we know that if ours is more than 2 older than the | 
|  | 2868 | * current transaction, we're done | 
|  | 2869 | */ | 
| Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2870 | for (;;) { | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2871 | prepare_to_wait(&root->log_commit_wait[index], | 
|  | 2872 | &wait, TASK_UNINTERRUPTIBLE); | 
| Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2873 |  | 
|  | 2874 | if (!(root->log_transid_committed < transid && | 
|  | 2875 | atomic_read(&root->log_commit[index]))) | 
|  | 2876 | break; | 
|  | 2877 |  | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2878 | mutex_unlock(&root->log_mutex); | 
| Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2879 | schedule(); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2880 | mutex_lock(&root->log_mutex); | 
| Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2881 | } | 
|  | 2882 | finish_wait(&root->log_commit_wait[index], &wait); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2883 | } | 
|  | 2884 |  | 
| Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2885 | static void wait_for_writer(struct btrfs_root *root) | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2886 | { | 
|  | 2887 | DEFINE_WAIT(wait); | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2888 |  | 
| Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2889 | for (;;) { | 
|  | 2890 | prepare_to_wait(&root->log_writer_wait, &wait, | 
|  | 2891 | TASK_UNINTERRUPTIBLE); | 
|  | 2892 | if (!atomic_read(&root->log_writers)) | 
|  | 2893 | break; | 
|  | 2894 |  | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2895 | mutex_unlock(&root->log_mutex); | 
| Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2896 | schedule(); | 
| Filipe Manana | 575849e | 2015-02-11 11:12:39 +0000 | [diff] [blame] | 2897 | mutex_lock(&root->log_mutex); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2898 | } | 
| Liu Bo | 49e83f5 | 2017-09-01 16:14:30 -0600 | [diff] [blame] | 2899 | finish_wait(&root->log_writer_wait, &wait); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2900 | } | 
|  | 2901 |  | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2902 | static inline void btrfs_remove_log_ctx(struct btrfs_root *root, | 
|  | 2903 | struct btrfs_log_ctx *ctx) | 
|  | 2904 | { | 
|  | 2905 | if (!ctx) | 
|  | 2906 | return; | 
|  | 2907 |  | 
|  | 2908 | mutex_lock(&root->log_mutex); | 
|  | 2909 | list_del_init(&ctx->list); | 
|  | 2910 | mutex_unlock(&root->log_mutex); | 
|  | 2911 | } | 
|  | 2912 |  | 
|  | 2913 | /* | 
|  | 2914 | * Invoked in log mutex context, or be sure there is no other task which | 
|  | 2915 | * can access the list. | 
|  | 2916 | */ | 
|  | 2917 | static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root, | 
|  | 2918 | int index, int error) | 
|  | 2919 | { | 
|  | 2920 | struct btrfs_log_ctx *ctx; | 
| Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2921 | struct btrfs_log_ctx *safe; | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2922 |  | 
| Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2923 | list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) { | 
|  | 2924 | list_del_init(&ctx->list); | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2925 | ctx->log_ret = error; | 
| Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 2926 | } | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2927 |  | 
|  | 2928 | INIT_LIST_HEAD(&root->log_ctxs[index]); | 
|  | 2929 | } | 
|  | 2930 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2931 | /* | 
|  | 2932 | * btrfs_sync_log does sends a given tree log down to the disk and | 
|  | 2933 | * updates the super blocks to record it.  When this call is done, | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2934 | * you know that any inodes previously logged are safely on disk only | 
|  | 2935 | * if it returns 0. | 
|  | 2936 | * | 
|  | 2937 | * Any other return value means you need to call btrfs_commit_transaction. | 
|  | 2938 | * Some of the edge cases for fsyncing directories that have had unlinks | 
|  | 2939 | * or renames done in the past mean that sometimes the only safe | 
|  | 2940 | * fsync is to commit the whole FS.  When btrfs_sync_log returns -EAGAIN, | 
|  | 2941 | * that has happened. | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2942 | */ | 
|  | 2943 | int btrfs_sync_log(struct btrfs_trans_handle *trans, | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2944 | struct btrfs_root *root, struct btrfs_log_ctx *ctx) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2945 | { | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2946 | int index1; | 
|  | 2947 | int index2; | 
| Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2948 | int mark; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2949 | int ret; | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2950 | struct btrfs_fs_info *fs_info = root->fs_info; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2951 | struct btrfs_root *log = root->log_root; | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2952 | struct btrfs_root *log_root_tree = fs_info->log_root_tree; | 
| Miao Xie | bb14a59 | 2014-02-20 18:08:56 +0800 | [diff] [blame] | 2953 | int log_transid = 0; | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2954 | struct btrfs_log_ctx root_log_ctx; | 
| Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 2955 | struct blk_plug plug; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2956 |  | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2957 | mutex_lock(&root->log_mutex); | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2958 | log_transid = ctx->log_transid; | 
|  | 2959 | if (root->log_transid_committed >= log_transid) { | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2960 | mutex_unlock(&root->log_mutex); | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2961 | return ctx->log_ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2962 | } | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2963 |  | 
|  | 2964 | index1 = log_transid % 2; | 
|  | 2965 | if (atomic_read(&root->log_commit[index1])) { | 
| Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2966 | wait_log_commit(root, log_transid); | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 2967 | mutex_unlock(&root->log_mutex); | 
|  | 2968 | return ctx->log_ret; | 
|  | 2969 | } | 
|  | 2970 | ASSERT(log_transid == root->log_transid); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2971 | atomic_set(&root->log_commit[index1], 1); | 
|  | 2972 |  | 
|  | 2973 | /* wait for previous tree log sync to complete */ | 
|  | 2974 | if (atomic_read(&root->log_commit[(index1 + 1) % 2])) | 
| Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2975 | wait_log_commit(root, log_transid - 1); | 
| Miao Xie | 48cab2e | 2014-02-20 18:08:52 +0800 | [diff] [blame] | 2976 |  | 
| Yan, Zheng | 86df7eb | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2977 | while (1) { | 
| Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2978 | int batch = atomic_read(&root->log_batch); | 
| Chris Mason | cd354ad | 2011-10-20 15:45:37 -0400 | [diff] [blame] | 2979 | /* when we're on an ssd, just kick the log commit out */ | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2980 | if (!btrfs_test_opt(fs_info, SSD) && | 
| Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 2981 | test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) { | 
| Yan, Zheng | 86df7eb | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2982 | mutex_unlock(&root->log_mutex); | 
|  | 2983 | schedule_timeout_uninterruptible(1); | 
|  | 2984 | mutex_lock(&root->log_mutex); | 
|  | 2985 | } | 
| Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 2986 | wait_for_writer(root); | 
| Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2987 | if (batch == atomic_read(&root->log_batch)) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2988 | break; | 
|  | 2989 | } | 
| Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2990 |  | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2991 | /* bail out if we need to do a full commit */ | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2992 | if (btrfs_need_log_full_commit(fs_info, trans)) { | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2993 | ret = -EAGAIN; | 
|  | 2994 | mutex_unlock(&root->log_mutex); | 
|  | 2995 | goto out; | 
|  | 2996 | } | 
|  | 2997 |  | 
| Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 2998 | if (log_transid % 2 == 0) | 
|  | 2999 | mark = EXTENT_DIRTY; | 
|  | 3000 | else | 
|  | 3001 | mark = EXTENT_NEW; | 
|  | 3002 |  | 
| Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 3003 | /* we start IO on  all the marked extents here, but we don't actually | 
|  | 3004 | * wait for them until later. | 
|  | 3005 | */ | 
| Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3006 | blk_start_plug(&plug); | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3007 | ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3008 | if (ret) { | 
| Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3009 | blk_finish_plug(&plug); | 
| Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3010 | btrfs_abort_transaction(trans, ret); | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3011 | btrfs_set_log_full_commit(fs_info, trans); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3012 | mutex_unlock(&root->log_mutex); | 
|  | 3013 | goto out; | 
|  | 3014 | } | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3015 |  | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3016 | btrfs_set_root_node(&log->root_item, log->node); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3017 |  | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3018 | root->log_transid++; | 
|  | 3019 | log->log_transid = root->log_transid; | 
| Josef Bacik | ff782e0 | 2009-10-08 15:30:04 -0400 | [diff] [blame] | 3020 | root->log_start_pid = 0; | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3021 | /* | 
| Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 3022 | * IO has been started, blocks of the log tree have WRITTEN flag set | 
|  | 3023 | * in their headers. new modifications of the log will be written to | 
|  | 3024 | * 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] | 3025 | */ | 
|  | 3026 | mutex_unlock(&root->log_mutex); | 
|  | 3027 |  | 
| Filipe Manana | 28a2359 | 2016-08-23 21:13:51 +0100 | [diff] [blame] | 3028 | btrfs_init_log_ctx(&root_log_ctx, NULL); | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3029 |  | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3030 | mutex_lock(&log_root_tree->log_mutex); | 
| Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 3031 | atomic_inc(&log_root_tree->log_batch); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3032 | atomic_inc(&log_root_tree->log_writers); | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3033 |  | 
|  | 3034 | index2 = log_root_tree->log_transid % 2; | 
|  | 3035 | list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]); | 
|  | 3036 | root_log_ctx.log_transid = log_root_tree->log_transid; | 
|  | 3037 |  | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3038 | mutex_unlock(&log_root_tree->log_mutex); | 
|  | 3039 |  | 
|  | 3040 | ret = update_log_root(trans, log); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3041 |  | 
|  | 3042 | mutex_lock(&log_root_tree->log_mutex); | 
|  | 3043 | if (atomic_dec_and_test(&log_root_tree->log_writers)) { | 
| David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3044 | /* atomic_dec_and_test implies a barrier */ | 
|  | 3045 | cond_wake_up_nomb(&log_root_tree->log_writer_wait); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3046 | } | 
|  | 3047 |  | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3048 | if (ret) { | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3049 | if (!list_empty(&root_log_ctx.list)) | 
|  | 3050 | list_del_init(&root_log_ctx.list); | 
|  | 3051 |  | 
| Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3052 | blk_finish_plug(&plug); | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3053 | btrfs_set_log_full_commit(fs_info, trans); | 
| Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 3054 |  | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3055 | if (ret != -ENOSPC) { | 
| Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3056 | btrfs_abort_transaction(trans, ret); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3057 | mutex_unlock(&log_root_tree->log_mutex); | 
|  | 3058 | goto out; | 
|  | 3059 | } | 
| Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3060 | btrfs_wait_tree_log_extents(log, mark); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3061 | mutex_unlock(&log_root_tree->log_mutex); | 
|  | 3062 | ret = -EAGAIN; | 
|  | 3063 | goto out; | 
|  | 3064 | } | 
|  | 3065 |  | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3066 | if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) { | 
| Forrest Liu | 3da5ab5 | 2015-01-30 19:42:12 +0800 | [diff] [blame] | 3067 | blk_finish_plug(&plug); | 
| Chris Mason | cbd60aa | 2016-09-06 05:37:40 -0700 | [diff] [blame] | 3068 | list_del_init(&root_log_ctx.list); | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3069 | mutex_unlock(&log_root_tree->log_mutex); | 
|  | 3070 | ret = root_log_ctx.log_ret; | 
|  | 3071 | goto out; | 
|  | 3072 | } | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3073 |  | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3074 | index2 = root_log_ctx.log_transid % 2; | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3075 | if (atomic_read(&log_root_tree->log_commit[index2])) { | 
| Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3076 | blk_finish_plug(&plug); | 
| Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3077 | ret = btrfs_wait_tree_log_extents(log, mark); | 
| Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3078 | wait_log_commit(log_root_tree, | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3079 | root_log_ctx.log_transid); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3080 | mutex_unlock(&log_root_tree->log_mutex); | 
| Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3081 | if (!ret) | 
|  | 3082 | ret = root_log_ctx.log_ret; | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3083 | goto out; | 
|  | 3084 | } | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3085 | ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid); | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3086 | atomic_set(&log_root_tree->log_commit[index2], 1); | 
|  | 3087 |  | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3088 | if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) { | 
| Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3089 | wait_log_commit(log_root_tree, | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3090 | root_log_ctx.log_transid - 1); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3091 | } | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3092 |  | 
| Zhaolei | 60d53eb | 2015-08-17 18:44:46 +0800 | [diff] [blame] | 3093 | wait_for_writer(log_root_tree); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3094 |  | 
|  | 3095 | /* | 
|  | 3096 | * now that we've moved on to the tree of log tree roots, | 
|  | 3097 | * check the full commit flag again | 
|  | 3098 | */ | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3099 | if (btrfs_need_log_full_commit(fs_info, trans)) { | 
| Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3100 | blk_finish_plug(&plug); | 
| Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3101 | btrfs_wait_tree_log_extents(log, mark); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3102 | mutex_unlock(&log_root_tree->log_mutex); | 
|  | 3103 | ret = -EAGAIN; | 
|  | 3104 | goto out_wake_log_root; | 
|  | 3105 | } | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3106 |  | 
| Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3107 | ret = btrfs_write_marked_extents(fs_info, | 
| Miao Xie | c6adc9c | 2013-05-28 10:05:39 +0000 | [diff] [blame] | 3108 | &log_root_tree->dirty_log_pages, | 
|  | 3109 | EXTENT_DIRTY | EXTENT_NEW); | 
|  | 3110 | blk_finish_plug(&plug); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3111 | if (ret) { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3112 | btrfs_set_log_full_commit(fs_info, trans); | 
| Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3113 | btrfs_abort_transaction(trans, ret); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3114 | mutex_unlock(&log_root_tree->log_mutex); | 
|  | 3115 | goto out_wake_log_root; | 
|  | 3116 | } | 
| Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3117 | ret = btrfs_wait_tree_log_extents(log, mark); | 
| Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3118 | if (!ret) | 
| Jeff Mahoney | bf89d38 | 2016-09-09 20:42:44 -0400 | [diff] [blame] | 3119 | ret = btrfs_wait_tree_log_extents(log_root_tree, | 
|  | 3120 | EXTENT_NEW | EXTENT_DIRTY); | 
| Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3121 | if (ret) { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3122 | btrfs_set_log_full_commit(fs_info, trans); | 
| Filipe Manana | 5ab5e44 | 2014-11-13 16:59:53 +0000 | [diff] [blame] | 3123 | mutex_unlock(&log_root_tree->log_mutex); | 
|  | 3124 | goto out_wake_log_root; | 
|  | 3125 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3126 |  | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3127 | btrfs_set_super_log_root(fs_info->super_for_commit, | 
|  | 3128 | log_root_tree->node->start); | 
|  | 3129 | btrfs_set_super_log_root_level(fs_info->super_for_commit, | 
|  | 3130 | btrfs_header_level(log_root_tree->node)); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3131 |  | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3132 | log_root_tree->log_transid++; | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3133 | mutex_unlock(&log_root_tree->log_mutex); | 
|  | 3134 |  | 
|  | 3135 | /* | 
|  | 3136 | * nobody else is going to jump in and write the the ctree | 
|  | 3137 | * super here because the log_commit atomic below is protecting | 
|  | 3138 | * us.  We must be called with a transaction handle pinning | 
|  | 3139 | * the running transaction open, so a full commit can't hop | 
|  | 3140 | * in and cause problems either. | 
|  | 3141 | */ | 
| David Sterba | eece6a9 | 2017-02-10 19:04:32 +0100 | [diff] [blame] | 3142 | ret = write_all_supers(fs_info, 1); | 
| Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3143 | if (ret) { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3144 | btrfs_set_log_full_commit(fs_info, trans); | 
| Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3145 | btrfs_abort_transaction(trans, ret); | 
| Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3146 | goto out_wake_log_root; | 
|  | 3147 | } | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3148 |  | 
| Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 3149 | mutex_lock(&root->log_mutex); | 
|  | 3150 | if (root->last_log_commit < log_transid) | 
|  | 3151 | root->last_log_commit = log_transid; | 
|  | 3152 | mutex_unlock(&root->log_mutex); | 
|  | 3153 |  | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3154 | out_wake_log_root: | 
| Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 3155 | mutex_lock(&log_root_tree->log_mutex); | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3156 | btrfs_remove_all_log_ctxs(log_root_tree, index2, ret); | 
|  | 3157 |  | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3158 | log_root_tree->log_transid_committed++; | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3159 | atomic_set(&log_root_tree->log_commit[index2], 0); | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3160 | mutex_unlock(&log_root_tree->log_mutex); | 
|  | 3161 |  | 
| David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3162 | /* | 
| David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3163 | * The barrier before waitqueue_active (in cond_wake_up) is needed so | 
|  | 3164 | * all the updates above are seen by the woken threads. It might not be | 
|  | 3165 | * necessary, but proving that seems to be hard. | 
| David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3166 | */ | 
| David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3167 | cond_wake_up(&log_root_tree->log_commit_wait[index2]); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3168 | out: | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3169 | mutex_lock(&root->log_mutex); | 
| Chris Mason | 570dd45 | 2016-10-27 10:42:20 -0700 | [diff] [blame] | 3170 | btrfs_remove_all_log_ctxs(root, index1, ret); | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3171 | root->log_transid_committed++; | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3172 | atomic_set(&root->log_commit[index1], 0); | 
| Miao Xie | d1433de | 2014-02-20 18:08:59 +0800 | [diff] [blame] | 3173 | mutex_unlock(&root->log_mutex); | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 3174 |  | 
| David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3175 | /* | 
| David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3176 | * The barrier before waitqueue_active (in cond_wake_up) is needed so | 
|  | 3177 | * all the updates above are seen by the woken threads. It might not be | 
|  | 3178 | * necessary, but proving that seems to be hard. | 
| David Sterba | 33a9eca | 2015-10-10 18:35:10 +0200 | [diff] [blame] | 3179 | */ | 
| David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 3180 | cond_wake_up(&root->log_commit_wait[index1]); | 
| Chris Mason | b31eabd | 2011-01-31 16:48:24 -0500 | [diff] [blame] | 3181 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3182 | } | 
|  | 3183 |  | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3184 | static void free_log_tree(struct btrfs_trans_handle *trans, | 
|  | 3185 | struct btrfs_root *log) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3186 | { | 
|  | 3187 | int ret; | 
| Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3188 | u64 start; | 
|  | 3189 | u64 end; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3190 | struct walk_control wc = { | 
|  | 3191 | .free = 1, | 
|  | 3192 | .process_func = process_one_buffer | 
|  | 3193 | }; | 
|  | 3194 |  | 
| Josef Bacik | 681ae50 | 2013-10-07 15:11:00 -0400 | [diff] [blame] | 3195 | ret = walk_log_tree(trans, log, &wc); | 
|  | 3196 | /* I don't think this can happen but just in case */ | 
|  | 3197 | if (ret) | 
| Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3198 | btrfs_abort_transaction(trans, ret); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3199 |  | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3200 | while (1) { | 
| Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3201 | ret = find_first_extent_bit(&log->dirty_log_pages, | 
| Liu Bo | 55237a5 | 2018-01-25 11:02:52 -0700 | [diff] [blame] | 3202 | 0, &start, &end, | 
|  | 3203 | EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT, | 
| Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 3204 | NULL); | 
| Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3205 | if (ret) | 
|  | 3206 | break; | 
|  | 3207 |  | 
| Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 3208 | clear_extent_bits(&log->dirty_log_pages, start, end, | 
| Liu Bo | 55237a5 | 2018-01-25 11:02:52 -0700 | [diff] [blame] | 3209 | EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT); | 
| Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3210 | } | 
|  | 3211 |  | 
| Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 3212 | free_extent_buffer(log->node); | 
|  | 3213 | kfree(log); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3214 | } | 
|  | 3215 |  | 
|  | 3216 | /* | 
|  | 3217 | * free all the extents used by the tree log.  This should be called | 
|  | 3218 | * at commit time of the full transaction | 
|  | 3219 | */ | 
|  | 3220 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) | 
|  | 3221 | { | 
|  | 3222 | if (root->log_root) { | 
|  | 3223 | free_log_tree(trans, root->log_root); | 
|  | 3224 | root->log_root = NULL; | 
|  | 3225 | } | 
|  | 3226 | return 0; | 
|  | 3227 | } | 
|  | 3228 |  | 
|  | 3229 | int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, | 
|  | 3230 | struct btrfs_fs_info *fs_info) | 
|  | 3231 | { | 
|  | 3232 | if (fs_info->log_root_tree) { | 
|  | 3233 | free_log_tree(trans, fs_info->log_root_tree); | 
|  | 3234 | fs_info->log_root_tree = NULL; | 
|  | 3235 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3236 | return 0; | 
|  | 3237 | } | 
|  | 3238 |  | 
|  | 3239 | /* | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3240 | * If both a file and directory are logged, and unlinks or renames are | 
|  | 3241 | * mixed in, we have a few interesting corners: | 
|  | 3242 | * | 
|  | 3243 | * create file X in dir Y | 
|  | 3244 | * link file X to X.link in dir Y | 
|  | 3245 | * fsync file X | 
|  | 3246 | * unlink file X but leave X.link | 
|  | 3247 | * fsync dir Y | 
|  | 3248 | * | 
|  | 3249 | * After a crash we would expect only X.link to exist.  But file X | 
|  | 3250 | * didn't get fsync'd again so the log has back refs for X and X.link. | 
|  | 3251 | * | 
|  | 3252 | * We solve this by removing directory entries and inode backrefs from the | 
|  | 3253 | * log when a file that was logged in the current transaction is | 
|  | 3254 | * unlinked.  Any later fsync will include the updated log entries, and | 
|  | 3255 | * we'll be able to reconstruct the proper directory items from backrefs. | 
|  | 3256 | * | 
|  | 3257 | * This optimizations allows us to avoid relogging the entire inode | 
|  | 3258 | * or the entire directory. | 
|  | 3259 | */ | 
|  | 3260 | int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, | 
|  | 3261 | struct btrfs_root *root, | 
|  | 3262 | const char *name, int name_len, | 
| Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3263 | struct btrfs_inode *dir, u64 index) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3264 | { | 
|  | 3265 | struct btrfs_root *log; | 
|  | 3266 | struct btrfs_dir_item *di; | 
|  | 3267 | struct btrfs_path *path; | 
|  | 3268 | int ret; | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3269 | int err = 0; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3270 | int bytes_del = 0; | 
| Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3271 | u64 dir_ino = btrfs_ino(dir); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3272 |  | 
| Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3273 | if (dir->logged_trans < trans->transid) | 
| Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 3274 | return 0; | 
|  | 3275 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3276 | ret = join_running_log_trans(root); | 
|  | 3277 | if (ret) | 
|  | 3278 | return 0; | 
|  | 3279 |  | 
| Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3280 | mutex_lock(&dir->log_mutex); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3281 |  | 
|  | 3282 | log = root->log_root; | 
|  | 3283 | path = btrfs_alloc_path(); | 
| Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3284 | if (!path) { | 
|  | 3285 | err = -ENOMEM; | 
|  | 3286 | goto out_unlock; | 
|  | 3287 | } | 
| liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 3288 |  | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3289 | di = btrfs_lookup_dir_item(trans, log, path, dir_ino, | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3290 | name, name_len, -1); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3291 | if (IS_ERR(di)) { | 
|  | 3292 | err = PTR_ERR(di); | 
|  | 3293 | goto fail; | 
|  | 3294 | } | 
|  | 3295 | if (di) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3296 | ret = btrfs_delete_one_dir_name(trans, log, path, di); | 
|  | 3297 | bytes_del += name_len; | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3298 | if (ret) { | 
|  | 3299 | err = ret; | 
|  | 3300 | goto fail; | 
|  | 3301 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3302 | } | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3303 | btrfs_release_path(path); | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3304 | di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3305 | index, name, name_len, -1); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3306 | if (IS_ERR(di)) { | 
|  | 3307 | err = PTR_ERR(di); | 
|  | 3308 | goto fail; | 
|  | 3309 | } | 
|  | 3310 | if (di) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3311 | ret = btrfs_delete_one_dir_name(trans, log, path, di); | 
|  | 3312 | bytes_del += name_len; | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3313 | if (ret) { | 
|  | 3314 | err = ret; | 
|  | 3315 | goto fail; | 
|  | 3316 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3317 | } | 
|  | 3318 |  | 
|  | 3319 | /* update the directory size in the log to reflect the names | 
|  | 3320 | * we have removed | 
|  | 3321 | */ | 
|  | 3322 | if (bytes_del) { | 
|  | 3323 | struct btrfs_key key; | 
|  | 3324 |  | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3325 | key.objectid = dir_ino; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3326 | key.offset = 0; | 
|  | 3327 | key.type = BTRFS_INODE_ITEM_KEY; | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3328 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3329 |  | 
|  | 3330 | ret = btrfs_search_slot(trans, log, &key, path, 0, 1); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3331 | if (ret < 0) { | 
|  | 3332 | err = ret; | 
|  | 3333 | goto fail; | 
|  | 3334 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3335 | if (ret == 0) { | 
|  | 3336 | struct btrfs_inode_item *item; | 
|  | 3337 | u64 i_size; | 
|  | 3338 |  | 
|  | 3339 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | 
|  | 3340 | struct btrfs_inode_item); | 
|  | 3341 | i_size = btrfs_inode_size(path->nodes[0], item); | 
|  | 3342 | if (i_size > bytes_del) | 
|  | 3343 | i_size -= bytes_del; | 
|  | 3344 | else | 
|  | 3345 | i_size = 0; | 
|  | 3346 | btrfs_set_inode_size(path->nodes[0], item, i_size); | 
|  | 3347 | btrfs_mark_buffer_dirty(path->nodes[0]); | 
|  | 3348 | } else | 
|  | 3349 | ret = 0; | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3350 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3351 | } | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3352 | fail: | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3353 | btrfs_free_path(path); | 
| Tsutomu Itoh | a62f44a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3354 | out_unlock: | 
| Nikolay Borisov | 49f34d1 | 2017-01-18 00:31:32 +0200 | [diff] [blame] | 3355 | mutex_unlock(&dir->log_mutex); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3356 | if (ret == -ENOSPC) { | 
| Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 3357 | btrfs_set_log_full_commit(root->fs_info, trans); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3358 | ret = 0; | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3359 | } else if (ret < 0) | 
| Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3360 | btrfs_abort_transaction(trans, ret); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3361 |  | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3362 | btrfs_end_log_trans(root); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3363 |  | 
| Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 3364 | return err; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3365 | } | 
|  | 3366 |  | 
|  | 3367 | /* see comments for btrfs_del_dir_entries_in_log */ | 
|  | 3368 | int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, | 
|  | 3369 | struct btrfs_root *root, | 
|  | 3370 | const char *name, int name_len, | 
| Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3371 | struct btrfs_inode *inode, u64 dirid) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3372 | { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3373 | struct btrfs_fs_info *fs_info = root->fs_info; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3374 | struct btrfs_root *log; | 
|  | 3375 | u64 index; | 
|  | 3376 | int ret; | 
|  | 3377 |  | 
| Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3378 | if (inode->logged_trans < trans->transid) | 
| Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 3379 | return 0; | 
|  | 3380 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3381 | ret = join_running_log_trans(root); | 
|  | 3382 | if (ret) | 
|  | 3383 | return 0; | 
|  | 3384 | log = root->log_root; | 
| Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3385 | mutex_lock(&inode->log_mutex); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3386 |  | 
| Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3387 | 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] | 3388 | dirid, &index); | 
| Nikolay Borisov | a491abb | 2017-01-18 00:31:33 +0200 | [diff] [blame] | 3389 | mutex_unlock(&inode->log_mutex); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3390 | if (ret == -ENOSPC) { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3391 | btrfs_set_log_full_commit(fs_info, trans); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3392 | ret = 0; | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3393 | } else if (ret < 0 && ret != -ENOENT) | 
| Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3394 | btrfs_abort_transaction(trans, ret); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3395 | btrfs_end_log_trans(root); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3396 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3397 | return ret; | 
|  | 3398 | } | 
|  | 3399 |  | 
|  | 3400 | /* | 
|  | 3401 | * creates a range item in the log for 'dirid'.  first_offset and | 
|  | 3402 | * last_offset tell us which parts of the key space the log should | 
|  | 3403 | * be considered authoritative for. | 
|  | 3404 | */ | 
|  | 3405 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, | 
|  | 3406 | struct btrfs_root *log, | 
|  | 3407 | struct btrfs_path *path, | 
|  | 3408 | int key_type, u64 dirid, | 
|  | 3409 | u64 first_offset, u64 last_offset) | 
|  | 3410 | { | 
|  | 3411 | int ret; | 
|  | 3412 | struct btrfs_key key; | 
|  | 3413 | struct btrfs_dir_log_item *item; | 
|  | 3414 |  | 
|  | 3415 | key.objectid = dirid; | 
|  | 3416 | key.offset = first_offset; | 
|  | 3417 | if (key_type == BTRFS_DIR_ITEM_KEY) | 
|  | 3418 | key.type = BTRFS_DIR_LOG_ITEM_KEY; | 
|  | 3419 | else | 
|  | 3420 | key.type = BTRFS_DIR_LOG_INDEX_KEY; | 
|  | 3421 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3422 | if (ret) | 
|  | 3423 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3424 |  | 
|  | 3425 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | 
|  | 3426 | struct btrfs_dir_log_item); | 
|  | 3427 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); | 
|  | 3428 | btrfs_mark_buffer_dirty(path->nodes[0]); | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3429 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3430 | return 0; | 
|  | 3431 | } | 
|  | 3432 |  | 
|  | 3433 | /* | 
|  | 3434 | * log all the items included in the current transaction for a given | 
|  | 3435 | * directory.  This also creates the range items in the log tree required | 
|  | 3436 | * to replay anything deleted before the fsync | 
|  | 3437 | */ | 
|  | 3438 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | 684a577 | 2017-01-18 00:31:41 +0200 | [diff] [blame] | 3439 | struct btrfs_root *root, struct btrfs_inode *inode, | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3440 | struct btrfs_path *path, | 
|  | 3441 | struct btrfs_path *dst_path, int key_type, | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3442 | struct btrfs_log_ctx *ctx, | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3443 | u64 min_offset, u64 *last_offset_ret) | 
|  | 3444 | { | 
|  | 3445 | struct btrfs_key min_key; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3446 | struct btrfs_root *log = root->log_root; | 
|  | 3447 | struct extent_buffer *src; | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3448 | int err = 0; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3449 | int ret; | 
|  | 3450 | int i; | 
|  | 3451 | int nritems; | 
|  | 3452 | u64 first_offset = min_offset; | 
|  | 3453 | u64 last_offset = (u64)-1; | 
| Nikolay Borisov | 684a577 | 2017-01-18 00:31:41 +0200 | [diff] [blame] | 3454 | u64 ino = btrfs_ino(inode); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3455 |  | 
|  | 3456 | log = root->log_root; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3457 |  | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3458 | min_key.objectid = ino; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3459 | min_key.type = key_type; | 
|  | 3460 | min_key.offset = min_offset; | 
|  | 3461 |  | 
| Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3462 | ret = btrfs_search_forward(root, &min_key, path, trans->transid); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3463 |  | 
|  | 3464 | /* | 
|  | 3465 | * we didn't find anything from this transaction, see if there | 
|  | 3466 | * is anything at all | 
|  | 3467 | */ | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3468 | if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) { | 
|  | 3469 | min_key.objectid = ino; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3470 | min_key.type = key_type; | 
|  | 3471 | min_key.offset = (u64)-1; | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3472 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3473 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); | 
|  | 3474 | if (ret < 0) { | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3475 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3476 | return ret; | 
|  | 3477 | } | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3478 | ret = btrfs_previous_item(root, path, ino, key_type); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3479 |  | 
|  | 3480 | /* if ret == 0 there are items for this type, | 
|  | 3481 | * create a range to tell us the last key of this type. | 
|  | 3482 | * otherwise, there are no items in this directory after | 
|  | 3483 | * *min_offset, and we create a range to indicate that. | 
|  | 3484 | */ | 
|  | 3485 | if (ret == 0) { | 
|  | 3486 | struct btrfs_key tmp; | 
|  | 3487 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, | 
|  | 3488 | path->slots[0]); | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3489 | if (key_type == tmp.type) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3490 | first_offset = max(min_offset, tmp.offset) + 1; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3491 | } | 
|  | 3492 | goto done; | 
|  | 3493 | } | 
|  | 3494 |  | 
|  | 3495 | /* go backward to find any previous key */ | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3496 | ret = btrfs_previous_item(root, path, ino, key_type); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3497 | if (ret == 0) { | 
|  | 3498 | struct btrfs_key tmp; | 
|  | 3499 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); | 
|  | 3500 | if (key_type == tmp.type) { | 
|  | 3501 | first_offset = tmp.offset; | 
|  | 3502 | ret = overwrite_item(trans, log, dst_path, | 
|  | 3503 | path->nodes[0], path->slots[0], | 
|  | 3504 | &tmp); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3505 | if (ret) { | 
|  | 3506 | err = ret; | 
|  | 3507 | goto done; | 
|  | 3508 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3509 | } | 
|  | 3510 | } | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3511 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3512 |  | 
|  | 3513 | /* find the first key from this transaction again */ | 
|  | 3514 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); | 
| Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3515 | if (WARN_ON(ret != 0)) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3516 | goto done; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3517 |  | 
|  | 3518 | /* | 
|  | 3519 | * we have a block from this transaction, log every item in it | 
|  | 3520 | * from our directory | 
|  | 3521 | */ | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3522 | while (1) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3523 | struct btrfs_key tmp; | 
|  | 3524 | src = path->nodes[0]; | 
|  | 3525 | nritems = btrfs_header_nritems(src); | 
|  | 3526 | for (i = path->slots[0]; i < nritems; i++) { | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3527 | struct btrfs_dir_item *di; | 
|  | 3528 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3529 | btrfs_item_key_to_cpu(src, &min_key, i); | 
|  | 3530 |  | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3531 | if (min_key.objectid != ino || min_key.type != key_type) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3532 | goto done; | 
|  | 3533 | ret = overwrite_item(trans, log, dst_path, src, i, | 
|  | 3534 | &min_key); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3535 | if (ret) { | 
|  | 3536 | err = ret; | 
|  | 3537 | goto done; | 
|  | 3538 | } | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3539 |  | 
|  | 3540 | /* | 
|  | 3541 | * We must make sure that when we log a directory entry, | 
|  | 3542 | * the corresponding inode, after log replay, has a | 
|  | 3543 | * matching link count. For example: | 
|  | 3544 | * | 
|  | 3545 | * touch foo | 
|  | 3546 | * mkdir mydir | 
|  | 3547 | * sync | 
|  | 3548 | * ln foo mydir/bar | 
|  | 3549 | * xfs_io -c "fsync" mydir | 
|  | 3550 | * <crash> | 
|  | 3551 | * <mount fs and log replay> | 
|  | 3552 | * | 
|  | 3553 | * Would result in a fsync log that when replayed, our | 
|  | 3554 | * file inode would have a link count of 1, but we get | 
|  | 3555 | * two directory entries pointing to the same inode. | 
|  | 3556 | * After removing one of the names, it would not be | 
|  | 3557 | * possible to remove the other name, which resulted | 
|  | 3558 | * always in stale file handle errors, and would not | 
|  | 3559 | * be possible to rmdir the parent directory, since | 
|  | 3560 | * its i_size could never decrement to the value | 
|  | 3561 | * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors. | 
|  | 3562 | */ | 
|  | 3563 | di = btrfs_item_ptr(src, i, struct btrfs_dir_item); | 
|  | 3564 | btrfs_dir_item_key_to_cpu(src, di, &tmp); | 
|  | 3565 | if (ctx && | 
|  | 3566 | (btrfs_dir_transid(src, di) == trans->transid || | 
|  | 3567 | btrfs_dir_type(src, di) == BTRFS_FT_DIR) && | 
|  | 3568 | tmp.type != BTRFS_ROOT_ITEM_KEY) | 
|  | 3569 | ctx->log_new_dentries = true; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3570 | } | 
|  | 3571 | path->slots[0] = nritems; | 
|  | 3572 |  | 
|  | 3573 | /* | 
|  | 3574 | * look ahead to the next item and see if it is also | 
|  | 3575 | * from this directory and from this transaction | 
|  | 3576 | */ | 
|  | 3577 | ret = btrfs_next_leaf(root, path); | 
| Liu Bo | 80c0b42 | 2018-04-03 01:59:47 +0800 | [diff] [blame] | 3578 | if (ret) { | 
|  | 3579 | if (ret == 1) | 
|  | 3580 | last_offset = (u64)-1; | 
|  | 3581 | else | 
|  | 3582 | err = ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3583 | goto done; | 
|  | 3584 | } | 
|  | 3585 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3586 | if (tmp.objectid != ino || tmp.type != key_type) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3587 | last_offset = (u64)-1; | 
|  | 3588 | goto done; | 
|  | 3589 | } | 
|  | 3590 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { | 
|  | 3591 | ret = overwrite_item(trans, log, dst_path, | 
|  | 3592 | path->nodes[0], path->slots[0], | 
|  | 3593 | &tmp); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3594 | if (ret) | 
|  | 3595 | err = ret; | 
|  | 3596 | else | 
|  | 3597 | last_offset = tmp.offset; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3598 | goto done; | 
|  | 3599 | } | 
|  | 3600 | } | 
|  | 3601 | done: | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3602 | btrfs_release_path(path); | 
|  | 3603 | btrfs_release_path(dst_path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3604 |  | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3605 | if (err == 0) { | 
|  | 3606 | *last_offset_ret = last_offset; | 
|  | 3607 | /* | 
|  | 3608 | * insert the log range keys to indicate where the log | 
|  | 3609 | * is valid | 
|  | 3610 | */ | 
|  | 3611 | ret = insert_dir_log_key(trans, log, path, key_type, | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3612 | ino, first_offset, last_offset); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3613 | if (ret) | 
|  | 3614 | err = ret; | 
|  | 3615 | } | 
|  | 3616 | return err; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3617 | } | 
|  | 3618 |  | 
|  | 3619 | /* | 
|  | 3620 | * logging directories is very similar to logging inodes, We find all the items | 
|  | 3621 | * from the current transaction and write them to the log. | 
|  | 3622 | * | 
|  | 3623 | * The recovery code scans the directory in the subvolume, and if it finds a | 
|  | 3624 | * key in the range logged that is not present in the log tree, then it means | 
|  | 3625 | * that dir entry was unlinked during the transaction. | 
|  | 3626 | * | 
|  | 3627 | * In order for that scan to work, we must include one key smaller than | 
|  | 3628 | * the smallest logged by this transaction and one key larger than the largest | 
|  | 3629 | * key logged by this transaction. | 
|  | 3630 | */ | 
|  | 3631 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | dbf39ea | 2017-01-18 00:31:42 +0200 | [diff] [blame] | 3632 | struct btrfs_root *root, struct btrfs_inode *inode, | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3633 | struct btrfs_path *path, | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 3634 | struct btrfs_path *dst_path, | 
|  | 3635 | struct btrfs_log_ctx *ctx) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3636 | { | 
|  | 3637 | u64 min_key; | 
|  | 3638 | u64 max_key; | 
|  | 3639 | int ret; | 
|  | 3640 | int key_type = BTRFS_DIR_ITEM_KEY; | 
|  | 3641 |  | 
|  | 3642 | again: | 
|  | 3643 | min_key = 0; | 
|  | 3644 | max_key = 0; | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3645 | while (1) { | 
| Nikolay Borisov | dbf39ea | 2017-01-18 00:31:42 +0200 | [diff] [blame] | 3646 | ret = log_dir_items(trans, root, inode, path, dst_path, key_type, | 
|  | 3647 | ctx, min_key, &max_key); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3648 | if (ret) | 
|  | 3649 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3650 | if (max_key == (u64)-1) | 
|  | 3651 | break; | 
|  | 3652 | min_key = max_key + 1; | 
|  | 3653 | } | 
|  | 3654 |  | 
|  | 3655 | if (key_type == BTRFS_DIR_ITEM_KEY) { | 
|  | 3656 | key_type = BTRFS_DIR_INDEX_KEY; | 
|  | 3657 | goto again; | 
|  | 3658 | } | 
|  | 3659 | return 0; | 
|  | 3660 | } | 
|  | 3661 |  | 
|  | 3662 | /* | 
|  | 3663 | * a helper function to drop items from the log before we relog an | 
|  | 3664 | * inode.  max_key_type indicates the highest item type to remove. | 
|  | 3665 | * This cannot be run for file data extents because it does not | 
|  | 3666 | * free the extents they point to. | 
|  | 3667 | */ | 
|  | 3668 | static int drop_objectid_items(struct btrfs_trans_handle *trans, | 
|  | 3669 | struct btrfs_root *log, | 
|  | 3670 | struct btrfs_path *path, | 
|  | 3671 | u64 objectid, int max_key_type) | 
|  | 3672 | { | 
|  | 3673 | int ret; | 
|  | 3674 | struct btrfs_key key; | 
|  | 3675 | struct btrfs_key found_key; | 
| Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 3676 | int start_slot; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3677 |  | 
|  | 3678 | key.objectid = objectid; | 
|  | 3679 | key.type = max_key_type; | 
|  | 3680 | key.offset = (u64)-1; | 
|  | 3681 |  | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3682 | while (1) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3683 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3684 | BUG_ON(ret == 0); /* Logic error */ | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3685 | if (ret < 0) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3686 | break; | 
|  | 3687 |  | 
|  | 3688 | if (path->slots[0] == 0) | 
|  | 3689 | break; | 
|  | 3690 |  | 
|  | 3691 | path->slots[0]--; | 
|  | 3692 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | 
|  | 3693 | path->slots[0]); | 
|  | 3694 |  | 
|  | 3695 | if (found_key.objectid != objectid) | 
|  | 3696 | break; | 
|  | 3697 |  | 
| Josef Bacik | 18ec90d | 2012-09-28 11:56:28 -0400 | [diff] [blame] | 3698 | found_key.offset = 0; | 
|  | 3699 | found_key.type = 0; | 
|  | 3700 | ret = btrfs_bin_search(path->nodes[0], &found_key, 0, | 
|  | 3701 | &start_slot); | 
|  | 3702 |  | 
|  | 3703 | ret = btrfs_del_items(trans, log, path, start_slot, | 
|  | 3704 | path->slots[0] - start_slot + 1); | 
|  | 3705 | /* | 
|  | 3706 | * If start slot isn't 0 then we don't need to re-search, we've | 
|  | 3707 | * found the last guy with the objectid in this tree. | 
|  | 3708 | */ | 
|  | 3709 | if (ret || start_slot != 0) | 
| Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 3710 | break; | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3711 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3712 | } | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3713 | btrfs_release_path(path); | 
| Josef Bacik | 5bdbeb2 | 2012-05-29 16:59:49 -0400 | [diff] [blame] | 3714 | if (ret > 0) | 
|  | 3715 | ret = 0; | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3716 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3717 | } | 
|  | 3718 |  | 
| Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3719 | static void fill_inode_item(struct btrfs_trans_handle *trans, | 
|  | 3720 | struct extent_buffer *leaf, | 
|  | 3721 | struct btrfs_inode_item *item, | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3722 | struct inode *inode, int log_inode_only, | 
|  | 3723 | u64 logged_isize) | 
| Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3724 | { | 
| Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3725 | struct btrfs_map_token token; | 
| Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3726 |  | 
| Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3727 | btrfs_init_map_token(&token); | 
| Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3728 |  | 
|  | 3729 | if (log_inode_only) { | 
|  | 3730 | /* set the generation to zero so the recover code | 
|  | 3731 | * can tell the difference between an logging | 
|  | 3732 | * just to say 'this inode exists' and a logging | 
|  | 3733 | * to say 'update this inode with these values' | 
|  | 3734 | */ | 
| Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3735 | btrfs_set_token_inode_generation(leaf, item, 0, &token); | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3736 | btrfs_set_token_inode_size(leaf, item, logged_isize, &token); | 
| Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3737 | } else { | 
| Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3738 | btrfs_set_token_inode_generation(leaf, item, | 
|  | 3739 | BTRFS_I(inode)->generation, | 
|  | 3740 | &token); | 
|  | 3741 | btrfs_set_token_inode_size(leaf, item, inode->i_size, &token); | 
| Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3742 | } | 
|  | 3743 |  | 
| Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3744 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); | 
|  | 3745 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | 
|  | 3746 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | 
|  | 3747 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | 
|  | 3748 |  | 
| David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3749 | btrfs_set_token_timespec_sec(leaf, &item->atime, | 
| Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3750 | inode->i_atime.tv_sec, &token); | 
| David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3751 | btrfs_set_token_timespec_nsec(leaf, &item->atime, | 
| Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3752 | inode->i_atime.tv_nsec, &token); | 
|  | 3753 |  | 
| David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3754 | btrfs_set_token_timespec_sec(leaf, &item->mtime, | 
| Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3755 | inode->i_mtime.tv_sec, &token); | 
| David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3756 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, | 
| Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3757 | inode->i_mtime.tv_nsec, &token); | 
|  | 3758 |  | 
| David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3759 | btrfs_set_token_timespec_sec(leaf, &item->ctime, | 
| Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3760 | inode->i_ctime.tv_sec, &token); | 
| David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3761 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, | 
| Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3762 | inode->i_ctime.tv_nsec, &token); | 
|  | 3763 |  | 
|  | 3764 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), | 
|  | 3765 | &token); | 
|  | 3766 |  | 
| Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3767 | btrfs_set_token_inode_sequence(leaf, item, | 
|  | 3768 | inode_peek_iversion(inode), &token); | 
| Josef Bacik | 0b1c6cc | 2012-10-23 16:03:44 -0400 | [diff] [blame] | 3769 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | 
|  | 3770 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | 
|  | 3771 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | 
|  | 3772 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | 
| Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3773 | } | 
|  | 3774 |  | 
| Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3775 | static int log_inode_item(struct btrfs_trans_handle *trans, | 
|  | 3776 | struct btrfs_root *log, struct btrfs_path *path, | 
| Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3777 | struct btrfs_inode *inode) | 
| Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3778 | { | 
|  | 3779 | struct btrfs_inode_item *inode_item; | 
| Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3780 | int ret; | 
|  | 3781 |  | 
| Filipe David Borba Manana | efd0c40 | 2013-10-07 21:20:44 +0100 | [diff] [blame] | 3782 | ret = btrfs_insert_empty_item(trans, log, path, | 
| Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3783 | &inode->location, sizeof(*inode_item)); | 
| Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3784 | if (ret && ret != -EEXIST) | 
|  | 3785 | return ret; | 
|  | 3786 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], | 
|  | 3787 | struct btrfs_inode_item); | 
| Nikolay Borisov | 6d889a3 | 2017-01-18 00:31:47 +0200 | [diff] [blame] | 3788 | fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode, | 
|  | 3789 | 0, 0); | 
| Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 3790 | btrfs_release_path(path); | 
|  | 3791 | return 0; | 
|  | 3792 | } | 
|  | 3793 |  | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3794 | static noinline int copy_items(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3795 | struct btrfs_inode *inode, | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3796 | struct btrfs_path *dst_path, | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3797 | struct btrfs_path *src_path, u64 *last_extent, | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3798 | int start_slot, int nr, int inode_only, | 
|  | 3799 | u64 logged_isize) | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3800 | { | 
| David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 3801 | struct btrfs_fs_info *fs_info = trans->fs_info; | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3802 | unsigned long src_offset; | 
|  | 3803 | unsigned long dst_offset; | 
| Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3804 | struct btrfs_root *log = inode->root->log_root; | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3805 | struct btrfs_file_extent_item *extent; | 
|  | 3806 | struct btrfs_inode_item *inode_item; | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3807 | struct extent_buffer *src = src_path->nodes[0]; | 
|  | 3808 | struct btrfs_key first_key, last_key, key; | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3809 | int ret; | 
|  | 3810 | struct btrfs_key *ins_keys; | 
|  | 3811 | u32 *ins_sizes; | 
|  | 3812 | char *ins_data; | 
|  | 3813 | int i; | 
| Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3814 | struct list_head ordered_sums; | 
| Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3815 | int skip_csum = inode->flags & BTRFS_INODE_NODATASUM; | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3816 | bool has_extents = false; | 
| Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 3817 | bool need_find_last_extent = true; | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3818 | bool done = false; | 
| Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3819 |  | 
|  | 3820 | INIT_LIST_HEAD(&ordered_sums); | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3821 |  | 
|  | 3822 | ins_data = kmalloc(nr * sizeof(struct btrfs_key) + | 
|  | 3823 | nr * sizeof(u32), GFP_NOFS); | 
| liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 3824 | if (!ins_data) | 
|  | 3825 | return -ENOMEM; | 
|  | 3826 |  | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3827 | first_key.objectid = (u64)-1; | 
|  | 3828 |  | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3829 | ins_sizes = (u32 *)ins_data; | 
|  | 3830 | ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); | 
|  | 3831 |  | 
|  | 3832 | for (i = 0; i < nr; i++) { | 
|  | 3833 | ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot); | 
|  | 3834 | btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot); | 
|  | 3835 | } | 
|  | 3836 | ret = btrfs_insert_empty_items(trans, log, dst_path, | 
|  | 3837 | ins_keys, ins_sizes, nr); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3838 | if (ret) { | 
|  | 3839 | kfree(ins_data); | 
|  | 3840 | return ret; | 
|  | 3841 | } | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3842 |  | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3843 | for (i = 0; i < nr; i++, dst_path->slots[0]++) { | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3844 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], | 
|  | 3845 | dst_path->slots[0]); | 
|  | 3846 |  | 
|  | 3847 | src_offset = btrfs_item_ptr_offset(src, start_slot + i); | 
|  | 3848 |  | 
| Matthias Kaehlcke | 0dde10b | 2017-07-27 14:30:23 -0700 | [diff] [blame] | 3849 | if (i == nr - 1) | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3850 | last_key = ins_keys[i]; | 
|  | 3851 |  | 
| Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3852 | if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) { | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3853 | inode_item = btrfs_item_ptr(dst_path->nodes[0], | 
|  | 3854 | dst_path->slots[0], | 
|  | 3855 | struct btrfs_inode_item); | 
| Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3856 | fill_inode_item(trans, dst_path->nodes[0], inode_item, | 
| David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3857 | &inode->vfs_inode, | 
|  | 3858 | inode_only == LOG_INODE_EXISTS, | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 3859 | logged_isize); | 
| Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3860 | } else { | 
|  | 3861 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, | 
|  | 3862 | src_offset, ins_sizes[i]); | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3863 | } | 
| Josef Bacik | 94edf4a | 2012-09-25 14:56:25 -0400 | [diff] [blame] | 3864 |  | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3865 | /* | 
|  | 3866 | * We set need_find_last_extent here in case we know we were | 
|  | 3867 | * processing other items and then walk into the first extent in | 
|  | 3868 | * the inode.  If we don't hit an extent then nothing changes, | 
|  | 3869 | * we'll do the last search the next time around. | 
|  | 3870 | */ | 
|  | 3871 | if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) { | 
|  | 3872 | has_extents = true; | 
| Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 3873 | if (first_key.objectid == (u64)-1) | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3874 | first_key = ins_keys[i]; | 
|  | 3875 | } else { | 
|  | 3876 | need_find_last_extent = false; | 
|  | 3877 | } | 
|  | 3878 |  | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3879 | /* take a reference on file data extents so that truncates | 
|  | 3880 | * or deletes of this inode don't have to relog the inode | 
|  | 3881 | * again | 
|  | 3882 | */ | 
| David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3883 | if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY && | 
| Liu Bo | d279440 | 2012-08-29 01:07:56 -0600 | [diff] [blame] | 3884 | !skip_csum) { | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3885 | int found_type; | 
|  | 3886 | extent = btrfs_item_ptr(src, start_slot + i, | 
|  | 3887 | struct btrfs_file_extent_item); | 
|  | 3888 |  | 
| liubo | 8e531cd | 2011-05-06 10:36:09 +0800 | [diff] [blame] | 3889 | if (btrfs_file_extent_generation(src, extent) < trans->transid) | 
|  | 3890 | continue; | 
|  | 3891 |  | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3892 | found_type = btrfs_file_extent_type(src, extent); | 
| Josef Bacik | 6f1fed7 | 2012-09-26 11:07:06 -0400 | [diff] [blame] | 3893 | if (found_type == BTRFS_FILE_EXTENT_REG) { | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3894 | u64 ds, dl, cs, cl; | 
|  | 3895 | ds = btrfs_file_extent_disk_bytenr(src, | 
|  | 3896 | extent); | 
|  | 3897 | /* ds == 0 is a hole */ | 
|  | 3898 | if (ds == 0) | 
|  | 3899 | continue; | 
|  | 3900 |  | 
|  | 3901 | dl = btrfs_file_extent_disk_num_bytes(src, | 
|  | 3902 | extent); | 
|  | 3903 | cs = btrfs_file_extent_offset(src, extent); | 
|  | 3904 | cl = btrfs_file_extent_num_bytes(src, | 
| Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 3905 | extent); | 
| Chris Mason | 580afd7 | 2008-12-08 19:15:39 -0500 | [diff] [blame] | 3906 | if (btrfs_file_extent_compression(src, | 
|  | 3907 | extent)) { | 
|  | 3908 | cs = 0; | 
|  | 3909 | cl = dl; | 
|  | 3910 | } | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3911 |  | 
|  | 3912 | ret = btrfs_lookup_csums_range( | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3913 | fs_info->csum_root, | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3914 | ds + cs, ds + cs + cl - 1, | 
| Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3915 | &ordered_sums, 0); | 
| Josef Bacik | 3650860 | 2013-04-25 16:23:32 -0400 | [diff] [blame] | 3916 | if (ret) { | 
|  | 3917 | btrfs_release_path(dst_path); | 
|  | 3918 | kfree(ins_data); | 
|  | 3919 | return ret; | 
|  | 3920 | } | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3921 | } | 
|  | 3922 | } | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3923 | } | 
|  | 3924 |  | 
|  | 3925 | btrfs_mark_buffer_dirty(dst_path->nodes[0]); | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3926 | btrfs_release_path(dst_path); | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3927 | kfree(ins_data); | 
| Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3928 |  | 
|  | 3929 | /* | 
|  | 3930 | * we have to do this after the loop above to avoid changing the | 
|  | 3931 | * log tree while trying to change the log tree. | 
|  | 3932 | */ | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3933 | ret = 0; | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3934 | while (!list_empty(&ordered_sums)) { | 
| Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3935 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, | 
|  | 3936 | struct btrfs_ordered_sum, | 
|  | 3937 | list); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 3938 | if (!ret) | 
|  | 3939 | ret = btrfs_csum_file_blocks(trans, log, sums); | 
| Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3940 | list_del(&sums->list); | 
|  | 3941 | kfree(sums); | 
|  | 3942 | } | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3943 |  | 
|  | 3944 | if (!has_extents) | 
|  | 3945 | return ret; | 
|  | 3946 |  | 
| Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 3947 | if (need_find_last_extent && *last_extent == first_key.offset) { | 
|  | 3948 | /* | 
|  | 3949 | * We don't have any leafs between our current one and the one | 
|  | 3950 | * we processed before that can have file extent items for our | 
|  | 3951 | * inode (and have a generation number smaller than our current | 
|  | 3952 | * transaction id). | 
|  | 3953 | */ | 
|  | 3954 | need_find_last_extent = false; | 
|  | 3955 | } | 
|  | 3956 |  | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3957 | /* | 
|  | 3958 | * Because we use btrfs_search_forward we could skip leaves that were | 
|  | 3959 | * not modified and then assume *last_extent is valid when it really | 
|  | 3960 | * isn't.  So back up to the previous leaf and read the end of the last | 
|  | 3961 | * extent before we go and fill in holes. | 
|  | 3962 | */ | 
|  | 3963 | if (need_find_last_extent) { | 
|  | 3964 | u64 len; | 
|  | 3965 |  | 
| Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3966 | ret = btrfs_prev_leaf(inode->root, src_path); | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3967 | if (ret < 0) | 
|  | 3968 | return ret; | 
|  | 3969 | if (ret) | 
|  | 3970 | goto fill_holes; | 
|  | 3971 | if (src_path->slots[0]) | 
|  | 3972 | src_path->slots[0]--; | 
|  | 3973 | src = src_path->nodes[0]; | 
|  | 3974 | btrfs_item_key_to_cpu(src, &key, src_path->slots[0]); | 
| Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 3975 | if (key.objectid != btrfs_ino(inode) || | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3976 | key.type != BTRFS_EXTENT_DATA_KEY) | 
|  | 3977 | goto fill_holes; | 
|  | 3978 | extent = btrfs_item_ptr(src, src_path->slots[0], | 
|  | 3979 | struct btrfs_file_extent_item); | 
|  | 3980 | if (btrfs_file_extent_type(src, extent) == | 
|  | 3981 | BTRFS_FILE_EXTENT_INLINE) { | 
| Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 3982 | len = btrfs_file_extent_ram_bytes(src, extent); | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3983 | *last_extent = ALIGN(key.offset + len, | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3984 | fs_info->sectorsize); | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 3985 | } else { | 
|  | 3986 | len = btrfs_file_extent_num_bytes(src, extent); | 
|  | 3987 | *last_extent = key.offset + len; | 
|  | 3988 | } | 
|  | 3989 | } | 
|  | 3990 | fill_holes: | 
|  | 3991 | /* So we did prev_leaf, now we need to move to the next leaf, but a few | 
|  | 3992 | * things could have happened | 
|  | 3993 | * | 
|  | 3994 | * 1) A merge could have happened, so we could currently be on a leaf | 
|  | 3995 | * that holds what we were copying in the first place. | 
|  | 3996 | * 2) A split could have happened, and now not all of the items we want | 
|  | 3997 | * are on the same leaf. | 
|  | 3998 | * | 
|  | 3999 | * So we need to adjust how we search for holes, we need to drop the | 
|  | 4000 | * path and re-search for the first extent key we found, and then walk | 
|  | 4001 | * forward until we hit the last one we copied. | 
|  | 4002 | */ | 
|  | 4003 | if (need_find_last_extent) { | 
|  | 4004 | /* btrfs_prev_leaf could return 1 without releasing the path */ | 
|  | 4005 | btrfs_release_path(src_path); | 
| David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4006 | ret = btrfs_search_slot(NULL, inode->root, &first_key, | 
|  | 4007 | src_path, 0, 0); | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4008 | if (ret < 0) | 
|  | 4009 | return ret; | 
|  | 4010 | ASSERT(ret == 0); | 
|  | 4011 | src = src_path->nodes[0]; | 
|  | 4012 | i = src_path->slots[0]; | 
|  | 4013 | } else { | 
|  | 4014 | i = start_slot; | 
|  | 4015 | } | 
|  | 4016 |  | 
|  | 4017 | /* | 
|  | 4018 | * Ok so here we need to go through and fill in any holes we may have | 
|  | 4019 | * to make sure that holes are punched for those areas in case they had | 
|  | 4020 | * extents previously. | 
|  | 4021 | */ | 
|  | 4022 | while (!done) { | 
|  | 4023 | u64 offset, len; | 
|  | 4024 | u64 extent_end; | 
|  | 4025 |  | 
|  | 4026 | if (i >= btrfs_header_nritems(src_path->nodes[0])) { | 
| Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 4027 | ret = btrfs_next_leaf(inode->root, src_path); | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4028 | if (ret < 0) | 
|  | 4029 | return ret; | 
|  | 4030 | ASSERT(ret == 0); | 
|  | 4031 | src = src_path->nodes[0]; | 
|  | 4032 | i = 0; | 
| Filipe Manana | 8434ec4 | 2018-03-26 23:59:12 +0100 | [diff] [blame] | 4033 | need_find_last_extent = true; | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4034 | } | 
|  | 4035 |  | 
|  | 4036 | btrfs_item_key_to_cpu(src, &key, i); | 
|  | 4037 | if (!btrfs_comp_cpu_keys(&key, &last_key)) | 
|  | 4038 | done = true; | 
| Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 4039 | if (key.objectid != btrfs_ino(inode) || | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4040 | key.type != BTRFS_EXTENT_DATA_KEY) { | 
|  | 4041 | i++; | 
|  | 4042 | continue; | 
|  | 4043 | } | 
|  | 4044 | extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item); | 
|  | 4045 | if (btrfs_file_extent_type(src, extent) == | 
|  | 4046 | BTRFS_FILE_EXTENT_INLINE) { | 
| Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 4047 | len = btrfs_file_extent_ram_bytes(src, extent); | 
| Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4048 | extent_end = ALIGN(key.offset + len, | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4049 | fs_info->sectorsize); | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4050 | } else { | 
|  | 4051 | len = btrfs_file_extent_num_bytes(src, extent); | 
|  | 4052 | extent_end = key.offset + len; | 
|  | 4053 | } | 
|  | 4054 | i++; | 
|  | 4055 |  | 
|  | 4056 | if (*last_extent == key.offset) { | 
|  | 4057 | *last_extent = extent_end; | 
|  | 4058 | continue; | 
|  | 4059 | } | 
|  | 4060 | offset = *last_extent; | 
|  | 4061 | len = key.offset - *last_extent; | 
| Nikolay Borisov | 44d70e1 | 2017-01-18 00:31:36 +0200 | [diff] [blame] | 4062 | ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode), | 
| David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4063 | offset, 0, 0, len, 0, len, 0, 0, 0); | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4064 | if (ret) | 
|  | 4065 | break; | 
| Filipe Manana | 74121f7c | 2014-08-07 12:00:44 +0100 | [diff] [blame] | 4066 | *last_extent = extent_end; | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4067 | } | 
| Filipe Manana | 4ee3fad | 2018-03-26 23:59:00 +0100 | [diff] [blame] | 4068 |  | 
|  | 4069 | /* | 
|  | 4070 | * Check if there is a hole between the last extent found in our leaf | 
|  | 4071 | * and the first extent in the next leaf. If there is one, we need to | 
|  | 4072 | * log an explicit hole so that at replay time we can punch the hole. | 
|  | 4073 | */ | 
|  | 4074 | if (ret == 0 && | 
|  | 4075 | key.objectid == btrfs_ino(inode) && | 
|  | 4076 | key.type == BTRFS_EXTENT_DATA_KEY && | 
|  | 4077 | i == btrfs_header_nritems(src_path->nodes[0])) { | 
|  | 4078 | ret = btrfs_next_leaf(inode->root, src_path); | 
|  | 4079 | need_find_last_extent = true; | 
|  | 4080 | if (ret > 0) { | 
|  | 4081 | ret = 0; | 
|  | 4082 | } else if (ret == 0) { | 
|  | 4083 | btrfs_item_key_to_cpu(src_path->nodes[0], &key, | 
|  | 4084 | src_path->slots[0]); | 
|  | 4085 | if (key.objectid == btrfs_ino(inode) && | 
|  | 4086 | key.type == BTRFS_EXTENT_DATA_KEY && | 
|  | 4087 | *last_extent < key.offset) { | 
|  | 4088 | const u64 len = key.offset - *last_extent; | 
|  | 4089 |  | 
|  | 4090 | ret = btrfs_insert_file_extent(trans, log, | 
|  | 4091 | btrfs_ino(inode), | 
|  | 4092 | *last_extent, 0, | 
|  | 4093 | 0, len, 0, len, | 
|  | 4094 | 0, 0, 0); | 
|  | 4095 | } | 
|  | 4096 | } | 
|  | 4097 | } | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4098 | /* | 
|  | 4099 | * Need to let the callers know we dropped the path so they should | 
|  | 4100 | * re-search. | 
|  | 4101 | */ | 
|  | 4102 | if (!ret && need_find_last_extent) | 
|  | 4103 | ret = 1; | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4104 | return ret; | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4105 | } | 
|  | 4106 |  | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4107 | static int extent_cmp(void *priv, struct list_head *a, struct list_head *b) | 
|  | 4108 | { | 
|  | 4109 | struct extent_map *em1, *em2; | 
|  | 4110 |  | 
|  | 4111 | em1 = list_entry(a, struct extent_map, list); | 
|  | 4112 | em2 = list_entry(b, struct extent_map, list); | 
|  | 4113 |  | 
|  | 4114 | if (em1->start < em2->start) | 
|  | 4115 | return -1; | 
|  | 4116 | else if (em1->start > em2->start) | 
|  | 4117 | return 1; | 
|  | 4118 | return 0; | 
|  | 4119 | } | 
|  | 4120 |  | 
| Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4121 | static int log_extent_csums(struct btrfs_trans_handle *trans, | 
|  | 4122 | struct btrfs_inode *inode, | 
| Nikolay Borisov | a9ecb65 | 2018-06-20 17:26:42 +0300 | [diff] [blame] | 4123 | struct btrfs_root *log_root, | 
| Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4124 | const struct extent_map *em) | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4125 | { | 
| Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4126 | u64 csum_offset; | 
|  | 4127 | u64 csum_len; | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4128 | LIST_HEAD(ordered_sums); | 
|  | 4129 | int ret = 0; | 
| Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4130 |  | 
| Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4131 | if (inode->flags & BTRFS_INODE_NODATASUM || | 
|  | 4132 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4133 | em->block_start == EXTENT_MAP_HOLE) | 
| Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4134 | return 0; | 
|  | 4135 |  | 
| Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4136 | /* If we're compressed we have to save the entire range of csums. */ | 
| Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4137 | if (em->compress_type) { | 
|  | 4138 | csum_offset = 0; | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4139 | csum_len = max(em->block_len, em->orig_block_len); | 
| Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4140 | } else { | 
| Josef Bacik | e7175a6 | 2018-05-23 11:58:34 -0400 | [diff] [blame] | 4141 | csum_offset = em->mod_start - em->start; | 
|  | 4142 | csum_len = em->mod_len; | 
| Filipe David Borba Manana | 488111a | 2013-10-28 16:30:29 +0000 | [diff] [blame] | 4143 | } | 
| Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4144 |  | 
| Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4145 | /* block start is already adjusted for the file extent offset. */ | 
| Nikolay Borisov | a9ecb65 | 2018-06-20 17:26:42 +0300 | [diff] [blame] | 4146 | ret = btrfs_lookup_csums_range(trans->fs_info->csum_root, | 
| Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4147 | em->block_start + csum_offset, | 
|  | 4148 | em->block_start + csum_offset + | 
|  | 4149 | csum_len - 1, &ordered_sums, 0); | 
|  | 4150 | if (ret) | 
|  | 4151 | return ret; | 
|  | 4152 |  | 
|  | 4153 | while (!list_empty(&ordered_sums)) { | 
|  | 4154 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, | 
|  | 4155 | struct btrfs_ordered_sum, | 
|  | 4156 | list); | 
|  | 4157 | if (!ret) | 
| Nikolay Borisov | a9ecb65 | 2018-06-20 17:26:42 +0300 | [diff] [blame] | 4158 | ret = btrfs_csum_file_blocks(trans, log_root, sums); | 
| Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 4159 | list_del(&sums->list); | 
|  | 4160 | kfree(sums); | 
|  | 4161 | } | 
|  | 4162 |  | 
|  | 4163 | return ret; | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4164 | } | 
|  | 4165 |  | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4166 | static int log_one_extent(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4167 | struct btrfs_inode *inode, struct btrfs_root *root, | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4168 | const struct extent_map *em, | 
|  | 4169 | struct btrfs_path *path, | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4170 | struct btrfs_log_ctx *ctx) | 
|  | 4171 | { | 
|  | 4172 | struct btrfs_root *log = root->log_root; | 
|  | 4173 | struct btrfs_file_extent_item *fi; | 
|  | 4174 | struct extent_buffer *leaf; | 
|  | 4175 | struct btrfs_map_token token; | 
|  | 4176 | struct btrfs_key key; | 
|  | 4177 | u64 extent_offset = em->start - em->orig_start; | 
|  | 4178 | u64 block_len; | 
|  | 4179 | int ret; | 
|  | 4180 | int extent_inserted = 0; | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4181 |  | 
| Nikolay Borisov | a9ecb65 | 2018-06-20 17:26:42 +0300 | [diff] [blame] | 4182 | ret = log_extent_csums(trans, inode, log, em); | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4183 | if (ret) | 
|  | 4184 | return ret; | 
|  | 4185 |  | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4186 | btrfs_init_map_token(&token); | 
|  | 4187 |  | 
| Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4188 | ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start, | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4189 | em->start + em->len, NULL, 0, 1, | 
|  | 4190 | sizeof(*fi), &extent_inserted); | 
|  | 4191 | if (ret) | 
|  | 4192 | return ret; | 
|  | 4193 |  | 
|  | 4194 | if (!extent_inserted) { | 
| Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4195 | key.objectid = btrfs_ino(inode); | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4196 | key.type = BTRFS_EXTENT_DATA_KEY; | 
|  | 4197 | key.offset = em->start; | 
|  | 4198 |  | 
|  | 4199 | ret = btrfs_insert_empty_item(trans, log, path, &key, | 
|  | 4200 | sizeof(*fi)); | 
|  | 4201 | if (ret) | 
|  | 4202 | return ret; | 
|  | 4203 | } | 
|  | 4204 | leaf = path->nodes[0]; | 
|  | 4205 | fi = btrfs_item_ptr(leaf, path->slots[0], | 
|  | 4206 | struct btrfs_file_extent_item); | 
|  | 4207 |  | 
| Josef Bacik | 50d9aa9 | 2014-11-21 14:52:38 -0500 | [diff] [blame] | 4208 | btrfs_set_token_file_extent_generation(leaf, fi, trans->transid, | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4209 | &token); | 
|  | 4210 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | 
|  | 4211 | btrfs_set_token_file_extent_type(leaf, fi, | 
|  | 4212 | BTRFS_FILE_EXTENT_PREALLOC, | 
|  | 4213 | &token); | 
|  | 4214 | else | 
|  | 4215 | btrfs_set_token_file_extent_type(leaf, fi, | 
|  | 4216 | BTRFS_FILE_EXTENT_REG, | 
|  | 4217 | &token); | 
|  | 4218 |  | 
|  | 4219 | block_len = max(em->block_len, em->orig_block_len); | 
|  | 4220 | if (em->compress_type != BTRFS_COMPRESS_NONE) { | 
|  | 4221 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, | 
|  | 4222 | em->block_start, | 
|  | 4223 | &token); | 
|  | 4224 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len, | 
|  | 4225 | &token); | 
|  | 4226 | } else if (em->block_start < EXTENT_MAP_LAST_BYTE) { | 
|  | 4227 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, | 
|  | 4228 | em->block_start - | 
|  | 4229 | extent_offset, &token); | 
|  | 4230 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len, | 
|  | 4231 | &token); | 
|  | 4232 | } else { | 
|  | 4233 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token); | 
|  | 4234 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0, | 
|  | 4235 | &token); | 
|  | 4236 | } | 
|  | 4237 |  | 
|  | 4238 | btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token); | 
|  | 4239 | btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token); | 
|  | 4240 | btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token); | 
|  | 4241 | btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type, | 
|  | 4242 | &token); | 
|  | 4243 | btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token); | 
|  | 4244 | btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token); | 
|  | 4245 | btrfs_mark_buffer_dirty(leaf); | 
|  | 4246 |  | 
|  | 4247 | btrfs_release_path(path); | 
|  | 4248 |  | 
|  | 4249 | return ret; | 
|  | 4250 | } | 
|  | 4251 |  | 
| Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4252 | /* | 
|  | 4253 | * Log all prealloc extents beyond the inode's i_size to make sure we do not | 
|  | 4254 | * lose them after doing a fast fsync and replaying the log. We scan the | 
|  | 4255 | * subvolume's root instead of iterating the inode's extent map tree because | 
|  | 4256 | * otherwise we can log incorrect extent items based on extent map conversion. | 
|  | 4257 | * That can happen due to the fact that extent maps are merged when they | 
|  | 4258 | * are not in the extent map tree's list of modified extents. | 
|  | 4259 | */ | 
|  | 4260 | static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans, | 
|  | 4261 | struct btrfs_inode *inode, | 
|  | 4262 | struct btrfs_path *path) | 
|  | 4263 | { | 
|  | 4264 | struct btrfs_root *root = inode->root; | 
|  | 4265 | struct btrfs_key key; | 
|  | 4266 | const u64 i_size = i_size_read(&inode->vfs_inode); | 
|  | 4267 | const u64 ino = btrfs_ino(inode); | 
|  | 4268 | struct btrfs_path *dst_path = NULL; | 
|  | 4269 | u64 last_extent = (u64)-1; | 
|  | 4270 | int ins_nr = 0; | 
|  | 4271 | int start_slot; | 
|  | 4272 | int ret; | 
|  | 4273 |  | 
|  | 4274 | if (!(inode->flags & BTRFS_INODE_PREALLOC)) | 
|  | 4275 | return 0; | 
|  | 4276 |  | 
|  | 4277 | key.objectid = ino; | 
|  | 4278 | key.type = BTRFS_EXTENT_DATA_KEY; | 
|  | 4279 | key.offset = i_size; | 
|  | 4280 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | 
|  | 4281 | if (ret < 0) | 
|  | 4282 | goto out; | 
|  | 4283 |  | 
|  | 4284 | while (true) { | 
|  | 4285 | struct extent_buffer *leaf = path->nodes[0]; | 
|  | 4286 | int slot = path->slots[0]; | 
|  | 4287 |  | 
|  | 4288 | if (slot >= btrfs_header_nritems(leaf)) { | 
|  | 4289 | if (ins_nr > 0) { | 
|  | 4290 | ret = copy_items(trans, inode, dst_path, path, | 
|  | 4291 | &last_extent, start_slot, | 
|  | 4292 | ins_nr, 1, 0); | 
|  | 4293 | if (ret < 0) | 
|  | 4294 | goto out; | 
|  | 4295 | ins_nr = 0; | 
|  | 4296 | } | 
|  | 4297 | ret = btrfs_next_leaf(root, path); | 
|  | 4298 | if (ret < 0) | 
|  | 4299 | goto out; | 
|  | 4300 | if (ret > 0) { | 
|  | 4301 | ret = 0; | 
|  | 4302 | break; | 
|  | 4303 | } | 
|  | 4304 | continue; | 
|  | 4305 | } | 
|  | 4306 |  | 
|  | 4307 | btrfs_item_key_to_cpu(leaf, &key, slot); | 
|  | 4308 | if (key.objectid > ino) | 
|  | 4309 | break; | 
|  | 4310 | if (WARN_ON_ONCE(key.objectid < ino) || | 
|  | 4311 | key.type < BTRFS_EXTENT_DATA_KEY || | 
|  | 4312 | key.offset < i_size) { | 
|  | 4313 | path->slots[0]++; | 
|  | 4314 | continue; | 
|  | 4315 | } | 
|  | 4316 | if (last_extent == (u64)-1) { | 
|  | 4317 | last_extent = key.offset; | 
|  | 4318 | /* | 
|  | 4319 | * Avoid logging extent items logged in past fsync calls | 
|  | 4320 | * and leading to duplicate keys in the log tree. | 
|  | 4321 | */ | 
|  | 4322 | do { | 
|  | 4323 | ret = btrfs_truncate_inode_items(trans, | 
|  | 4324 | root->log_root, | 
|  | 4325 | &inode->vfs_inode, | 
|  | 4326 | i_size, | 
|  | 4327 | BTRFS_EXTENT_DATA_KEY); | 
|  | 4328 | } while (ret == -EAGAIN); | 
|  | 4329 | if (ret) | 
|  | 4330 | goto out; | 
|  | 4331 | } | 
|  | 4332 | if (ins_nr == 0) | 
|  | 4333 | start_slot = slot; | 
|  | 4334 | ins_nr++; | 
|  | 4335 | path->slots[0]++; | 
|  | 4336 | if (!dst_path) { | 
|  | 4337 | dst_path = btrfs_alloc_path(); | 
|  | 4338 | if (!dst_path) { | 
|  | 4339 | ret = -ENOMEM; | 
|  | 4340 | goto out; | 
|  | 4341 | } | 
|  | 4342 | } | 
|  | 4343 | } | 
|  | 4344 | if (ins_nr > 0) { | 
|  | 4345 | ret = copy_items(trans, inode, dst_path, path, &last_extent, | 
|  | 4346 | start_slot, ins_nr, 1, 0); | 
|  | 4347 | if (ret > 0) | 
|  | 4348 | ret = 0; | 
|  | 4349 | } | 
|  | 4350 | out: | 
|  | 4351 | btrfs_release_path(path); | 
|  | 4352 | btrfs_free_path(dst_path); | 
|  | 4353 | return ret; | 
|  | 4354 | } | 
|  | 4355 |  | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4356 | static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, | 
|  | 4357 | struct btrfs_root *root, | 
| Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4358 | struct btrfs_inode *inode, | 
| Miao Xie | 827463c | 2014-01-14 20:31:51 +0800 | [diff] [blame] | 4359 | struct btrfs_path *path, | 
| Filipe Manana | de0ee0e | 2016-01-21 10:17:54 +0000 | [diff] [blame] | 4360 | struct btrfs_log_ctx *ctx, | 
|  | 4361 | const u64 start, | 
|  | 4362 | const u64 end) | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4363 | { | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4364 | struct extent_map *em, *n; | 
|  | 4365 | struct list_head extents; | 
| Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4366 | struct extent_map_tree *tree = &inode->extent_tree; | 
| Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4367 | u64 logged_start, logged_end; | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4368 | u64 test_gen; | 
|  | 4369 | int ret = 0; | 
| Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4370 | int num = 0; | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4371 |  | 
|  | 4372 | INIT_LIST_HEAD(&extents); | 
|  | 4373 |  | 
| Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4374 | down_write(&inode->dio_sem); | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4375 | write_lock(&tree->lock); | 
|  | 4376 | test_gen = root->fs_info->last_trans_committed; | 
| Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4377 | logged_start = start; | 
|  | 4378 | logged_end = end; | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4379 |  | 
|  | 4380 | list_for_each_entry_safe(em, n, &tree->modified_extents, list) { | 
|  | 4381 | list_del_init(&em->list); | 
| Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4382 | /* | 
|  | 4383 | * Just an arbitrary number, this can be really CPU intensive | 
|  | 4384 | * once we start getting a lot of extents, and really once we | 
|  | 4385 | * have a bunch of extents we just want to commit since it will | 
|  | 4386 | * be faster. | 
|  | 4387 | */ | 
|  | 4388 | if (++num > 32768) { | 
|  | 4389 | list_del_init(&tree->modified_extents); | 
|  | 4390 | ret = -EFBIG; | 
|  | 4391 | goto process; | 
|  | 4392 | } | 
|  | 4393 |  | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4394 | if (em->generation <= test_gen) | 
|  | 4395 | continue; | 
| Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4396 |  | 
| Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4397 | /* We log prealloc extents beyond eof later. */ | 
|  | 4398 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && | 
|  | 4399 | em->start >= i_size_read(&inode->vfs_inode)) | 
|  | 4400 | continue; | 
|  | 4401 |  | 
| Josef Bacik | 8c6c592 | 2017-08-29 10:11:39 -0400 | [diff] [blame] | 4402 | if (em->start < logged_start) | 
|  | 4403 | logged_start = em->start; | 
|  | 4404 | if ((em->start + em->len - 1) > logged_end) | 
|  | 4405 | logged_end = em->start + em->len - 1; | 
|  | 4406 |  | 
| Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4407 | /* Need a ref to keep it from getting evicted from cache */ | 
| Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 4408 | refcount_inc(&em->refs); | 
| Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4409 | set_bit(EXTENT_FLAG_LOGGING, &em->flags); | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4410 | list_add_tail(&em->list, &extents); | 
| Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4411 | num++; | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4412 | } | 
|  | 4413 |  | 
|  | 4414 | list_sort(NULL, &extents, extent_cmp); | 
| Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 4415 | process: | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4416 | while (!list_empty(&extents)) { | 
|  | 4417 | em = list_entry(extents.next, struct extent_map, list); | 
|  | 4418 |  | 
|  | 4419 | list_del_init(&em->list); | 
|  | 4420 |  | 
|  | 4421 | /* | 
|  | 4422 | * If we had an error we just need to delete everybody from our | 
|  | 4423 | * private list. | 
|  | 4424 | */ | 
| Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4425 | if (ret) { | 
| Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 4426 | clear_em_logging(tree, em); | 
| Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4427 | free_extent_map(em); | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4428 | continue; | 
| Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4429 | } | 
|  | 4430 |  | 
|  | 4431 | write_unlock(&tree->lock); | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4432 |  | 
| Josef Bacik | a2120a4 | 2018-05-23 11:58:35 -0400 | [diff] [blame] | 4433 | ret = log_one_extent(trans, inode, root, em, path, ctx); | 
| Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4434 | write_lock(&tree->lock); | 
| Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 4435 | clear_em_logging(tree, em); | 
|  | 4436 | free_extent_map(em); | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4437 | } | 
| Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 4438 | WARN_ON(!list_empty(&extents)); | 
|  | 4439 | write_unlock(&tree->lock); | 
| Nikolay Borisov | 9d12262 | 2017-01-18 00:31:40 +0200 | [diff] [blame] | 4440 | up_write(&inode->dio_sem); | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4441 |  | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4442 | btrfs_release_path(path); | 
| Filipe Manana | 31d11b8 | 2018-05-09 16:01:46 +0100 | [diff] [blame] | 4443 | if (!ret) | 
|  | 4444 | ret = btrfs_log_prealloc_extents(trans, inode, path); | 
|  | 4445 |  | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4446 | return ret; | 
|  | 4447 | } | 
|  | 4448 |  | 
| Nikolay Borisov | 481b01c | 2017-01-18 00:31:34 +0200 | [diff] [blame] | 4449 | static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode, | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4450 | struct btrfs_path *path, u64 *size_ret) | 
|  | 4451 | { | 
|  | 4452 | struct btrfs_key key; | 
|  | 4453 | int ret; | 
|  | 4454 |  | 
| Nikolay Borisov | 481b01c | 2017-01-18 00:31:34 +0200 | [diff] [blame] | 4455 | key.objectid = btrfs_ino(inode); | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4456 | key.type = BTRFS_INODE_ITEM_KEY; | 
|  | 4457 | key.offset = 0; | 
|  | 4458 |  | 
|  | 4459 | ret = btrfs_search_slot(NULL, log, &key, path, 0, 0); | 
|  | 4460 | if (ret < 0) { | 
|  | 4461 | return ret; | 
|  | 4462 | } else if (ret > 0) { | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 4463 | *size_ret = 0; | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4464 | } else { | 
|  | 4465 | struct btrfs_inode_item *item; | 
|  | 4466 |  | 
|  | 4467 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | 
|  | 4468 | struct btrfs_inode_item); | 
|  | 4469 | *size_ret = btrfs_inode_size(path->nodes[0], item); | 
|  | 4470 | } | 
|  | 4471 |  | 
|  | 4472 | btrfs_release_path(path); | 
|  | 4473 | return 0; | 
|  | 4474 | } | 
|  | 4475 |  | 
| Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4476 | /* | 
|  | 4477 | * At the moment we always log all xattrs. This is to figure out at log replay | 
|  | 4478 | * time which xattrs must have their deletion replayed. If a xattr is missing | 
|  | 4479 | * in the log tree and exists in the fs/subvol tree, we delete it. This is | 
|  | 4480 | * because if a xattr is deleted, the inode is fsynced and a power failure | 
|  | 4481 | * happens, causing the log to be replayed the next time the fs is mounted, | 
|  | 4482 | * we want the xattr to not exist anymore (same behaviour as other filesystems | 
|  | 4483 | * with a journal, ext3/4, xfs, f2fs, etc). | 
|  | 4484 | */ | 
|  | 4485 | static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans, | 
|  | 4486 | struct btrfs_root *root, | 
| Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4487 | struct btrfs_inode *inode, | 
| Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4488 | struct btrfs_path *path, | 
|  | 4489 | struct btrfs_path *dst_path) | 
|  | 4490 | { | 
|  | 4491 | int ret; | 
|  | 4492 | struct btrfs_key key; | 
| Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4493 | const u64 ino = btrfs_ino(inode); | 
| Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4494 | int ins_nr = 0; | 
|  | 4495 | int start_slot = 0; | 
|  | 4496 |  | 
|  | 4497 | key.objectid = ino; | 
|  | 4498 | key.type = BTRFS_XATTR_ITEM_KEY; | 
|  | 4499 | key.offset = 0; | 
|  | 4500 |  | 
|  | 4501 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | 
|  | 4502 | if (ret < 0) | 
|  | 4503 | return ret; | 
|  | 4504 |  | 
|  | 4505 | while (true) { | 
|  | 4506 | int slot = path->slots[0]; | 
|  | 4507 | struct extent_buffer *leaf = path->nodes[0]; | 
|  | 4508 | int nritems = btrfs_header_nritems(leaf); | 
|  | 4509 |  | 
|  | 4510 | if (slot >= nritems) { | 
|  | 4511 | if (ins_nr > 0) { | 
|  | 4512 | u64 last_extent = 0; | 
|  | 4513 |  | 
| Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4514 | ret = copy_items(trans, inode, dst_path, path, | 
| Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4515 | &last_extent, start_slot, | 
|  | 4516 | ins_nr, 1, 0); | 
|  | 4517 | /* can't be 1, extent items aren't processed */ | 
|  | 4518 | ASSERT(ret <= 0); | 
|  | 4519 | if (ret < 0) | 
|  | 4520 | return ret; | 
|  | 4521 | ins_nr = 0; | 
|  | 4522 | } | 
|  | 4523 | ret = btrfs_next_leaf(root, path); | 
|  | 4524 | if (ret < 0) | 
|  | 4525 | return ret; | 
|  | 4526 | else if (ret > 0) | 
|  | 4527 | break; | 
|  | 4528 | continue; | 
|  | 4529 | } | 
|  | 4530 |  | 
|  | 4531 | btrfs_item_key_to_cpu(leaf, &key, slot); | 
|  | 4532 | if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) | 
|  | 4533 | break; | 
|  | 4534 |  | 
|  | 4535 | if (ins_nr == 0) | 
|  | 4536 | start_slot = slot; | 
|  | 4537 | ins_nr++; | 
|  | 4538 | path->slots[0]++; | 
|  | 4539 | cond_resched(); | 
|  | 4540 | } | 
|  | 4541 | if (ins_nr > 0) { | 
|  | 4542 | u64 last_extent = 0; | 
|  | 4543 |  | 
| Nikolay Borisov | 1a93c36 | 2017-01-18 00:31:37 +0200 | [diff] [blame] | 4544 | ret = copy_items(trans, inode, dst_path, path, | 
| Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 4545 | &last_extent, start_slot, | 
|  | 4546 | ins_nr, 1, 0); | 
|  | 4547 | /* can't be 1, extent items aren't processed */ | 
|  | 4548 | ASSERT(ret <= 0); | 
|  | 4549 | if (ret < 0) | 
|  | 4550 | return ret; | 
|  | 4551 | } | 
|  | 4552 |  | 
|  | 4553 | return 0; | 
|  | 4554 | } | 
|  | 4555 |  | 
| Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4556 | /* | 
|  | 4557 | * If the no holes feature is enabled we need to make sure any hole between the | 
|  | 4558 | * last extent and the i_size of our inode is explicitly marked in the log. This | 
|  | 4559 | * is to make sure that doing something like: | 
|  | 4560 | * | 
|  | 4561 | *      1) create file with 128Kb of data | 
|  | 4562 | *      2) truncate file to 64Kb | 
|  | 4563 | *      3) truncate file to 256Kb | 
|  | 4564 | *      4) fsync file | 
|  | 4565 | *      5) <crash/power failure> | 
|  | 4566 | *      6) mount fs and trigger log replay | 
|  | 4567 | * | 
|  | 4568 | * Will give us a file with a size of 256Kb, the first 64Kb of data match what | 
|  | 4569 | * the file had in its first 64Kb of data at step 1 and the last 192Kb of the | 
|  | 4570 | * file correspond to a hole. The presence of explicit holes in a log tree is | 
|  | 4571 | * what guarantees that log replay will remove/adjust file extent items in the | 
|  | 4572 | * fs/subvol tree. | 
|  | 4573 | * | 
|  | 4574 | * Here we do not need to care about holes between extents, that is already done | 
|  | 4575 | * by copy_items(). We also only need to do this in the full sync path, where we | 
|  | 4576 | * lookup for extents from the fs/subvol tree only. In the fast path case, we | 
|  | 4577 | * lookup the list of modified extent maps and if any represents a hole, we | 
|  | 4578 | * insert a corresponding extent representing a hole in the log tree. | 
|  | 4579 | */ | 
|  | 4580 | static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans, | 
|  | 4581 | struct btrfs_root *root, | 
| Nikolay Borisov | a0308dd | 2017-01-18 00:31:38 +0200 | [diff] [blame] | 4582 | struct btrfs_inode *inode, | 
| Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4583 | struct btrfs_path *path) | 
|  | 4584 | { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4585 | struct btrfs_fs_info *fs_info = root->fs_info; | 
| Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4586 | int ret; | 
|  | 4587 | struct btrfs_key key; | 
|  | 4588 | u64 hole_start; | 
|  | 4589 | u64 hole_size; | 
|  | 4590 | struct extent_buffer *leaf; | 
|  | 4591 | struct btrfs_root *log = root->log_root; | 
| Nikolay Borisov | a0308dd | 2017-01-18 00:31:38 +0200 | [diff] [blame] | 4592 | const u64 ino = btrfs_ino(inode); | 
|  | 4593 | const u64 i_size = i_size_read(&inode->vfs_inode); | 
| Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4594 |  | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4595 | if (!btrfs_fs_incompat(fs_info, NO_HOLES)) | 
| Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4596 | return 0; | 
|  | 4597 |  | 
|  | 4598 | key.objectid = ino; | 
|  | 4599 | key.type = BTRFS_EXTENT_DATA_KEY; | 
|  | 4600 | key.offset = (u64)-1; | 
|  | 4601 |  | 
|  | 4602 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | 
|  | 4603 | ASSERT(ret != 0); | 
|  | 4604 | if (ret < 0) | 
|  | 4605 | return ret; | 
|  | 4606 |  | 
|  | 4607 | ASSERT(path->slots[0] > 0); | 
|  | 4608 | path->slots[0]--; | 
|  | 4609 | leaf = path->nodes[0]; | 
|  | 4610 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | 
|  | 4611 |  | 
|  | 4612 | if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) { | 
|  | 4613 | /* inode does not have any extents */ | 
|  | 4614 | hole_start = 0; | 
|  | 4615 | hole_size = i_size; | 
|  | 4616 | } else { | 
|  | 4617 | struct btrfs_file_extent_item *extent; | 
|  | 4618 | u64 len; | 
|  | 4619 |  | 
|  | 4620 | /* | 
|  | 4621 | * If there's an extent beyond i_size, an explicit hole was | 
|  | 4622 | * already inserted by copy_items(). | 
|  | 4623 | */ | 
|  | 4624 | if (key.offset >= i_size) | 
|  | 4625 | return 0; | 
|  | 4626 |  | 
|  | 4627 | extent = btrfs_item_ptr(leaf, path->slots[0], | 
|  | 4628 | struct btrfs_file_extent_item); | 
|  | 4629 |  | 
|  | 4630 | if (btrfs_file_extent_type(leaf, extent) == | 
|  | 4631 | BTRFS_FILE_EXTENT_INLINE) { | 
| Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 4632 | len = btrfs_file_extent_ram_bytes(leaf, extent); | 
| Filipe Manana | 6399fb5 | 2017-07-28 15:22:36 +0100 | [diff] [blame] | 4633 | ASSERT(len == i_size || | 
|  | 4634 | (len == fs_info->sectorsize && | 
|  | 4635 | btrfs_file_extent_compression(leaf, extent) != | 
|  | 4636 | BTRFS_COMPRESS_NONE)); | 
| Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4637 | return 0; | 
|  | 4638 | } | 
|  | 4639 |  | 
|  | 4640 | len = btrfs_file_extent_num_bytes(leaf, extent); | 
|  | 4641 | /* Last extent goes beyond i_size, no need to log a hole. */ | 
|  | 4642 | if (key.offset + len > i_size) | 
|  | 4643 | return 0; | 
|  | 4644 | hole_start = key.offset + len; | 
|  | 4645 | hole_size = i_size - hole_start; | 
|  | 4646 | } | 
|  | 4647 | btrfs_release_path(path); | 
|  | 4648 |  | 
|  | 4649 | /* Last extent ends at i_size. */ | 
|  | 4650 | if (hole_size == 0) | 
|  | 4651 | return 0; | 
|  | 4652 |  | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4653 | hole_size = ALIGN(hole_size, fs_info->sectorsize); | 
| Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 4654 | ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0, | 
|  | 4655 | hole_size, 0, hole_size, 0, 0, 0); | 
|  | 4656 | return ret; | 
|  | 4657 | } | 
|  | 4658 |  | 
| Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4659 | /* | 
|  | 4660 | * When we are logging a new inode X, check if it doesn't have a reference that | 
|  | 4661 | * matches the reference from some other inode Y created in a past transaction | 
|  | 4662 | * and that was renamed in the current transaction. If we don't do this, then at | 
|  | 4663 | * log replay time we can lose inode Y (and all its files if it's a directory): | 
|  | 4664 | * | 
|  | 4665 | * mkdir /mnt/x | 
|  | 4666 | * echo "hello world" > /mnt/x/foobar | 
|  | 4667 | * sync | 
|  | 4668 | * mv /mnt/x /mnt/y | 
|  | 4669 | * mkdir /mnt/x                 # or touch /mnt/x | 
|  | 4670 | * xfs_io -c fsync /mnt/x | 
|  | 4671 | * <power fail> | 
|  | 4672 | * mount fs, trigger log replay | 
|  | 4673 | * | 
|  | 4674 | * After the log replay procedure, we would lose the first directory and all its | 
|  | 4675 | * files (file foobar). | 
|  | 4676 | * For the case where inode Y is not a directory we simply end up losing it: | 
|  | 4677 | * | 
|  | 4678 | * echo "123" > /mnt/foo | 
|  | 4679 | * sync | 
|  | 4680 | * mv /mnt/foo /mnt/bar | 
|  | 4681 | * echo "abc" > /mnt/foo | 
|  | 4682 | * xfs_io -c fsync /mnt/foo | 
|  | 4683 | * <power fail> | 
|  | 4684 | * | 
|  | 4685 | * We also need this for cases where a snapshot entry is replaced by some other | 
|  | 4686 | * entry (file or directory) otherwise we end up with an unreplayable log due to | 
|  | 4687 | * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as | 
|  | 4688 | * if it were a regular entry: | 
|  | 4689 | * | 
|  | 4690 | * mkdir /mnt/x | 
|  | 4691 | * btrfs subvolume snapshot /mnt /mnt/x/snap | 
|  | 4692 | * btrfs subvolume delete /mnt/x/snap | 
|  | 4693 | * rmdir /mnt/x | 
|  | 4694 | * mkdir /mnt/x | 
|  | 4695 | * fsync /mnt/x or fsync some new file inside it | 
|  | 4696 | * <power fail> | 
|  | 4697 | * | 
|  | 4698 | * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in | 
|  | 4699 | * the same transaction. | 
|  | 4700 | */ | 
|  | 4701 | static int btrfs_check_ref_name_override(struct extent_buffer *eb, | 
|  | 4702 | const int slot, | 
|  | 4703 | const struct btrfs_key *key, | 
| Nikolay Borisov | 4791c8f | 2017-01-18 00:31:35 +0200 | [diff] [blame] | 4704 | struct btrfs_inode *inode, | 
| Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4705 | u64 *other_ino) | 
| Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4706 | { | 
|  | 4707 | int ret; | 
|  | 4708 | struct btrfs_path *search_path; | 
|  | 4709 | char *name = NULL; | 
|  | 4710 | u32 name_len = 0; | 
|  | 4711 | u32 item_size = btrfs_item_size_nr(eb, slot); | 
|  | 4712 | u32 cur_offset = 0; | 
|  | 4713 | unsigned long ptr = btrfs_item_ptr_offset(eb, slot); | 
|  | 4714 |  | 
|  | 4715 | search_path = btrfs_alloc_path(); | 
|  | 4716 | if (!search_path) | 
|  | 4717 | return -ENOMEM; | 
|  | 4718 | search_path->search_commit_root = 1; | 
|  | 4719 | search_path->skip_locking = 1; | 
|  | 4720 |  | 
|  | 4721 | while (cur_offset < item_size) { | 
|  | 4722 | u64 parent; | 
|  | 4723 | u32 this_name_len; | 
|  | 4724 | u32 this_len; | 
|  | 4725 | unsigned long name_ptr; | 
|  | 4726 | struct btrfs_dir_item *di; | 
|  | 4727 |  | 
|  | 4728 | if (key->type == BTRFS_INODE_REF_KEY) { | 
|  | 4729 | struct btrfs_inode_ref *iref; | 
|  | 4730 |  | 
|  | 4731 | iref = (struct btrfs_inode_ref *)(ptr + cur_offset); | 
|  | 4732 | parent = key->offset; | 
|  | 4733 | this_name_len = btrfs_inode_ref_name_len(eb, iref); | 
|  | 4734 | name_ptr = (unsigned long)(iref + 1); | 
|  | 4735 | this_len = sizeof(*iref) + this_name_len; | 
|  | 4736 | } else { | 
|  | 4737 | struct btrfs_inode_extref *extref; | 
|  | 4738 |  | 
|  | 4739 | extref = (struct btrfs_inode_extref *)(ptr + | 
|  | 4740 | cur_offset); | 
|  | 4741 | parent = btrfs_inode_extref_parent(eb, extref); | 
|  | 4742 | this_name_len = btrfs_inode_extref_name_len(eb, extref); | 
|  | 4743 | name_ptr = (unsigned long)&extref->name; | 
|  | 4744 | this_len = sizeof(*extref) + this_name_len; | 
|  | 4745 | } | 
|  | 4746 |  | 
|  | 4747 | if (this_name_len > name_len) { | 
|  | 4748 | char *new_name; | 
|  | 4749 |  | 
|  | 4750 | new_name = krealloc(name, this_name_len, GFP_NOFS); | 
|  | 4751 | if (!new_name) { | 
|  | 4752 | ret = -ENOMEM; | 
|  | 4753 | goto out; | 
|  | 4754 | } | 
|  | 4755 | name_len = this_name_len; | 
|  | 4756 | name = new_name; | 
|  | 4757 | } | 
|  | 4758 |  | 
|  | 4759 | read_extent_buffer(eb, name, name_ptr, this_name_len); | 
| Nikolay Borisov | 4791c8f | 2017-01-18 00:31:35 +0200 | [diff] [blame] | 4760 | di = btrfs_lookup_dir_item(NULL, inode->root, search_path, | 
|  | 4761 | parent, name, this_name_len, 0); | 
| Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4762 | if (di && !IS_ERR(di)) { | 
| Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4763 | struct btrfs_key di_key; | 
|  | 4764 |  | 
|  | 4765 | btrfs_dir_item_key_to_cpu(search_path->nodes[0], | 
|  | 4766 | di, &di_key); | 
|  | 4767 | if (di_key.type == BTRFS_INODE_ITEM_KEY) { | 
|  | 4768 | ret = 1; | 
|  | 4769 | *other_ino = di_key.objectid; | 
|  | 4770 | } else { | 
|  | 4771 | ret = -EAGAIN; | 
|  | 4772 | } | 
| Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4773 | goto out; | 
|  | 4774 | } else if (IS_ERR(di)) { | 
|  | 4775 | ret = PTR_ERR(di); | 
|  | 4776 | goto out; | 
|  | 4777 | } | 
|  | 4778 | btrfs_release_path(search_path); | 
|  | 4779 |  | 
|  | 4780 | cur_offset += this_len; | 
|  | 4781 | } | 
|  | 4782 | ret = 0; | 
|  | 4783 | out: | 
|  | 4784 | btrfs_free_path(search_path); | 
|  | 4785 | kfree(name); | 
|  | 4786 | return ret; | 
|  | 4787 | } | 
|  | 4788 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4789 | /* log a single inode in the tree log. | 
|  | 4790 | * At least one parent directory for this inode must exist in the tree | 
|  | 4791 | * or be logged already. | 
|  | 4792 | * | 
|  | 4793 | * Any items from this inode changed by the current transaction are copied | 
|  | 4794 | * to the log tree.  An extra reference is taken on any extents in this | 
|  | 4795 | * file, allowing us to avoid a whole pile of corner cases around logging | 
|  | 4796 | * blocks that have been removed from the tree. | 
|  | 4797 | * | 
|  | 4798 | * See LOG_INODE_ALL and related defines for a description of what inode_only | 
|  | 4799 | * does. | 
|  | 4800 | * | 
|  | 4801 | * This handles both files and directories. | 
|  | 4802 | */ | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4803 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4804 | struct btrfs_root *root, struct btrfs_inode *inode, | 
| Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 4805 | int inode_only, | 
|  | 4806 | const loff_t start, | 
| Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 4807 | const loff_t end, | 
|  | 4808 | struct btrfs_log_ctx *ctx) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4809 | { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4810 | struct btrfs_fs_info *fs_info = root->fs_info; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4811 | struct btrfs_path *path; | 
|  | 4812 | struct btrfs_path *dst_path; | 
|  | 4813 | struct btrfs_key min_key; | 
|  | 4814 | struct btrfs_key max_key; | 
|  | 4815 | struct btrfs_root *log = root->log_root; | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4816 | u64 last_extent = 0; | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4817 | int err = 0; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4818 | int ret; | 
| Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 4819 | int nritems; | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4820 | int ins_start_slot = 0; | 
|  | 4821 | int ins_nr; | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4822 | bool fast_search = false; | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4823 | u64 ino = btrfs_ino(inode); | 
|  | 4824 | struct extent_map_tree *em_tree = &inode->extent_tree; | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4825 | u64 logged_isize = 0; | 
| Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 4826 | bool need_log_inode_item = true; | 
| Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 4827 | bool xattrs_logged = false; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4828 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4829 | path = btrfs_alloc_path(); | 
| Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 4830 | if (!path) | 
|  | 4831 | return -ENOMEM; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4832 | dst_path = btrfs_alloc_path(); | 
| Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 4833 | if (!dst_path) { | 
|  | 4834 | btrfs_free_path(path); | 
|  | 4835 | return -ENOMEM; | 
|  | 4836 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4837 |  | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4838 | min_key.objectid = ino; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4839 | min_key.type = BTRFS_INODE_ITEM_KEY; | 
|  | 4840 | min_key.offset = 0; | 
|  | 4841 |  | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4842 | max_key.objectid = ino; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4843 |  | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4844 |  | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4845 | /* today the code can only do partial logging of directories */ | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4846 | if (S_ISDIR(inode->vfs_inode.i_mode) || | 
| Miao Xie | 5269b67 | 2012-11-01 07:35:23 +0000 | [diff] [blame] | 4847 | (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4848 | &inode->runtime_flags) && | 
| Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4849 | inode_only >= LOG_INODE_EXISTS)) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4850 | max_key.type = BTRFS_XATTR_ITEM_KEY; | 
|  | 4851 | else | 
|  | 4852 | max_key.type = (u8)-1; | 
|  | 4853 | max_key.offset = (u64)-1; | 
|  | 4854 |  | 
| Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4855 | /* | 
|  | 4856 | * Only run delayed items if we are a dir or a new file. | 
|  | 4857 | * Otherwise commit the delayed inode only, which is needed in | 
|  | 4858 | * order for the log replay code to mark inodes for link count | 
|  | 4859 | * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items). | 
|  | 4860 | */ | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4861 | if (S_ISDIR(inode->vfs_inode.i_mode) || | 
|  | 4862 | inode->generation > fs_info->last_trans_committed) | 
|  | 4863 | ret = btrfs_commit_inode_delayed_items(trans, inode); | 
| Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4864 | else | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4865 | ret = btrfs_commit_inode_delayed_inode(inode); | 
| Filipe Manana | 2c2c452 | 2015-01-13 16:40:04 +0000 | [diff] [blame] | 4866 |  | 
|  | 4867 | if (ret) { | 
|  | 4868 | btrfs_free_path(path); | 
|  | 4869 | btrfs_free_path(dst_path); | 
|  | 4870 | return ret; | 
| Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4871 | } | 
|  | 4872 |  | 
| Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4873 | if (inode_only == LOG_OTHER_INODE) { | 
|  | 4874 | inode_only = LOG_INODE_EXISTS; | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4875 | mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING); | 
| Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4876 | } else { | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4877 | mutex_lock(&inode->log_mutex); | 
| Liu Bo | 781feef | 2016-11-30 16:20:25 -0800 | [diff] [blame] | 4878 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4879 |  | 
| Filipe Manana | 5e33a2b | 2016-02-25 23:19:38 +0000 | [diff] [blame] | 4880 | /* | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4881 | * a brute force approach to making sure we get the most uptodate | 
|  | 4882 | * copies of everything. | 
|  | 4883 | */ | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4884 | if (S_ISDIR(inode->vfs_inode.i_mode)) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4885 | int max_key_type = BTRFS_DIR_LOG_INDEX_KEY; | 
|  | 4886 |  | 
| Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4887 | if (inode_only == LOG_INODE_EXISTS) | 
|  | 4888 | max_key_type = BTRFS_XATTR_ITEM_KEY; | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4889 | ret = drop_objectid_items(trans, log, path, ino, max_key_type); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4890 | } else { | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4891 | if (inode_only == LOG_INODE_EXISTS) { | 
|  | 4892 | /* | 
|  | 4893 | * Make sure the new inode item we write to the log has | 
|  | 4894 | * the same isize as the current one (if it exists). | 
|  | 4895 | * This is necessary to prevent data loss after log | 
|  | 4896 | * replay, and also to prevent doing a wrong expanding | 
|  | 4897 | * truncate - for e.g. create file, write 4K into offset | 
|  | 4898 | * 0, fsync, write 4K into offset 4096, add hard link, | 
|  | 4899 | * fsync some other file (to sync log), power fail - if | 
|  | 4900 | * we use the inode's current i_size, after log replay | 
|  | 4901 | * we get a 8Kb file, with the last 4Kb extent as a hole | 
|  | 4902 | * (zeroes), as if an expanding truncate happened, | 
|  | 4903 | * instead of getting a file of 4Kb only. | 
|  | 4904 | */ | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4905 | err = logged_inode_size(log, inode, path, &logged_isize); | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 4906 | if (err) | 
|  | 4907 | goto out_unlock; | 
|  | 4908 | } | 
| Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4909 | if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4910 | &inode->runtime_flags)) { | 
| Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4911 | if (inode_only == LOG_INODE_EXISTS) { | 
| Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4912 | max_key.type = BTRFS_XATTR_ITEM_KEY; | 
| Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4913 | ret = drop_objectid_items(trans, log, path, ino, | 
|  | 4914 | max_key.type); | 
|  | 4915 | } else { | 
|  | 4916 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4917 | &inode->runtime_flags); | 
| Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4918 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4919 | &inode->runtime_flags); | 
| Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4920 | while(1) { | 
|  | 4921 | ret = btrfs_truncate_inode_items(trans, | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4922 | log, &inode->vfs_inode, 0, 0); | 
| Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4923 | if (ret != -EAGAIN) | 
|  | 4924 | break; | 
|  | 4925 | } | 
| Filipe Manana | a742994 | 2015-02-13 16:56:14 +0000 | [diff] [blame] | 4926 | } | 
| Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4927 | } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING, | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4928 | &inode->runtime_flags) || | 
| Josef Bacik | 6cfab85 | 2013-11-12 16:25:58 -0500 | [diff] [blame] | 4929 | inode_only == LOG_INODE_EXISTS) { | 
| Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4930 | if (inode_only == LOG_INODE_ALL) | 
| Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4931 | fast_search = true; | 
| Filipe Manana | 4f764e5 | 2015-02-23 19:53:35 +0000 | [diff] [blame] | 4932 | max_key.type = BTRFS_XATTR_ITEM_KEY; | 
| Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4933 | ret = drop_objectid_items(trans, log, path, ino, | 
|  | 4934 | max_key.type); | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4935 | } else { | 
| Liu Bo | 183f37f | 2012-11-01 06:38:47 +0000 | [diff] [blame] | 4936 | if (inode_only == LOG_INODE_ALL) | 
|  | 4937 | fast_search = true; | 
| Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4938 | goto log_extents; | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4939 | } | 
| Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 4940 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4941 | } | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 4942 | if (ret) { | 
|  | 4943 | err = ret; | 
|  | 4944 | goto out_unlock; | 
|  | 4945 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4946 |  | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4947 | while (1) { | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4948 | ins_nr = 0; | 
| Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 4949 | ret = btrfs_search_forward(root, &min_key, | 
| Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4950 | path, trans->transid); | 
| Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 4951 | if (ret < 0) { | 
|  | 4952 | err = ret; | 
|  | 4953 | goto out_unlock; | 
|  | 4954 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4955 | if (ret != 0) | 
|  | 4956 | break; | 
| Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 4957 | again: | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4958 | /* note, ins_nr might be > 0 here, cleanup outside the loop */ | 
| Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4959 | if (min_key.objectid != ino) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4960 | break; | 
|  | 4961 | if (min_key.type > max_key.type) | 
|  | 4962 | break; | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4963 |  | 
| Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 4964 | if (min_key.type == BTRFS_INODE_ITEM_KEY) | 
|  | 4965 | need_log_inode_item = false; | 
|  | 4966 |  | 
| Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4967 | if ((min_key.type == BTRFS_INODE_REF_KEY || | 
|  | 4968 | min_key.type == BTRFS_INODE_EXTREF_KEY) && | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4969 | inode->generation == trans->transid) { | 
| Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4970 | u64 other_ino = 0; | 
|  | 4971 |  | 
| Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4972 | ret = btrfs_check_ref_name_override(path->nodes[0], | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4973 | path->slots[0], &min_key, inode, | 
|  | 4974 | &other_ino); | 
| Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 4975 | if (ret < 0) { | 
|  | 4976 | err = ret; | 
|  | 4977 | goto out_unlock; | 
| Filipe Manana | 28a2359 | 2016-08-23 21:13:51 +0100 | [diff] [blame] | 4978 | } else if (ret > 0 && ctx && | 
| Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4979 | other_ino != btrfs_ino(BTRFS_I(ctx->inode))) { | 
| Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4980 | struct btrfs_key inode_key; | 
|  | 4981 | struct inode *other_inode; | 
|  | 4982 |  | 
|  | 4983 | if (ins_nr > 0) { | 
|  | 4984 | ins_nr++; | 
|  | 4985 | } else { | 
|  | 4986 | ins_nr = 1; | 
|  | 4987 | ins_start_slot = path->slots[0]; | 
|  | 4988 | } | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 4989 | ret = copy_items(trans, inode, dst_path, path, | 
| Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4990 | &last_extent, ins_start_slot, | 
|  | 4991 | ins_nr, inode_only, | 
|  | 4992 | logged_isize); | 
|  | 4993 | if (ret < 0) { | 
|  | 4994 | err = ret; | 
|  | 4995 | goto out_unlock; | 
|  | 4996 | } | 
|  | 4997 | ins_nr = 0; | 
|  | 4998 | btrfs_release_path(path); | 
|  | 4999 | inode_key.objectid = other_ino; | 
|  | 5000 | inode_key.type = BTRFS_INODE_ITEM_KEY; | 
|  | 5001 | inode_key.offset = 0; | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5002 | other_inode = btrfs_iget(fs_info->sb, | 
| Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5003 | &inode_key, root, | 
|  | 5004 | NULL); | 
|  | 5005 | /* | 
|  | 5006 | * If the other inode that had a conflicting dir | 
|  | 5007 | * entry was deleted in the current transaction, | 
|  | 5008 | * we don't need to do more work nor fallback to | 
|  | 5009 | * a transaction commit. | 
|  | 5010 | */ | 
| Al Viro | 8d9e220 | 2018-07-29 23:04:46 +0100 | [diff] [blame] | 5011 | if (other_inode == ERR_PTR(-ENOENT)) { | 
| Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5012 | goto next_key; | 
|  | 5013 | } else if (IS_ERR(other_inode)) { | 
|  | 5014 | err = PTR_ERR(other_inode); | 
|  | 5015 | goto out_unlock; | 
|  | 5016 | } | 
|  | 5017 | /* | 
|  | 5018 | * We are safe logging the other inode without | 
|  | 5019 | * acquiring its i_mutex as long as we log with | 
|  | 5020 | * the LOG_INODE_EXISTS mode. We're safe against | 
|  | 5021 | * concurrent renames of the other inode as well | 
|  | 5022 | * because during a rename we pin the log and | 
|  | 5023 | * update the log with the new name before we | 
|  | 5024 | * unpin it. | 
|  | 5025 | */ | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5026 | err = btrfs_log_inode(trans, root, | 
|  | 5027 | BTRFS_I(other_inode), | 
|  | 5028 | LOG_OTHER_INODE, 0, LLONG_MAX, | 
|  | 5029 | ctx); | 
| Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5030 | iput(other_inode); | 
|  | 5031 | if (err) | 
|  | 5032 | goto out_unlock; | 
|  | 5033 | else | 
|  | 5034 | goto next_key; | 
| Filipe Manana | 56f23fd | 2016-03-30 23:37:21 +0100 | [diff] [blame] | 5035 | } | 
|  | 5036 | } | 
|  | 5037 |  | 
| Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5038 | /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */ | 
|  | 5039 | if (min_key.type == BTRFS_XATTR_ITEM_KEY) { | 
|  | 5040 | if (ins_nr == 0) | 
|  | 5041 | goto next_slot; | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5042 | ret = copy_items(trans, inode, dst_path, path, | 
| Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5043 | &last_extent, ins_start_slot, | 
|  | 5044 | ins_nr, inode_only, logged_isize); | 
|  | 5045 | if (ret < 0) { | 
|  | 5046 | err = ret; | 
|  | 5047 | goto out_unlock; | 
|  | 5048 | } | 
|  | 5049 | ins_nr = 0; | 
|  | 5050 | if (ret) { | 
|  | 5051 | btrfs_release_path(path); | 
|  | 5052 | continue; | 
|  | 5053 | } | 
|  | 5054 | goto next_slot; | 
|  | 5055 | } | 
|  | 5056 |  | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5057 | if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { | 
|  | 5058 | ins_nr++; | 
|  | 5059 | goto next_slot; | 
|  | 5060 | } else if (!ins_nr) { | 
|  | 5061 | ins_start_slot = path->slots[0]; | 
|  | 5062 | ins_nr = 1; | 
|  | 5063 | goto next_slot; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5064 | } | 
|  | 5065 |  | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5066 | ret = copy_items(trans, inode, dst_path, path, &last_extent, | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5067 | ins_start_slot, ins_nr, inode_only, | 
|  | 5068 | logged_isize); | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5069 | if (ret < 0) { | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5070 | err = ret; | 
|  | 5071 | goto out_unlock; | 
| Rasmus Villemoes | a71db86 | 2014-06-20 21:51:43 +0200 | [diff] [blame] | 5072 | } | 
|  | 5073 | if (ret) { | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5074 | ins_nr = 0; | 
|  | 5075 | btrfs_release_path(path); | 
|  | 5076 | continue; | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5077 | } | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5078 | ins_nr = 1; | 
|  | 5079 | ins_start_slot = path->slots[0]; | 
|  | 5080 | next_slot: | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5081 |  | 
| Chris Mason | 3a5f1d4 | 2008-09-11 15:53:37 -0400 | [diff] [blame] | 5082 | nritems = btrfs_header_nritems(path->nodes[0]); | 
|  | 5083 | path->slots[0]++; | 
|  | 5084 | if (path->slots[0] < nritems) { | 
|  | 5085 | btrfs_item_key_to_cpu(path->nodes[0], &min_key, | 
|  | 5086 | path->slots[0]); | 
|  | 5087 | goto again; | 
|  | 5088 | } | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5089 | if (ins_nr) { | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5090 | ret = copy_items(trans, inode, dst_path, path, | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5091 | &last_extent, ins_start_slot, | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5092 | ins_nr, inode_only, logged_isize); | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5093 | if (ret < 0) { | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5094 | err = ret; | 
|  | 5095 | goto out_unlock; | 
|  | 5096 | } | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5097 | ret = 0; | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5098 | ins_nr = 0; | 
|  | 5099 | } | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5100 | btrfs_release_path(path); | 
| Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5101 | next_key: | 
| Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5102 | if (min_key.offset < (u64)-1) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5103 | min_key.offset++; | 
| Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5104 | } else if (min_key.type < max_key.type) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5105 | min_key.type++; | 
| Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5106 | min_key.offset = 0; | 
|  | 5107 | } else { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5108 | break; | 
| Filipe David Borba Manana | 3d41d70 | 2013-10-01 17:06:53 +0100 | [diff] [blame] | 5109 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5110 | } | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5111 | if (ins_nr) { | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5112 | ret = copy_items(trans, inode, dst_path, path, &last_extent, | 
| Filipe Manana | 1a4bcf4 | 2015-02-13 12:30:56 +0000 | [diff] [blame] | 5113 | ins_start_slot, ins_nr, inode_only, | 
|  | 5114 | logged_isize); | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5115 | if (ret < 0) { | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5116 | err = ret; | 
|  | 5117 | goto out_unlock; | 
|  | 5118 | } | 
| Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5119 | ret = 0; | 
| Chris Mason | 31ff1cd | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5120 | ins_nr = 0; | 
|  | 5121 | } | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5122 |  | 
| Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5123 | btrfs_release_path(path); | 
|  | 5124 | btrfs_release_path(dst_path); | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5125 | err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path); | 
| Filipe Manana | 36283bf | 2015-06-20 00:44:51 +0100 | [diff] [blame] | 5126 | if (err) | 
|  | 5127 | goto out_unlock; | 
| Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 5128 | xattrs_logged = true; | 
| Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5129 | if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) { | 
|  | 5130 | btrfs_release_path(path); | 
|  | 5131 | btrfs_release_path(dst_path); | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5132 | err = btrfs_log_trailing_hole(trans, root, inode, path); | 
| Filipe Manana | a89ca6f | 2015-06-25 04:17:46 +0100 | [diff] [blame] | 5133 | if (err) | 
|  | 5134 | goto out_unlock; | 
|  | 5135 | } | 
| Josef Bacik | a95249b | 2012-10-11 16:17:34 -0400 | [diff] [blame] | 5136 | log_extents: | 
| Josef Bacik | f3b15cc | 2013-07-22 12:54:30 -0400 | [diff] [blame] | 5137 | btrfs_release_path(path); | 
|  | 5138 | btrfs_release_path(dst_path); | 
| Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 5139 | if (need_log_inode_item) { | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5140 | err = log_inode_item(trans, log, dst_path, inode); | 
| Filipe Manana | 9a8fca6 | 2018-05-11 16:42:42 +0100 | [diff] [blame] | 5141 | if (!err && !xattrs_logged) { | 
|  | 5142 | err = btrfs_log_all_xattrs(trans, root, inode, path, | 
|  | 5143 | dst_path); | 
|  | 5144 | btrfs_release_path(path); | 
|  | 5145 | } | 
| Filipe Manana | e4545de | 2015-06-17 12:49:23 +0100 | [diff] [blame] | 5146 | if (err) | 
|  | 5147 | goto out_unlock; | 
|  | 5148 | } | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5149 | if (fast_search) { | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5150 | ret = btrfs_log_changed_extents(trans, root, inode, dst_path, | 
| Josef Bacik | a2120a4 | 2018-05-23 11:58:35 -0400 | [diff] [blame] | 5151 | ctx, start, end); | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5152 | if (ret) { | 
|  | 5153 | err = ret; | 
|  | 5154 | goto out_unlock; | 
|  | 5155 | } | 
| Josef Bacik | d006a04 | 2013-11-12 20:54:09 -0500 | [diff] [blame] | 5156 | } else if (inode_only == LOG_INODE_ALL) { | 
| Liu Bo | 06d3d22 | 2012-08-27 10:52:19 -0600 | [diff] [blame] | 5157 | struct extent_map *em, *n; | 
|  | 5158 |  | 
| Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5159 | write_lock(&em_tree->lock); | 
|  | 5160 | /* | 
|  | 5161 | * We can't just remove every em if we're called for a ranged | 
|  | 5162 | * fsync - that is, one that doesn't cover the whole possible | 
|  | 5163 | * file range (0 to LLONG_MAX). This is because we can have | 
|  | 5164 | * em's that fall outside the range we're logging and therefore | 
|  | 5165 | * their ordered operations haven't completed yet | 
|  | 5166 | * (btrfs_finish_ordered_io() not invoked yet). This means we | 
|  | 5167 | * didn't get their respective file extent item in the fs/subvol | 
|  | 5168 | * tree yet, and need to let the next fast fsync (one which | 
|  | 5169 | * consults the list of modified extent maps) find the em so | 
|  | 5170 | * that it logs a matching file extent item and waits for the | 
|  | 5171 | * respective ordered operation to complete (if it's still | 
|  | 5172 | * running). | 
|  | 5173 | * | 
|  | 5174 | * Removing every em outside the range we're logging would make | 
|  | 5175 | * the next fast fsync not log their matching file extent items, | 
|  | 5176 | * therefore making us lose data after a log replay. | 
|  | 5177 | */ | 
|  | 5178 | list_for_each_entry_safe(em, n, &em_tree->modified_extents, | 
|  | 5179 | list) { | 
|  | 5180 | const u64 mod_end = em->mod_start + em->mod_len - 1; | 
|  | 5181 |  | 
|  | 5182 | if (em->mod_start >= start && mod_end <= end) | 
|  | 5183 | list_del_init(&em->list); | 
|  | 5184 | } | 
|  | 5185 | write_unlock(&em_tree->lock); | 
| Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5186 | } | 
|  | 5187 |  | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5188 | if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) { | 
|  | 5189 | ret = log_directory_changes(trans, root, inode, path, dst_path, | 
|  | 5190 | ctx); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5191 | if (ret) { | 
|  | 5192 | err = ret; | 
|  | 5193 | goto out_unlock; | 
|  | 5194 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5195 | } | 
| Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5196 |  | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5197 | spin_lock(&inode->lock); | 
|  | 5198 | inode->logged_trans = trans->transid; | 
|  | 5199 | inode->last_log_commit = inode->last_sub_trans; | 
|  | 5200 | spin_unlock(&inode->lock); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5201 | out_unlock: | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5202 | mutex_unlock(&inode->log_mutex); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5203 |  | 
|  | 5204 | btrfs_free_path(path); | 
|  | 5205 | btrfs_free_path(dst_path); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5206 | return err; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5207 | } | 
|  | 5208 |  | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5209 | /* | 
| Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5210 | * Check if we must fallback to a transaction commit when logging an inode. | 
|  | 5211 | * This must be called after logging the inode and is used only in the context | 
|  | 5212 | * when fsyncing an inode requires the need to log some other inode - in which | 
|  | 5213 | * case we can't lock the i_mutex of each other inode we need to log as that | 
|  | 5214 | * can lead to deadlocks with concurrent fsync against other inodes (as we can | 
|  | 5215 | * log inodes up or down in the hierarchy) or rename operations for example. So | 
|  | 5216 | * we take the log_mutex of the inode after we have logged it and then check for | 
|  | 5217 | * its last_unlink_trans value - this is safe because any task setting | 
|  | 5218 | * last_unlink_trans must take the log_mutex and it must do this before it does | 
|  | 5219 | * the actual unlink operation, so if we do this check before a concurrent task | 
|  | 5220 | * sets last_unlink_trans it means we've logged a consistent version/state of | 
|  | 5221 | * all the inode items, otherwise we are not sure and must do a transaction | 
| Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5222 | * commit (the concurrent task might have only updated last_unlink_trans before | 
| Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5223 | * we logged the inode or it might have also done the unlink). | 
|  | 5224 | */ | 
|  | 5225 | static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5226 | struct btrfs_inode *inode) | 
| Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5227 | { | 
| Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5228 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | 
| Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5229 | bool ret = false; | 
|  | 5230 |  | 
| Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5231 | mutex_lock(&inode->log_mutex); | 
|  | 5232 | if (inode->last_unlink_trans > fs_info->last_trans_committed) { | 
| Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5233 | /* | 
|  | 5234 | * Make sure any commits to the log are forced to be full | 
|  | 5235 | * commits. | 
|  | 5236 | */ | 
|  | 5237 | btrfs_set_log_full_commit(fs_info, trans); | 
|  | 5238 | ret = true; | 
|  | 5239 | } | 
| Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5240 | mutex_unlock(&inode->log_mutex); | 
| Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5241 |  | 
|  | 5242 | return ret; | 
|  | 5243 | } | 
|  | 5244 |  | 
|  | 5245 | /* | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5246 | * follow the dentry parent pointers up the chain and see if any | 
|  | 5247 | * of the directories in it require a full commit before they can | 
|  | 5248 | * be logged.  Returns zero if nothing special needs to be done or 1 if | 
|  | 5249 | * a full commit is required. | 
|  | 5250 | */ | 
|  | 5251 | static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5252 | struct btrfs_inode *inode, | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5253 | struct dentry *parent, | 
|  | 5254 | struct super_block *sb, | 
|  | 5255 | u64 last_committed) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5256 | { | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5257 | int ret = 0; | 
| Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5258 | struct dentry *old_parent = NULL; | 
| Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5259 | struct btrfs_inode *orig_inode = inode; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5260 |  | 
| Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5261 | /* | 
|  | 5262 | * for regular files, if its inode is already on disk, we don't | 
|  | 5263 | * have to worry about the parents at all.  This is because | 
|  | 5264 | * we can use the last_unlink_trans field to record renames | 
|  | 5265 | * and other fun in this file. | 
|  | 5266 | */ | 
| Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5267 | if (S_ISREG(inode->vfs_inode.i_mode) && | 
|  | 5268 | inode->generation <= last_committed && | 
|  | 5269 | inode->last_unlink_trans <= last_committed) | 
|  | 5270 | goto out; | 
| Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5271 |  | 
| Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5272 | if (!S_ISDIR(inode->vfs_inode.i_mode)) { | 
| Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5273 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5274 | goto out; | 
| Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5275 | inode = BTRFS_I(d_inode(parent)); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5276 | } | 
|  | 5277 |  | 
|  | 5278 | while (1) { | 
| Josef Bacik | de2b530 | 2013-09-11 09:36:30 -0400 | [diff] [blame] | 5279 | /* | 
|  | 5280 | * If we are logging a directory then we start with our inode, | 
| Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5281 | * not our parent's inode, so we need to skip setting the | 
| Josef Bacik | de2b530 | 2013-09-11 09:36:30 -0400 | [diff] [blame] | 5282 | * logged_trans so that further down in the log code we don't | 
|  | 5283 | * think this inode has already been logged. | 
|  | 5284 | */ | 
|  | 5285 | if (inode != orig_inode) | 
| Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5286 | inode->logged_trans = trans->transid; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5287 | smp_mb(); | 
|  | 5288 |  | 
| Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5289 | if (btrfs_must_commit_transaction(trans, inode)) { | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5290 | ret = 1; | 
|  | 5291 | break; | 
|  | 5292 | } | 
|  | 5293 |  | 
| Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5294 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5295 | break; | 
|  | 5296 |  | 
| Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5297 | if (IS_ROOT(parent)) { | 
| Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5298 | inode = BTRFS_I(d_inode(parent)); | 
|  | 5299 | if (btrfs_must_commit_transaction(trans, inode)) | 
| Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5300 | ret = 1; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5301 | break; | 
| Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 5302 | } | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5303 |  | 
| Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5304 | parent = dget_parent(parent); | 
|  | 5305 | dput(old_parent); | 
|  | 5306 | old_parent = parent; | 
| Nikolay Borisov | aefa611 | 2017-02-20 13:51:00 +0200 | [diff] [blame] | 5307 | inode = BTRFS_I(d_inode(parent)); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5308 |  | 
|  | 5309 | } | 
| Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5310 | dput(old_parent); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5311 | out: | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5312 | return ret; | 
|  | 5313 | } | 
|  | 5314 |  | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5315 | struct btrfs_dir_list { | 
|  | 5316 | u64 ino; | 
|  | 5317 | struct list_head list; | 
|  | 5318 | }; | 
|  | 5319 |  | 
|  | 5320 | /* | 
|  | 5321 | * Log the inodes of the new dentries of a directory. See log_dir_items() for | 
|  | 5322 | * details about the why it is needed. | 
|  | 5323 | * This is a recursive operation - if an existing dentry corresponds to a | 
|  | 5324 | * directory, that directory's new entries are logged too (same behaviour as | 
|  | 5325 | * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes | 
|  | 5326 | * the dentries point to we do not lock their i_mutex, otherwise lockdep | 
|  | 5327 | * complains about the following circular lock dependency / possible deadlock: | 
|  | 5328 | * | 
|  | 5329 | *        CPU0                                        CPU1 | 
|  | 5330 | *        ----                                        ---- | 
|  | 5331 | * lock(&type->i_mutex_dir_key#3/2); | 
|  | 5332 | *                                            lock(sb_internal#2); | 
|  | 5333 | *                                            lock(&type->i_mutex_dir_key#3/2); | 
|  | 5334 | * lock(&sb->s_type->i_mutex_key#14); | 
|  | 5335 | * | 
|  | 5336 | * Where sb_internal is the lock (a counter that works as a lock) acquired by | 
|  | 5337 | * sb_start_intwrite() in btrfs_start_transaction(). | 
|  | 5338 | * Not locking i_mutex of the inodes is still safe because: | 
|  | 5339 | * | 
|  | 5340 | * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible | 
|  | 5341 | *    that while logging the inode new references (names) are added or removed | 
|  | 5342 | *    from the inode, leaving the logged inode item with a link count that does | 
|  | 5343 | *    not match the number of logged inode reference items. This is fine because | 
|  | 5344 | *    at log replay time we compute the real number of links and correct the | 
|  | 5345 | *    link count in the inode item (see replay_one_buffer() and | 
|  | 5346 | *    link_to_fixup_dir()); | 
|  | 5347 | * | 
|  | 5348 | * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that | 
|  | 5349 | *    while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and | 
|  | 5350 | *    BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item | 
|  | 5351 | *    has a size that doesn't match the sum of the lengths of all the logged | 
|  | 5352 | *    names. This does not result in a problem because if a dir_item key is | 
|  | 5353 | *    logged but its matching dir_index key is not logged, at log replay time we | 
|  | 5354 | *    don't use it to replay the respective name (see replay_one_name()). On the | 
|  | 5355 | *    other hand if only the dir_index key ends up being logged, the respective | 
|  | 5356 | *    name is added to the fs/subvol tree with both the dir_item and dir_index | 
|  | 5357 | *    keys created (see replay_one_name()). | 
|  | 5358 | *    The directory's inode item with a wrong i_size is not a problem as well, | 
|  | 5359 | *    since we don't use it at log replay time to set the i_size in the inode | 
|  | 5360 | *    item of the fs/subvol tree (see overwrite_item()). | 
|  | 5361 | */ | 
|  | 5362 | static int log_new_dir_dentries(struct btrfs_trans_handle *trans, | 
|  | 5363 | struct btrfs_root *root, | 
| Nikolay Borisov | 51cc0d3 | 2017-01-18 00:31:43 +0200 | [diff] [blame] | 5364 | struct btrfs_inode *start_inode, | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5365 | struct btrfs_log_ctx *ctx) | 
|  | 5366 | { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5367 | struct btrfs_fs_info *fs_info = root->fs_info; | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5368 | struct btrfs_root *log = root->log_root; | 
|  | 5369 | struct btrfs_path *path; | 
|  | 5370 | LIST_HEAD(dir_list); | 
|  | 5371 | struct btrfs_dir_list *dir_elem; | 
|  | 5372 | int ret = 0; | 
|  | 5373 |  | 
|  | 5374 | path = btrfs_alloc_path(); | 
|  | 5375 | if (!path) | 
|  | 5376 | return -ENOMEM; | 
|  | 5377 |  | 
|  | 5378 | dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS); | 
|  | 5379 | if (!dir_elem) { | 
|  | 5380 | btrfs_free_path(path); | 
|  | 5381 | return -ENOMEM; | 
|  | 5382 | } | 
| Nikolay Borisov | 51cc0d3 | 2017-01-18 00:31:43 +0200 | [diff] [blame] | 5383 | dir_elem->ino = btrfs_ino(start_inode); | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5384 | list_add_tail(&dir_elem->list, &dir_list); | 
|  | 5385 |  | 
|  | 5386 | while (!list_empty(&dir_list)) { | 
|  | 5387 | struct extent_buffer *leaf; | 
|  | 5388 | struct btrfs_key min_key; | 
|  | 5389 | int nritems; | 
|  | 5390 | int i; | 
|  | 5391 |  | 
|  | 5392 | dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, | 
|  | 5393 | list); | 
|  | 5394 | if (ret) | 
|  | 5395 | goto next_dir_inode; | 
|  | 5396 |  | 
|  | 5397 | min_key.objectid = dir_elem->ino; | 
|  | 5398 | min_key.type = BTRFS_DIR_ITEM_KEY; | 
|  | 5399 | min_key.offset = 0; | 
|  | 5400 | again: | 
|  | 5401 | btrfs_release_path(path); | 
|  | 5402 | ret = btrfs_search_forward(log, &min_key, path, trans->transid); | 
|  | 5403 | if (ret < 0) { | 
|  | 5404 | goto next_dir_inode; | 
|  | 5405 | } else if (ret > 0) { | 
|  | 5406 | ret = 0; | 
|  | 5407 | goto next_dir_inode; | 
|  | 5408 | } | 
|  | 5409 |  | 
|  | 5410 | process_leaf: | 
|  | 5411 | leaf = path->nodes[0]; | 
|  | 5412 | nritems = btrfs_header_nritems(leaf); | 
|  | 5413 | for (i = path->slots[0]; i < nritems; i++) { | 
|  | 5414 | struct btrfs_dir_item *di; | 
|  | 5415 | struct btrfs_key di_key; | 
|  | 5416 | struct inode *di_inode; | 
|  | 5417 | struct btrfs_dir_list *new_dir_elem; | 
|  | 5418 | int log_mode = LOG_INODE_EXISTS; | 
|  | 5419 | int type; | 
|  | 5420 |  | 
|  | 5421 | btrfs_item_key_to_cpu(leaf, &min_key, i); | 
|  | 5422 | if (min_key.objectid != dir_elem->ino || | 
|  | 5423 | min_key.type != BTRFS_DIR_ITEM_KEY) | 
|  | 5424 | goto next_dir_inode; | 
|  | 5425 |  | 
|  | 5426 | di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item); | 
|  | 5427 | type = btrfs_dir_type(leaf, di); | 
|  | 5428 | if (btrfs_dir_transid(leaf, di) < trans->transid && | 
|  | 5429 | type != BTRFS_FT_DIR) | 
|  | 5430 | continue; | 
|  | 5431 | btrfs_dir_item_key_to_cpu(leaf, di, &di_key); | 
|  | 5432 | if (di_key.type == BTRFS_ROOT_ITEM_KEY) | 
|  | 5433 | continue; | 
|  | 5434 |  | 
| Robbie Ko | ec125cf | 2016-10-28 10:48:26 +0800 | [diff] [blame] | 5435 | btrfs_release_path(path); | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5436 | di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL); | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5437 | if (IS_ERR(di_inode)) { | 
|  | 5438 | ret = PTR_ERR(di_inode); | 
|  | 5439 | goto next_dir_inode; | 
|  | 5440 | } | 
|  | 5441 |  | 
| Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 5442 | if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) { | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5443 | iput(di_inode); | 
| Robbie Ko | ec125cf | 2016-10-28 10:48:26 +0800 | [diff] [blame] | 5444 | break; | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5445 | } | 
|  | 5446 |  | 
|  | 5447 | ctx->log_new_dentries = false; | 
| Filipe Manana | 3f9749f | 2016-04-25 04:45:02 +0100 | [diff] [blame] | 5448 | if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK) | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5449 | log_mode = LOG_INODE_ALL; | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5450 | ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode), | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5451 | log_mode, 0, LLONG_MAX, ctx); | 
| Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5452 | if (!ret && | 
| Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5453 | btrfs_must_commit_transaction(trans, BTRFS_I(di_inode))) | 
| Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5454 | ret = 1; | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5455 | iput(di_inode); | 
|  | 5456 | if (ret) | 
|  | 5457 | goto next_dir_inode; | 
|  | 5458 | if (ctx->log_new_dentries) { | 
|  | 5459 | new_dir_elem = kmalloc(sizeof(*new_dir_elem), | 
|  | 5460 | GFP_NOFS); | 
|  | 5461 | if (!new_dir_elem) { | 
|  | 5462 | ret = -ENOMEM; | 
|  | 5463 | goto next_dir_inode; | 
|  | 5464 | } | 
|  | 5465 | new_dir_elem->ino = di_key.objectid; | 
|  | 5466 | list_add_tail(&new_dir_elem->list, &dir_list); | 
|  | 5467 | } | 
|  | 5468 | break; | 
|  | 5469 | } | 
|  | 5470 | if (i == nritems) { | 
|  | 5471 | ret = btrfs_next_leaf(log, path); | 
|  | 5472 | if (ret < 0) { | 
|  | 5473 | goto next_dir_inode; | 
|  | 5474 | } else if (ret > 0) { | 
|  | 5475 | ret = 0; | 
|  | 5476 | goto next_dir_inode; | 
|  | 5477 | } | 
|  | 5478 | goto process_leaf; | 
|  | 5479 | } | 
|  | 5480 | if (min_key.offset < (u64)-1) { | 
|  | 5481 | min_key.offset++; | 
|  | 5482 | goto again; | 
|  | 5483 | } | 
|  | 5484 | next_dir_inode: | 
|  | 5485 | list_del(&dir_elem->list); | 
|  | 5486 | kfree(dir_elem); | 
|  | 5487 | } | 
|  | 5488 |  | 
|  | 5489 | btrfs_free_path(path); | 
|  | 5490 | return ret; | 
|  | 5491 | } | 
|  | 5492 |  | 
| Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5493 | static int btrfs_log_all_parents(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 5494 | struct btrfs_inode *inode, | 
| Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5495 | struct btrfs_log_ctx *ctx) | 
|  | 5496 | { | 
| David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 5497 | struct btrfs_fs_info *fs_info = trans->fs_info; | 
| Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5498 | int ret; | 
|  | 5499 | struct btrfs_path *path; | 
|  | 5500 | struct btrfs_key key; | 
| Nikolay Borisov | d0a0b78 | 2017-02-20 13:50:30 +0200 | [diff] [blame] | 5501 | struct btrfs_root *root = inode->root; | 
|  | 5502 | const u64 ino = btrfs_ino(inode); | 
| Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5503 |  | 
|  | 5504 | path = btrfs_alloc_path(); | 
|  | 5505 | if (!path) | 
|  | 5506 | return -ENOMEM; | 
|  | 5507 | path->skip_locking = 1; | 
|  | 5508 | path->search_commit_root = 1; | 
|  | 5509 |  | 
|  | 5510 | key.objectid = ino; | 
|  | 5511 | key.type = BTRFS_INODE_REF_KEY; | 
|  | 5512 | key.offset = 0; | 
|  | 5513 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | 
|  | 5514 | if (ret < 0) | 
|  | 5515 | goto out; | 
|  | 5516 |  | 
|  | 5517 | while (true) { | 
|  | 5518 | struct extent_buffer *leaf = path->nodes[0]; | 
|  | 5519 | int slot = path->slots[0]; | 
|  | 5520 | u32 cur_offset = 0; | 
|  | 5521 | u32 item_size; | 
|  | 5522 | unsigned long ptr; | 
|  | 5523 |  | 
|  | 5524 | if (slot >= btrfs_header_nritems(leaf)) { | 
|  | 5525 | ret = btrfs_next_leaf(root, path); | 
|  | 5526 | if (ret < 0) | 
|  | 5527 | goto out; | 
|  | 5528 | else if (ret > 0) | 
|  | 5529 | break; | 
|  | 5530 | continue; | 
|  | 5531 | } | 
|  | 5532 |  | 
|  | 5533 | btrfs_item_key_to_cpu(leaf, &key, slot); | 
|  | 5534 | /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */ | 
|  | 5535 | if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY) | 
|  | 5536 | break; | 
|  | 5537 |  | 
|  | 5538 | item_size = btrfs_item_size_nr(leaf, slot); | 
|  | 5539 | ptr = btrfs_item_ptr_offset(leaf, slot); | 
|  | 5540 | while (cur_offset < item_size) { | 
|  | 5541 | struct btrfs_key inode_key; | 
|  | 5542 | struct inode *dir_inode; | 
|  | 5543 |  | 
|  | 5544 | inode_key.type = BTRFS_INODE_ITEM_KEY; | 
|  | 5545 | inode_key.offset = 0; | 
|  | 5546 |  | 
|  | 5547 | if (key.type == BTRFS_INODE_EXTREF_KEY) { | 
|  | 5548 | struct btrfs_inode_extref *extref; | 
|  | 5549 |  | 
|  | 5550 | extref = (struct btrfs_inode_extref *) | 
|  | 5551 | (ptr + cur_offset); | 
|  | 5552 | inode_key.objectid = btrfs_inode_extref_parent( | 
|  | 5553 | leaf, extref); | 
|  | 5554 | cur_offset += sizeof(*extref); | 
|  | 5555 | cur_offset += btrfs_inode_extref_name_len(leaf, | 
|  | 5556 | extref); | 
|  | 5557 | } else { | 
|  | 5558 | inode_key.objectid = key.offset; | 
|  | 5559 | cur_offset = item_size; | 
|  | 5560 | } | 
|  | 5561 |  | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5562 | dir_inode = btrfs_iget(fs_info->sb, &inode_key, | 
| Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5563 | root, NULL); | 
|  | 5564 | /* If parent inode was deleted, skip it. */ | 
|  | 5565 | if (IS_ERR(dir_inode)) | 
|  | 5566 | continue; | 
|  | 5567 |  | 
| Filipe Manana | 657ed1a | 2016-04-06 17:11:56 +0100 | [diff] [blame] | 5568 | if (ctx) | 
|  | 5569 | ctx->log_new_dentries = false; | 
| Nikolay Borisov | a59108a | 2017-01-18 00:31:48 +0200 | [diff] [blame] | 5570 | ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode), | 
| Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5571 | LOG_INODE_ALL, 0, LLONG_MAX, ctx); | 
| Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5572 | if (!ret && | 
| Nikolay Borisov | ab1717b | 2017-01-18 00:31:27 +0200 | [diff] [blame] | 5573 | btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode))) | 
| Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5574 | ret = 1; | 
| Filipe Manana | 657ed1a | 2016-04-06 17:11:56 +0100 | [diff] [blame] | 5575 | if (!ret && ctx && ctx->log_new_dentries) | 
|  | 5576 | ret = log_new_dir_dentries(trans, root, | 
| David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5577 | BTRFS_I(dir_inode), ctx); | 
| Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5578 | iput(dir_inode); | 
|  | 5579 | if (ret) | 
|  | 5580 | goto out; | 
|  | 5581 | } | 
|  | 5582 | path->slots[0]++; | 
|  | 5583 | } | 
|  | 5584 | ret = 0; | 
|  | 5585 | out: | 
|  | 5586 | btrfs_free_path(path); | 
|  | 5587 | return ret; | 
|  | 5588 | } | 
|  | 5589 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5590 | /* | 
|  | 5591 | * helper function around btrfs_log_inode to make sure newly created | 
|  | 5592 | * parent directories also end up in the log.  A minimal inode and backref | 
|  | 5593 | * only logging is done of any parent directories that are older than | 
|  | 5594 | * the last committed transaction | 
|  | 5595 | */ | 
| Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5596 | static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5597 | struct btrfs_inode *inode, | 
| Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5598 | struct dentry *parent, | 
|  | 5599 | const loff_t start, | 
|  | 5600 | const loff_t end, | 
| Edmund Nadolski | 41a1ead | 2017-11-20 13:24:47 -0700 | [diff] [blame] | 5601 | int inode_only, | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5602 | struct btrfs_log_ctx *ctx) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5603 | { | 
| Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5604 | struct btrfs_root *root = inode->root; | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5605 | struct btrfs_fs_info *fs_info = root->fs_info; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5606 | struct super_block *sb; | 
| Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5607 | struct dentry *old_parent = NULL; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5608 | int ret = 0; | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5609 | u64 last_committed = fs_info->last_trans_committed; | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5610 | bool log_dentries = false; | 
| Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5611 | struct btrfs_inode *orig_inode = inode; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5612 |  | 
| Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5613 | sb = inode->vfs_inode.i_sb; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5614 |  | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5615 | if (btrfs_test_opt(fs_info, NOTREELOG)) { | 
| Sage Weil | 3a5e140 | 2009-04-02 16:49:40 -0400 | [diff] [blame] | 5616 | ret = 1; | 
|  | 5617 | goto end_no_trans; | 
|  | 5618 | } | 
|  | 5619 |  | 
| Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 5620 | /* | 
|  | 5621 | * The prev transaction commit doesn't complete, we need do | 
|  | 5622 | * full commit by ourselves. | 
|  | 5623 | */ | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5624 | if (fs_info->last_trans_log_full_commit > | 
|  | 5625 | fs_info->last_trans_committed) { | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5626 | ret = 1; | 
|  | 5627 | goto end_no_trans; | 
|  | 5628 | } | 
|  | 5629 |  | 
| Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5630 | if (btrfs_root_refs(&root->root_item) == 0) { | 
| Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5631 | ret = 1; | 
|  | 5632 | goto end_no_trans; | 
|  | 5633 | } | 
|  | 5634 |  | 
| Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5635 | ret = check_parent_dirs_for_sync(trans, inode, parent, sb, | 
|  | 5636 | last_committed); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5637 | if (ret) | 
|  | 5638 | goto end_no_trans; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5639 |  | 
| Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5640 | if (btrfs_inode_in_log(inode, trans->transid)) { | 
| Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5641 | ret = BTRFS_NO_LOG_SYNC; | 
|  | 5642 | goto end_no_trans; | 
|  | 5643 | } | 
|  | 5644 |  | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5645 | ret = start_log_trans(trans, root, ctx); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5646 | if (ret) | 
| Miao Xie | e87ac13 | 2014-02-20 18:08:53 +0800 | [diff] [blame] | 5647 | goto end_no_trans; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5648 |  | 
| Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5649 | ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5650 | if (ret) | 
|  | 5651 | goto end_trans; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5652 |  | 
| Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5653 | /* | 
|  | 5654 | * for regular files, if its inode is already on disk, we don't | 
|  | 5655 | * have to worry about the parents at all.  This is because | 
|  | 5656 | * we can use the last_unlink_trans field to record renames | 
|  | 5657 | * and other fun in this file. | 
|  | 5658 | */ | 
| Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5659 | if (S_ISREG(inode->vfs_inode.i_mode) && | 
|  | 5660 | inode->generation <= last_committed && | 
|  | 5661 | inode->last_unlink_trans <= last_committed) { | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5662 | ret = 0; | 
|  | 5663 | goto end_trans; | 
|  | 5664 | } | 
| Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5665 |  | 
| Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5666 | if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries) | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5667 | log_dentries = true; | 
|  | 5668 |  | 
| Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5669 | /* | 
| Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5670 | * On unlink we must make sure all our current and old parent directory | 
| Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5671 | * inodes are fully logged. This is to prevent leaving dangling | 
|  | 5672 | * directory index entries in directories that were our parents but are | 
|  | 5673 | * not anymore. Not doing this results in old parent directory being | 
|  | 5674 | * impossible to delete after log replay (rmdir will always fail with | 
|  | 5675 | * error -ENOTEMPTY). | 
|  | 5676 | * | 
|  | 5677 | * Example 1: | 
|  | 5678 | * | 
|  | 5679 | * mkdir testdir | 
|  | 5680 | * touch testdir/foo | 
|  | 5681 | * ln testdir/foo testdir/bar | 
|  | 5682 | * sync | 
|  | 5683 | * unlink testdir/bar | 
|  | 5684 | * xfs_io -c fsync testdir/foo | 
|  | 5685 | * <power failure> | 
|  | 5686 | * mount fs, triggers log replay | 
|  | 5687 | * | 
|  | 5688 | * If we don't log the parent directory (testdir), after log replay the | 
|  | 5689 | * directory still has an entry pointing to the file inode using the bar | 
|  | 5690 | * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and | 
|  | 5691 | * the file inode has a link count of 1. | 
|  | 5692 | * | 
|  | 5693 | * Example 2: | 
|  | 5694 | * | 
|  | 5695 | * mkdir testdir | 
|  | 5696 | * touch foo | 
|  | 5697 | * ln foo testdir/foo2 | 
|  | 5698 | * ln foo testdir/foo3 | 
|  | 5699 | * sync | 
|  | 5700 | * unlink testdir/foo3 | 
|  | 5701 | * xfs_io -c fsync foo | 
|  | 5702 | * <power failure> | 
|  | 5703 | * mount fs, triggers log replay | 
|  | 5704 | * | 
|  | 5705 | * Similar as the first example, after log replay the parent directory | 
|  | 5706 | * testdir still has an entry pointing to the inode file with name foo3 | 
|  | 5707 | * but the file inode does not have a matching BTRFS_INODE_REF_KEY item | 
|  | 5708 | * and has a link count of 2. | 
|  | 5709 | */ | 
| Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5710 | if (inode->last_unlink_trans > last_committed) { | 
| Filipe Manana | 18aa092 | 2015-08-05 16:49:08 +0100 | [diff] [blame] | 5711 | ret = btrfs_log_all_parents(trans, orig_inode, ctx); | 
|  | 5712 | if (ret) | 
|  | 5713 | goto end_trans; | 
|  | 5714 | } | 
|  | 5715 |  | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5716 | while (1) { | 
| Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 5717 | if (!parent || d_really_is_negative(parent) || sb != parent->d_sb) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5718 | break; | 
|  | 5719 |  | 
| Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5720 | inode = BTRFS_I(d_inode(parent)); | 
|  | 5721 | if (root != inode->root) | 
| Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5722 | break; | 
|  | 5723 |  | 
| Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5724 | if (inode->generation > last_committed) { | 
|  | 5725 | ret = btrfs_log_inode(trans, root, inode, | 
|  | 5726 | LOG_INODE_EXISTS, 0, LLONG_MAX, ctx); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5727 | if (ret) | 
|  | 5728 | goto end_trans; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5729 | } | 
| Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5730 | if (IS_ROOT(parent)) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5731 | break; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5732 |  | 
| Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5733 | parent = dget_parent(parent); | 
|  | 5734 | dput(old_parent); | 
|  | 5735 | old_parent = parent; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5736 | } | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5737 | if (log_dentries) | 
| Nikolay Borisov | 19df27a | 2017-02-20 13:51:01 +0200 | [diff] [blame] | 5738 | ret = log_new_dir_dentries(trans, root, orig_inode, ctx); | 
| Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 5739 | else | 
|  | 5740 | ret = 0; | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5741 | end_trans: | 
| Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5742 | dput(old_parent); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5743 | if (ret < 0) { | 
| Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5744 | btrfs_set_log_full_commit(fs_info, trans); | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5745 | ret = 1; | 
|  | 5746 | } | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5747 |  | 
|  | 5748 | if (ret) | 
|  | 5749 | btrfs_remove_log_ctx(root, ctx); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5750 | btrfs_end_log_trans(root); | 
|  | 5751 | end_no_trans: | 
|  | 5752 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5753 | } | 
|  | 5754 |  | 
|  | 5755 | /* | 
|  | 5756 | * it is not safe to log dentry if the chunk root has added new | 
|  | 5757 | * chunks.  This returns 0 if the dentry was logged, and 1 otherwise. | 
|  | 5758 | * If this returns 1, you must commit the transaction to safely get your | 
|  | 5759 | * data on disk. | 
|  | 5760 | */ | 
|  | 5761 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | e5b84f7a | 2018-02-27 17:37:18 +0200 | [diff] [blame] | 5762 | struct dentry *dentry, | 
| Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 5763 | const loff_t start, | 
|  | 5764 | const loff_t end, | 
| Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 5765 | struct btrfs_log_ctx *ctx) | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5766 | { | 
| Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5767 | struct dentry *parent = dget_parent(dentry); | 
|  | 5768 | int ret; | 
|  | 5769 |  | 
| Nikolay Borisov | f882274 | 2018-02-27 17:37:17 +0200 | [diff] [blame] | 5770 | ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent, | 
|  | 5771 | start, end, LOG_INODE_ALL, ctx); | 
| Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5772 | dput(parent); | 
|  | 5773 |  | 
|  | 5774 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5775 | } | 
|  | 5776 |  | 
|  | 5777 | /* | 
|  | 5778 | * should be called during mount to recover any replay any log trees | 
|  | 5779 | * from the FS | 
|  | 5780 | */ | 
|  | 5781 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) | 
|  | 5782 | { | 
|  | 5783 | int ret; | 
|  | 5784 | struct btrfs_path *path; | 
|  | 5785 | struct btrfs_trans_handle *trans; | 
|  | 5786 | struct btrfs_key key; | 
|  | 5787 | struct btrfs_key found_key; | 
|  | 5788 | struct btrfs_key tmp_key; | 
|  | 5789 | struct btrfs_root *log; | 
|  | 5790 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; | 
|  | 5791 | struct walk_control wc = { | 
|  | 5792 | .process_func = process_one_buffer, | 
|  | 5793 | .stage = 0, | 
|  | 5794 | }; | 
|  | 5795 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5796 | path = btrfs_alloc_path(); | 
| Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5797 | if (!path) | 
|  | 5798 | return -ENOMEM; | 
|  | 5799 |  | 
| Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 5800 | set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5801 |  | 
| Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5802 | trans = btrfs_start_transaction(fs_info->tree_root, 0); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5803 | if (IS_ERR(trans)) { | 
|  | 5804 | ret = PTR_ERR(trans); | 
|  | 5805 | goto error; | 
|  | 5806 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5807 |  | 
|  | 5808 | wc.trans = trans; | 
|  | 5809 | wc.pin = 1; | 
|  | 5810 |  | 
| Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5811 | ret = walk_log_tree(trans, log_root_tree, &wc); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5812 | if (ret) { | 
| Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5813 | btrfs_handle_fs_error(fs_info, ret, | 
|  | 5814 | "Failed to pin buffers while recovering log root tree."); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5815 | goto error; | 
|  | 5816 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5817 |  | 
|  | 5818 | again: | 
|  | 5819 | key.objectid = BTRFS_TREE_LOG_OBJECTID; | 
|  | 5820 | key.offset = (u64)-1; | 
| David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5821 | key.type = BTRFS_ROOT_ITEM_KEY; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5822 |  | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5823 | while (1) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5824 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5825 |  | 
|  | 5826 | if (ret < 0) { | 
| Anand Jain | 34d9700 | 2016-03-16 16:43:06 +0800 | [diff] [blame] | 5827 | btrfs_handle_fs_error(fs_info, ret, | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5828 | "Couldn't find tree log root."); | 
|  | 5829 | goto error; | 
|  | 5830 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5831 | if (ret > 0) { | 
|  | 5832 | if (path->slots[0] == 0) | 
|  | 5833 | break; | 
|  | 5834 | path->slots[0]--; | 
|  | 5835 | } | 
|  | 5836 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | 
|  | 5837 | path->slots[0]); | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5838 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5839 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) | 
|  | 5840 | break; | 
|  | 5841 |  | 
| Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 5842 | log = btrfs_read_fs_root(log_root_tree, &found_key); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5843 | if (IS_ERR(log)) { | 
|  | 5844 | ret = PTR_ERR(log); | 
| Anand Jain | 34d9700 | 2016-03-16 16:43:06 +0800 | [diff] [blame] | 5845 | btrfs_handle_fs_error(fs_info, ret, | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5846 | "Couldn't read tree log root."); | 
|  | 5847 | goto error; | 
|  | 5848 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5849 |  | 
|  | 5850 | tmp_key.objectid = found_key.offset; | 
|  | 5851 | tmp_key.type = BTRFS_ROOT_ITEM_KEY; | 
|  | 5852 | tmp_key.offset = (u64)-1; | 
|  | 5853 |  | 
|  | 5854 | 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] | 5855 | if (IS_ERR(wc.replay_dest)) { | 
|  | 5856 | ret = PTR_ERR(wc.replay_dest); | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5857 | free_extent_buffer(log->node); | 
|  | 5858 | free_extent_buffer(log->commit_root); | 
|  | 5859 | kfree(log); | 
| Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5860 | btrfs_handle_fs_error(fs_info, ret, | 
|  | 5861 | "Couldn't read target root for tree log recovery."); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5862 | goto error; | 
|  | 5863 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5864 |  | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 5865 | wc.replay_dest->log_root = log; | 
| Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5866 | btrfs_record_root_in_trans(trans, wc.replay_dest); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5867 | ret = walk_log_tree(trans, log, &wc); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5868 |  | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5869 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5870 | ret = fixup_inode_link_counts(trans, wc.replay_dest, | 
|  | 5871 | path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5872 | } | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5873 |  | 
| Liu Bo | 900c998 | 2018-01-25 11:02:56 -0700 | [diff] [blame] | 5874 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { | 
|  | 5875 | struct btrfs_root *root = wc.replay_dest; | 
|  | 5876 |  | 
|  | 5877 | btrfs_release_path(path); | 
|  | 5878 |  | 
|  | 5879 | /* | 
|  | 5880 | * We have just replayed everything, and the highest | 
|  | 5881 | * objectid of fs roots probably has changed in case | 
|  | 5882 | * some inode_item's got replayed. | 
|  | 5883 | * | 
|  | 5884 | * root->objectid_mutex is not acquired as log replay | 
|  | 5885 | * could only happen during mount. | 
|  | 5886 | */ | 
|  | 5887 | ret = btrfs_find_highest_objectid(root, | 
|  | 5888 | &root->highest_objectid); | 
|  | 5889 | } | 
|  | 5890 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5891 | key.offset = found_key.offset - 1; | 
| Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 5892 | wc.replay_dest->log_root = NULL; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5893 | free_extent_buffer(log->node); | 
| Chris Mason | b263c2c | 2009-06-11 11:24:47 -0400 | [diff] [blame] | 5894 | free_extent_buffer(log->commit_root); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5895 | kfree(log); | 
|  | 5896 |  | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5897 | if (ret) | 
|  | 5898 | goto error; | 
|  | 5899 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5900 | if (found_key.offset == 0) | 
|  | 5901 | break; | 
|  | 5902 | } | 
| David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5903 | btrfs_release_path(path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5904 |  | 
|  | 5905 | /* step one is to pin it all, step two is to replay just inodes */ | 
|  | 5906 | if (wc.pin) { | 
|  | 5907 | wc.pin = 0; | 
|  | 5908 | wc.process_func = replay_one_buffer; | 
|  | 5909 | wc.stage = LOG_WALK_REPLAY_INODES; | 
|  | 5910 | goto again; | 
|  | 5911 | } | 
|  | 5912 | /* step three is to replay everything */ | 
|  | 5913 | if (wc.stage < LOG_WALK_REPLAY_ALL) { | 
|  | 5914 | wc.stage++; | 
|  | 5915 | goto again; | 
|  | 5916 | } | 
|  | 5917 |  | 
|  | 5918 | btrfs_free_path(path); | 
|  | 5919 |  | 
| Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 5920 | /* step 4: commit the transaction, which also unpins the blocks */ | 
| Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5921 | ret = btrfs_commit_transaction(trans); | 
| Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 5922 | if (ret) | 
|  | 5923 | return ret; | 
|  | 5924 |  | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5925 | free_extent_buffer(log_root_tree->node); | 
|  | 5926 | log_root_tree->log_root = NULL; | 
| Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 5927 | clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5928 | kfree(log_root_tree); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5929 |  | 
| Josef Bacik | abefa55 | 2013-04-24 16:40:05 -0400 | [diff] [blame] | 5930 | return 0; | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5931 | error: | 
| Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5932 | if (wc.trans) | 
| Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5933 | btrfs_end_transaction(wc.trans); | 
| Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5934 | btrfs_free_path(path); | 
|  | 5935 | return ret; | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5936 | } | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5937 |  | 
|  | 5938 | /* | 
|  | 5939 | * there are some corner cases where we want to force a full | 
|  | 5940 | * commit instead of allowing a directory to be logged. | 
|  | 5941 | * | 
|  | 5942 | * They revolve around files there were unlinked from the directory, and | 
|  | 5943 | * this function updates the parent directory so that a full commit is | 
|  | 5944 | * properly done if it is fsync'd later after the unlinks are done. | 
| Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 5945 | * | 
|  | 5946 | * Must be called before the unlink operations (updates to the subvolume tree, | 
|  | 5947 | * inodes, etc) are done. | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5948 | */ | 
|  | 5949 | void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5950 | struct btrfs_inode *dir, struct btrfs_inode *inode, | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5951 | int for_rename) | 
|  | 5952 | { | 
|  | 5953 | /* | 
| Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5954 | * when we're logging a file, if it hasn't been renamed | 
|  | 5955 | * or unlinked, and its inode is fully committed on disk, | 
|  | 5956 | * we don't have to worry about walking up the directory chain | 
|  | 5957 | * to log its parents. | 
|  | 5958 | * | 
|  | 5959 | * So, we use the last_unlink_trans field to put this transid | 
|  | 5960 | * into the file.  When the file is logged we check it and | 
|  | 5961 | * don't log the parents if the file is fully on disk. | 
|  | 5962 | */ | 
| Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5963 | mutex_lock(&inode->log_mutex); | 
|  | 5964 | inode->last_unlink_trans = trans->transid; | 
|  | 5965 | mutex_unlock(&inode->log_mutex); | 
| Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 5966 |  | 
|  | 5967 | /* | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5968 | * if this directory was already logged any new | 
|  | 5969 | * names for this file/dir will get recorded | 
|  | 5970 | */ | 
|  | 5971 | smp_mb(); | 
| Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5972 | if (dir->logged_trans == trans->transid) | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5973 | return; | 
|  | 5974 |  | 
|  | 5975 | /* | 
|  | 5976 | * if the inode we're about to unlink was logged, | 
|  | 5977 | * the log will be properly updated for any new names | 
|  | 5978 | */ | 
| Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5979 | if (inode->logged_trans == trans->transid) | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5980 | return; | 
|  | 5981 |  | 
|  | 5982 | /* | 
|  | 5983 | * when renaming files across directories, if the directory | 
|  | 5984 | * there we're unlinking from gets fsync'd later on, there's | 
|  | 5985 | * no way to find the destination directory later and fsync it | 
|  | 5986 | * properly.  So, we have to be conservative and force commits | 
|  | 5987 | * so the new name gets discovered. | 
|  | 5988 | */ | 
|  | 5989 | if (for_rename) | 
|  | 5990 | goto record; | 
|  | 5991 |  | 
|  | 5992 | /* we can safely do the unlink without any special recording */ | 
|  | 5993 | return; | 
|  | 5994 |  | 
|  | 5995 | record: | 
| Nikolay Borisov | 4176bdb | 2017-01-18 00:31:28 +0200 | [diff] [blame] | 5996 | mutex_lock(&dir->log_mutex); | 
|  | 5997 | dir->last_unlink_trans = trans->transid; | 
|  | 5998 | mutex_unlock(&dir->log_mutex); | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5999 | } | 
|  | 6000 |  | 
|  | 6001 | /* | 
| Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 6002 | * Make sure that if someone attempts to fsync the parent directory of a deleted | 
|  | 6003 | * snapshot, it ends up triggering a transaction commit. This is to guarantee | 
|  | 6004 | * that after replaying the log tree of the parent directory's root we will not | 
|  | 6005 | * see the snapshot anymore and at log replay time we will not see any log tree | 
|  | 6006 | * corresponding to the deleted snapshot's root, which could lead to replaying | 
|  | 6007 | * it after replaying the log tree of the parent directory (which would replay | 
|  | 6008 | * the snapshot delete operation). | 
| Filipe Manana | 2be63d5 | 2016-02-12 11:34:23 +0000 | [diff] [blame] | 6009 | * | 
|  | 6010 | * Must be called before the actual snapshot destroy operation (updates to the | 
|  | 6011 | * parent root and tree of tree roots trees, etc) are done. | 
| Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 6012 | */ | 
|  | 6013 | void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | 4366355 | 2017-01-18 00:31:29 +0200 | [diff] [blame] | 6014 | struct btrfs_inode *dir) | 
| Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 6015 | { | 
| Nikolay Borisov | 4366355 | 2017-01-18 00:31:29 +0200 | [diff] [blame] | 6016 | mutex_lock(&dir->log_mutex); | 
|  | 6017 | dir->last_unlink_trans = trans->transid; | 
|  | 6018 | mutex_unlock(&dir->log_mutex); | 
| Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 6019 | } | 
|  | 6020 |  | 
|  | 6021 | /* | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6022 | * Call this after adding a new name for a file and it will properly | 
|  | 6023 | * update the log to reflect the new name. | 
|  | 6024 | * | 
| Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6025 | * @ctx can not be NULL when @sync_log is false, and should be NULL when it's | 
|  | 6026 | * true (because it's not used). | 
|  | 6027 | * | 
|  | 6028 | * Return value depends on whether @sync_log is true or false. | 
|  | 6029 | * When true: returns BTRFS_NEED_TRANS_COMMIT if the transaction needs to be | 
|  | 6030 | *            committed by the caller, and BTRFS_DONT_NEED_TRANS_COMMIT | 
|  | 6031 | *            otherwise. | 
|  | 6032 | * When false: returns BTRFS_DONT_NEED_LOG_SYNC if the caller does not need to | 
|  | 6033 | *             to sync the log, BTRFS_NEED_LOG_SYNC if it needs to sync the log, | 
|  | 6034 | *             or BTRFS_NEED_TRANS_COMMIT if the transaction needs to be | 
|  | 6035 | *             committed (without attempting to sync the log). | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6036 | */ | 
|  | 6037 | int btrfs_log_new_name(struct btrfs_trans_handle *trans, | 
| Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6038 | struct btrfs_inode *inode, struct btrfs_inode *old_dir, | 
| Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6039 | struct dentry *parent, | 
|  | 6040 | bool sync_log, struct btrfs_log_ctx *ctx) | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6041 | { | 
| David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 6042 | struct btrfs_fs_info *fs_info = trans->fs_info; | 
| Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6043 | int ret; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6044 |  | 
|  | 6045 | /* | 
| Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 6046 | * this will force the logging code to walk the dentry chain | 
|  | 6047 | * up for the file | 
|  | 6048 | */ | 
| Filipe Manana | 9a6509c | 2018-02-28 15:55:40 +0000 | [diff] [blame] | 6049 | if (!S_ISDIR(inode->vfs_inode.i_mode)) | 
| Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6050 | inode->last_unlink_trans = trans->transid; | 
| Chris Mason | af4176b | 2009-03-24 10:24:31 -0400 | [diff] [blame] | 6051 |  | 
|  | 6052 | /* | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6053 | * if this inode hasn't been logged and directory we're renaming it | 
|  | 6054 | * from hasn't been logged, we don't need to log it | 
|  | 6055 | */ | 
| Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6056 | if (inode->logged_trans <= fs_info->last_trans_committed && | 
|  | 6057 | (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed)) | 
| Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6058 | return sync_log ? BTRFS_DONT_NEED_TRANS_COMMIT : | 
|  | 6059 | BTRFS_DONT_NEED_LOG_SYNC; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6060 |  | 
| Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6061 | if (sync_log) { | 
|  | 6062 | struct btrfs_log_ctx ctx2; | 
|  | 6063 |  | 
|  | 6064 | btrfs_init_log_ctx(&ctx2, &inode->vfs_inode); | 
|  | 6065 | ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX, | 
|  | 6066 | LOG_INODE_EXISTS, &ctx2); | 
|  | 6067 | if (ret == BTRFS_NO_LOG_SYNC) | 
|  | 6068 | return BTRFS_DONT_NEED_TRANS_COMMIT; | 
|  | 6069 | else if (ret) | 
|  | 6070 | return BTRFS_NEED_TRANS_COMMIT; | 
|  | 6071 |  | 
|  | 6072 | ret = btrfs_sync_log(trans, inode->root, &ctx2); | 
|  | 6073 | if (ret) | 
|  | 6074 | return BTRFS_NEED_TRANS_COMMIT; | 
|  | 6075 | return BTRFS_DONT_NEED_TRANS_COMMIT; | 
|  | 6076 | } | 
|  | 6077 |  | 
|  | 6078 | ASSERT(ctx); | 
|  | 6079 | ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX, | 
|  | 6080 | LOG_INODE_EXISTS, ctx); | 
|  | 6081 | if (ret == BTRFS_NO_LOG_SYNC) | 
|  | 6082 | return BTRFS_DONT_NEED_LOG_SYNC; | 
|  | 6083 | else if (ret) | 
|  | 6084 | return BTRFS_NEED_TRANS_COMMIT; | 
|  | 6085 |  | 
|  | 6086 | return BTRFS_NEED_LOG_SYNC; | 
| Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6087 | } | 
|  | 6088 |  |