blob: 8778401665c31d016ce96e594aca6fbfb8d1624c [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Masone02119d2008-09-05 16:13:11 -04002/*
3 * Copyright (C) 2008 Oracle. All rights reserved.
Chris Masone02119d2008-09-05 16:13:11 -04004 */
5
6#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Miao Xiec6adc9c2013-05-28 10:05:39 +00008#include <linux/blkdev.h>
Josef Bacik5dc562c2012-08-17 13:14:17 -04009#include <linux/list_sort.h>
Jeff Laytonc7f88c42017-12-11 06:35:12 -050010#include <linux/iversion.h>
David Sterba602cbe92019-08-21 18:48:25 +020011#include "misc.h"
Nikolay Borisov9678c542018-01-08 11:45:05 +020012#include "ctree.h"
Miao Xie995946d2014-04-02 19:51:06 +080013#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040014#include "disk-io.h"
15#include "locking.h"
16#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070017#include "backref.h"
Anand Jainebb87652016-03-10 17:26:59 +080018#include "compression.h"
Qu Wenruodf2c95f2016-08-15 10:36:52 +080019#include "qgroup.h"
Nikolay Borisov6787bb92020-01-20 16:09:10 +020020#include "block-group.h"
21#include "space-info.h"
Naohiro Aotad35751562021-02-04 19:21:54 +090022#include "zoned.h"
Chris Masone02119d2008-09-05 16:13:11 -040023
24/* magic values for the inode_only field in btrfs_log_inode:
25 *
26 * LOG_INODE_ALL means to log everything
27 * LOG_INODE_EXISTS means to log just enough to recreate the inode
28 * during log replay
29 */
David Sterbae13976c2019-08-01 14:50:30 +020030enum {
31 LOG_INODE_ALL,
32 LOG_INODE_EXISTS,
33 LOG_OTHER_INODE,
34 LOG_OTHER_INODE_ALL,
35};
Chris Masone02119d2008-09-05 16:13:11 -040036
37/*
Chris Mason12fcfd22009-03-24 10:24:20 -040038 * directory trouble cases
39 *
40 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
41 * log, we must force a full commit before doing an fsync of the directory
42 * where the unlink was done.
43 * ---> record transid of last unlink/rename per directory
44 *
45 * mkdir foo/some_dir
46 * normal commit
47 * rename foo/some_dir foo2/some_dir
48 * mkdir foo/some_dir
49 * fsync foo/some_dir/some_file
50 *
51 * The fsync above will unlink the original some_dir without recording
52 * it in its new location (foo2). After a crash, some_dir will be gone
53 * unless the fsync of some_file forces a full commit
54 *
55 * 2) we must log any new names for any file or dir that is in the fsync
56 * log. ---> check inode while renaming/linking.
57 *
58 * 2a) we must log any new names for any file or dir during rename
59 * when the directory they are being removed from was logged.
60 * ---> check inode and old parent dir during rename
61 *
62 * 2a is actually the more important variant. With the extra logging
63 * a crash might unlink the old name without recreating the new one
64 *
65 * 3) after a crash, we must go through any directories with a link count
66 * of zero and redo the rm -rf
67 *
68 * mkdir f1/foo
69 * normal commit
70 * rm -rf f1/foo
71 * fsync(f1)
72 *
73 * The directory f1 was fully removed from the FS, but fsync was never
74 * called on f1, only its parent dir. After a crash the rm -rf must
75 * be replayed. This must be able to recurse down the entire
76 * directory tree. The inode link count fixup code takes care of the
77 * ugly details.
78 */
79
80/*
Chris Masone02119d2008-09-05 16:13:11 -040081 * stages for the tree walking. The first
82 * stage (0) is to only pin down the blocks we find
83 * the second stage (1) is to make sure that all the inodes
84 * we find in the log are created in the subvolume.
85 *
86 * The last stage is to deal with directories and links and extents
87 * and all the other fun semantics
88 */
David Sterbae13976c2019-08-01 14:50:30 +020089enum {
90 LOG_WALK_PIN_ONLY,
91 LOG_WALK_REPLAY_INODES,
92 LOG_WALK_REPLAY_DIR_INDEX,
93 LOG_WALK_REPLAY_ALL,
94};
Chris Masone02119d2008-09-05 16:13:11 -040095
Chris Mason12fcfd22009-03-24 10:24:20 -040096static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana90d04512021-09-16 11:32:10 +010097 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +010098 int inode_only,
Filipe Manana8407f552014-09-05 15:14:39 +010099 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -0500100static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
101 struct btrfs_root *root,
102 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400103static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
104 struct btrfs_root *root,
105 struct btrfs_root *log,
106 struct btrfs_path *path,
107 u64 dirid, int del_all);
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900108static void wait_log_commit(struct btrfs_root *root, int transid);
Chris Masone02119d2008-09-05 16:13:11 -0400109
110/*
111 * tree logging is a special write ahead log used to make sure that
112 * fsyncs and O_SYNCs can happen without doing full tree commits.
113 *
114 * Full tree commits are expensive because they require commonly
115 * modified blocks to be recowed, creating many dirty pages in the
116 * extent tree an 4x-6x higher write load than ext3.
117 *
118 * Instead of doing a tree commit on every fsync, we use the
119 * key ranges and transaction ids to find items for a given file or directory
120 * that have changed in this transaction. Those items are copied into
121 * a special tree (one per subvolume root), that tree is written to disk
122 * and then the fsync is considered complete.
123 *
124 * After a crash, items are copied out of the log-tree back into the
125 * subvolume tree. Any file data extents found are recorded in the extent
126 * allocation tree, and the log-tree freed.
127 *
128 * The log tree is read three times, once to pin down all the extents it is
129 * using in ram and once, once to create all the inodes logged in the tree
130 * and once to do all the other items.
131 */
132
133/*
Chris Masone02119d2008-09-05 16:13:11 -0400134 * start a sub transaction and setup the log tree
135 * this increments the log tree writer count to make the people
136 * syncing the tree wait for us to finish
137 */
138static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800139 struct btrfs_root *root,
140 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400141{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400142 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana47876f72020-11-25 12:19:28 +0000143 struct btrfs_root *tree_root = fs_info->tree_root;
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900144 const bool zoned = btrfs_is_zoned(fs_info);
Zhaolei34eb2a52015-08-17 18:44:45 +0800145 int ret = 0;
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900146 bool created = false;
Yan Zheng7237f182009-01-21 12:54:03 -0500147
Filipe Manana47876f72020-11-25 12:19:28 +0000148 /*
149 * First check if the log root tree was already created. If not, create
150 * it before locking the root's log_mutex, just to keep lockdep happy.
151 */
152 if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state)) {
153 mutex_lock(&tree_root->log_mutex);
154 if (!fs_info->log_root_tree) {
155 ret = btrfs_init_log_root_tree(trans, fs_info);
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900156 if (!ret) {
Filipe Manana47876f72020-11-25 12:19:28 +0000157 set_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state);
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900158 created = true;
159 }
Filipe Manana47876f72020-11-25 12:19:28 +0000160 }
161 mutex_unlock(&tree_root->log_mutex);
162 if (ret)
163 return ret;
164 }
165
Yan Zheng7237f182009-01-21 12:54:03 -0500166 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800167
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900168again:
Yan Zheng7237f182009-01-21 12:54:03 -0500169 if (root->log_root) {
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900170 int index = (root->log_transid + 1) % 2;
171
David Sterba4884b8e2019-03-20 13:25:34 +0100172 if (btrfs_need_log_full_commit(trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800173 ret = -EAGAIN;
174 goto out;
175 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800176
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900177 if (zoned && atomic_read(&root->log_commit[index])) {
178 wait_log_commit(root, root->log_transid - 1);
179 goto again;
180 }
181
Josef Bacikff782e02009-10-08 15:30:04 -0400182 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800183 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800184 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400185 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800186 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400187 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800188 } else {
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900189 /*
190 * This means fs_info->log_root_tree was already created
191 * for some other FS trees. Do the full commit not to mix
192 * nodes from multiple log transactions to do sequential
193 * writing.
194 */
195 if (zoned && !created) {
196 ret = -EAGAIN;
197 goto out;
198 }
199
Chris Masone02119d2008-09-05 16:13:11 -0400200 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400201 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800202 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800203
Filipe Mananae7a79812020-06-15 10:38:44 +0100204 set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800205 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
206 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400207 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800208
Yan Zheng7237f182009-01-21 12:54:03 -0500209 atomic_inc(&root->log_writers);
Filipe Manana289cffc2021-08-31 15:30:32 +0100210 if (!ctx->logging_new_name) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800211 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800212 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800213 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800214 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800215
Miao Xiee87ac132014-02-20 18:08:53 +0800216out:
Yan Zheng7237f182009-01-21 12:54:03 -0500217 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800218 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400219}
220
221/*
222 * returns 0 if there was a log transaction running and we were able
223 * to join, or returns -ENOENT if there were not transactions
224 * in progress
225 */
226static int join_running_log_trans(struct btrfs_root *root)
227{
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900228 const bool zoned = btrfs_is_zoned(root->fs_info);
Chris Masone02119d2008-09-05 16:13:11 -0400229 int ret = -ENOENT;
230
Filipe Mananae7a79812020-06-15 10:38:44 +0100231 if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
232 return ret;
233
Yan Zheng7237f182009-01-21 12:54:03 -0500234 mutex_lock(&root->log_mutex);
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900235again:
Chris Masone02119d2008-09-05 16:13:11 -0400236 if (root->log_root) {
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900237 int index = (root->log_transid + 1) % 2;
238
Chris Masone02119d2008-09-05 16:13:11 -0400239 ret = 0;
Naohiro Aotafa1a0f42021-02-04 19:22:19 +0900240 if (zoned && atomic_read(&root->log_commit[index])) {
241 wait_log_commit(root, root->log_transid - 1);
242 goto again;
243 }
Yan Zheng7237f182009-01-21 12:54:03 -0500244 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400245 }
Yan Zheng7237f182009-01-21 12:54:03 -0500246 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400247 return ret;
248}
249
250/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400251 * This either makes the current running log transaction wait
252 * until you call btrfs_end_log_trans() or it makes any future
253 * log transactions wait until you call btrfs_end_log_trans()
254 */
zhong jiang45128b02018-08-17 00:37:15 +0800255void btrfs_pin_log_trans(struct btrfs_root *root)
Chris Mason12fcfd22009-03-24 10:24:20 -0400256{
Chris Mason12fcfd22009-03-24 10:24:20 -0400257 atomic_inc(&root->log_writers);
Chris Mason12fcfd22009-03-24 10:24:20 -0400258}
259
260/*
Chris Masone02119d2008-09-05 16:13:11 -0400261 * indicate we're done making changes to the log tree
262 * and wake up anyone waiting to do a sync
263 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100264void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400265{
Yan Zheng7237f182009-01-21 12:54:03 -0500266 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +0100267 /* atomic_dec_and_test implies a barrier */
268 cond_wake_up_nomb(&root->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -0500269 }
Chris Masone02119d2008-09-05 16:13:11 -0400270}
271
David Sterba247462a2019-03-21 20:21:05 +0100272static int btrfs_write_tree_block(struct extent_buffer *buf)
273{
274 return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start,
275 buf->start + buf->len - 1);
276}
277
278static void btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
279{
280 filemap_fdatawait_range(buf->pages[0]->mapping,
281 buf->start, buf->start + buf->len - 1);
282}
Chris Masone02119d2008-09-05 16:13:11 -0400283
284/*
285 * the walk control struct is used to pass state down the chain when
286 * processing the log tree. The stage field tells us which part
287 * of the log tree processing we are currently doing. The others
288 * are state fields used for that specific part
289 */
290struct walk_control {
291 /* should we free the extent on disk when done? This is used
292 * at transaction commit time while freeing a log tree
293 */
294 int free;
295
296 /* should we write out the extent buffer? This is used
297 * while flushing the log tree to disk during a sync
298 */
299 int write;
300
301 /* should we wait for the extent buffer io to finish? Also used
302 * while flushing the log tree to disk for a sync
303 */
304 int wait;
305
306 /* pin only walk, we record which extents on disk belong to the
307 * log trees
308 */
309 int pin;
310
311 /* what stage of the replay code we're currently in */
312 int stage;
313
Filipe Mananaf2d72f42018-10-08 11:12:55 +0100314 /*
315 * Ignore any items from the inode currently being processed. Needs
316 * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
317 * the LOG_WALK_REPLAY_INODES stage.
318 */
319 bool ignore_cur_inode;
320
Chris Masone02119d2008-09-05 16:13:11 -0400321 /* the root we are currently replaying */
322 struct btrfs_root *replay_dest;
323
324 /* the trans handle for the current replay */
325 struct btrfs_trans_handle *trans;
326
327 /* the function that gets used to process blocks we find in the
328 * tree. Note the extent_buffer might not be up to date when it is
329 * passed in, and it must be checked or read if you need the data
330 * inside it
331 */
332 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800333 struct walk_control *wc, u64 gen, int level);
Chris Masone02119d2008-09-05 16:13:11 -0400334};
335
336/*
337 * process_func used to pin down extents, write them or wait on them
338 */
339static int process_one_buffer(struct btrfs_root *log,
340 struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800341 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -0400342{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400343 struct btrfs_fs_info *fs_info = log->fs_info;
Josef Bacikb50c6e22013-04-25 15:55:30 -0400344 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400345
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400346 /*
347 * If this fs is mixed then we need to be able to process the leaves to
348 * pin down any logged extents, so we have to read the block.
349 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400350 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Qu Wenruo581c1762018-03-29 09:08:11 +0800351 ret = btrfs_read_buffer(eb, gen, level, NULL);
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400352 if (ret)
353 return ret;
354 }
355
Josef Bacikb50c6e22013-04-25 15:55:30 -0400356 if (wc->pin)
Nikolay Borisov9fce5702020-01-20 16:09:13 +0200357 ret = btrfs_pin_extent_for_log_replay(wc->trans, eb->start,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400358 eb->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400359
360 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400361 if (wc->pin && btrfs_header_level(eb) == 0)
David Sterbabcdc4282019-03-20 12:14:33 +0100362 ret = btrfs_exclude_logged_extents(eb);
Chris Masone02119d2008-09-05 16:13:11 -0400363 if (wc->write)
364 btrfs_write_tree_block(eb);
365 if (wc->wait)
366 btrfs_wait_tree_block_writeback(eb);
367 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400368 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400369}
370
Filipe Manana086dcbf2021-09-16 11:32:13 +0100371static int do_overwrite_item(struct btrfs_trans_handle *trans,
372 struct btrfs_root *root,
373 struct btrfs_path *path,
374 struct extent_buffer *eb, int slot,
375 struct btrfs_key *key)
Chris Masone02119d2008-09-05 16:13:11 -0400376{
377 int ret;
378 u32 item_size;
379 u64 saved_i_size = 0;
380 int save_old_i_size = 0;
381 unsigned long src_ptr;
382 unsigned long dst_ptr;
383 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000384 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400385
386 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
387 overwrite_root = 1;
388
389 item_size = btrfs_item_size_nr(eb, slot);
390 src_ptr = btrfs_item_ptr_offset(eb, slot);
391
Filipe Manana086dcbf2021-09-16 11:32:13 +0100392 /* Our caller must have done a search for the key for us. */
393 ASSERT(path->nodes[0] != NULL);
394
395 /*
396 * And the slot must point to the exact key or the slot where the key
397 * should be at (the first item with a key greater than 'key')
398 */
399 if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
400 struct btrfs_key found_key;
401
402 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
403 ret = btrfs_comp_cpu_keys(&found_key, key);
404 ASSERT(ret >= 0);
405 } else {
406 ret = 1;
407 }
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000408
Chris Masone02119d2008-09-05 16:13:11 -0400409 if (ret == 0) {
410 char *src_copy;
411 char *dst_copy;
412 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
413 path->slots[0]);
414 if (dst_size != item_size)
415 goto insert;
416
417 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200418 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400419 return 0;
420 }
421 dst_copy = kmalloc(item_size, GFP_NOFS);
422 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000423 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200424 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000425 kfree(dst_copy);
426 kfree(src_copy);
427 return -ENOMEM;
428 }
Chris Masone02119d2008-09-05 16:13:11 -0400429
430 read_extent_buffer(eb, src_copy, src_ptr, item_size);
431
432 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
433 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
434 item_size);
435 ret = memcmp(dst_copy, src_copy, item_size);
436
437 kfree(dst_copy);
438 kfree(src_copy);
439 /*
440 * they have the same contents, just return, this saves
441 * us from cowing blocks in the destination tree and doing
442 * extra writes that may not have been done by a previous
443 * sync
444 */
445 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200446 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400447 return 0;
448 }
449
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000450 /*
451 * We need to load the old nbytes into the inode so when we
452 * replay the extents we've logged we get the right nbytes.
453 */
454 if (inode_item) {
455 struct btrfs_inode_item *item;
456 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400457 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000458
459 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
460 struct btrfs_inode_item);
461 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
462 item = btrfs_item_ptr(eb, slot,
463 struct btrfs_inode_item);
464 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400465
466 /*
467 * If this is a directory we need to reset the i_size to
468 * 0 so that we can set it up properly when replaying
469 * the rest of the items in this log.
470 */
471 mode = btrfs_inode_mode(eb, item);
472 if (S_ISDIR(mode))
473 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000474 }
475 } else if (inode_item) {
476 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400477 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000478
479 /*
480 * New inode, set nbytes to 0 so that the nbytes comes out
481 * properly when we replay the extents.
482 */
483 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
484 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400485
486 /*
487 * If this is a directory we need to reset the i_size to 0 so
488 * that we can set it up properly when replaying the rest of
489 * the items in this log.
490 */
491 mode = btrfs_inode_mode(eb, item);
492 if (S_ISDIR(mode))
493 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400494 }
495insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200496 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400497 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000498 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400499 ret = btrfs_insert_empty_item(trans, root, path,
500 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000501 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400502
503 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000504 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400505 u32 found_size;
506 found_size = btrfs_item_size_nr(path->nodes[0],
507 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100508 if (found_size > item_size)
David Sterba78ac4f92019-03-20 14:49:12 +0100509 btrfs_truncate_item(path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100510 else if (found_size < item_size)
David Sterbac71dd882019-03-20 14:51:10 +0100511 btrfs_extend_item(path, item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400512 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400513 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400514 }
515 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
516 path->slots[0]);
517
518 /* don't overwrite an existing inode if the generation number
519 * was logged as zero. This is done when the tree logging code
520 * is just logging an inode to make sure it exists after recovery.
521 *
522 * Also, don't overwrite i_size on directories during replay.
523 * log replay inserts and removes directory items based on the
524 * state of the tree found in the subvolume, and i_size is modified
525 * as it goes
526 */
527 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
528 struct btrfs_inode_item *src_item;
529 struct btrfs_inode_item *dst_item;
530
531 src_item = (struct btrfs_inode_item *)src_ptr;
532 dst_item = (struct btrfs_inode_item *)dst_ptr;
533
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000534 if (btrfs_inode_generation(eb, src_item) == 0) {
535 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000536 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000537
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000538 /*
539 * For regular files an ino_size == 0 is used only when
540 * logging that an inode exists, as part of a directory
541 * fsync, and the inode wasn't fsynced before. In this
542 * case don't set the size of the inode in the fs/subvol
543 * tree, otherwise we would be throwing valid data away.
544 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000545 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000546 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
David Sterba60d48e22020-04-29 15:29:53 +0200547 ino_size != 0)
548 btrfs_set_inode_size(dst_eb, dst_item, ino_size);
Chris Masone02119d2008-09-05 16:13:11 -0400549 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000550 }
Chris Masone02119d2008-09-05 16:13:11 -0400551
552 if (overwrite_root &&
553 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
554 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
555 save_old_i_size = 1;
556 saved_i_size = btrfs_inode_size(path->nodes[0],
557 dst_item);
558 }
559 }
560
561 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
562 src_ptr, item_size);
563
564 if (save_old_i_size) {
565 struct btrfs_inode_item *dst_item;
566 dst_item = (struct btrfs_inode_item *)dst_ptr;
567 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
568 }
569
570 /* make sure the generation is filled in */
571 if (key->type == BTRFS_INODE_ITEM_KEY) {
572 struct btrfs_inode_item *dst_item;
573 dst_item = (struct btrfs_inode_item *)dst_ptr;
574 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
575 btrfs_set_inode_generation(path->nodes[0], dst_item,
576 trans->transid);
577 }
578 }
579no_copy:
580 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200581 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400582 return 0;
583}
584
585/*
Filipe Manana086dcbf2021-09-16 11:32:13 +0100586 * Item overwrite used by replay and tree logging. eb, slot and key all refer
587 * to the src data we are copying out.
588 *
589 * root is the tree we are copying into, and path is a scratch
590 * path for use in this function (it should be released on entry and
591 * will be released on exit).
592 *
593 * If the key is already in the destination tree the existing item is
594 * overwritten. If the existing item isn't big enough, it is extended.
595 * If it is too large, it is truncated.
596 *
597 * If the key isn't in the destination yet, a new item is inserted.
598 */
599static int overwrite_item(struct btrfs_trans_handle *trans,
600 struct btrfs_root *root,
601 struct btrfs_path *path,
602 struct extent_buffer *eb, int slot,
603 struct btrfs_key *key)
604{
605 int ret;
606
607 /* Look for the key in the destination tree. */
608 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
609 if (ret < 0)
610 return ret;
611
612 return do_overwrite_item(trans, root, path, eb, slot, key);
613}
614
615/*
Chris Masone02119d2008-09-05 16:13:11 -0400616 * simple helper to read an inode off the disk from a given root
617 * This can only be called for subvolume roots and not for the log
618 */
619static noinline struct inode *read_one_inode(struct btrfs_root *root,
620 u64 objectid)
621{
622 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400623
David Sterba0202e832020-05-15 19:35:59 +0200624 inode = btrfs_iget(root->fs_info->sb, objectid, root);
Al Viro2e19f1f2018-07-29 23:04:45 +0100625 if (IS_ERR(inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400626 inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -0400627 return inode;
628}
629
630/* replays a single extent in 'eb' at 'slot' with 'key' into the
631 * subvolume 'root'. path is released on entry and should be released
632 * on exit.
633 *
634 * extents in the log tree have not been allocated out of the extent
635 * tree yet. So, this completes the allocation, taking a reference
636 * as required if the extent already exists or creating a new extent
637 * if it isn't in the extent allocation tree yet.
638 *
639 * The extent is inserted into the file, dropping any existing extents
640 * from the file that overlap the new one.
641 */
642static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
643 struct btrfs_root *root,
644 struct btrfs_path *path,
645 struct extent_buffer *eb, int slot,
646 struct btrfs_key *key)
647{
Filipe Manana5893dfb2020-11-04 11:07:32 +0000648 struct btrfs_drop_extents_args drop_args = { 0 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400649 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400650 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400651 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400652 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000653 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400654 struct btrfs_file_extent_item *item;
655 struct inode *inode = NULL;
656 unsigned long size;
657 int ret = 0;
658
659 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
660 found_type = btrfs_file_extent_type(eb, item);
661
Yan Zhengd899e052008-10-30 14:25:28 -0400662 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000663 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
664 nbytes = btrfs_file_extent_num_bytes(eb, item);
665 extent_end = start + nbytes;
666
667 /*
668 * We don't add to the inodes nbytes if we are prealloc or a
669 * hole.
670 */
671 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
672 nbytes = 0;
673 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +0800674 size = btrfs_file_extent_ram_bytes(eb, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000675 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400676 extent_end = ALIGN(start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400677 fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400678 } else {
679 ret = 0;
680 goto out;
681 }
682
683 inode = read_one_inode(root, key->objectid);
684 if (!inode) {
685 ret = -EIO;
686 goto out;
687 }
688
689 /*
690 * first check to see if we already have this extent in the
691 * file. This must be done before the btrfs_drop_extents run
692 * so we don't try to drop this extent.
693 */
David Sterbaf85b7372017-01-20 14:54:07 +0100694 ret = btrfs_lookup_file_extent(trans, root, path,
695 btrfs_ino(BTRFS_I(inode)), start, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400696
Yan Zhengd899e052008-10-30 14:25:28 -0400697 if (ret == 0 &&
698 (found_type == BTRFS_FILE_EXTENT_REG ||
699 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400700 struct btrfs_file_extent_item cmp1;
701 struct btrfs_file_extent_item cmp2;
702 struct btrfs_file_extent_item *existing;
703 struct extent_buffer *leaf;
704
705 leaf = path->nodes[0];
706 existing = btrfs_item_ptr(leaf, path->slots[0],
707 struct btrfs_file_extent_item);
708
709 read_extent_buffer(eb, &cmp1, (unsigned long)item,
710 sizeof(cmp1));
711 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
712 sizeof(cmp2));
713
714 /*
715 * we already have a pointer to this exact extent,
716 * we don't have to do anything
717 */
718 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200719 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400720 goto out;
721 }
722 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200723 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400724
725 /* drop any overlapping extents */
Filipe Manana5893dfb2020-11-04 11:07:32 +0000726 drop_args.start = start;
727 drop_args.end = extent_end;
728 drop_args.drop_cache = true;
729 ret = btrfs_drop_extents(trans, root, BTRFS_I(inode), &drop_args);
Josef Bacik36508602013-04-25 16:23:32 -0400730 if (ret)
731 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400732
Yan Zheng07d400a2009-01-06 11:42:00 -0500733 if (found_type == BTRFS_FILE_EXTENT_REG ||
734 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400735 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500736 unsigned long dest_offset;
737 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400738
Filipe Manana3168021c2017-02-01 14:58:02 +0000739 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
740 btrfs_fs_incompat(fs_info, NO_HOLES))
741 goto update_inode;
742
Yan Zheng07d400a2009-01-06 11:42:00 -0500743 ret = btrfs_insert_empty_item(trans, root, path, key,
744 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400745 if (ret)
746 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500747 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
748 path->slots[0]);
749 copy_extent_buffer(path->nodes[0], eb, dest_offset,
750 (unsigned long)item, sizeof(*item));
751
752 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
753 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
754 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400755 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500756
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800757 /*
758 * Manually record dirty extent, as here we did a shallow
759 * file extent item copy and skip normal backref update,
760 * but modifying extent tree all by ourselves.
761 * So need to manually record dirty extent for qgroup,
762 * as the owner of the file extent changed from log tree
763 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
764 */
Lu Fengqia95f3aa2018-07-18 16:28:03 +0800765 ret = btrfs_qgroup_trace_extent(trans,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800766 btrfs_file_extent_disk_bytenr(eb, item),
767 btrfs_file_extent_disk_num_bytes(eb, item),
768 GFP_NOFS);
769 if (ret < 0)
770 goto out;
771
Yan Zheng07d400a2009-01-06 11:42:00 -0500772 if (ins.objectid > 0) {
Qu Wenruo82fa1132019-04-04 14:45:35 +0800773 struct btrfs_ref ref = { 0 };
Yan Zheng07d400a2009-01-06 11:42:00 -0500774 u64 csum_start;
775 u64 csum_end;
776 LIST_HEAD(ordered_sums);
Qu Wenruo82fa1132019-04-04 14:45:35 +0800777
Yan Zheng07d400a2009-01-06 11:42:00 -0500778 /*
779 * is this extent already allocated in the extent
780 * allocation tree? If so, just add a reference
781 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400782 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500783 ins.offset);
Marcos Paulo de Souza37361272021-08-02 09:34:00 -0300784 if (ret < 0) {
785 goto out;
786 } else if (ret == 0) {
Qu Wenruo82fa1132019-04-04 14:45:35 +0800787 btrfs_init_generic_ref(&ref,
788 BTRFS_ADD_DELAYED_REF,
789 ins.objectid, ins.offset, 0);
790 btrfs_init_data_ref(&ref,
791 root->root_key.objectid,
Nikolay Borisovf42c5da2021-10-12 11:21:35 +0300792 key->objectid, offset, 0, false);
Qu Wenruo82fa1132019-04-04 14:45:35 +0800793 ret = btrfs_inc_extent_ref(trans, &ref);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400794 if (ret)
795 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500796 } else {
797 /*
798 * insert the extent pointer in the extent
799 * allocation tree
800 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400801 ret = btrfs_alloc_logged_file_extent(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400802 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400803 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400804 if (ret)
805 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500806 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200807 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500808
809 if (btrfs_file_extent_compression(eb, item)) {
810 csum_start = ins.objectid;
811 csum_end = csum_start + ins.offset;
812 } else {
813 csum_start = ins.objectid +
814 btrfs_file_extent_offset(eb, item);
815 csum_end = csum_start +
816 btrfs_file_extent_num_bytes(eb, item);
817 }
818
819 ret = btrfs_lookup_csums_range(root->log_root,
820 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100821 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400822 if (ret)
823 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100824 /*
825 * Now delete all existing cums in the csum root that
826 * cover our range. We do this because we can have an
827 * extent that is completely referenced by one file
828 * extent item and partially referenced by another
829 * file extent item (like after using the clone or
830 * extent_same ioctls). In this case if we end up doing
831 * the replay of the one that partially references the
832 * extent first, and we do not do the csum deletion
833 * below, we can get 2 csum items in the csum tree that
834 * overlap each other. For example, imagine our log has
835 * the two following file extent items:
836 *
837 * key (257 EXTENT_DATA 409600)
838 * extent data disk byte 12845056 nr 102400
839 * extent data offset 20480 nr 20480 ram 102400
840 *
841 * key (257 EXTENT_DATA 819200)
842 * extent data disk byte 12845056 nr 102400
843 * extent data offset 0 nr 102400 ram 102400
844 *
845 * Where the second one fully references the 100K extent
846 * that starts at disk byte 12845056, and the log tree
847 * has a single csum item that covers the entire range
848 * of the extent:
849 *
850 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
851 *
852 * After the first file extent item is replayed, the
853 * csum tree gets the following csum item:
854 *
855 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
856 *
857 * Which covers the 20K sub-range starting at offset 20K
858 * of our extent. Now when we replay the second file
859 * extent item, if we do not delete existing csum items
860 * that cover any of its blocks, we end up getting two
861 * csum items in our csum tree that overlap each other:
862 *
863 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
864 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
865 *
866 * Which is a problem, because after this anyone trying
867 * to lookup up for the checksum of any block of our
868 * extent starting at an offset of 40K or higher, will
869 * end up looking at the second csum item only, which
870 * does not contain the checksum for any block starting
871 * at offset 40K or higher of our extent.
872 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500873 while (!list_empty(&ordered_sums)) {
874 struct btrfs_ordered_sum *sums;
875 sums = list_entry(ordered_sums.next,
876 struct btrfs_ordered_sum,
877 list);
Josef Bacik36508602013-04-25 16:23:32 -0400878 if (!ret)
Filipe Manana40e046a2019-12-05 16:58:30 +0000879 ret = btrfs_del_csums(trans,
880 fs_info->csum_root,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400881 sums->bytenr,
882 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100883 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400884 ret = btrfs_csum_file_blocks(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400885 fs_info->csum_root, sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500886 list_del(&sums->list);
887 kfree(sums);
888 }
Josef Bacik36508602013-04-25 16:23:32 -0400889 if (ret)
890 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500891 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200892 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500893 }
894 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
895 /* inline extents are easy, we just overwrite them */
896 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400897 if (ret)
898 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500899 }
900
Josef Bacik9ddc9592020-01-17 09:02:22 -0500901 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start,
902 extent_end - start);
903 if (ret)
904 goto out;
905
Filipe Manana3168021c2017-02-01 14:58:02 +0000906update_inode:
Filipe Manana2766ff62020-11-04 11:07:34 +0000907 btrfs_update_inode_bytes(BTRFS_I(inode), nbytes, drop_args.bytes_found);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200908 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masone02119d2008-09-05 16:13:11 -0400909out:
910 if (inode)
911 iput(inode);
912 return ret;
913}
914
915/*
916 * when cleaning up conflicts between the directory names in the
917 * subvolume, directory names in the log and directory names in the
918 * inode back references, we may have to unlink inodes from directories.
919 *
920 * This is a helper function to do the unlink of a specific directory
921 * item
922 */
923static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -0400924 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200925 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400926 struct btrfs_dir_item *di)
927{
Filipe Manana9798ba22021-10-25 17:31:49 +0100928 struct btrfs_root *root = dir->root;
Chris Masone02119d2008-09-05 16:13:11 -0400929 struct inode *inode;
930 char *name;
931 int name_len;
932 struct extent_buffer *leaf;
933 struct btrfs_key location;
934 int ret;
935
936 leaf = path->nodes[0];
937
938 btrfs_dir_item_key_to_cpu(leaf, di, &location);
939 name_len = btrfs_dir_name_len(leaf, di);
940 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000941 if (!name)
942 return -ENOMEM;
943
Chris Masone02119d2008-09-05 16:13:11 -0400944 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200945 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400946
947 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000948 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400949 ret = -EIO;
950 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000951 }
Chris Masone02119d2008-09-05 16:13:11 -0400952
Yan Zhengec051c02009-01-05 15:43:42 -0500953 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400954 if (ret)
955 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400956
Filipe Manana4467af82021-10-25 17:31:50 +0100957 ret = btrfs_unlink_inode(trans, dir, BTRFS_I(inode), name,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200958 name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400959 if (ret)
960 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100961 else
Nikolay Borisove5c304e62018-02-07 17:55:43 +0200962 ret = btrfs_run_delayed_items(trans);
Josef Bacik36508602013-04-25 16:23:32 -0400963out:
964 kfree(name);
965 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400966 return ret;
967}
968
969/*
Filipe Manana77a5b9e2021-10-01 13:52:30 +0100970 * See if a given name and sequence number found in an inode back reference are
971 * already in a directory and correctly point to this inode.
972 *
973 * Returns: < 0 on error, 0 if the directory entry does not exists and 1 if it
974 * exists.
Chris Masone02119d2008-09-05 16:13:11 -0400975 */
976static noinline int inode_in_dir(struct btrfs_root *root,
977 struct btrfs_path *path,
978 u64 dirid, u64 objectid, u64 index,
979 const char *name, int name_len)
980{
981 struct btrfs_dir_item *di;
982 struct btrfs_key location;
Filipe Manana77a5b9e2021-10-01 13:52:30 +0100983 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400984
985 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
986 index, name, name_len, 0);
Filipe Manana77a5b9e2021-10-01 13:52:30 +0100987 if (IS_ERR(di)) {
Filipe Manana8dcbc262021-10-01 13:52:33 +0100988 ret = PTR_ERR(di);
Filipe Manana77a5b9e2021-10-01 13:52:30 +0100989 goto out;
990 } else if (di) {
Chris Masone02119d2008-09-05 16:13:11 -0400991 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
992 if (location.objectid != objectid)
993 goto out;
Filipe Manana77a5b9e2021-10-01 13:52:30 +0100994 } else {
Chris Masone02119d2008-09-05 16:13:11 -0400995 goto out;
Filipe Manana77a5b9e2021-10-01 13:52:30 +0100996 }
Chris Masone02119d2008-09-05 16:13:11 -0400997
Filipe Manana77a5b9e2021-10-01 13:52:30 +0100998 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400999 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
Filipe Manana77a5b9e2021-10-01 13:52:30 +01001000 if (IS_ERR(di)) {
1001 ret = PTR_ERR(di);
Chris Masone02119d2008-09-05 16:13:11 -04001002 goto out;
Filipe Manana77a5b9e2021-10-01 13:52:30 +01001003 } else if (di) {
1004 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
1005 if (location.objectid == objectid)
1006 ret = 1;
1007 }
Chris Masone02119d2008-09-05 16:13:11 -04001008out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001009 btrfs_release_path(path);
Filipe Manana77a5b9e2021-10-01 13:52:30 +01001010 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001011}
1012
1013/*
1014 * helper function to check a log tree for a named back reference in
1015 * an inode. This is used to decide if a back reference that is
1016 * found in the subvolume conflicts with what we find in the log.
1017 *
1018 * inode backreferences may have multiple refs in a single item,
1019 * during replay we process one reference at a time, and we don't
1020 * want to delete valid links to a file from the subvolume if that
1021 * link is also in the log.
1022 */
1023static noinline int backref_in_log(struct btrfs_root *log,
1024 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -07001025 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +00001026 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -04001027{
1028 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04001029 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04001030
1031 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +00001032 if (!path)
1033 return -ENOMEM;
1034
Chris Masone02119d2008-09-05 16:13:11 -04001035 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
Nikolay Borisovd3316c82019-09-25 14:03:03 +03001036 if (ret < 0) {
1037 goto out;
1038 } else if (ret == 1) {
Nikolay Borisov89cbf5f6b2019-08-30 17:44:47 +03001039 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001040 goto out;
Nikolay Borisov89cbf5f6b2019-08-30 17:44:47 +03001041 }
Chris Masone02119d2008-09-05 16:13:11 -04001042
Nikolay Borisov89cbf5f6b2019-08-30 17:44:47 +03001043 if (key->type == BTRFS_INODE_EXTREF_KEY)
1044 ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
1045 path->slots[0],
1046 ref_objectid,
1047 name, namelen);
1048 else
1049 ret = !!btrfs_find_name_in_backref(path->nodes[0],
Filipe Manana1f250e92018-02-28 15:56:10 +00001050 path->slots[0],
Nikolay Borisov89cbf5f6b2019-08-30 17:44:47 +03001051 name, namelen);
Chris Masone02119d2008-09-05 16:13:11 -04001052out:
1053 btrfs_free_path(path);
Nikolay Borisov89cbf5f6b2019-08-30 17:44:47 +03001054 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001055}
1056
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001057static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
1058 struct btrfs_root *root,
1059 struct btrfs_path *path,
1060 struct btrfs_root *log_root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001061 struct btrfs_inode *dir,
1062 struct btrfs_inode *inode,
Mark Fashehf1863732012-08-08 11:32:27 -07001063 u64 inode_objectid, u64 parent_objectid,
1064 u64 ref_index, char *name, int namelen,
1065 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001066{
1067 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001068 char *victim_name;
1069 int victim_name_len;
1070 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001071 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -07001072 struct btrfs_key search_key;
1073 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001074
Mark Fashehf1863732012-08-08 11:32:27 -07001075again:
1076 /* Search old style refs */
1077 search_key.objectid = inode_objectid;
1078 search_key.type = BTRFS_INODE_REF_KEY;
1079 search_key.offset = parent_objectid;
1080 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001081 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001082 struct btrfs_inode_ref *victim_ref;
1083 unsigned long ptr;
1084 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001085
1086 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001087
1088 /* are we trying to overwrite a back ref for the root directory
1089 * if so, just jump out, we're done
1090 */
Mark Fashehf1863732012-08-08 11:32:27 -07001091 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001092 return 1;
1093
1094 /* check all the names in this back reference to see
1095 * if they are in the log. if so, we allow them to stay
1096 * otherwise they must be unlinked as a conflict
1097 */
1098 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1099 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1100 while (ptr < ptr_end) {
1101 victim_ref = (struct btrfs_inode_ref *)ptr;
1102 victim_name_len = btrfs_inode_ref_name_len(leaf,
1103 victim_ref);
1104 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001105 if (!victim_name)
1106 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001107
1108 read_extent_buffer(leaf, victim_name,
1109 (unsigned long)(victim_ref + 1),
1110 victim_name_len);
1111
Nikolay Borisovd3316c82019-09-25 14:03:03 +03001112 ret = backref_in_log(log_root, &search_key,
1113 parent_objectid, victim_name,
1114 victim_name_len);
1115 if (ret < 0) {
1116 kfree(victim_name);
1117 return ret;
1118 } else if (!ret) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001119 inc_nlink(&inode->vfs_inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001120 btrfs_release_path(path);
1121
Filipe Manana4467af82021-10-25 17:31:50 +01001122 ret = btrfs_unlink_inode(trans, dir, inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001123 victim_name, victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001124 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001125 if (ret)
1126 return ret;
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001127 ret = btrfs_run_delayed_items(trans);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001128 if (ret)
1129 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001130 *search_done = 1;
1131 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001132 }
1133 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001134
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001135 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1136 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001137
1138 /*
1139 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001140 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001141 */
1142 *search_done = 1;
1143 }
1144 btrfs_release_path(path);
1145
Mark Fashehf1863732012-08-08 11:32:27 -07001146 /* Same search but for extended refs */
1147 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1148 inode_objectid, parent_objectid, 0,
1149 0);
1150 if (!IS_ERR_OR_NULL(extref)) {
1151 u32 item_size;
1152 u32 cur_offset = 0;
1153 unsigned long base;
1154 struct inode *victim_parent;
1155
1156 leaf = path->nodes[0];
1157
1158 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1159 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1160
1161 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001162 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001163
1164 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1165
1166 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1167 goto next;
1168
1169 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001170 if (!victim_name)
1171 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001172 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1173 victim_name_len);
1174
1175 search_key.objectid = inode_objectid;
1176 search_key.type = BTRFS_INODE_EXTREF_KEY;
1177 search_key.offset = btrfs_extref_hash(parent_objectid,
1178 victim_name,
1179 victim_name_len);
Nikolay Borisovd3316c82019-09-25 14:03:03 +03001180 ret = backref_in_log(log_root, &search_key,
1181 parent_objectid, victim_name,
1182 victim_name_len);
1183 if (ret < 0) {
Jianglei Nief35838a2021-12-09 14:56:31 +08001184 kfree(victim_name);
Nikolay Borisovd3316c82019-09-25 14:03:03 +03001185 return ret;
1186 } else if (!ret) {
Mark Fashehf1863732012-08-08 11:32:27 -07001187 ret = -ENOENT;
1188 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001189 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001190 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001191 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001192 btrfs_release_path(path);
1193
Filipe Manana4467af82021-10-25 17:31:50 +01001194 ret = btrfs_unlink_inode(trans,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001195 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001196 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001197 victim_name,
1198 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001199 if (!ret)
1200 ret = btrfs_run_delayed_items(
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001201 trans);
Mark Fashehf1863732012-08-08 11:32:27 -07001202 }
Mark Fashehf1863732012-08-08 11:32:27 -07001203 iput(victim_parent);
1204 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001205 if (ret)
1206 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001207 *search_done = 1;
1208 goto again;
1209 }
1210 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001211next:
1212 cur_offset += victim_name_len + sizeof(*extref);
1213 }
1214 *search_done = 1;
1215 }
1216 btrfs_release_path(path);
1217
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001218 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001219 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001220 ref_index, name, namelen, 0);
Filipe Manana52db7772021-10-01 13:52:32 +01001221 if (IS_ERR(di)) {
Filipe Manana8dcbc262021-10-01 13:52:33 +01001222 return PTR_ERR(di);
Filipe Manana52db7772021-10-01 13:52:32 +01001223 } else if (di) {
Filipe Manana9798ba22021-10-25 17:31:49 +01001224 ret = drop_one_dir_item(trans, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001225 if (ret)
1226 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001227 }
1228 btrfs_release_path(path);
1229
Andrea Gelmini52042d82018-11-28 12:05:13 +01001230 /* look for a conflicting name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001231 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001232 name, namelen, 0);
Filipe Manana52db7772021-10-01 13:52:32 +01001233 if (IS_ERR(di)) {
1234 return PTR_ERR(di);
1235 } else if (di) {
Filipe Manana9798ba22021-10-25 17:31:49 +01001236 ret = drop_one_dir_item(trans, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001237 if (ret)
1238 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001239 }
1240 btrfs_release_path(path);
1241
1242 return 0;
1243}
Chris Masone02119d2008-09-05 16:13:11 -04001244
Qu Wenruobae15d92017-11-08 08:54:26 +08001245static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1246 u32 *namelen, char **name, u64 *index,
1247 u64 *parent_objectid)
Mark Fashehf1863732012-08-08 11:32:27 -07001248{
1249 struct btrfs_inode_extref *extref;
1250
1251 extref = (struct btrfs_inode_extref *)ref_ptr;
1252
1253 *namelen = btrfs_inode_extref_name_len(eb, extref);
1254 *name = kmalloc(*namelen, GFP_NOFS);
1255 if (*name == NULL)
1256 return -ENOMEM;
1257
1258 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1259 *namelen);
1260
Filipe Manana1f250e92018-02-28 15:56:10 +00001261 if (index)
1262 *index = btrfs_inode_extref_index(eb, extref);
Mark Fashehf1863732012-08-08 11:32:27 -07001263 if (parent_objectid)
1264 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1265
1266 return 0;
1267}
1268
Qu Wenruobae15d92017-11-08 08:54:26 +08001269static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1270 u32 *namelen, char **name, u64 *index)
Mark Fashehf1863732012-08-08 11:32:27 -07001271{
1272 struct btrfs_inode_ref *ref;
1273
1274 ref = (struct btrfs_inode_ref *)ref_ptr;
1275
1276 *namelen = btrfs_inode_ref_name_len(eb, ref);
1277 *name = kmalloc(*namelen, GFP_NOFS);
1278 if (*name == NULL)
1279 return -ENOMEM;
1280
1281 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1282
Filipe Manana1f250e92018-02-28 15:56:10 +00001283 if (index)
1284 *index = btrfs_inode_ref_index(eb, ref);
Mark Fashehf1863732012-08-08 11:32:27 -07001285
1286 return 0;
1287}
1288
Chris Masone02119d2008-09-05 16:13:11 -04001289/*
Filipe Manana1f250e92018-02-28 15:56:10 +00001290 * Take an inode reference item from the log tree and iterate all names from the
1291 * inode reference item in the subvolume tree with the same key (if it exists).
1292 * For any name that is not in the inode reference item from the log tree, do a
1293 * proper unlink of that name (that is, remove its entry from the inode
1294 * reference item and both dir index keys).
1295 */
1296static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1297 struct btrfs_root *root,
1298 struct btrfs_path *path,
1299 struct btrfs_inode *inode,
1300 struct extent_buffer *log_eb,
1301 int log_slot,
1302 struct btrfs_key *key)
1303{
1304 int ret;
1305 unsigned long ref_ptr;
1306 unsigned long ref_end;
1307 struct extent_buffer *eb;
1308
1309again:
1310 btrfs_release_path(path);
1311 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1312 if (ret > 0) {
1313 ret = 0;
1314 goto out;
1315 }
1316 if (ret < 0)
1317 goto out;
1318
1319 eb = path->nodes[0];
1320 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1321 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1322 while (ref_ptr < ref_end) {
1323 char *name = NULL;
1324 int namelen;
1325 u64 parent_id;
1326
1327 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1328 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1329 NULL, &parent_id);
1330 } else {
1331 parent_id = key->offset;
1332 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1333 NULL);
1334 }
1335 if (ret)
1336 goto out;
1337
1338 if (key->type == BTRFS_INODE_EXTREF_KEY)
Nikolay Borisov6ff49c62019-08-27 14:46:29 +03001339 ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot,
1340 parent_id, name,
1341 namelen);
Filipe Manana1f250e92018-02-28 15:56:10 +00001342 else
Nikolay Borisov9bb84072019-08-27 14:46:28 +03001343 ret = !!btrfs_find_name_in_backref(log_eb, log_slot,
1344 name, namelen);
Filipe Manana1f250e92018-02-28 15:56:10 +00001345
1346 if (!ret) {
1347 struct inode *dir;
1348
1349 btrfs_release_path(path);
1350 dir = read_one_inode(root, parent_id);
1351 if (!dir) {
1352 ret = -ENOENT;
1353 kfree(name);
1354 goto out;
1355 }
Filipe Manana4467af82021-10-25 17:31:50 +01001356 ret = btrfs_unlink_inode(trans, BTRFS_I(dir),
Filipe Manana1f250e92018-02-28 15:56:10 +00001357 inode, name, namelen);
1358 kfree(name);
1359 iput(dir);
1360 if (ret)
1361 goto out;
1362 goto again;
1363 }
1364
1365 kfree(name);
1366 ref_ptr += namelen;
1367 if (key->type == BTRFS_INODE_EXTREF_KEY)
1368 ref_ptr += sizeof(struct btrfs_inode_extref);
1369 else
1370 ref_ptr += sizeof(struct btrfs_inode_ref);
1371 }
1372 ret = 0;
1373 out:
1374 btrfs_release_path(path);
1375 return ret;
1376}
1377
Filipe Manana0d836392018-07-20 10:59:06 +01001378static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
1379 const u8 ref_type, const char *name,
1380 const int namelen)
1381{
1382 struct btrfs_key key;
1383 struct btrfs_path *path;
1384 const u64 parent_id = btrfs_ino(BTRFS_I(dir));
1385 int ret;
1386
1387 path = btrfs_alloc_path();
1388 if (!path)
1389 return -ENOMEM;
1390
1391 key.objectid = btrfs_ino(BTRFS_I(inode));
1392 key.type = ref_type;
1393 if (key.type == BTRFS_INODE_REF_KEY)
1394 key.offset = parent_id;
1395 else
1396 key.offset = btrfs_extref_hash(parent_id, name, namelen);
1397
1398 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
1399 if (ret < 0)
1400 goto out;
1401 if (ret > 0) {
1402 ret = 0;
1403 goto out;
1404 }
1405 if (key.type == BTRFS_INODE_EXTREF_KEY)
Nikolay Borisov6ff49c62019-08-27 14:46:29 +03001406 ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
1407 path->slots[0], parent_id, name, namelen);
Filipe Manana0d836392018-07-20 10:59:06 +01001408 else
Nikolay Borisov9bb84072019-08-27 14:46:28 +03001409 ret = !!btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
1410 name, namelen);
Filipe Manana0d836392018-07-20 10:59:06 +01001411
1412out:
1413 btrfs_free_path(path);
1414 return ret;
1415}
1416
Filipe Manana6d9cc072021-10-25 17:31:51 +01001417static int add_link(struct btrfs_trans_handle *trans,
Filipe Manana6b5fc432019-02-13 12:14:03 +00001418 struct inode *dir, struct inode *inode, const char *name,
1419 int namelen, u64 ref_index)
1420{
Filipe Manana6d9cc072021-10-25 17:31:51 +01001421 struct btrfs_root *root = BTRFS_I(dir)->root;
Filipe Manana6b5fc432019-02-13 12:14:03 +00001422 struct btrfs_dir_item *dir_item;
1423 struct btrfs_key key;
1424 struct btrfs_path *path;
1425 struct inode *other_inode = NULL;
1426 int ret;
1427
1428 path = btrfs_alloc_path();
1429 if (!path)
1430 return -ENOMEM;
1431
1432 dir_item = btrfs_lookup_dir_item(NULL, root, path,
1433 btrfs_ino(BTRFS_I(dir)),
1434 name, namelen, 0);
1435 if (!dir_item) {
1436 btrfs_release_path(path);
1437 goto add_link;
1438 } else if (IS_ERR(dir_item)) {
1439 ret = PTR_ERR(dir_item);
1440 goto out;
1441 }
1442
1443 /*
1444 * Our inode's dentry collides with the dentry of another inode which is
1445 * in the log but not yet processed since it has a higher inode number.
1446 * So delete that other dentry.
1447 */
1448 btrfs_dir_item_key_to_cpu(path->nodes[0], dir_item, &key);
1449 btrfs_release_path(path);
1450 other_inode = read_one_inode(root, key.objectid);
1451 if (!other_inode) {
1452 ret = -ENOENT;
1453 goto out;
1454 }
Filipe Manana4467af82021-10-25 17:31:50 +01001455 ret = btrfs_unlink_inode(trans, BTRFS_I(dir), BTRFS_I(other_inode),
Filipe Manana6b5fc432019-02-13 12:14:03 +00001456 name, namelen);
1457 if (ret)
1458 goto out;
1459 /*
1460 * If we dropped the link count to 0, bump it so that later the iput()
1461 * on the inode will not free it. We will fixup the link count later.
1462 */
1463 if (other_inode->i_nlink == 0)
1464 inc_nlink(other_inode);
1465
1466 ret = btrfs_run_delayed_items(trans);
1467 if (ret)
1468 goto out;
1469add_link:
1470 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
1471 name, namelen, 0, ref_index);
1472out:
1473 iput(other_inode);
1474 btrfs_free_path(path);
1475
1476 return ret;
1477}
1478
Filipe Manana1f250e92018-02-28 15:56:10 +00001479/*
Chris Masone02119d2008-09-05 16:13:11 -04001480 * replay one inode back reference item found in the log tree.
1481 * eb, slot and key refer to the buffer and key found in the log tree.
1482 * root is the destination we are replaying into, and path is for temp
1483 * use by this function. (it should be released on return).
1484 */
1485static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1486 struct btrfs_root *root,
1487 struct btrfs_root *log,
1488 struct btrfs_path *path,
1489 struct extent_buffer *eb, int slot,
1490 struct btrfs_key *key)
1491{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001492 struct inode *dir = NULL;
1493 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001494 unsigned long ref_ptr;
1495 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001496 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001497 int namelen;
1498 int ret;
liuboc622ae62011-03-26 08:01:12 -04001499 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001500 int log_ref_ver = 0;
1501 u64 parent_objectid;
1502 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001503 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001504 int ref_struct_size;
1505
1506 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1507 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1508
1509 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1510 struct btrfs_inode_extref *r;
1511
1512 ref_struct_size = sizeof(struct btrfs_inode_extref);
1513 log_ref_ver = 1;
1514 r = (struct btrfs_inode_extref *)ref_ptr;
1515 parent_objectid = btrfs_inode_extref_parent(eb, r);
1516 } else {
1517 ref_struct_size = sizeof(struct btrfs_inode_ref);
1518 parent_objectid = key->offset;
1519 }
1520 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001521
Chris Masone02119d2008-09-05 16:13:11 -04001522 /*
1523 * it is possible that we didn't log all the parent directories
1524 * for a given inode. If we don't find the dir, just don't
1525 * copy the back ref in. The link count fixup code will take
1526 * care of the rest
1527 */
Mark Fashehf1863732012-08-08 11:32:27 -07001528 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001529 if (!dir) {
1530 ret = -ENOENT;
1531 goto out;
1532 }
Chris Masone02119d2008-09-05 16:13:11 -04001533
Mark Fashehf1863732012-08-08 11:32:27 -07001534 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001535 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001536 ret = -EIO;
1537 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001538 }
Chris Masone02119d2008-09-05 16:13:11 -04001539
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001540 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001541 if (log_ref_ver) {
Qu Wenruobae15d92017-11-08 08:54:26 +08001542 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1543 &ref_index, &parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001544 /*
1545 * parent object can change from one array
1546 * item to another.
1547 */
1548 if (!dir)
1549 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001550 if (!dir) {
1551 ret = -ENOENT;
1552 goto out;
1553 }
Mark Fashehf1863732012-08-08 11:32:27 -07001554 } else {
Qu Wenruobae15d92017-11-08 08:54:26 +08001555 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1556 &ref_index);
Mark Fashehf1863732012-08-08 11:32:27 -07001557 }
1558 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001559 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001560
Filipe Manana77a5b9e2021-10-01 13:52:30 +01001561 ret = inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1562 btrfs_ino(BTRFS_I(inode)), ref_index,
1563 name, namelen);
1564 if (ret < 0) {
1565 goto out;
1566 } else if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001567 /*
1568 * look for a conflicting back reference in the
1569 * metadata. if we find one we have to unlink that name
1570 * of the file before we add our new link. Later on, we
1571 * overwrite any existing back reference, and we don't
1572 * want to create dangling pointers in the directory.
1573 */
Chris Masone02119d2008-09-05 16:13:11 -04001574
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001575 if (!search_done) {
1576 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001577 BTRFS_I(dir),
David Sterbad75eefd2017-02-10 20:20:19 +01001578 BTRFS_I(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001579 inode_objectid,
1580 parent_objectid,
1581 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001582 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001583 if (ret) {
1584 if (ret == 1)
1585 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001586 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001587 }
Chris Masone02119d2008-09-05 16:13:11 -04001588 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001589
Filipe Manana0d836392018-07-20 10:59:06 +01001590 /*
1591 * If a reference item already exists for this inode
1592 * with the same parent and name, but different index,
1593 * drop it and the corresponding directory index entries
1594 * from the parent before adding the new reference item
1595 * and dir index entries, otherwise we would fail with
1596 * -EEXIST returned from btrfs_add_link() below.
1597 */
1598 ret = btrfs_inode_ref_exists(inode, dir, key->type,
1599 name, namelen);
1600 if (ret > 0) {
Filipe Manana4467af82021-10-25 17:31:50 +01001601 ret = btrfs_unlink_inode(trans,
Filipe Manana0d836392018-07-20 10:59:06 +01001602 BTRFS_I(dir),
1603 BTRFS_I(inode),
1604 name, namelen);
1605 /*
1606 * If we dropped the link count to 0, bump it so
1607 * that later the iput() on the inode will not
1608 * free it. We will fixup the link count later.
1609 */
1610 if (!ret && inode->i_nlink == 0)
1611 inc_nlink(inode);
1612 }
1613 if (ret < 0)
1614 goto out;
1615
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001616 /* insert our name */
Filipe Manana6d9cc072021-10-25 17:31:51 +01001617 ret = add_link(trans, dir, inode, name, namelen,
Filipe Manana6b5fc432019-02-13 12:14:03 +00001618 ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001619 if (ret)
1620 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001621
Josef Bacikf96d4472021-05-19 11:26:25 -04001622 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
1623 if (ret)
1624 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001625 }
Filipe Manana77a5b9e2021-10-01 13:52:30 +01001626 /* Else, ret == 1, we already have a perfect match, we're done. */
liuboc622ae62011-03-26 08:01:12 -04001627
Mark Fashehf1863732012-08-08 11:32:27 -07001628 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001629 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001630 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001631 if (log_ref_ver) {
1632 iput(dir);
1633 dir = NULL;
1634 }
Chris Masone02119d2008-09-05 16:13:11 -04001635 }
Chris Masone02119d2008-09-05 16:13:11 -04001636
Filipe Manana1f250e92018-02-28 15:56:10 +00001637 /*
1638 * Before we overwrite the inode reference item in the subvolume tree
1639 * with the item from the log tree, we must unlink all names from the
1640 * parent directory that are in the subvolume's tree inode reference
1641 * item, otherwise we end up with an inconsistent subvolume tree where
1642 * dir index entries exist for a name but there is no inode reference
1643 * item with the same name.
1644 */
1645 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1646 key);
1647 if (ret)
1648 goto out;
1649
Chris Masone02119d2008-09-05 16:13:11 -04001650 /* finally write the back reference in the inode */
1651 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001652out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001653 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001654 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001655 iput(dir);
1656 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001657 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001658}
1659
Mark Fashehf1863732012-08-08 11:32:27 -07001660static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001661 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001662{
Mark Fashehf1863732012-08-08 11:32:27 -07001663 int ret = 0;
1664 int name_len;
1665 unsigned int nlink = 0;
1666 u32 item_size;
1667 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001668 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001669 u64 offset = 0;
1670 unsigned long ptr;
1671 struct btrfs_inode_extref *extref;
1672 struct extent_buffer *leaf;
1673
1674 while (1) {
1675 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1676 &extref, &offset);
1677 if (ret)
1678 break;
1679
1680 leaf = path->nodes[0];
1681 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1682 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001683 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001684
1685 while (cur_offset < item_size) {
1686 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1687 name_len = btrfs_inode_extref_name_len(leaf, extref);
1688
1689 nlink++;
1690
1691 cur_offset += name_len + sizeof(*extref);
1692 }
1693
1694 offset++;
1695 btrfs_release_path(path);
1696 }
1697 btrfs_release_path(path);
1698
Filipe Manana2c2c4522015-01-13 16:40:04 +00001699 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001700 return ret;
1701 return nlink;
1702}
1703
1704static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001705 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001706{
Chris Masone02119d2008-09-05 16:13:11 -04001707 int ret;
1708 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001709 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001710 unsigned long ptr;
1711 unsigned long ptr_end;
1712 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001713 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001714
Li Zefan33345d012011-04-20 10:31:50 +08001715 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001716 key.type = BTRFS_INODE_REF_KEY;
1717 key.offset = (u64)-1;
1718
Chris Masond3977122009-01-05 21:25:51 -05001719 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001720 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1721 if (ret < 0)
1722 break;
1723 if (ret > 0) {
1724 if (path->slots[0] == 0)
1725 break;
1726 path->slots[0]--;
1727 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001728process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001729 btrfs_item_key_to_cpu(path->nodes[0], &key,
1730 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001731 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001732 key.type != BTRFS_INODE_REF_KEY)
1733 break;
1734 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1735 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1736 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001737 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001738 struct btrfs_inode_ref *ref;
1739
1740 ref = (struct btrfs_inode_ref *)ptr;
1741 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1742 ref);
1743 ptr = (unsigned long)(ref + 1) + name_len;
1744 nlink++;
1745 }
1746
1747 if (key.offset == 0)
1748 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001749 if (path->slots[0] > 0) {
1750 path->slots[0]--;
1751 goto process_slot;
1752 }
Chris Masone02119d2008-09-05 16:13:11 -04001753 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001754 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001755 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001756 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001757
1758 return nlink;
1759}
1760
1761/*
1762 * There are a few corners where the link count of the file can't
1763 * be properly maintained during replay. So, instead of adding
1764 * lots of complexity to the log code, we just scan the backrefs
1765 * for any file that has been through replay.
1766 *
1767 * The scan will update the link count on the inode to reflect the
1768 * number of back refs found. If it goes down to zero, the iput
1769 * will free the inode.
1770 */
1771static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1772 struct btrfs_root *root,
1773 struct inode *inode)
1774{
1775 struct btrfs_path *path;
1776 int ret;
1777 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001778 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001779
1780 path = btrfs_alloc_path();
1781 if (!path)
1782 return -ENOMEM;
1783
Nikolay Borisovf329e312017-01-18 00:31:50 +02001784 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001785 if (ret < 0)
1786 goto out;
1787
1788 nlink = ret;
1789
Nikolay Borisov36283652017-01-18 00:31:49 +02001790 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001791 if (ret < 0)
1792 goto out;
1793
1794 nlink += ret;
1795
1796 ret = 0;
1797
Chris Masone02119d2008-09-05 16:13:11 -04001798 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001799 set_nlink(inode, nlink);
Josef Bacikf96d4472021-05-19 11:26:25 -04001800 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
1801 if (ret)
1802 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001803 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001804 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001805
Yan, Zhengc71bf092009-11-12 09:34:40 +00001806 if (inode->i_nlink == 0) {
1807 if (S_ISDIR(inode->i_mode)) {
1808 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001809 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001810 if (ret)
1811 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001812 }
Nikolay Borisovecdcf3c2020-10-22 18:40:46 +03001813 ret = btrfs_insert_orphan_item(trans, root, ino);
1814 if (ret == -EEXIST)
1815 ret = 0;
Chris Mason12fcfd22009-03-24 10:24:20 -04001816 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001817
Mark Fashehf1863732012-08-08 11:32:27 -07001818out:
1819 btrfs_free_path(path);
1820 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001821}
1822
1823static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1824 struct btrfs_root *root,
1825 struct btrfs_path *path)
1826{
1827 int ret;
1828 struct btrfs_key key;
1829 struct inode *inode;
1830
1831 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1832 key.type = BTRFS_ORPHAN_ITEM_KEY;
1833 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001834 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001835 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1836 if (ret < 0)
1837 break;
1838
1839 if (ret == 1) {
Josef Bacik011b28a2021-05-19 13:13:15 -04001840 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001841 if (path->slots[0] == 0)
1842 break;
1843 path->slots[0]--;
1844 }
1845
1846 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1847 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1848 key.type != BTRFS_ORPHAN_ITEM_KEY)
1849 break;
1850
1851 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001852 if (ret)
Josef Bacik011b28a2021-05-19 13:13:15 -04001853 break;
Chris Masone02119d2008-09-05 16:13:11 -04001854
David Sterbab3b4aa72011-04-21 01:20:15 +02001855 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001856 inode = read_one_inode(root, key.offset);
Josef Bacik011b28a2021-05-19 13:13:15 -04001857 if (!inode) {
1858 ret = -EIO;
1859 break;
1860 }
Chris Masone02119d2008-09-05 16:13:11 -04001861
1862 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001863 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001864 if (ret)
Josef Bacik011b28a2021-05-19 13:13:15 -04001865 break;
Chris Masone02119d2008-09-05 16:13:11 -04001866
Chris Mason12fcfd22009-03-24 10:24:20 -04001867 /*
1868 * fixup on a directory may create new entries,
1869 * make sure we always look for the highset possible
1870 * offset
1871 */
1872 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001873 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001874 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001875 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001876}
1877
1878
1879/*
1880 * record a given inode in the fixup dir so we can check its link
1881 * count when replay is done. The link count is incremented here
1882 * so the inode won't go away until we check it
1883 */
1884static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1885 struct btrfs_root *root,
1886 struct btrfs_path *path,
1887 u64 objectid)
1888{
1889 struct btrfs_key key;
1890 int ret = 0;
1891 struct inode *inode;
1892
1893 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001894 if (!inode)
1895 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001896
1897 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001898 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001899 key.offset = objectid;
1900
1901 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1902
David Sterbab3b4aa72011-04-21 01:20:15 +02001903 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001904 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001905 if (!inode->i_nlink)
1906 set_nlink(inode, 1);
1907 else
Zach Brown8b558c52013-10-16 12:10:34 -07001908 inc_nlink(inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02001909 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masone02119d2008-09-05 16:13:11 -04001910 } else if (ret == -EEXIST) {
1911 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001912 }
1913 iput(inode);
1914
1915 return ret;
1916}
1917
1918/*
1919 * when replaying the log for a directory, we only insert names
1920 * for inodes that actually exist. This means an fsync on a directory
1921 * does not implicitly fsync all the new files in it
1922 */
1923static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1924 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001925 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001926 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001927 struct btrfs_key *location)
1928{
1929 struct inode *inode;
1930 struct inode *dir;
1931 int ret;
1932
1933 inode = read_one_inode(root, location->objectid);
1934 if (!inode)
1935 return -ENOENT;
1936
1937 dir = read_one_inode(root, dirid);
1938 if (!dir) {
1939 iput(inode);
1940 return -EIO;
1941 }
Josef Bacikd5554382013-09-11 14:17:00 -04001942
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001943 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1944 name_len, 1, index);
Chris Masone02119d2008-09-05 16:13:11 -04001945
1946 /* FIXME, put inode into FIXUP list */
1947
1948 iput(inode);
1949 iput(dir);
1950 return ret;
1951}
1952
1953/*
1954 * take a single entry in a log directory item and replay it into
1955 * the subvolume.
1956 *
1957 * if a conflicting item exists in the subdirectory already,
1958 * the inode it points to is unlinked and put into the link count
1959 * fix up tree.
1960 *
1961 * If a name from the log points to a file or directory that does
1962 * not exist in the FS, it is skipped. fsyncs on directories
1963 * do not force down inodes inside that directory, just changes to the
1964 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001965 *
1966 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1967 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001968 */
1969static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1970 struct btrfs_root *root,
1971 struct btrfs_path *path,
1972 struct extent_buffer *eb,
1973 struct btrfs_dir_item *di,
1974 struct btrfs_key *key)
1975{
1976 char *name;
1977 int name_len;
1978 struct btrfs_dir_item *dst_di;
1979 struct btrfs_key found_key;
1980 struct btrfs_key log_key;
1981 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001982 u8 log_type;
Filipe Mananacfd31262021-10-01 13:48:18 +01001983 bool exists;
1984 int ret;
Josef Bacikd5554382013-09-11 14:17:00 -04001985 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001986 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001987
1988 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001989 if (!dir)
1990 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001991
1992 name_len = btrfs_dir_name_len(eb, di);
1993 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001994 if (!name) {
1995 ret = -ENOMEM;
1996 goto out;
1997 }
liubo2a29edc2011-01-26 06:22:08 +00001998
Chris Masone02119d2008-09-05 16:13:11 -04001999 log_type = btrfs_dir_type(eb, di);
2000 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2001 name_len);
2002
2003 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Filipe Mananacfd31262021-10-01 13:48:18 +01002004 ret = btrfs_lookup_inode(trans, root, path, &log_key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02002005 btrfs_release_path(path);
Filipe Mananacfd31262021-10-01 13:48:18 +01002006 if (ret < 0)
2007 goto out;
2008 exists = (ret == 0);
2009 ret = 0;
Chris Mason4bef0842008-09-08 11:18:08 -04002010
Chris Masone02119d2008-09-05 16:13:11 -04002011 if (key->type == BTRFS_DIR_ITEM_KEY) {
2012 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
2013 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05002014 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002015 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
2016 key->objectid,
2017 key->offset, name,
2018 name_len, 1);
2019 } else {
Josef Bacik36508602013-04-25 16:23:32 -04002020 /* Corruption */
2021 ret = -EINVAL;
2022 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002023 }
Filipe Mananae15ac642021-10-01 13:52:31 +01002024
Filipe Mananae15ac642021-10-01 13:52:31 +01002025 if (IS_ERR(dst_di)) {
2026 ret = PTR_ERR(dst_di);
2027 goto out;
2028 } else if (!dst_di) {
Chris Masone02119d2008-09-05 16:13:11 -04002029 /* we need a sequence number to insert, so we only
2030 * do inserts for the BTRFS_DIR_INDEX_KEY types
2031 */
2032 if (key->type != BTRFS_DIR_INDEX_KEY)
2033 goto out;
2034 goto insert;
2035 }
2036
2037 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
2038 /* the existing item matches the logged item */
2039 if (found_key.objectid == log_key.objectid &&
2040 found_key.type == log_key.type &&
2041 found_key.offset == log_key.offset &&
2042 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01002043 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04002044 goto out;
2045 }
2046
2047 /*
2048 * don't drop the conflicting directory entry if the inode
2049 * for the new entry doesn't exist
2050 */
Chris Mason4bef0842008-09-08 11:18:08 -04002051 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04002052 goto out;
2053
Filipe Manana9798ba22021-10-25 17:31:49 +01002054 ret = drop_one_dir_item(trans, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04002055 if (ret)
2056 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002057
2058 if (key->type == BTRFS_DIR_INDEX_KEY)
2059 goto insert;
2060out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002061 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04002062 if (!ret && update_size) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02002063 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02002064 ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
Josef Bacikd5554382013-09-11 14:17:00 -04002065 }
Chris Masone02119d2008-09-05 16:13:11 -04002066 kfree(name);
2067 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01002068 if (!ret && name_added)
2069 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04002070 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002071
2072insert:
Nikolay Borisov725af922019-08-30 17:44:49 +03002073 /*
2074 * Check if the inode reference exists in the log for the given name,
2075 * inode and parent inode
2076 */
2077 found_key.objectid = log_key.objectid;
2078 found_key.type = BTRFS_INODE_REF_KEY;
2079 found_key.offset = key->objectid;
2080 ret = backref_in_log(root->log_root, &found_key, 0, name, name_len);
2081 if (ret < 0) {
2082 goto out;
2083 } else if (ret) {
2084 /* The dentry will be added later. */
2085 ret = 0;
2086 update_size = false;
2087 goto out;
2088 }
2089
2090 found_key.objectid = log_key.objectid;
2091 found_key.type = BTRFS_INODE_EXTREF_KEY;
2092 found_key.offset = key->objectid;
2093 ret = backref_in_log(root->log_root, &found_key, key->objectid, name,
2094 name_len);
2095 if (ret < 0) {
2096 goto out;
2097 } else if (ret) {
Filipe Mananadf8d1162015-01-14 01:52:25 +00002098 /* The dentry will be added later. */
2099 ret = 0;
2100 update_size = false;
2101 goto out;
2102 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002103 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08002104 ret = insert_one_name(trans, root, key->objectid, key->offset,
2105 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00002106 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04002107 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002108 if (!ret)
2109 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04002110 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04002111 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002112 goto out;
2113}
2114
2115/*
2116 * find all the names in a directory item and reconcile them into
2117 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
2118 * one name in a directory item, but the same code gets used for
2119 * both directory index types
2120 */
2121static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
2122 struct btrfs_root *root,
2123 struct btrfs_path *path,
2124 struct extent_buffer *eb, int slot,
2125 struct btrfs_key *key)
2126{
Filipe Mananabb53eda2015-07-15 23:26:43 +01002127 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002128 u32 item_size = btrfs_item_size_nr(eb, slot);
2129 struct btrfs_dir_item *di;
2130 int name_len;
2131 unsigned long ptr;
2132 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002133 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04002134
2135 ptr = btrfs_item_ptr_offset(eb, slot);
2136 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002137 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002138 di = (struct btrfs_dir_item *)ptr;
2139 name_len = btrfs_dir_name_len(eb, di);
2140 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01002141 if (ret < 0)
2142 break;
Chris Masone02119d2008-09-05 16:13:11 -04002143 ptr = (unsigned long)(di + 1);
2144 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002145
2146 /*
2147 * If this entry refers to a non-directory (directories can not
2148 * have a link count > 1) and it was added in the transaction
2149 * that was not committed, make sure we fixup the link count of
2150 * the inode it the entry points to. Otherwise something like
2151 * the following would result in a directory pointing to an
2152 * inode with a wrong link that does not account for this dir
2153 * entry:
2154 *
2155 * mkdir testdir
2156 * touch testdir/foo
2157 * touch testdir/bar
2158 * sync
2159 *
2160 * ln testdir/bar testdir/bar_link
2161 * ln testdir/foo testdir/foo_link
2162 * xfs_io -c "fsync" testdir/bar
2163 *
2164 * <power failure>
2165 *
2166 * mount fs, log replay happens
2167 *
2168 * File foo would remain with a link count of 1 when it has two
2169 * entries pointing to it in the directory testdir. This would
2170 * make it impossible to ever delete the parent directory has
2171 * it would result in stale dentries that can never be deleted.
2172 */
2173 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
2174 struct btrfs_key di_key;
2175
2176 if (!fixup_path) {
2177 fixup_path = btrfs_alloc_path();
2178 if (!fixup_path) {
2179 ret = -ENOMEM;
2180 break;
2181 }
2182 }
2183
2184 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2185 ret = link_to_fixup_dir(trans, root, fixup_path,
2186 di_key.objectid);
2187 if (ret)
2188 break;
2189 }
2190 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002191 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01002192 btrfs_free_path(fixup_path);
2193 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002194}
2195
2196/*
2197 * directory replay has two parts. There are the standard directory
2198 * items in the log copied from the subvolume, and range items
2199 * created in the log while the subvolume was logged.
2200 *
2201 * The range items tell us which parts of the key space the log
2202 * is authoritative for. During replay, if a key in the subvolume
2203 * directory is in a logged range item, but not actually in the log
2204 * that means it was deleted from the directory before the fsync
2205 * and should be removed.
2206 */
2207static noinline int find_dir_range(struct btrfs_root *root,
2208 struct btrfs_path *path,
2209 u64 dirid, int key_type,
2210 u64 *start_ret, u64 *end_ret)
2211{
2212 struct btrfs_key key;
2213 u64 found_end;
2214 struct btrfs_dir_log_item *item;
2215 int ret;
2216 int nritems;
2217
2218 if (*start_ret == (u64)-1)
2219 return 1;
2220
2221 key.objectid = dirid;
2222 key.type = key_type;
2223 key.offset = *start_ret;
2224
2225 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2226 if (ret < 0)
2227 goto out;
2228 if (ret > 0) {
2229 if (path->slots[0] == 0)
2230 goto out;
2231 path->slots[0]--;
2232 }
2233 if (ret != 0)
2234 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2235
2236 if (key.type != key_type || key.objectid != dirid) {
2237 ret = 1;
2238 goto next;
2239 }
2240 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2241 struct btrfs_dir_log_item);
2242 found_end = btrfs_dir_log_end(path->nodes[0], item);
2243
2244 if (*start_ret >= key.offset && *start_ret <= found_end) {
2245 ret = 0;
2246 *start_ret = key.offset;
2247 *end_ret = found_end;
2248 goto out;
2249 }
2250 ret = 1;
2251next:
2252 /* check the next slot in the tree to see if it is a valid item */
2253 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08002254 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04002255 if (path->slots[0] >= nritems) {
2256 ret = btrfs_next_leaf(root, path);
2257 if (ret)
2258 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002259 }
2260
2261 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2262
2263 if (key.type != key_type || key.objectid != dirid) {
2264 ret = 1;
2265 goto out;
2266 }
2267 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2268 struct btrfs_dir_log_item);
2269 found_end = btrfs_dir_log_end(path->nodes[0], item);
2270 *start_ret = key.offset;
2271 *end_ret = found_end;
2272 ret = 0;
2273out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002274 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002275 return ret;
2276}
2277
2278/*
2279 * this looks for a given directory item in the log. If the directory
2280 * item is not in the log, the item is removed and the inode it points
2281 * to is unlinked
2282 */
2283static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002284 struct btrfs_root *log,
2285 struct btrfs_path *path,
2286 struct btrfs_path *log_path,
2287 struct inode *dir,
2288 struct btrfs_key *dir_key)
2289{
Filipe Mananad1ed82f2021-10-25 17:31:52 +01002290 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masone02119d2008-09-05 16:13:11 -04002291 int ret;
2292 struct extent_buffer *eb;
2293 int slot;
2294 u32 item_size;
2295 struct btrfs_dir_item *di;
2296 struct btrfs_dir_item *log_di;
2297 int name_len;
2298 unsigned long ptr;
2299 unsigned long ptr_end;
2300 char *name;
2301 struct inode *inode;
2302 struct btrfs_key location;
2303
2304again:
2305 eb = path->nodes[0];
2306 slot = path->slots[0];
2307 item_size = btrfs_item_size_nr(eb, slot);
2308 ptr = btrfs_item_ptr_offset(eb, slot);
2309 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002310 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002311 di = (struct btrfs_dir_item *)ptr;
2312 name_len = btrfs_dir_name_len(eb, di);
2313 name = kmalloc(name_len, GFP_NOFS);
2314 if (!name) {
2315 ret = -ENOMEM;
2316 goto out;
2317 }
2318 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2319 name_len);
2320 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002321 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002322 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2323 dir_key->objectid,
2324 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002325 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002326 log_di = btrfs_lookup_dir_index_item(trans, log,
2327 log_path,
2328 dir_key->objectid,
2329 dir_key->offset,
2330 name, name_len, 0);
2331 }
Filipe Manana8dcbc262021-10-01 13:52:33 +01002332 if (!log_di) {
Chris Masone02119d2008-09-05 16:13:11 -04002333 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002334 btrfs_release_path(path);
2335 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002336 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002337 if (!inode) {
2338 kfree(name);
2339 return -EIO;
2340 }
Chris Masone02119d2008-09-05 16:13:11 -04002341
2342 ret = link_to_fixup_dir(trans, root,
2343 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002344 if (ret) {
2345 kfree(name);
2346 iput(inode);
2347 goto out;
2348 }
2349
Zach Brown8b558c52013-10-16 12:10:34 -07002350 inc_nlink(inode);
Filipe Manana4467af82021-10-25 17:31:50 +01002351 ret = btrfs_unlink_inode(trans, BTRFS_I(dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002352 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002353 if (!ret)
Nikolay Borisove5c304e62018-02-07 17:55:43 +02002354 ret = btrfs_run_delayed_items(trans);
Chris Masone02119d2008-09-05 16:13:11 -04002355 kfree(name);
2356 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002357 if (ret)
2358 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002359
2360 /* there might still be more names under this key
2361 * check and repeat if required
2362 */
2363 ret = btrfs_search_slot(NULL, root, dir_key, path,
2364 0, 0);
2365 if (ret == 0)
2366 goto again;
2367 ret = 0;
2368 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002369 } else if (IS_ERR(log_di)) {
2370 kfree(name);
2371 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002372 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002373 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002374 kfree(name);
2375
2376 ptr = (unsigned long)(di + 1);
2377 ptr += name_len;
2378 }
2379 ret = 0;
2380out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002381 btrfs_release_path(path);
2382 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002383 return ret;
2384}
2385
Filipe Manana4f764e52015-02-23 19:53:35 +00002386static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2387 struct btrfs_root *root,
2388 struct btrfs_root *log,
2389 struct btrfs_path *path,
2390 const u64 ino)
2391{
2392 struct btrfs_key search_key;
2393 struct btrfs_path *log_path;
2394 int i;
2395 int nritems;
2396 int ret;
2397
2398 log_path = btrfs_alloc_path();
2399 if (!log_path)
2400 return -ENOMEM;
2401
2402 search_key.objectid = ino;
2403 search_key.type = BTRFS_XATTR_ITEM_KEY;
2404 search_key.offset = 0;
2405again:
2406 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2407 if (ret < 0)
2408 goto out;
2409process_leaf:
2410 nritems = btrfs_header_nritems(path->nodes[0]);
2411 for (i = path->slots[0]; i < nritems; i++) {
2412 struct btrfs_key key;
2413 struct btrfs_dir_item *di;
2414 struct btrfs_dir_item *log_di;
2415 u32 total_size;
2416 u32 cur;
2417
2418 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2419 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2420 ret = 0;
2421 goto out;
2422 }
2423
2424 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2425 total_size = btrfs_item_size_nr(path->nodes[0], i);
2426 cur = 0;
2427 while (cur < total_size) {
2428 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2429 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2430 u32 this_len = sizeof(*di) + name_len + data_len;
2431 char *name;
2432
2433 name = kmalloc(name_len, GFP_NOFS);
2434 if (!name) {
2435 ret = -ENOMEM;
2436 goto out;
2437 }
2438 read_extent_buffer(path->nodes[0], name,
2439 (unsigned long)(di + 1), name_len);
2440
2441 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2442 name, name_len, 0);
2443 btrfs_release_path(log_path);
2444 if (!log_di) {
2445 /* Doesn't exist in log tree, so delete it. */
2446 btrfs_release_path(path);
2447 di = btrfs_lookup_xattr(trans, root, path, ino,
2448 name, name_len, -1);
2449 kfree(name);
2450 if (IS_ERR(di)) {
2451 ret = PTR_ERR(di);
2452 goto out;
2453 }
2454 ASSERT(di);
2455 ret = btrfs_delete_one_dir_name(trans, root,
2456 path, di);
2457 if (ret)
2458 goto out;
2459 btrfs_release_path(path);
2460 search_key = key;
2461 goto again;
2462 }
2463 kfree(name);
2464 if (IS_ERR(log_di)) {
2465 ret = PTR_ERR(log_di);
2466 goto out;
2467 }
2468 cur += this_len;
2469 di = (struct btrfs_dir_item *)((char *)di + this_len);
2470 }
2471 }
2472 ret = btrfs_next_leaf(root, path);
2473 if (ret > 0)
2474 ret = 0;
2475 else if (ret == 0)
2476 goto process_leaf;
2477out:
2478 btrfs_free_path(log_path);
2479 btrfs_release_path(path);
2480 return ret;
2481}
2482
2483
Chris Masone02119d2008-09-05 16:13:11 -04002484/*
2485 * deletion replay happens before we copy any new directory items
2486 * out of the log or out of backreferences from inodes. It
2487 * scans the log to find ranges of keys that log is authoritative for,
2488 * and then scans the directory to find items in those ranges that are
2489 * not present in the log.
2490 *
2491 * Anything we don't find in the log is unlinked and removed from the
2492 * directory.
2493 */
2494static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2495 struct btrfs_root *root,
2496 struct btrfs_root *log,
2497 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002498 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002499{
2500 u64 range_start;
2501 u64 range_end;
2502 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2503 int ret = 0;
2504 struct btrfs_key dir_key;
2505 struct btrfs_key found_key;
2506 struct btrfs_path *log_path;
2507 struct inode *dir;
2508
2509 dir_key.objectid = dirid;
2510 dir_key.type = BTRFS_DIR_ITEM_KEY;
2511 log_path = btrfs_alloc_path();
2512 if (!log_path)
2513 return -ENOMEM;
2514
2515 dir = read_one_inode(root, dirid);
2516 /* it isn't an error if the inode isn't there, that can happen
2517 * because we replay the deletes before we copy in the inode item
2518 * from the log
2519 */
2520 if (!dir) {
2521 btrfs_free_path(log_path);
2522 return 0;
2523 }
2524again:
2525 range_start = 0;
2526 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002527 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002528 if (del_all)
2529 range_end = (u64)-1;
2530 else {
2531 ret = find_dir_range(log, path, dirid, key_type,
2532 &range_start, &range_end);
Filipe Manana10adb112021-10-14 17:26:04 +01002533 if (ret < 0)
2534 goto out;
2535 else if (ret > 0)
Chris Mason12fcfd22009-03-24 10:24:20 -04002536 break;
2537 }
Chris Masone02119d2008-09-05 16:13:11 -04002538
2539 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002540 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002541 int nritems;
2542 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2543 0, 0);
2544 if (ret < 0)
2545 goto out;
2546
2547 nritems = btrfs_header_nritems(path->nodes[0]);
2548 if (path->slots[0] >= nritems) {
2549 ret = btrfs_next_leaf(root, path);
Liu Bob98def72018-04-03 01:59:48 +08002550 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002551 break;
Liu Bob98def72018-04-03 01:59:48 +08002552 else if (ret < 0)
2553 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002554 }
2555 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2556 path->slots[0]);
2557 if (found_key.objectid != dirid ||
2558 found_key.type != dir_key.type)
2559 goto next_type;
2560
2561 if (found_key.offset > range_end)
2562 break;
2563
Filipe Mananad1ed82f2021-10-25 17:31:52 +01002564 ret = check_item_in_log(trans, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002565 log_path, dir,
2566 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002567 if (ret)
2568 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002569 if (found_key.offset == (u64)-1)
2570 break;
2571 dir_key.offset = found_key.offset + 1;
2572 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002573 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002574 if (range_end == (u64)-1)
2575 break;
2576 range_start = range_end + 1;
2577 }
2578
2579next_type:
2580 ret = 0;
2581 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2582 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2583 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002584 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002585 goto again;
2586 }
2587out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002588 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002589 btrfs_free_path(log_path);
2590 iput(dir);
2591 return ret;
2592}
2593
2594/*
2595 * the process_func used to replay items from the log tree. This
2596 * gets called in two different stages. The first stage just looks
2597 * for inodes and makes sure they are all copied into the subvolume.
2598 *
2599 * The second stage copies all the other item types from the log into
2600 * the subvolume. The two stage approach is slower, but gets rid of
2601 * lots of complexity around inodes referencing other inodes that exist
2602 * only in the log (references come from either directory items or inode
2603 * back refs).
2604 */
2605static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +08002606 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -04002607{
2608 int nritems;
2609 struct btrfs_path *path;
2610 struct btrfs_root *root = wc->replay_dest;
2611 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002612 int i;
2613 int ret;
2614
Qu Wenruo581c1762018-03-29 09:08:11 +08002615 ret = btrfs_read_buffer(eb, gen, level, NULL);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002616 if (ret)
2617 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002618
2619 level = btrfs_header_level(eb);
2620
2621 if (level != 0)
2622 return 0;
2623
2624 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002625 if (!path)
2626 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002627
2628 nritems = btrfs_header_nritems(eb);
2629 for (i = 0; i < nritems; i++) {
2630 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002631
2632 /* inode keys are done during the first stage */
2633 if (key.type == BTRFS_INODE_ITEM_KEY &&
2634 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002635 struct btrfs_inode_item *inode_item;
2636 u32 mode;
2637
2638 inode_item = btrfs_item_ptr(eb, i,
2639 struct btrfs_inode_item);
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002640 /*
2641 * If we have a tmpfile (O_TMPFILE) that got fsync'ed
2642 * and never got linked before the fsync, skip it, as
2643 * replaying it is pointless since it would be deleted
2644 * later. We skip logging tmpfiles, but it's always
2645 * possible we are replaying a log created with a kernel
2646 * that used to log tmpfiles.
2647 */
2648 if (btrfs_inode_nlink(eb, inode_item) == 0) {
2649 wc->ignore_cur_inode = true;
2650 continue;
2651 } else {
2652 wc->ignore_cur_inode = false;
2653 }
Filipe Manana4f764e52015-02-23 19:53:35 +00002654 ret = replay_xattr_deletes(wc->trans, root, log,
2655 path, key.objectid);
2656 if (ret)
2657 break;
Chris Masone02119d2008-09-05 16:13:11 -04002658 mode = btrfs_inode_mode(eb, inode_item);
2659 if (S_ISDIR(mode)) {
2660 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002661 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002662 if (ret)
2663 break;
Chris Masone02119d2008-09-05 16:13:11 -04002664 }
2665 ret = overwrite_item(wc->trans, root, path,
2666 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002667 if (ret)
2668 break;
Chris Masone02119d2008-09-05 16:13:11 -04002669
Filipe Manana471d5572018-04-05 22:55:12 +01002670 /*
2671 * Before replaying extents, truncate the inode to its
2672 * size. We need to do it now and not after log replay
2673 * because before an fsync we can have prealloc extents
2674 * added beyond the inode's i_size. If we did it after,
2675 * through orphan cleanup for example, we would drop
2676 * those prealloc extents just after replaying them.
Chris Masone02119d2008-09-05 16:13:11 -04002677 */
2678 if (S_ISREG(mode)) {
Filipe Manana5893dfb2020-11-04 11:07:32 +00002679 struct btrfs_drop_extents_args drop_args = { 0 };
Filipe Manana471d5572018-04-05 22:55:12 +01002680 struct inode *inode;
2681 u64 from;
2682
2683 inode = read_one_inode(root, key.objectid);
2684 if (!inode) {
2685 ret = -EIO;
2686 break;
2687 }
2688 from = ALIGN(i_size_read(inode),
2689 root->fs_info->sectorsize);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002690 drop_args.start = from;
2691 drop_args.end = (u64)-1;
2692 drop_args.drop_cache = true;
2693 ret = btrfs_drop_extents(wc->trans, root,
2694 BTRFS_I(inode),
2695 &drop_args);
Filipe Manana471d5572018-04-05 22:55:12 +01002696 if (!ret) {
Filipe Manana2766ff62020-11-04 11:07:34 +00002697 inode_sub_bytes(inode,
2698 drop_args.bytes_found);
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002699 /* Update the inode's nbytes. */
Filipe Manana471d5572018-04-05 22:55:12 +01002700 ret = btrfs_update_inode(wc->trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02002701 root, BTRFS_I(inode));
Filipe Manana471d5572018-04-05 22:55:12 +01002702 }
2703 iput(inode);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002704 if (ret)
2705 break;
Chris Masone02119d2008-09-05 16:13:11 -04002706 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002707
Chris Masone02119d2008-09-05 16:13:11 -04002708 ret = link_to_fixup_dir(wc->trans, root,
2709 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002710 if (ret)
2711 break;
Chris Masone02119d2008-09-05 16:13:11 -04002712 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002713
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002714 if (wc->ignore_cur_inode)
2715 continue;
2716
Josef Bacikdd8e7212013-09-11 11:57:23 -04002717 if (key.type == BTRFS_DIR_INDEX_KEY &&
2718 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2719 ret = replay_one_dir_item(wc->trans, root, path,
2720 eb, i, &key);
2721 if (ret)
2722 break;
2723 }
2724
Chris Masone02119d2008-09-05 16:13:11 -04002725 if (wc->stage < LOG_WALK_REPLAY_ALL)
2726 continue;
2727
2728 /* these keys are simply copied */
2729 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2730 ret = overwrite_item(wc->trans, root, path,
2731 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002732 if (ret)
2733 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002734 } else if (key.type == BTRFS_INODE_REF_KEY ||
2735 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002736 ret = add_inode_ref(wc->trans, root, log, path,
2737 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002738 if (ret && ret != -ENOENT)
2739 break;
2740 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002741 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2742 ret = replay_one_extent(wc->trans, root, path,
2743 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002744 if (ret)
2745 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002746 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002747 ret = replay_one_dir_item(wc->trans, root, path,
2748 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002749 if (ret)
2750 break;
Chris Masone02119d2008-09-05 16:13:11 -04002751 }
2752 }
2753 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002754 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002755}
2756
Nikolay Borisov6787bb92020-01-20 16:09:10 +02002757/*
2758 * Correctly adjust the reserved bytes occupied by a log tree extent buffer
2759 */
2760static void unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start)
2761{
2762 struct btrfs_block_group *cache;
2763
2764 cache = btrfs_lookup_block_group(fs_info, start);
2765 if (!cache) {
2766 btrfs_err(fs_info, "unable to find block group for %llu", start);
2767 return;
2768 }
2769
2770 spin_lock(&cache->space_info->lock);
2771 spin_lock(&cache->lock);
2772 cache->reserved -= fs_info->nodesize;
2773 cache->space_info->bytes_reserved -= fs_info->nodesize;
2774 spin_unlock(&cache->lock);
2775 spin_unlock(&cache->space_info->lock);
2776
2777 btrfs_put_block_group(cache);
2778}
2779
Chris Masond3977122009-01-05 21:25:51 -05002780static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002781 struct btrfs_root *root,
2782 struct btrfs_path *path, int *level,
2783 struct walk_control *wc)
2784{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002785 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002786 u64 bytenr;
2787 u64 ptr_gen;
2788 struct extent_buffer *next;
2789 struct extent_buffer *cur;
Chris Masone02119d2008-09-05 16:13:11 -04002790 u32 blocksize;
2791 int ret = 0;
2792
Chris Masond3977122009-01-05 21:25:51 -05002793 while (*level > 0) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002794 struct btrfs_key first_key;
2795
Chris Masone02119d2008-09-05 16:13:11 -04002796 cur = path->nodes[*level];
2797
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302798 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002799
2800 if (path->slots[*level] >=
2801 btrfs_header_nritems(cur))
2802 break;
2803
2804 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2805 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08002806 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002807 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002808
Josef Bacik3fbaf252020-11-05 10:45:20 -05002809 next = btrfs_find_create_tree_block(fs_info, bytenr,
2810 btrfs_header_owner(cur),
2811 *level - 1);
Liu Boc871b0f2016-06-06 12:01:23 -07002812 if (IS_ERR(next))
2813 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002814
Chris Masone02119d2008-09-05 16:13:11 -04002815 if (*level == 1) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002816 ret = wc->process_func(root, next, wc, ptr_gen,
2817 *level - 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002818 if (ret) {
2819 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002820 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002821 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002822
Chris Masone02119d2008-09-05 16:13:11 -04002823 path->slots[*level]++;
2824 if (wc->free) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002825 ret = btrfs_read_buffer(next, ptr_gen,
2826 *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002827 if (ret) {
2828 free_extent_buffer(next);
2829 return ret;
2830 }
Chris Masone02119d2008-09-05 16:13:11 -04002831
Josef Bacik681ae502013-10-07 15:11:00 -04002832 if (trans) {
2833 btrfs_tree_lock(next);
David Sterba6a884d7d2019-03-20 14:30:02 +01002834 btrfs_clean_tree_block(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002835 btrfs_wait_tree_block_writeback(next);
2836 btrfs_tree_unlock(next);
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02002837 ret = btrfs_pin_reserved_extent(trans,
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002838 bytenr, blocksize);
2839 if (ret) {
2840 free_extent_buffer(next);
2841 return ret;
2842 }
Naohiro Aotad35751562021-02-04 19:21:54 +09002843 btrfs_redirty_list_add(
2844 trans->transaction, next);
Liu Bo18464302018-01-25 11:02:51 -07002845 } else {
2846 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2847 clear_extent_buffer_dirty(next);
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002848 unaccount_log_buffer(fs_info, bytenr);
Josef Bacik36508602013-04-25 16:23:32 -04002849 }
Chris Masone02119d2008-09-05 16:13:11 -04002850 }
2851 free_extent_buffer(next);
2852 continue;
2853 }
Qu Wenruo581c1762018-03-29 09:08:11 +08002854 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002855 if (ret) {
2856 free_extent_buffer(next);
2857 return ret;
2858 }
Chris Masone02119d2008-09-05 16:13:11 -04002859
Chris Masone02119d2008-09-05 16:13:11 -04002860 if (path->nodes[*level-1])
2861 free_extent_buffer(path->nodes[*level-1]);
2862 path->nodes[*level-1] = next;
2863 *level = btrfs_header_level(next);
2864 path->slots[*level] = 0;
2865 cond_resched();
2866 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002867 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002868
2869 cond_resched();
2870 return 0;
2871}
2872
Chris Masond3977122009-01-05 21:25:51 -05002873static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002874 struct btrfs_root *root,
2875 struct btrfs_path *path, int *level,
2876 struct walk_control *wc)
2877{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002878 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002879 int i;
2880 int slot;
2881 int ret;
2882
Chris Masond3977122009-01-05 21:25:51 -05002883 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002884 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002885 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002886 path->slots[i]++;
2887 *level = i;
2888 WARN_ON(*level == 0);
2889 return 0;
2890 } else {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002891 ret = wc->process_func(root, path->nodes[*level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002892 btrfs_header_generation(path->nodes[*level]),
2893 *level);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002894 if (ret)
2895 return ret;
2896
Chris Masone02119d2008-09-05 16:13:11 -04002897 if (wc->free) {
2898 struct extent_buffer *next;
2899
2900 next = path->nodes[*level];
2901
Josef Bacik681ae502013-10-07 15:11:00 -04002902 if (trans) {
2903 btrfs_tree_lock(next);
David Sterba6a884d7d2019-03-20 14:30:02 +01002904 btrfs_clean_tree_block(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002905 btrfs_wait_tree_block_writeback(next);
2906 btrfs_tree_unlock(next);
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02002907 ret = btrfs_pin_reserved_extent(trans,
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002908 path->nodes[*level]->start,
2909 path->nodes[*level]->len);
2910 if (ret)
2911 return ret;
Naohiro Aota84c25442021-11-30 12:40:21 +09002912 btrfs_redirty_list_add(trans->transaction,
2913 next);
Liu Bo18464302018-01-25 11:02:51 -07002914 } else {
2915 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2916 clear_extent_buffer_dirty(next);
Chris Masone02119d2008-09-05 16:13:11 -04002917
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002918 unaccount_log_buffer(fs_info,
2919 path->nodes[*level]->start);
2920 }
Chris Masone02119d2008-09-05 16:13:11 -04002921 }
2922 free_extent_buffer(path->nodes[*level]);
2923 path->nodes[*level] = NULL;
2924 *level = i + 1;
2925 }
2926 }
2927 return 1;
2928}
2929
2930/*
2931 * drop the reference count on the tree rooted at 'snap'. This traverses
2932 * the tree freeing any blocks that have a ref count of zero after being
2933 * decremented.
2934 */
2935static int walk_log_tree(struct btrfs_trans_handle *trans,
2936 struct btrfs_root *log, struct walk_control *wc)
2937{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002938 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002939 int ret = 0;
2940 int wret;
2941 int level;
2942 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002943 int orig_level;
2944
2945 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002946 if (!path)
2947 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002948
2949 level = btrfs_header_level(log->node);
2950 orig_level = level;
2951 path->nodes[level] = log->node;
David Sterba67439da2019-10-08 13:28:47 +02002952 atomic_inc(&log->node->refs);
Chris Masone02119d2008-09-05 16:13:11 -04002953 path->slots[level] = 0;
2954
Chris Masond3977122009-01-05 21:25:51 -05002955 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002956 wret = walk_down_log_tree(trans, log, path, &level, wc);
2957 if (wret > 0)
2958 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002959 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002960 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002961 goto out;
2962 }
Chris Masone02119d2008-09-05 16:13:11 -04002963
2964 wret = walk_up_log_tree(trans, log, path, &level, wc);
2965 if (wret > 0)
2966 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002967 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002968 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002969 goto out;
2970 }
Chris Masone02119d2008-09-05 16:13:11 -04002971 }
2972
2973 /* was the root node processed? if not, catch it here */
2974 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002975 ret = wc->process_func(log, path->nodes[orig_level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002976 btrfs_header_generation(path->nodes[orig_level]),
2977 orig_level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002978 if (ret)
2979 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002980 if (wc->free) {
2981 struct extent_buffer *next;
2982
2983 next = path->nodes[orig_level];
2984
Josef Bacik681ae502013-10-07 15:11:00 -04002985 if (trans) {
2986 btrfs_tree_lock(next);
David Sterba6a884d7d2019-03-20 14:30:02 +01002987 btrfs_clean_tree_block(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002988 btrfs_wait_tree_block_writeback(next);
2989 btrfs_tree_unlock(next);
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02002990 ret = btrfs_pin_reserved_extent(trans,
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002991 next->start, next->len);
2992 if (ret)
2993 goto out;
Naohiro Aota84c25442021-11-30 12:40:21 +09002994 btrfs_redirty_list_add(trans->transaction, next);
Liu Bo18464302018-01-25 11:02:51 -07002995 } else {
2996 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2997 clear_extent_buffer_dirty(next);
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002998 unaccount_log_buffer(fs_info, next->start);
Josef Bacik681ae502013-10-07 15:11:00 -04002999 }
Chris Masone02119d2008-09-05 16:13:11 -04003000 }
3001 }
3002
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003003out:
Chris Masone02119d2008-09-05 16:13:11 -04003004 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003005 return ret;
3006}
3007
Yan Zheng7237f182009-01-21 12:54:03 -05003008/*
3009 * helper function to update the item for a given subvolumes log root
3010 * in the tree of log roots
3011 */
3012static int update_log_root(struct btrfs_trans_handle *trans,
Josef Bacik4203e962019-09-30 16:27:25 -04003013 struct btrfs_root *log,
3014 struct btrfs_root_item *root_item)
Yan Zheng7237f182009-01-21 12:54:03 -05003015{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003016 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05003017 int ret;
3018
3019 if (log->log_transid == 1) {
3020 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003021 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Josef Bacik4203e962019-09-30 16:27:25 -04003022 &log->root_key, root_item);
Yan Zheng7237f182009-01-21 12:54:03 -05003023 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003024 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Josef Bacik4203e962019-09-30 16:27:25 -04003025 &log->root_key, root_item);
Yan Zheng7237f182009-01-21 12:54:03 -05003026 }
3027 return ret;
3028}
3029
Zhaolei60d53eb2015-08-17 18:44:46 +08003030static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04003031{
3032 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05003033 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04003034
Yan Zheng7237f182009-01-21 12:54:03 -05003035 /*
3036 * we only allow two pending log transactions at a time,
3037 * so we know that if ours is more than 2 older than the
3038 * current transaction, we're done
3039 */
Liu Bo49e83f52017-09-01 16:14:30 -06003040 for (;;) {
Yan Zheng7237f182009-01-21 12:54:03 -05003041 prepare_to_wait(&root->log_commit_wait[index],
3042 &wait, TASK_UNINTERRUPTIBLE);
Liu Bo49e83f52017-09-01 16:14:30 -06003043
3044 if (!(root->log_transid_committed < transid &&
3045 atomic_read(&root->log_commit[index])))
3046 break;
3047
Yan Zheng7237f182009-01-21 12:54:03 -05003048 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06003049 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05003050 mutex_lock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06003051 }
3052 finish_wait(&root->log_commit_wait[index], &wait);
Yan Zheng7237f182009-01-21 12:54:03 -05003053}
3054
Zhaolei60d53eb2015-08-17 18:44:46 +08003055static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05003056{
3057 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08003058
Liu Bo49e83f52017-09-01 16:14:30 -06003059 for (;;) {
3060 prepare_to_wait(&root->log_writer_wait, &wait,
3061 TASK_UNINTERRUPTIBLE);
3062 if (!atomic_read(&root->log_writers))
3063 break;
3064
Yan Zheng7237f182009-01-21 12:54:03 -05003065 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06003066 schedule();
Filipe Manana575849e2015-02-11 11:12:39 +00003067 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05003068 }
Liu Bo49e83f52017-09-01 16:14:30 -06003069 finish_wait(&root->log_writer_wait, &wait);
Chris Masone02119d2008-09-05 16:13:11 -04003070}
3071
Miao Xie8b050d32014-02-20 18:08:58 +08003072static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
3073 struct btrfs_log_ctx *ctx)
3074{
Miao Xie8b050d32014-02-20 18:08:58 +08003075 mutex_lock(&root->log_mutex);
3076 list_del_init(&ctx->list);
3077 mutex_unlock(&root->log_mutex);
3078}
3079
3080/*
3081 * Invoked in log mutex context, or be sure there is no other task which
3082 * can access the list.
3083 */
3084static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
3085 int index, int error)
3086{
3087 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07003088 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08003089
Chris Mason570dd452016-10-27 10:42:20 -07003090 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
3091 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08003092 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07003093 }
Miao Xie8b050d32014-02-20 18:08:58 +08003094}
3095
Chris Masone02119d2008-09-05 16:13:11 -04003096/*
3097 * btrfs_sync_log does sends a given tree log down to the disk and
3098 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04003099 * you know that any inodes previously logged are safely on disk only
3100 * if it returns 0.
3101 *
3102 * Any other return value means you need to call btrfs_commit_transaction.
3103 * Some of the edge cases for fsyncing directories that have had unlinks
3104 * or renames done in the past mean that sometimes the only safe
3105 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
3106 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04003107 */
3108int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08003109 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003110{
Yan Zheng7237f182009-01-21 12:54:03 -05003111 int index1;
3112 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003113 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04003114 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003115 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003116 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003117 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Josef Bacik4203e962019-09-30 16:27:25 -04003118 struct btrfs_root_item new_root_item;
Miao Xiebb14a592014-02-20 18:08:56 +08003119 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08003120 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00003121 struct blk_plug plug;
Filipe Manana47876f72020-11-25 12:19:28 +00003122 u64 log_root_start;
3123 u64 log_root_level;
Chris Masone02119d2008-09-05 16:13:11 -04003124
Yan Zheng7237f182009-01-21 12:54:03 -05003125 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08003126 log_transid = ctx->log_transid;
3127 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05003128 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003129 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04003130 }
Miao Xied1433de2014-02-20 18:08:59 +08003131
3132 index1 = log_transid % 2;
3133 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003134 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08003135 mutex_unlock(&root->log_mutex);
3136 return ctx->log_ret;
3137 }
3138 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003139 atomic_set(&root->log_commit[index1], 1);
3140
3141 /* wait for previous tree log sync to complete */
3142 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08003143 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08003144
Yan, Zheng86df7eb2009-10-14 09:24:59 -04003145 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06003146 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04003147 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003148 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08003149 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04003150 mutex_unlock(&root->log_mutex);
3151 schedule_timeout_uninterruptible(1);
3152 mutex_lock(&root->log_mutex);
3153 }
Zhaolei60d53eb2015-08-17 18:44:46 +08003154 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06003155 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04003156 break;
3157 }
Chris Masond0c803c2008-09-11 16:17:57 -04003158
Chris Mason12fcfd22009-03-24 10:24:20 -04003159 /* bail out if we need to do a full commit */
David Sterba4884b8e2019-03-20 13:25:34 +01003160 if (btrfs_need_log_full_commit(trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003161 ret = -EAGAIN;
3162 mutex_unlock(&root->log_mutex);
3163 goto out;
3164 }
3165
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003166 if (log_transid % 2 == 0)
3167 mark = EXTENT_DIRTY;
3168 else
3169 mark = EXTENT_NEW;
3170
Chris Mason690587d2009-10-13 13:29:19 -04003171 /* we start IO on all the marked extents here, but we don't actually
3172 * wait for them until later.
3173 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00003174 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003175 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Naohiro Aotab528f462021-02-05 23:58:36 +09003176 /*
3177 * -EAGAIN happens when someone, e.g., a concurrent transaction
3178 * commit, writes a dirty extent in this tree-log commit. This
3179 * concurrent write will create a hole writing out the extents,
3180 * and we cannot proceed on a zoned filesystem, requiring
3181 * sequential writing. While we can bail out to a full commit
3182 * here, but we can continue hoping the concurrent writing fills
3183 * the hole.
3184 */
3185 if (ret == -EAGAIN && btrfs_is_zoned(fs_info))
3186 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003187 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003188 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04003189 btrfs_abort_transaction(trans, ret);
David Sterba90787762019-03-20 13:28:05 +01003190 btrfs_set_log_full_commit(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003191 mutex_unlock(&root->log_mutex);
3192 goto out;
3193 }
Yan Zheng7237f182009-01-21 12:54:03 -05003194
Josef Bacik4203e962019-09-30 16:27:25 -04003195 /*
3196 * We _must_ update under the root->log_mutex in order to make sure we
3197 * have a consistent view of the log root we are trying to commit at
3198 * this moment.
3199 *
3200 * We _must_ copy this into a local copy, because we are not holding the
3201 * log_root_tree->log_mutex yet. This is important because when we
3202 * commit the log_root_tree we must have a consistent view of the
3203 * log_root_tree when we update the super block to point at the
3204 * log_root_tree bytenr. If we update the log_root_tree here we'll race
3205 * with the commit and possibly point at the new block which we may not
3206 * have written out.
3207 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003208 btrfs_set_root_node(&log->root_item, log->node);
Josef Bacik4203e962019-09-30 16:27:25 -04003209 memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
Yan Zheng7237f182009-01-21 12:54:03 -05003210
Yan Zheng7237f182009-01-21 12:54:03 -05003211 root->log_transid++;
3212 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04003213 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05003214 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003215 * IO has been started, blocks of the log tree have WRITTEN flag set
3216 * in their headers. new modifications of the log will be written to
3217 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05003218 */
3219 mutex_unlock(&root->log_mutex);
3220
Naohiro Aota3ddebf22021-02-04 19:22:20 +09003221 if (btrfs_is_zoned(fs_info)) {
Naohiro Aotae75f9fd2021-03-24 23:23:11 +09003222 mutex_lock(&fs_info->tree_root->log_mutex);
Naohiro Aota3ddebf22021-02-04 19:22:20 +09003223 if (!log_root_tree->node) {
3224 ret = btrfs_alloc_log_tree_node(trans, log_root_tree);
3225 if (ret) {
Filipe Mananaea32af42021-07-07 12:23:45 +01003226 mutex_unlock(&fs_info->tree_root->log_mutex);
Naohiro Aota3ddebf22021-02-04 19:22:20 +09003227 goto out;
3228 }
3229 }
Naohiro Aotae75f9fd2021-03-24 23:23:11 +09003230 mutex_unlock(&fs_info->tree_root->log_mutex);
Naohiro Aota3ddebf22021-02-04 19:22:20 +09003231 }
3232
Naohiro Aotae75f9fd2021-03-24 23:23:11 +09003233 btrfs_init_log_ctx(&root_log_ctx, NULL);
3234
3235 mutex_lock(&log_root_tree->log_mutex);
3236
Filipe Mananae3d3b412021-03-11 15:13:30 +00003237 index2 = log_root_tree->log_transid % 2;
3238 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3239 root_log_ctx.log_transid = log_root_tree->log_transid;
3240
Josef Bacik4203e962019-09-30 16:27:25 -04003241 /*
3242 * Now we are safe to update the log_root_tree because we're under the
3243 * log_mutex, and we're a current writer so we're holding the commit
3244 * open until we drop the log_mutex.
3245 */
3246 ret = update_log_root(trans, log, &new_root_item);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003247 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08003248 if (!list_empty(&root_log_ctx.list))
3249 list_del_init(&root_log_ctx.list);
3250
Miao Xiec6adc9c2013-05-28 10:05:39 +00003251 blk_finish_plug(&plug);
David Sterba90787762019-03-20 13:28:05 +01003252 btrfs_set_log_full_commit(trans);
Miao Xie995946d2014-04-02 19:51:06 +08003253
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003254 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003255 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003256 mutex_unlock(&log_root_tree->log_mutex);
3257 goto out;
3258 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003259 btrfs_wait_tree_log_extents(log, mark);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003260 mutex_unlock(&log_root_tree->log_mutex);
3261 ret = -EAGAIN;
3262 goto out;
3263 }
3264
Miao Xied1433de2014-02-20 18:08:59 +08003265 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08003266 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07003267 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08003268 mutex_unlock(&log_root_tree->log_mutex);
3269 ret = root_log_ctx.log_ret;
3270 goto out;
3271 }
Miao Xie8b050d32014-02-20 18:08:58 +08003272
Miao Xied1433de2014-02-20 18:08:59 +08003273 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05003274 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003275 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003276 ret = btrfs_wait_tree_log_extents(log, mark);
Zhaolei60d53eb2015-08-17 18:44:46 +08003277 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003278 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003279 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003280 if (!ret)
3281 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05003282 goto out;
3283 }
Miao Xied1433de2014-02-20 18:08:59 +08003284 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003285 atomic_set(&log_root_tree->log_commit[index2], 1);
3286
Chris Mason12fcfd22009-03-24 10:24:20 -04003287 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003288 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003289 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04003290 }
Yan Zheng7237f182009-01-21 12:54:03 -05003291
Chris Mason12fcfd22009-03-24 10:24:20 -04003292 /*
3293 * now that we've moved on to the tree of log tree roots,
3294 * check the full commit flag again
3295 */
David Sterba4884b8e2019-03-20 13:25:34 +01003296 if (btrfs_need_log_full_commit(trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003297 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003298 btrfs_wait_tree_log_extents(log, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04003299 mutex_unlock(&log_root_tree->log_mutex);
3300 ret = -EAGAIN;
3301 goto out_wake_log_root;
3302 }
Yan Zheng7237f182009-01-21 12:54:03 -05003303
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003304 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00003305 &log_root_tree->dirty_log_pages,
3306 EXTENT_DIRTY | EXTENT_NEW);
3307 blk_finish_plug(&plug);
Naohiro Aotab528f462021-02-05 23:58:36 +09003308 /*
3309 * As described above, -EAGAIN indicates a hole in the extents. We
3310 * cannot wait for these write outs since the waiting cause a
3311 * deadlock. Bail out to the full commit instead.
3312 */
3313 if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) {
3314 btrfs_set_log_full_commit(trans);
3315 btrfs_wait_tree_log_extents(log, mark);
3316 mutex_unlock(&log_root_tree->log_mutex);
3317 goto out_wake_log_root;
3318 } else if (ret) {
David Sterba90787762019-03-20 13:28:05 +01003319 btrfs_set_log_full_commit(trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003320 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003321 mutex_unlock(&log_root_tree->log_mutex);
3322 goto out_wake_log_root;
3323 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003324 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003325 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003326 ret = btrfs_wait_tree_log_extents(log_root_tree,
3327 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003328 if (ret) {
David Sterba90787762019-03-20 13:28:05 +01003329 btrfs_set_log_full_commit(trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003330 mutex_unlock(&log_root_tree->log_mutex);
3331 goto out_wake_log_root;
3332 }
Chris Masone02119d2008-09-05 16:13:11 -04003333
Filipe Manana47876f72020-11-25 12:19:28 +00003334 log_root_start = log_root_tree->node->start;
3335 log_root_level = btrfs_header_level(log_root_tree->node);
Yan Zheng7237f182009-01-21 12:54:03 -05003336 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05003337 mutex_unlock(&log_root_tree->log_mutex);
3338
3339 /*
Filipe Manana47876f72020-11-25 12:19:28 +00003340 * Here we are guaranteed that nobody is going to write the superblock
3341 * for the current transaction before us and that neither we do write
3342 * our superblock before the previous transaction finishes its commit
3343 * and writes its superblock, because:
3344 *
3345 * 1) We are holding a handle on the current transaction, so no body
3346 * can commit it until we release the handle;
3347 *
3348 * 2) Before writing our superblock we acquire the tree_log_mutex, so
3349 * if the previous transaction is still committing, and hasn't yet
3350 * written its superblock, we wait for it to do it, because a
3351 * transaction commit acquires the tree_log_mutex when the commit
3352 * begins and releases it only after writing its superblock.
Yan Zheng7237f182009-01-21 12:54:03 -05003353 */
Filipe Manana47876f72020-11-25 12:19:28 +00003354 mutex_lock(&fs_info->tree_log_mutex);
Josef Bacik165ea852021-05-19 17:15:53 -04003355
3356 /*
3357 * The previous transaction writeout phase could have failed, and thus
3358 * marked the fs in an error state. We must not commit here, as we
3359 * could have updated our generation in the super_for_commit and
3360 * writing the super here would result in transid mismatches. If there
3361 * is an error here just bail.
3362 */
Josef Bacik84961532021-10-05 16:35:25 -04003363 if (BTRFS_FS_ERROR(fs_info)) {
Josef Bacik165ea852021-05-19 17:15:53 -04003364 ret = -EIO;
3365 btrfs_set_log_full_commit(trans);
3366 btrfs_abort_transaction(trans, ret);
3367 mutex_unlock(&fs_info->tree_log_mutex);
3368 goto out_wake_log_root;
3369 }
3370
Filipe Manana47876f72020-11-25 12:19:28 +00003371 btrfs_set_super_log_root(fs_info->super_for_commit, log_root_start);
3372 btrfs_set_super_log_root_level(fs_info->super_for_commit, log_root_level);
David Sterbaeece6a92017-02-10 19:04:32 +01003373 ret = write_all_supers(fs_info, 1);
Filipe Manana47876f72020-11-25 12:19:28 +00003374 mutex_unlock(&fs_info->tree_log_mutex);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003375 if (ret) {
David Sterba90787762019-03-20 13:28:05 +01003376 btrfs_set_log_full_commit(trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003377 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003378 goto out_wake_log_root;
3379 }
Yan Zheng7237f182009-01-21 12:54:03 -05003380
Filipe Mananae1a6d262021-07-20 16:03:41 +01003381 /*
3382 * We know there can only be one task here, since we have not yet set
3383 * root->log_commit[index1] to 0 and any task attempting to sync the
3384 * log must wait for the previous log transaction to commit if it's
3385 * still in progress or wait for the current log transaction commit if
3386 * someone else already started it. We use <= and not < because the
3387 * first log transaction has an ID of 0.
3388 */
3389 ASSERT(root->last_log_commit <= log_transid);
3390 root->last_log_commit = log_transid;
Chris Mason257c62e2009-10-13 13:21:08 -04003391
Chris Mason12fcfd22009-03-24 10:24:20 -04003392out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07003393 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003394 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3395
Miao Xied1433de2014-02-20 18:08:59 +08003396 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003397 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003398 mutex_unlock(&log_root_tree->log_mutex);
3399
David Sterba33a9eca2015-10-10 18:35:10 +02003400 /*
David Sterba093258e2018-02-26 16:15:17 +01003401 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3402 * all the updates above are seen by the woken threads. It might not be
3403 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003404 */
David Sterba093258e2018-02-26 16:15:17 +01003405 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04003406out:
Miao Xied1433de2014-02-20 18:08:59 +08003407 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07003408 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08003409 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003410 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003411 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003412
David Sterba33a9eca2015-10-10 18:35:10 +02003413 /*
David Sterba093258e2018-02-26 16:15:17 +01003414 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3415 * all the updates above are seen by the woken threads. It might not be
3416 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003417 */
David Sterba093258e2018-02-26 16:15:17 +01003418 cond_wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003419 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003420}
3421
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003422static void free_log_tree(struct btrfs_trans_handle *trans,
3423 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003424{
3425 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04003426 struct walk_control wc = {
3427 .free = 1,
3428 .process_func = process_one_buffer
3429 };
3430
Naohiro Aota3ddebf22021-02-04 19:22:20 +09003431 if (log->node) {
3432 ret = walk_log_tree(trans, log, &wc);
3433 if (ret) {
3434 if (trans)
3435 btrfs_abort_transaction(trans, ret);
3436 else
3437 btrfs_handle_fs_error(log->fs_info, ret, NULL);
3438 }
Jeff Mahoney374b0e22018-09-06 16:59:33 -04003439 }
Chris Masone02119d2008-09-05 16:13:11 -04003440
Filipe Manana59b07132018-11-09 10:43:08 +00003441 clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
3442 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Filipe Mananae289f032020-05-18 12:14:50 +01003443 extent_io_tree_release(&log->log_csum_range);
Naohiro Aotad35751562021-02-04 19:21:54 +09003444
Josef Bacik00246522020-01-24 09:33:01 -05003445 btrfs_put_root(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003446}
3447
3448/*
3449 * free all the extents used by the tree log. This should be called
3450 * at commit time of the full transaction
3451 */
3452int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3453{
3454 if (root->log_root) {
3455 free_log_tree(trans, root->log_root);
3456 root->log_root = NULL;
Filipe Mananae7a79812020-06-15 10:38:44 +01003457 clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003458 }
3459 return 0;
3460}
3461
3462int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3463 struct btrfs_fs_info *fs_info)
3464{
3465 if (fs_info->log_root_tree) {
3466 free_log_tree(trans, fs_info->log_root_tree);
3467 fs_info->log_root_tree = NULL;
Filipe Manana47876f72020-11-25 12:19:28 +00003468 clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &fs_info->tree_root->state);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003469 }
Chris Masone02119d2008-09-05 16:13:11 -04003470 return 0;
3471}
3472
3473/*
Filipe Manana6e8e7772021-07-27 11:24:44 +01003474 * Check if an inode was logged in the current transaction. This may often
3475 * return some false positives, because logged_trans is an in memory only field,
3476 * not persisted anywhere. This is meant to be used in contexts where a false
3477 * positive has no functional consequences.
Filipe Manana803f0f62019-06-19 13:05:39 +01003478 */
3479static bool inode_logged(struct btrfs_trans_handle *trans,
3480 struct btrfs_inode *inode)
3481{
3482 if (inode->logged_trans == trans->transid)
3483 return true;
3484
Filipe Manana1e0860f2021-08-31 15:30:31 +01003485 if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state))
3486 return false;
3487
Filipe Manana6e8e7772021-07-27 11:24:44 +01003488 /*
3489 * The inode's logged_trans is always 0 when we load it (because it is
3490 * not persisted in the inode item or elsewhere). So if it is 0, the
Filipe Mananad135a532021-07-29 15:29:01 +01003491 * inode was last modified in the current transaction then the inode may
3492 * have been logged before in the current transaction, then evicted and
3493 * loaded again in the current transaction - or may have never been logged
3494 * in the current transaction, but since we can not be sure, we have to
3495 * assume it was, otherwise our callers can leave an inconsistent log.
Filipe Manana6e8e7772021-07-27 11:24:44 +01003496 */
3497 if (inode->logged_trans == 0 &&
3498 inode->last_trans == trans->transid &&
Filipe Manana803f0f62019-06-19 13:05:39 +01003499 !test_bit(BTRFS_FS_LOG_RECOVERING, &trans->fs_info->flags))
3500 return true;
3501
3502 return false;
3503}
3504
3505/*
Chris Masone02119d2008-09-05 16:13:11 -04003506 * If both a file and directory are logged, and unlinks or renames are
3507 * mixed in, we have a few interesting corners:
3508 *
3509 * create file X in dir Y
3510 * link file X to X.link in dir Y
3511 * fsync file X
3512 * unlink file X but leave X.link
3513 * fsync dir Y
3514 *
3515 * After a crash we would expect only X.link to exist. But file X
3516 * didn't get fsync'd again so the log has back refs for X and X.link.
3517 *
3518 * We solve this by removing directory entries and inode backrefs from the
3519 * log when a file that was logged in the current transaction is
3520 * unlinked. Any later fsync will include the updated log entries, and
3521 * we'll be able to reconstruct the proper directory items from backrefs.
3522 *
3523 * This optimizations allows us to avoid relogging the entire inode
3524 * or the entire directory.
3525 */
Josef Bacik9a35fc92021-10-05 16:35:24 -04003526void btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3527 struct btrfs_root *root,
3528 const char *name, int name_len,
3529 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003530{
3531 struct btrfs_root *log;
3532 struct btrfs_dir_item *di;
3533 struct btrfs_path *path;
3534 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003535 int err = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003536 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003537
Filipe Manana803f0f62019-06-19 13:05:39 +01003538 if (!inode_logged(trans, dir))
Josef Bacik9a35fc92021-10-05 16:35:24 -04003539 return;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003540
Chris Masone02119d2008-09-05 16:13:11 -04003541 ret = join_running_log_trans(root);
3542 if (ret)
Josef Bacik9a35fc92021-10-05 16:35:24 -04003543 return;
Chris Masone02119d2008-09-05 16:13:11 -04003544
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003545 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003546
3547 log = root->log_root;
3548 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003549 if (!path) {
3550 err = -ENOMEM;
3551 goto out_unlock;
3552 }
liubo2a29edc2011-01-26 06:22:08 +00003553
Li Zefan33345d012011-04-20 10:31:50 +08003554 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003555 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003556 if (IS_ERR(di)) {
3557 err = PTR_ERR(di);
3558 goto fail;
3559 }
3560 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003561 ret = btrfs_delete_one_dir_name(trans, log, path, di);
Josef Bacik36508602013-04-25 16:23:32 -04003562 if (ret) {
3563 err = ret;
3564 goto fail;
3565 }
Chris Masone02119d2008-09-05 16:13:11 -04003566 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003567 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003568 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003569 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003570 if (IS_ERR(di)) {
3571 err = PTR_ERR(di);
3572 goto fail;
3573 }
3574 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003575 ret = btrfs_delete_one_dir_name(trans, log, path, di);
Josef Bacik36508602013-04-25 16:23:32 -04003576 if (ret) {
3577 err = ret;
3578 goto fail;
3579 }
Chris Masone02119d2008-09-05 16:13:11 -04003580 }
3581
Filipe Mananaddffcf62021-01-27 10:34:54 +00003582 /*
3583 * We do not need to update the size field of the directory's inode item
3584 * because on log replay we update the field to reflect all existing
3585 * entries in the directory (see overwrite_item()).
Chris Masone02119d2008-09-05 16:13:11 -04003586 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003587fail:
Chris Masone02119d2008-09-05 16:13:11 -04003588 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003589out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003590 mutex_unlock(&dir->log_mutex);
Josef Bacik9a35fc92021-10-05 16:35:24 -04003591 if (err < 0)
David Sterba90787762019-03-20 13:28:05 +01003592 btrfs_set_log_full_commit(trans);
Chris Mason12fcfd22009-03-24 10:24:20 -04003593 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003594}
3595
3596/* see comments for btrfs_del_dir_entries_in_log */
Josef Bacik9a35fc92021-10-05 16:35:24 -04003597void btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3598 struct btrfs_root *root,
3599 const char *name, int name_len,
3600 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003601{
3602 struct btrfs_root *log;
3603 u64 index;
3604 int ret;
3605
Filipe Manana803f0f62019-06-19 13:05:39 +01003606 if (!inode_logged(trans, inode))
Josef Bacik9a35fc92021-10-05 16:35:24 -04003607 return;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003608
Chris Masone02119d2008-09-05 16:13:11 -04003609 ret = join_running_log_trans(root);
3610 if (ret)
Josef Bacik9a35fc92021-10-05 16:35:24 -04003611 return;
Chris Masone02119d2008-09-05 16:13:11 -04003612 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003613 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003614
Nikolay Borisova491abb2017-01-18 00:31:33 +02003615 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003616 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003617 mutex_unlock(&inode->log_mutex);
Josef Bacik9a35fc92021-10-05 16:35:24 -04003618 if (ret < 0 && ret != -ENOENT)
David Sterba90787762019-03-20 13:28:05 +01003619 btrfs_set_log_full_commit(trans);
Chris Mason12fcfd22009-03-24 10:24:20 -04003620 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003621}
3622
3623/*
3624 * creates a range item in the log for 'dirid'. first_offset and
3625 * last_offset tell us which parts of the key space the log should
3626 * be considered authoritative for.
3627 */
3628static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3629 struct btrfs_root *log,
3630 struct btrfs_path *path,
3631 int key_type, u64 dirid,
3632 u64 first_offset, u64 last_offset)
3633{
3634 int ret;
3635 struct btrfs_key key;
3636 struct btrfs_dir_log_item *item;
3637
3638 key.objectid = dirid;
3639 key.offset = first_offset;
3640 if (key_type == BTRFS_DIR_ITEM_KEY)
3641 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3642 else
3643 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3644 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003645 if (ret)
3646 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003647
3648 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3649 struct btrfs_dir_log_item);
3650 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3651 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003652 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003653 return 0;
3654}
3655
Filipe Manana086dcbf2021-09-16 11:32:13 +01003656static int flush_dir_items_batch(struct btrfs_trans_handle *trans,
3657 struct btrfs_root *log,
3658 struct extent_buffer *src,
3659 struct btrfs_path *dst_path,
3660 int start_slot,
3661 int count)
3662{
3663 char *ins_data = NULL;
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003664 struct btrfs_item_batch batch;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003665 struct extent_buffer *dst;
Filipe Mananada1b8112021-09-24 12:28:15 +01003666 unsigned long src_offset;
3667 unsigned long dst_offset;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003668 struct btrfs_key key;
3669 u32 item_size;
3670 int ret;
3671 int i;
3672
3673 ASSERT(count > 0);
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003674 batch.nr = count;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003675
3676 if (count == 1) {
3677 btrfs_item_key_to_cpu(src, &key, start_slot);
3678 item_size = btrfs_item_size_nr(src, start_slot);
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003679 batch.keys = &key;
3680 batch.data_sizes = &item_size;
3681 batch.total_data_size = item_size;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003682 } else {
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003683 struct btrfs_key *ins_keys;
3684 u32 *ins_sizes;
3685
Filipe Manana086dcbf2021-09-16 11:32:13 +01003686 ins_data = kmalloc(count * sizeof(u32) +
3687 count * sizeof(struct btrfs_key), GFP_NOFS);
3688 if (!ins_data)
3689 return -ENOMEM;
3690
3691 ins_sizes = (u32 *)ins_data;
3692 ins_keys = (struct btrfs_key *)(ins_data + count * sizeof(u32));
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003693 batch.keys = ins_keys;
3694 batch.data_sizes = ins_sizes;
3695 batch.total_data_size = 0;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003696
3697 for (i = 0; i < count; i++) {
3698 const int slot = start_slot + i;
3699
3700 btrfs_item_key_to_cpu(src, &ins_keys[i], slot);
3701 ins_sizes[i] = btrfs_item_size_nr(src, slot);
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003702 batch.total_data_size += ins_sizes[i];
Filipe Manana086dcbf2021-09-16 11:32:13 +01003703 }
3704 }
3705
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003706 ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
Filipe Manana086dcbf2021-09-16 11:32:13 +01003707 if (ret)
3708 goto out;
3709
3710 dst = dst_path->nodes[0];
Filipe Mananada1b8112021-09-24 12:28:15 +01003711 /*
3712 * Copy all the items in bulk, in a single copy operation. Item data is
3713 * organized such that it's placed at the end of a leaf and from right
3714 * to left. For example, the data for the second item ends at an offset
3715 * that matches the offset where the data for the first item starts, the
3716 * data for the third item ends at an offset that matches the offset
3717 * where the data of the second items starts, and so on.
3718 * Therefore our source and destination start offsets for copy match the
3719 * offsets of the last items (highest slots).
3720 */
3721 dst_offset = btrfs_item_ptr_offset(dst, dst_path->slots[0] + count - 1);
3722 src_offset = btrfs_item_ptr_offset(src, start_slot + count - 1);
3723 copy_extent_buffer(dst, src, dst_offset, src_offset, batch.total_data_size);
Filipe Manana086dcbf2021-09-16 11:32:13 +01003724 btrfs_release_path(dst_path);
3725out:
3726 kfree(ins_data);
3727
3728 return ret;
3729}
3730
Filipe Mananaeb10d852021-09-16 11:32:12 +01003731static int process_dir_items_leaf(struct btrfs_trans_handle *trans,
3732 struct btrfs_inode *inode,
3733 struct btrfs_path *path,
3734 struct btrfs_path *dst_path,
3735 int key_type,
3736 struct btrfs_log_ctx *ctx)
3737{
3738 struct btrfs_root *log = inode->root->log_root;
3739 struct extent_buffer *src = path->nodes[0];
3740 const int nritems = btrfs_header_nritems(src);
3741 const u64 ino = btrfs_ino(inode);
Filipe Manana086dcbf2021-09-16 11:32:13 +01003742 const bool inode_logged_before = inode_logged(trans, inode);
Filipe Mananadc287222021-09-16 11:32:14 +01003743 u64 last_logged_key_offset;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003744 bool last_found = false;
3745 int batch_start = 0;
3746 int batch_size = 0;
Filipe Mananaeb10d852021-09-16 11:32:12 +01003747 int i;
3748
Filipe Mananadc287222021-09-16 11:32:14 +01003749 if (key_type == BTRFS_DIR_ITEM_KEY)
3750 last_logged_key_offset = inode->last_dir_item_offset;
3751 else
3752 last_logged_key_offset = inode->last_dir_index_offset;
3753
Filipe Mananaeb10d852021-09-16 11:32:12 +01003754 for (i = path->slots[0]; i < nritems; i++) {
3755 struct btrfs_key key;
Filipe Mananaeb10d852021-09-16 11:32:12 +01003756 int ret;
3757
3758 btrfs_item_key_to_cpu(src, &key, i);
3759
Filipe Manana086dcbf2021-09-16 11:32:13 +01003760 if (key.objectid != ino || key.type != key_type) {
3761 last_found = true;
3762 break;
3763 }
Filipe Mananaeb10d852021-09-16 11:32:12 +01003764
Filipe Mananadc287222021-09-16 11:32:14 +01003765 ctx->last_dir_item_offset = key.offset;
Filipe Mananaeb10d852021-09-16 11:32:12 +01003766 /*
3767 * We must make sure that when we log a directory entry, the
3768 * corresponding inode, after log replay, has a matching link
3769 * count. For example:
3770 *
3771 * touch foo
3772 * mkdir mydir
3773 * sync
3774 * ln foo mydir/bar
3775 * xfs_io -c "fsync" mydir
3776 * <crash>
3777 * <mount fs and log replay>
3778 *
3779 * Would result in a fsync log that when replayed, our file inode
3780 * would have a link count of 1, but we get two directory entries
3781 * pointing to the same inode. After removing one of the names,
3782 * it would not be possible to remove the other name, which
3783 * resulted always in stale file handle errors, and would not be
3784 * possible to rmdir the parent directory, since its i_size could
3785 * never be decremented to the value BTRFS_EMPTY_DIR_SIZE,
3786 * resulting in -ENOTEMPTY errors.
3787 */
Filipe Manana086dcbf2021-09-16 11:32:13 +01003788 if (!ctx->log_new_dentries) {
3789 struct btrfs_dir_item *di;
3790 struct btrfs_key di_key;
3791
3792 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3793 btrfs_dir_item_key_to_cpu(src, di, &di_key);
3794 if ((btrfs_dir_transid(src, di) == trans->transid ||
3795 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3796 di_key.type != BTRFS_ROOT_ITEM_KEY)
3797 ctx->log_new_dentries = true;
3798 }
3799
3800 if (!inode_logged_before)
3801 goto add_to_batch;
Filipe Mananadc287222021-09-16 11:32:14 +01003802
3803 /*
3804 * If we were logged before and have logged dir items, we can skip
3805 * checking if any item with a key offset larger than the last one
3806 * we logged is in the log tree, saving time and avoiding adding
3807 * contention on the log tree.
3808 */
3809 if (key.offset > last_logged_key_offset)
3810 goto add_to_batch;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003811 /*
3812 * Check if the key was already logged before. If not we can add
3813 * it to a batch for bulk insertion.
3814 */
3815 ret = btrfs_search_slot(NULL, log, &key, dst_path, 0, 0);
3816 if (ret < 0) {
3817 return ret;
3818 } else if (ret > 0) {
3819 btrfs_release_path(dst_path);
3820 goto add_to_batch;
3821 }
3822
3823 /*
3824 * Item exists in the log. Overwrite the item in the log if it
3825 * has different content or do nothing if it has exactly the same
3826 * content. And then flush the current batch if any - do it after
3827 * overwriting the current item, or we would deadlock otherwise,
3828 * since we are holding a path for the existing item.
3829 */
3830 ret = do_overwrite_item(trans, log, dst_path, src, i, &key);
3831 if (ret < 0)
3832 return ret;
3833
3834 if (batch_size > 0) {
3835 ret = flush_dir_items_batch(trans, log, src, dst_path,
3836 batch_start, batch_size);
3837 if (ret < 0)
3838 return ret;
3839 batch_size = 0;
3840 }
3841 continue;
3842add_to_batch:
3843 if (batch_size == 0)
3844 batch_start = i;
3845 batch_size++;
Filipe Mananaeb10d852021-09-16 11:32:12 +01003846 }
3847
Filipe Manana086dcbf2021-09-16 11:32:13 +01003848 if (batch_size > 0) {
3849 int ret;
3850
3851 ret = flush_dir_items_batch(trans, log, src, dst_path,
3852 batch_start, batch_size);
3853 if (ret < 0)
3854 return ret;
3855 }
3856
3857 return last_found ? 1 : 0;
Filipe Mananaeb10d852021-09-16 11:32:12 +01003858}
3859
Chris Masone02119d2008-09-05 16:13:11 -04003860/*
3861 * log all the items included in the current transaction for a given
3862 * directory. This also creates the range items in the log tree required
3863 * to replay anything deleted before the fsync
3864 */
3865static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Filipe Manana90d04512021-09-16 11:32:10 +01003866 struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003867 struct btrfs_path *path,
3868 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003869 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003870 u64 min_offset, u64 *last_offset_ret)
3871{
3872 struct btrfs_key min_key;
Filipe Manana90d04512021-09-16 11:32:10 +01003873 struct btrfs_root *root = inode->root;
Chris Masone02119d2008-09-05 16:13:11 -04003874 struct btrfs_root *log = root->log_root;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003875 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003876 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04003877 u64 first_offset = min_offset;
3878 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003879 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003880
Li Zefan33345d012011-04-20 10:31:50 +08003881 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003882 min_key.type = key_type;
3883 min_key.offset = min_offset;
3884
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003885 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003886
3887 /*
3888 * we didn't find anything from this transaction, see if there
3889 * is anything at all
3890 */
Li Zefan33345d012011-04-20 10:31:50 +08003891 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3892 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003893 min_key.type = key_type;
3894 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003895 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003896 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3897 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003898 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003899 return ret;
3900 }
Li Zefan33345d012011-04-20 10:31:50 +08003901 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003902
3903 /* if ret == 0 there are items for this type,
3904 * create a range to tell us the last key of this type.
3905 * otherwise, there are no items in this directory after
3906 * *min_offset, and we create a range to indicate that.
3907 */
3908 if (ret == 0) {
3909 struct btrfs_key tmp;
3910 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3911 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003912 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003913 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003914 }
3915 goto done;
3916 }
3917
3918 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003919 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003920 if (ret == 0) {
3921 struct btrfs_key tmp;
3922 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3923 if (key_type == tmp.type) {
3924 first_offset = tmp.offset;
3925 ret = overwrite_item(trans, log, dst_path,
3926 path->nodes[0], path->slots[0],
3927 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003928 if (ret) {
3929 err = ret;
3930 goto done;
3931 }
Chris Masone02119d2008-09-05 16:13:11 -04003932 }
3933 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003934 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003935
Josef Bacik2cc83342019-03-06 17:13:04 -05003936 /*
3937 * Find the first key from this transaction again. See the note for
3938 * log_new_dir_dentries, if we're logging a directory recursively we
3939 * won't be holding its i_mutex, which means we can modify the directory
3940 * while we're logging it. If we remove an entry between our first
3941 * search and this search we'll not find the key again and can just
3942 * bail.
3943 */
Filipe Mananabb56f022020-09-14 15:27:50 +01003944search:
Chris Masone02119d2008-09-05 16:13:11 -04003945 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Josef Bacik2cc83342019-03-06 17:13:04 -05003946 if (ret != 0)
Chris Masone02119d2008-09-05 16:13:11 -04003947 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003948
3949 /*
3950 * we have a block from this transaction, log every item in it
3951 * from our directory
3952 */
Chris Masond3977122009-01-05 21:25:51 -05003953 while (1) {
Filipe Mananaeb10d852021-09-16 11:32:12 +01003954 ret = process_dir_items_leaf(trans, inode, path, dst_path,
3955 key_type, ctx);
3956 if (ret != 0) {
3957 if (ret < 0)
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003958 err = ret;
Filipe Mananaeb10d852021-09-16 11:32:12 +01003959 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003960 }
Filipe Mananaeb10d852021-09-16 11:32:12 +01003961 path->slots[0] = btrfs_header_nritems(path->nodes[0]);
Chris Masone02119d2008-09-05 16:13:11 -04003962
3963 /*
3964 * look ahead to the next item and see if it is also
3965 * from this directory and from this transaction
3966 */
3967 ret = btrfs_next_leaf(root, path);
Liu Bo80c0b422018-04-03 01:59:47 +08003968 if (ret) {
3969 if (ret == 1)
3970 last_offset = (u64)-1;
3971 else
3972 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003973 goto done;
3974 }
Filipe Mananaeb10d852021-09-16 11:32:12 +01003975 btrfs_item_key_to_cpu(path->nodes[0], &min_key, path->slots[0]);
3976 if (min_key.objectid != ino || min_key.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003977 last_offset = (u64)-1;
3978 goto done;
3979 }
3980 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3981 ret = overwrite_item(trans, log, dst_path,
3982 path->nodes[0], path->slots[0],
Filipe Mananaeb10d852021-09-16 11:32:12 +01003983 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003984 if (ret)
3985 err = ret;
3986 else
Filipe Mananaeb10d852021-09-16 11:32:12 +01003987 last_offset = min_key.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003988 goto done;
3989 }
Filipe Mananaeb10d852021-09-16 11:32:12 +01003990 if (need_resched()) {
3991 btrfs_release_path(path);
3992 cond_resched();
3993 goto search;
3994 }
Chris Masone02119d2008-09-05 16:13:11 -04003995 }
3996done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003997 btrfs_release_path(path);
3998 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003999
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004000 if (err == 0) {
4001 *last_offset_ret = last_offset;
4002 /*
4003 * insert the log range keys to indicate where the log
4004 * is valid
4005 */
4006 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08004007 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004008 if (ret)
4009 err = ret;
4010 }
4011 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004012}
4013
4014/*
4015 * logging directories is very similar to logging inodes, We find all the items
4016 * from the current transaction and write them to the log.
4017 *
4018 * The recovery code scans the directory in the subvolume, and if it finds a
4019 * key in the range logged that is not present in the log tree, then it means
4020 * that dir entry was unlinked during the transaction.
4021 *
4022 * In order for that scan to work, we must include one key smaller than
4023 * the smallest logged by this transaction and one key larger than the largest
4024 * key logged by this transaction.
4025 */
4026static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Filipe Manana90d04512021-09-16 11:32:10 +01004027 struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04004028 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004029 struct btrfs_path *dst_path,
4030 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004031{
4032 u64 min_key;
4033 u64 max_key;
4034 int ret;
4035 int key_type = BTRFS_DIR_ITEM_KEY;
4036
Filipe Mananadc287222021-09-16 11:32:14 +01004037 /*
4038 * If this is the first time we are being logged in the current
4039 * transaction, or we were logged before but the inode was evicted and
4040 * reloaded later, in which case its logged_trans is 0, reset the values
4041 * of the last logged key offsets. Note that we don't use the helper
4042 * function inode_logged() here - that is because the function returns
4043 * true after an inode eviction, assuming the worst case as it can not
4044 * know for sure if the inode was logged before. So we can not skip key
4045 * searches in the case the inode was evicted, because it may not have
4046 * been logged in this transaction and may have been logged in a past
4047 * transaction, so we need to reset the last dir item and index offsets
4048 * to (u64)-1.
4049 */
4050 if (inode->logged_trans != trans->transid) {
4051 inode->last_dir_item_offset = (u64)-1;
4052 inode->last_dir_index_offset = (u64)-1;
4053 }
Chris Masone02119d2008-09-05 16:13:11 -04004054again:
4055 min_key = 0;
4056 max_key = 0;
Filipe Mananadc287222021-09-16 11:32:14 +01004057 if (key_type == BTRFS_DIR_ITEM_KEY)
4058 ctx->last_dir_item_offset = inode->last_dir_item_offset;
4059 else
4060 ctx->last_dir_item_offset = inode->last_dir_index_offset;
4061
Chris Masond3977122009-01-05 21:25:51 -05004062 while (1) {
Filipe Manana90d04512021-09-16 11:32:10 +01004063 ret = log_dir_items(trans, inode, path, dst_path, key_type,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02004064 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004065 if (ret)
4066 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004067 if (max_key == (u64)-1)
4068 break;
4069 min_key = max_key + 1;
4070 }
4071
4072 if (key_type == BTRFS_DIR_ITEM_KEY) {
Filipe Mananadc287222021-09-16 11:32:14 +01004073 inode->last_dir_item_offset = ctx->last_dir_item_offset;
Chris Masone02119d2008-09-05 16:13:11 -04004074 key_type = BTRFS_DIR_INDEX_KEY;
4075 goto again;
Filipe Mananadc287222021-09-16 11:32:14 +01004076 } else {
4077 inode->last_dir_index_offset = ctx->last_dir_item_offset;
Chris Masone02119d2008-09-05 16:13:11 -04004078 }
4079 return 0;
4080}
4081
4082/*
4083 * a helper function to drop items from the log before we relog an
4084 * inode. max_key_type indicates the highest item type to remove.
4085 * This cannot be run for file data extents because it does not
4086 * free the extents they point to.
4087 */
Filipe Manana88e221c2021-08-31 15:30:35 +01004088static int drop_inode_items(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04004089 struct btrfs_root *log,
4090 struct btrfs_path *path,
Filipe Manana88e221c2021-08-31 15:30:35 +01004091 struct btrfs_inode *inode,
4092 int max_key_type)
Chris Masone02119d2008-09-05 16:13:11 -04004093{
4094 int ret;
4095 struct btrfs_key key;
4096 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04004097 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004098
Filipe Manana88e221c2021-08-31 15:30:35 +01004099 if (!inode_logged(trans, inode))
4100 return 0;
4101
4102 key.objectid = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04004103 key.type = max_key_type;
4104 key.offset = (u64)-1;
4105
Chris Masond3977122009-01-05 21:25:51 -05004106 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004107 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04004108 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004109 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04004110 break;
4111
4112 if (path->slots[0] == 0)
4113 break;
4114
4115 path->slots[0]--;
4116 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4117 path->slots[0]);
4118
Filipe Manana88e221c2021-08-31 15:30:35 +01004119 if (found_key.objectid != key.objectid)
Chris Masone02119d2008-09-05 16:13:11 -04004120 break;
4121
Josef Bacik18ec90d2012-09-28 11:56:28 -04004122 found_key.offset = 0;
4123 found_key.type = 0;
Qu Wenruoe3b83362020-04-17 15:08:21 +08004124 ret = btrfs_bin_search(path->nodes[0], &found_key, &start_slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00004125 if (ret < 0)
4126 break;
Josef Bacik18ec90d2012-09-28 11:56:28 -04004127
4128 ret = btrfs_del_items(trans, log, path, start_slot,
4129 path->slots[0] - start_slot + 1);
4130 /*
4131 * If start slot isn't 0 then we don't need to re-search, we've
4132 * found the last guy with the objectid in this tree.
4133 */
4134 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00004135 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02004136 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004137 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004138 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04004139 if (ret > 0)
4140 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004141 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004142}
4143
Filipe Manana8a2b3da2021-08-31 15:30:36 +01004144static int truncate_inode_items(struct btrfs_trans_handle *trans,
4145 struct btrfs_root *log_root,
4146 struct btrfs_inode *inode,
4147 u64 new_size, u32 min_type)
4148{
4149 int ret;
4150
4151 do {
4152 ret = btrfs_truncate_inode_items(trans, log_root, inode,
4153 new_size, min_type, NULL);
4154 } while (ret == -EAGAIN);
4155
4156 return ret;
4157}
4158
Josef Bacik94edf4a2012-09-25 14:56:25 -04004159static void fill_inode_item(struct btrfs_trans_handle *trans,
4160 struct extent_buffer *leaf,
4161 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004162 struct inode *inode, int log_inode_only,
4163 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004164{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04004165 struct btrfs_map_token token;
Boris Burkov77eea052021-06-30 13:01:48 -07004166 u64 flags;
Josef Bacik94edf4a2012-09-25 14:56:25 -04004167
David Sterbac82f8232019-08-09 17:48:21 +02004168 btrfs_init_map_token(&token, leaf);
Josef Bacik94edf4a2012-09-25 14:56:25 -04004169
4170 if (log_inode_only) {
4171 /* set the generation to zero so the recover code
4172 * can tell the difference between an logging
4173 * just to say 'this inode exists' and a logging
4174 * to say 'update this inode with these values'
4175 */
David Sterbacc4c13d2020-04-29 02:15:56 +02004176 btrfs_set_token_inode_generation(&token, item, 0);
4177 btrfs_set_token_inode_size(&token, item, logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04004178 } else {
David Sterbacc4c13d2020-04-29 02:15:56 +02004179 btrfs_set_token_inode_generation(&token, item,
4180 BTRFS_I(inode)->generation);
4181 btrfs_set_token_inode_size(&token, item, inode->i_size);
Josef Bacik94edf4a2012-09-25 14:56:25 -04004182 }
4183
David Sterbacc4c13d2020-04-29 02:15:56 +02004184 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
4185 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
4186 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
4187 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04004188
David Sterbacc4c13d2020-04-29 02:15:56 +02004189 btrfs_set_token_timespec_sec(&token, &item->atime,
4190 inode->i_atime.tv_sec);
4191 btrfs_set_token_timespec_nsec(&token, &item->atime,
4192 inode->i_atime.tv_nsec);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04004193
David Sterbacc4c13d2020-04-29 02:15:56 +02004194 btrfs_set_token_timespec_sec(&token, &item->mtime,
4195 inode->i_mtime.tv_sec);
4196 btrfs_set_token_timespec_nsec(&token, &item->mtime,
4197 inode->i_mtime.tv_nsec);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04004198
David Sterbacc4c13d2020-04-29 02:15:56 +02004199 btrfs_set_token_timespec_sec(&token, &item->ctime,
4200 inode->i_ctime.tv_sec);
4201 btrfs_set_token_timespec_nsec(&token, &item->ctime,
4202 inode->i_ctime.tv_nsec);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04004203
Filipe Mananae593e542021-01-27 10:34:55 +00004204 /*
4205 * We do not need to set the nbytes field, in fact during a fast fsync
4206 * its value may not even be correct, since a fast fsync does not wait
4207 * for ordered extent completion, which is where we update nbytes, it
4208 * only waits for writeback to complete. During log replay as we find
4209 * file extent items and replay them, we adjust the nbytes field of the
4210 * inode item in subvolume tree as needed (see overwrite_item()).
4211 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04004212
David Sterbacc4c13d2020-04-29 02:15:56 +02004213 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
4214 btrfs_set_token_inode_transid(&token, item, trans->transid);
4215 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
Boris Burkov77eea052021-06-30 13:01:48 -07004216 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
4217 BTRFS_I(inode)->ro_flags);
4218 btrfs_set_token_inode_flags(&token, item, flags);
David Sterbacc4c13d2020-04-29 02:15:56 +02004219 btrfs_set_token_inode_block_group(&token, item, 0);
Josef Bacik94edf4a2012-09-25 14:56:25 -04004220}
4221
Josef Bacika95249b2012-10-11 16:17:34 -04004222static int log_inode_item(struct btrfs_trans_handle *trans,
4223 struct btrfs_root *log, struct btrfs_path *path,
Filipe Manana2ac691d2021-07-20 16:03:43 +01004224 struct btrfs_inode *inode, bool inode_item_dropped)
Josef Bacika95249b2012-10-11 16:17:34 -04004225{
4226 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04004227 int ret;
4228
Filipe Manana2ac691d2021-07-20 16:03:43 +01004229 /*
4230 * If we are doing a fast fsync and the inode was logged before in the
4231 * current transaction, then we know the inode was previously logged and
4232 * it exists in the log tree. For performance reasons, in this case use
4233 * btrfs_search_slot() directly with ins_len set to 0 so that we never
4234 * attempt a write lock on the leaf's parent, which adds unnecessary lock
4235 * contention in case there are concurrent fsyncs for other inodes of the
4236 * same subvolume. Using btrfs_insert_empty_item() when the inode item
4237 * already exists can also result in unnecessarily splitting a leaf.
4238 */
4239 if (!inode_item_dropped && inode->logged_trans == trans->transid) {
4240 ret = btrfs_search_slot(trans, log, &inode->location, path, 0, 1);
4241 ASSERT(ret <= 0);
4242 if (ret > 0)
4243 ret = -ENOENT;
4244 } else {
4245 /*
4246 * This means it is the first fsync in the current transaction,
4247 * so the inode item is not in the log and we need to insert it.
4248 * We can never get -EEXIST because we are only called for a fast
4249 * fsync and in case an inode eviction happens after the inode was
4250 * logged before in the current transaction, when we load again
4251 * the inode, we set BTRFS_INODE_NEEDS_FULL_SYNC on its runtime
4252 * flags and set ->logged_trans to 0.
4253 */
4254 ret = btrfs_insert_empty_item(trans, log, path, &inode->location,
4255 sizeof(*inode_item));
4256 ASSERT(ret != -EEXIST);
4257 }
4258 if (ret)
Josef Bacika95249b2012-10-11 16:17:34 -04004259 return ret;
4260 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4261 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02004262 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
4263 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04004264 btrfs_release_path(path);
4265 return 0;
4266}
4267
Filipe Manana40e046a2019-12-05 16:58:30 +00004268static int log_csums(struct btrfs_trans_handle *trans,
Filipe Manana3ebac172020-07-15 12:30:43 +01004269 struct btrfs_inode *inode,
Filipe Manana40e046a2019-12-05 16:58:30 +00004270 struct btrfs_root *log_root,
4271 struct btrfs_ordered_sum *sums)
4272{
Filipe Mananae289f032020-05-18 12:14:50 +01004273 const u64 lock_end = sums->bytenr + sums->len - 1;
4274 struct extent_state *cached_state = NULL;
Filipe Manana40e046a2019-12-05 16:58:30 +00004275 int ret;
4276
4277 /*
Filipe Manana3ebac172020-07-15 12:30:43 +01004278 * If this inode was not used for reflink operations in the current
4279 * transaction with new extents, then do the fast path, no need to
4280 * worry about logging checksum items with overlapping ranges.
4281 */
4282 if (inode->last_reflink_trans < trans->transid)
4283 return btrfs_csum_file_blocks(trans, log_root, sums);
4284
4285 /*
Filipe Mananae289f032020-05-18 12:14:50 +01004286 * Serialize logging for checksums. This is to avoid racing with the
4287 * same checksum being logged by another task that is logging another
4288 * file which happens to refer to the same extent as well. Such races
4289 * can leave checksum items in the log with overlapping ranges.
4290 */
4291 ret = lock_extent_bits(&log_root->log_csum_range, sums->bytenr,
4292 lock_end, &cached_state);
4293 if (ret)
4294 return ret;
4295 /*
Filipe Manana40e046a2019-12-05 16:58:30 +00004296 * Due to extent cloning, we might have logged a csum item that covers a
4297 * subrange of a cloned extent, and later we can end up logging a csum
4298 * item for a larger subrange of the same extent or the entire range.
4299 * This would leave csum items in the log tree that cover the same range
4300 * and break the searches for checksums in the log tree, resulting in
4301 * some checksums missing in the fs/subvolume tree. So just delete (or
4302 * trim and adjust) any existing csum items in the log for this range.
4303 */
4304 ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len);
Filipe Mananae289f032020-05-18 12:14:50 +01004305 if (!ret)
4306 ret = btrfs_csum_file_blocks(trans, log_root, sums);
Filipe Manana40e046a2019-12-05 16:58:30 +00004307
Filipe Mananae289f032020-05-18 12:14:50 +01004308 unlock_extent_cached(&log_root->log_csum_range, sums->bytenr, lock_end,
4309 &cached_state);
4310
4311 return ret;
Filipe Manana40e046a2019-12-05 16:58:30 +00004312}
4313
Chris Mason31ff1cd2008-09-11 16:17:57 -04004314static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004315 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04004316 struct btrfs_path *dst_path,
Filipe Manana0e563152019-11-19 12:07:33 +00004317 struct btrfs_path *src_path,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004318 int start_slot, int nr, int inode_only,
4319 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04004320{
David Sterba3ffbd682018-06-29 10:56:42 +02004321 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004322 unsigned long src_offset;
4323 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004324 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004325 struct btrfs_file_extent_item *extent;
4326 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04004327 struct extent_buffer *src = src_path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004328 int ret;
4329 struct btrfs_key *ins_keys;
4330 u32 *ins_sizes;
Filipe Mananab7ef5f32021-09-24 12:28:13 +01004331 struct btrfs_item_batch batch;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004332 char *ins_data;
4333 int i;
Chris Masond20f7042008-12-08 16:58:54 -05004334 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004335 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Chris Masond20f7042008-12-08 16:58:54 -05004336
4337 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04004338
4339 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
4340 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00004341 if (!ins_data)
4342 return -ENOMEM;
4343
Chris Mason31ff1cd2008-09-11 16:17:57 -04004344 ins_sizes = (u32 *)ins_data;
4345 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
Filipe Mananab7ef5f32021-09-24 12:28:13 +01004346 batch.keys = ins_keys;
4347 batch.data_sizes = ins_sizes;
4348 batch.total_data_size = 0;
4349 batch.nr = nr;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004350
4351 for (i = 0; i < nr; i++) {
4352 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
Filipe Mananab7ef5f32021-09-24 12:28:13 +01004353 batch.total_data_size += ins_sizes[i];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004354 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
4355 }
Filipe Mananab7ef5f32021-09-24 12:28:13 +01004356 ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004357 if (ret) {
4358 kfree(ins_data);
4359 return ret;
4360 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004361
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004362 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004363 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
4364 dst_path->slots[0]);
4365
4366 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
4367
Josef Bacik94edf4a2012-09-25 14:56:25 -04004368 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004369 inode_item = btrfs_item_ptr(dst_path->nodes[0],
4370 dst_path->slots[0],
4371 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04004372 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01004373 &inode->vfs_inode,
4374 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004375 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04004376 } else {
4377 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
4378 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04004379 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04004380
Chris Mason31ff1cd2008-09-11 16:17:57 -04004381 /* take a reference on file data extents so that truncates
4382 * or deletes of this inode don't have to relog the inode
4383 * again
4384 */
David Sterba962a2982014-06-04 18:41:45 +02004385 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06004386 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004387 int found_type;
4388 extent = btrfs_item_ptr(src, start_slot + i,
4389 struct btrfs_file_extent_item);
4390
liubo8e531cd2011-05-06 10:36:09 +08004391 if (btrfs_file_extent_generation(src, extent) < trans->transid)
4392 continue;
4393
Chris Mason31ff1cd2008-09-11 16:17:57 -04004394 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04004395 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004396 u64 ds, dl, cs, cl;
4397 ds = btrfs_file_extent_disk_bytenr(src,
4398 extent);
4399 /* ds == 0 is a hole */
4400 if (ds == 0)
4401 continue;
4402
4403 dl = btrfs_file_extent_disk_num_bytes(src,
4404 extent);
4405 cs = btrfs_file_extent_offset(src, extent);
4406 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07004407 extent);
Chris Mason580afd72008-12-08 19:15:39 -05004408 if (btrfs_file_extent_compression(src,
4409 extent)) {
4410 cs = 0;
4411 cl = dl;
4412 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004413
4414 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004415 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004416 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01004417 &ordered_sums, 0);
Filipe Manana4f264332020-07-29 10:17:50 +01004418 if (ret)
4419 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004420 }
4421 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004422 }
4423
4424 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004425 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04004426 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05004427
4428 /*
4429 * we have to do this after the loop above to avoid changing the
4430 * log tree while trying to change the log tree.
4431 */
Chris Masond3977122009-01-05 21:25:51 -05004432 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05004433 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4434 struct btrfs_ordered_sum,
4435 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004436 if (!ret)
Filipe Manana3ebac172020-07-15 12:30:43 +01004437 ret = log_csums(trans, inode, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05004438 list_del(&sums->list);
4439 kfree(sums);
4440 }
Josef Bacik16e75492013-10-22 12:18:51 -04004441
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004442 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004443}
4444
Sami Tolvanen4f0f5862021-04-08 11:28:34 -07004445static int extent_cmp(void *priv, const struct list_head *a,
4446 const struct list_head *b)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004447{
David Sterba214cc182021-07-26 14:15:26 +02004448 const struct extent_map *em1, *em2;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004449
4450 em1 = list_entry(a, struct extent_map, list);
4451 em2 = list_entry(b, struct extent_map, list);
4452
4453 if (em1->start < em2->start)
4454 return -1;
4455 else if (em1->start > em2->start)
4456 return 1;
4457 return 0;
4458}
4459
Josef Bacike7175a62018-05-23 11:58:34 -04004460static int log_extent_csums(struct btrfs_trans_handle *trans,
4461 struct btrfs_inode *inode,
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004462 struct btrfs_root *log_root,
Filipe Manana48778172020-08-11 12:43:58 +01004463 const struct extent_map *em,
4464 struct btrfs_log_ctx *ctx)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004465{
Filipe Manana48778172020-08-11 12:43:58 +01004466 struct btrfs_ordered_extent *ordered;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004467 u64 csum_offset;
4468 u64 csum_len;
Filipe Manana48778172020-08-11 12:43:58 +01004469 u64 mod_start = em->mod_start;
4470 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004471 LIST_HEAD(ordered_sums);
4472 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004473
Josef Bacike7175a62018-05-23 11:58:34 -04004474 if (inode->flags & BTRFS_INODE_NODATASUM ||
4475 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
Filipe Manana8407f552014-09-05 15:14:39 +01004476 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04004477 return 0;
4478
Filipe Manana48778172020-08-11 12:43:58 +01004479 list_for_each_entry(ordered, &ctx->ordered_extents, log_list) {
4480 const u64 ordered_end = ordered->file_offset + ordered->num_bytes;
4481 const u64 mod_end = mod_start + mod_len;
4482 struct btrfs_ordered_sum *sums;
4483
4484 if (mod_len == 0)
4485 break;
4486
4487 if (ordered_end <= mod_start)
4488 continue;
4489 if (mod_end <= ordered->file_offset)
4490 break;
4491
4492 /*
4493 * We are going to copy all the csums on this ordered extent, so
4494 * go ahead and adjust mod_start and mod_len in case this ordered
4495 * extent has already been logged.
4496 */
4497 if (ordered->file_offset > mod_start) {
4498 if (ordered_end >= mod_end)
4499 mod_len = ordered->file_offset - mod_start;
4500 /*
4501 * If we have this case
4502 *
4503 * |--------- logged extent ---------|
4504 * |----- ordered extent ----|
4505 *
4506 * Just don't mess with mod_start and mod_len, we'll
4507 * just end up logging more csums than we need and it
4508 * will be ok.
4509 */
4510 } else {
4511 if (ordered_end < mod_end) {
4512 mod_len = mod_end - ordered_end;
4513 mod_start = ordered_end;
4514 } else {
4515 mod_len = 0;
4516 }
4517 }
4518
4519 /*
4520 * To keep us from looping for the above case of an ordered
4521 * extent that falls inside of the logged extent.
4522 */
4523 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags))
4524 continue;
4525
4526 list_for_each_entry(sums, &ordered->list, list) {
4527 ret = log_csums(trans, inode, log_root, sums);
4528 if (ret)
4529 return ret;
4530 }
4531 }
4532
4533 /* We're done, found all csums in the ordered extents. */
4534 if (mod_len == 0)
4535 return 0;
4536
Josef Bacike7175a62018-05-23 11:58:34 -04004537 /* If we're compressed we have to save the entire range of csums. */
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004538 if (em->compress_type) {
4539 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004540 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004541 } else {
Filipe Manana48778172020-08-11 12:43:58 +01004542 csum_offset = mod_start - em->start;
4543 csum_len = mod_len;
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004544 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004545
Josef Bacik70c8a912012-10-11 16:54:30 -04004546 /* block start is already adjusted for the file extent offset. */
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004547 ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004548 em->block_start + csum_offset,
4549 em->block_start + csum_offset +
4550 csum_len - 1, &ordered_sums, 0);
4551 if (ret)
4552 return ret;
4553
4554 while (!list_empty(&ordered_sums)) {
4555 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4556 struct btrfs_ordered_sum,
4557 list);
4558 if (!ret)
Filipe Manana3ebac172020-07-15 12:30:43 +01004559 ret = log_csums(trans, inode, log_root, sums);
Josef Bacik70c8a912012-10-11 16:54:30 -04004560 list_del(&sums->list);
4561 kfree(sums);
4562 }
4563
4564 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004565}
4566
Filipe Manana8407f552014-09-05 15:14:39 +01004567static int log_one_extent(struct btrfs_trans_handle *trans,
Filipe Manana90d04512021-09-16 11:32:10 +01004568 struct btrfs_inode *inode,
Filipe Manana8407f552014-09-05 15:14:39 +01004569 const struct extent_map *em,
4570 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004571 struct btrfs_log_ctx *ctx)
4572{
Filipe Manana5893dfb2020-11-04 11:07:32 +00004573 struct btrfs_drop_extents_args drop_args = { 0 };
Filipe Manana90d04512021-09-16 11:32:10 +01004574 struct btrfs_root *log = inode->root->log_root;
Filipe Manana8407f552014-09-05 15:14:39 +01004575 struct btrfs_file_extent_item *fi;
4576 struct extent_buffer *leaf;
4577 struct btrfs_map_token token;
4578 struct btrfs_key key;
4579 u64 extent_offset = em->start - em->orig_start;
4580 u64 block_len;
4581 int ret;
Filipe Manana8407f552014-09-05 15:14:39 +01004582
Filipe Manana48778172020-08-11 12:43:58 +01004583 ret = log_extent_csums(trans, inode, log, em, ctx);
Filipe Manana8407f552014-09-05 15:14:39 +01004584 if (ret)
4585 return ret;
4586
Filipe Manana5328b2a2021-08-31 15:30:39 +01004587 /*
4588 * If this is the first time we are logging the inode in the current
4589 * transaction, we can avoid btrfs_drop_extents(), which is expensive
4590 * because it does a deletion search, which always acquires write locks
4591 * for extent buffers at levels 2, 1 and 0. This not only wastes time
4592 * but also adds significant contention in a log tree, since log trees
4593 * are small, with a root at level 2 or 3 at most, due to their short
4594 * life span.
4595 */
4596 if (inode_logged(trans, inode)) {
4597 drop_args.path = path;
4598 drop_args.start = em->start;
4599 drop_args.end = em->start + em->len;
4600 drop_args.replace_extent = true;
4601 drop_args.extent_item_size = sizeof(*fi);
4602 ret = btrfs_drop_extents(trans, log, inode, &drop_args);
4603 if (ret)
4604 return ret;
4605 }
Filipe Manana8407f552014-09-05 15:14:39 +01004606
Filipe Manana5893dfb2020-11-04 11:07:32 +00004607 if (!drop_args.extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004608 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004609 key.type = BTRFS_EXTENT_DATA_KEY;
4610 key.offset = em->start;
4611
4612 ret = btrfs_insert_empty_item(trans, log, path, &key,
4613 sizeof(*fi));
4614 if (ret)
4615 return ret;
4616 }
4617 leaf = path->nodes[0];
David Sterbac82f8232019-08-09 17:48:21 +02004618 btrfs_init_map_token(&token, leaf);
Filipe Manana8407f552014-09-05 15:14:39 +01004619 fi = btrfs_item_ptr(leaf, path->slots[0],
4620 struct btrfs_file_extent_item);
4621
David Sterbacc4c13d2020-04-29 02:15:56 +02004622 btrfs_set_token_file_extent_generation(&token, fi, trans->transid);
Filipe Manana8407f552014-09-05 15:14:39 +01004623 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
David Sterbacc4c13d2020-04-29 02:15:56 +02004624 btrfs_set_token_file_extent_type(&token, fi,
4625 BTRFS_FILE_EXTENT_PREALLOC);
Filipe Manana8407f552014-09-05 15:14:39 +01004626 else
David Sterbacc4c13d2020-04-29 02:15:56 +02004627 btrfs_set_token_file_extent_type(&token, fi,
4628 BTRFS_FILE_EXTENT_REG);
Filipe Manana8407f552014-09-05 15:14:39 +01004629
4630 block_len = max(em->block_len, em->orig_block_len);
4631 if (em->compress_type != BTRFS_COMPRESS_NONE) {
David Sterbacc4c13d2020-04-29 02:15:56 +02004632 btrfs_set_token_file_extent_disk_bytenr(&token, fi,
4633 em->block_start);
4634 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
Filipe Manana8407f552014-09-05 15:14:39 +01004635 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
David Sterbacc4c13d2020-04-29 02:15:56 +02004636 btrfs_set_token_file_extent_disk_bytenr(&token, fi,
Filipe Manana8407f552014-09-05 15:14:39 +01004637 em->block_start -
David Sterbacc4c13d2020-04-29 02:15:56 +02004638 extent_offset);
4639 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
Filipe Manana8407f552014-09-05 15:14:39 +01004640 } else {
David Sterbacc4c13d2020-04-29 02:15:56 +02004641 btrfs_set_token_file_extent_disk_bytenr(&token, fi, 0);
4642 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, 0);
Filipe Manana8407f552014-09-05 15:14:39 +01004643 }
4644
David Sterbacc4c13d2020-04-29 02:15:56 +02004645 btrfs_set_token_file_extent_offset(&token, fi, extent_offset);
4646 btrfs_set_token_file_extent_num_bytes(&token, fi, em->len);
4647 btrfs_set_token_file_extent_ram_bytes(&token, fi, em->ram_bytes);
4648 btrfs_set_token_file_extent_compression(&token, fi, em->compress_type);
4649 btrfs_set_token_file_extent_encryption(&token, fi, 0);
4650 btrfs_set_token_file_extent_other_encoding(&token, fi, 0);
Filipe Manana8407f552014-09-05 15:14:39 +01004651 btrfs_mark_buffer_dirty(leaf);
4652
4653 btrfs_release_path(path);
4654
4655 return ret;
4656}
4657
Filipe Manana31d11b82018-05-09 16:01:46 +01004658/*
4659 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4660 * lose them after doing a fast fsync and replaying the log. We scan the
4661 * subvolume's root instead of iterating the inode's extent map tree because
4662 * otherwise we can log incorrect extent items based on extent map conversion.
4663 * That can happen due to the fact that extent maps are merged when they
4664 * are not in the extent map tree's list of modified extents.
4665 */
4666static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4667 struct btrfs_inode *inode,
4668 struct btrfs_path *path)
4669{
4670 struct btrfs_root *root = inode->root;
4671 struct btrfs_key key;
4672 const u64 i_size = i_size_read(&inode->vfs_inode);
4673 const u64 ino = btrfs_ino(inode);
4674 struct btrfs_path *dst_path = NULL;
Filipe Manana0e563152019-11-19 12:07:33 +00004675 bool dropped_extents = false;
Filipe Mananaf135cea2020-04-23 16:30:53 +01004676 u64 truncate_offset = i_size;
4677 struct extent_buffer *leaf;
4678 int slot;
Filipe Manana31d11b82018-05-09 16:01:46 +01004679 int ins_nr = 0;
4680 int start_slot;
4681 int ret;
4682
4683 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4684 return 0;
4685
4686 key.objectid = ino;
4687 key.type = BTRFS_EXTENT_DATA_KEY;
4688 key.offset = i_size;
4689 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4690 if (ret < 0)
4691 goto out;
4692
Filipe Mananaf135cea2020-04-23 16:30:53 +01004693 /*
4694 * We must check if there is a prealloc extent that starts before the
4695 * i_size and crosses the i_size boundary. This is to ensure later we
4696 * truncate down to the end of that extent and not to the i_size, as
4697 * otherwise we end up losing part of the prealloc extent after a log
4698 * replay and with an implicit hole if there is another prealloc extent
4699 * that starts at an offset beyond i_size.
4700 */
4701 ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
4702 if (ret < 0)
4703 goto out;
4704
4705 if (ret == 0) {
4706 struct btrfs_file_extent_item *ei;
4707
4708 leaf = path->nodes[0];
4709 slot = path->slots[0];
4710 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4711
4712 if (btrfs_file_extent_type(leaf, ei) ==
4713 BTRFS_FILE_EXTENT_PREALLOC) {
4714 u64 extent_end;
4715
4716 btrfs_item_key_to_cpu(leaf, &key, slot);
4717 extent_end = key.offset +
4718 btrfs_file_extent_num_bytes(leaf, ei);
4719
4720 if (extent_end > i_size)
4721 truncate_offset = extent_end;
4722 }
4723 } else {
4724 ret = 0;
4725 }
4726
Filipe Manana31d11b82018-05-09 16:01:46 +01004727 while (true) {
Filipe Mananaf135cea2020-04-23 16:30:53 +01004728 leaf = path->nodes[0];
4729 slot = path->slots[0];
Filipe Manana31d11b82018-05-09 16:01:46 +01004730
4731 if (slot >= btrfs_header_nritems(leaf)) {
4732 if (ins_nr > 0) {
4733 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana0e563152019-11-19 12:07:33 +00004734 start_slot, ins_nr, 1, 0);
Filipe Manana31d11b82018-05-09 16:01:46 +01004735 if (ret < 0)
4736 goto out;
4737 ins_nr = 0;
4738 }
4739 ret = btrfs_next_leaf(root, path);
4740 if (ret < 0)
4741 goto out;
4742 if (ret > 0) {
4743 ret = 0;
4744 break;
4745 }
4746 continue;
4747 }
4748
4749 btrfs_item_key_to_cpu(leaf, &key, slot);
4750 if (key.objectid > ino)
4751 break;
4752 if (WARN_ON_ONCE(key.objectid < ino) ||
4753 key.type < BTRFS_EXTENT_DATA_KEY ||
4754 key.offset < i_size) {
4755 path->slots[0]++;
4756 continue;
4757 }
Filipe Manana0e563152019-11-19 12:07:33 +00004758 if (!dropped_extents) {
Filipe Manana31d11b82018-05-09 16:01:46 +01004759 /*
4760 * Avoid logging extent items logged in past fsync calls
4761 * and leading to duplicate keys in the log tree.
4762 */
Filipe Manana8a2b3da2021-08-31 15:30:36 +01004763 ret = truncate_inode_items(trans, root->log_root, inode,
4764 truncate_offset,
4765 BTRFS_EXTENT_DATA_KEY);
Filipe Manana31d11b82018-05-09 16:01:46 +01004766 if (ret)
4767 goto out;
Filipe Manana0e563152019-11-19 12:07:33 +00004768 dropped_extents = true;
Filipe Manana31d11b82018-05-09 16:01:46 +01004769 }
4770 if (ins_nr == 0)
4771 start_slot = slot;
4772 ins_nr++;
4773 path->slots[0]++;
4774 if (!dst_path) {
4775 dst_path = btrfs_alloc_path();
4776 if (!dst_path) {
4777 ret = -ENOMEM;
4778 goto out;
4779 }
4780 }
4781 }
Filipe Manana0bc2d3c2020-04-21 11:25:31 +01004782 if (ins_nr > 0)
Filipe Manana0e563152019-11-19 12:07:33 +00004783 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana31d11b82018-05-09 16:01:46 +01004784 start_slot, ins_nr, 1, 0);
Filipe Manana31d11b82018-05-09 16:01:46 +01004785out:
4786 btrfs_release_path(path);
4787 btrfs_free_path(dst_path);
4788 return ret;
4789}
4790
Josef Bacik5dc562c2012-08-17 13:14:17 -04004791static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004792 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004793 struct btrfs_path *path,
Filipe Manana48778172020-08-11 12:43:58 +01004794 struct btrfs_log_ctx *ctx)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004795{
Filipe Manana48778172020-08-11 12:43:58 +01004796 struct btrfs_ordered_extent *ordered;
4797 struct btrfs_ordered_extent *tmp;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004798 struct extent_map *em, *n;
4799 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004800 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004801 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004802 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004803
4804 INIT_LIST_HEAD(&extents);
4805
Josef Bacik5dc562c2012-08-17 13:14:17 -04004806 write_lock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004807
4808 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4809 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004810 /*
4811 * Just an arbitrary number, this can be really CPU intensive
4812 * once we start getting a lot of extents, and really once we
4813 * have a bunch of extents we just want to commit since it will
4814 * be faster.
4815 */
4816 if (++num > 32768) {
4817 list_del_init(&tree->modified_extents);
4818 ret = -EFBIG;
4819 goto process;
4820 }
4821
Filipe Manana5f96bfb2020-11-25 12:19:24 +00004822 if (em->generation < trans->transid)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004823 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004824
Filipe Manana31d11b82018-05-09 16:01:46 +01004825 /* We log prealloc extents beyond eof later. */
4826 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4827 em->start >= i_size_read(&inode->vfs_inode))
4828 continue;
4829
Josef Bacikff44c6e2012-09-14 12:59:20 -04004830 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004831 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004832 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004833 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004834 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004835 }
4836
4837 list_sort(NULL, &extents, extent_cmp);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004838process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004839 while (!list_empty(&extents)) {
4840 em = list_entry(extents.next, struct extent_map, list);
4841
4842 list_del_init(&em->list);
4843
4844 /*
4845 * If we had an error we just need to delete everybody from our
4846 * private list.
4847 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004848 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004849 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004850 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004851 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004852 }
4853
4854 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004855
Filipe Manana90d04512021-09-16 11:32:10 +01004856 ret = log_one_extent(trans, inode, em, path, ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004857 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004858 clear_em_logging(tree, em);
4859 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004860 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004861 WARN_ON(!list_empty(&extents));
4862 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004863
Josef Bacik5dc562c2012-08-17 13:14:17 -04004864 btrfs_release_path(path);
Filipe Manana31d11b82018-05-09 16:01:46 +01004865 if (!ret)
4866 ret = btrfs_log_prealloc_extents(trans, inode, path);
Filipe Manana48778172020-08-11 12:43:58 +01004867 if (ret)
4868 return ret;
Filipe Manana31d11b82018-05-09 16:01:46 +01004869
Filipe Manana48778172020-08-11 12:43:58 +01004870 /*
4871 * We have logged all extents successfully, now make sure the commit of
4872 * the current transaction waits for the ordered extents to complete
4873 * before it commits and wipes out the log trees, otherwise we would
4874 * lose data if an ordered extents completes after the transaction
4875 * commits and a power failure happens after the transaction commit.
4876 */
4877 list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
4878 list_del_init(&ordered->log_list);
4879 set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
4880
4881 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4882 spin_lock_irq(&inode->ordered_tree.lock);
4883 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4884 set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
4885 atomic_inc(&trans->transaction->pending_ordered);
4886 }
4887 spin_unlock_irq(&inode->ordered_tree.lock);
4888 }
4889 btrfs_put_ordered_extent(ordered);
4890 }
4891
4892 return 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004893}
4894
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004895static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004896 struct btrfs_path *path, u64 *size_ret)
4897{
4898 struct btrfs_key key;
4899 int ret;
4900
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004901 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004902 key.type = BTRFS_INODE_ITEM_KEY;
4903 key.offset = 0;
4904
4905 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4906 if (ret < 0) {
4907 return ret;
4908 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004909 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004910 } else {
4911 struct btrfs_inode_item *item;
4912
4913 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4914 struct btrfs_inode_item);
4915 *size_ret = btrfs_inode_size(path->nodes[0], item);
Filipe Mananabf504112019-03-04 14:06:12 +00004916 /*
4917 * If the in-memory inode's i_size is smaller then the inode
4918 * size stored in the btree, return the inode's i_size, so
4919 * that we get a correct inode size after replaying the log
4920 * when before a power failure we had a shrinking truncate
4921 * followed by addition of a new name (rename / new hard link).
4922 * Otherwise return the inode size from the btree, to avoid
4923 * data loss when replaying a log due to previously doing a
4924 * write that expands the inode's size and logging a new name
4925 * immediately after.
4926 */
4927 if (*size_ret > inode->vfs_inode.i_size)
4928 *size_ret = inode->vfs_inode.i_size;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004929 }
4930
4931 btrfs_release_path(path);
4932 return 0;
4933}
4934
Filipe Manana36283bf2015-06-20 00:44:51 +01004935/*
4936 * At the moment we always log all xattrs. This is to figure out at log replay
4937 * time which xattrs must have their deletion replayed. If a xattr is missing
4938 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4939 * because if a xattr is deleted, the inode is fsynced and a power failure
4940 * happens, causing the log to be replayed the next time the fs is mounted,
4941 * we want the xattr to not exist anymore (same behaviour as other filesystems
4942 * with a journal, ext3/4, xfs, f2fs, etc).
4943 */
4944static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004945 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004946 struct btrfs_path *path,
4947 struct btrfs_path *dst_path)
4948{
Filipe Manana90d04512021-09-16 11:32:10 +01004949 struct btrfs_root *root = inode->root;
Filipe Manana36283bf2015-06-20 00:44:51 +01004950 int ret;
4951 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004952 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004953 int ins_nr = 0;
4954 int start_slot = 0;
Filipe Mananaf2f121a2020-11-13 11:21:49 +00004955 bool found_xattrs = false;
4956
4957 if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags))
4958 return 0;
Filipe Manana36283bf2015-06-20 00:44:51 +01004959
4960 key.objectid = ino;
4961 key.type = BTRFS_XATTR_ITEM_KEY;
4962 key.offset = 0;
4963
4964 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4965 if (ret < 0)
4966 return ret;
4967
4968 while (true) {
4969 int slot = path->slots[0];
4970 struct extent_buffer *leaf = path->nodes[0];
4971 int nritems = btrfs_header_nritems(leaf);
4972
4973 if (slot >= nritems) {
4974 if (ins_nr > 0) {
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004975 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana0e563152019-11-19 12:07:33 +00004976 start_slot, ins_nr, 1, 0);
Filipe Manana36283bf2015-06-20 00:44:51 +01004977 if (ret < 0)
4978 return ret;
4979 ins_nr = 0;
4980 }
4981 ret = btrfs_next_leaf(root, path);
4982 if (ret < 0)
4983 return ret;
4984 else if (ret > 0)
4985 break;
4986 continue;
4987 }
4988
4989 btrfs_item_key_to_cpu(leaf, &key, slot);
4990 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4991 break;
4992
4993 if (ins_nr == 0)
4994 start_slot = slot;
4995 ins_nr++;
4996 path->slots[0]++;
Filipe Mananaf2f121a2020-11-13 11:21:49 +00004997 found_xattrs = true;
Filipe Manana36283bf2015-06-20 00:44:51 +01004998 cond_resched();
4999 }
5000 if (ins_nr > 0) {
Nikolay Borisov1a93c362017-01-18 00:31:37 +02005001 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana0e563152019-11-19 12:07:33 +00005002 start_slot, ins_nr, 1, 0);
Filipe Manana36283bf2015-06-20 00:44:51 +01005003 if (ret < 0)
5004 return ret;
5005 }
5006
Filipe Mananaf2f121a2020-11-13 11:21:49 +00005007 if (!found_xattrs)
5008 set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags);
5009
Filipe Manana36283bf2015-06-20 00:44:51 +01005010 return 0;
5011}
5012
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005013/*
Filipe Manana0e563152019-11-19 12:07:33 +00005014 * When using the NO_HOLES feature if we punched a hole that causes the
5015 * deletion of entire leafs or all the extent items of the first leaf (the one
5016 * that contains the inode item and references) we may end up not processing
5017 * any extents, because there are no leafs with a generation matching the
5018 * current transaction that have extent items for our inode. So we need to find
5019 * if any holes exist and then log them. We also need to log holes after any
5020 * truncate operation that changes the inode's size.
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005021 */
Filipe Manana0e563152019-11-19 12:07:33 +00005022static int btrfs_log_holes(struct btrfs_trans_handle *trans,
Filipe Manana0e563152019-11-19 12:07:33 +00005023 struct btrfs_inode *inode,
Filipe Manana7af59742020-04-07 11:37:44 +01005024 struct btrfs_path *path)
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005025{
Filipe Manana90d04512021-09-16 11:32:10 +01005026 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005027 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005028 struct btrfs_key key;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02005029 const u64 ino = btrfs_ino(inode);
5030 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Manana7af59742020-04-07 11:37:44 +01005031 u64 prev_extent_end = 0;
Filipe Manana0e563152019-11-19 12:07:33 +00005032 int ret;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005033
Filipe Manana0e563152019-11-19 12:07:33 +00005034 if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005035 return 0;
5036
5037 key.objectid = ino;
5038 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana7af59742020-04-07 11:37:44 +01005039 key.offset = 0;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005040
5041 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005042 if (ret < 0)
5043 return ret;
5044
Filipe Manana0e563152019-11-19 12:07:33 +00005045 while (true) {
Filipe Manana0e563152019-11-19 12:07:33 +00005046 struct extent_buffer *leaf = path->nodes[0];
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005047
Filipe Manana0e563152019-11-19 12:07:33 +00005048 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5049 ret = btrfs_next_leaf(root, path);
5050 if (ret < 0)
5051 return ret;
5052 if (ret > 0) {
5053 ret = 0;
5054 break;
5055 }
5056 leaf = path->nodes[0];
5057 }
5058
5059 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5060 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
5061 break;
5062
5063 /* We have a hole, log it. */
5064 if (prev_extent_end < key.offset) {
Filipe Manana7af59742020-04-07 11:37:44 +01005065 const u64 hole_len = key.offset - prev_extent_end;
Filipe Manana0e563152019-11-19 12:07:33 +00005066
5067 /*
5068 * Release the path to avoid deadlocks with other code
5069 * paths that search the root while holding locks on
5070 * leafs from the log root.
5071 */
5072 btrfs_release_path(path);
5073 ret = btrfs_insert_file_extent(trans, root->log_root,
5074 ino, prev_extent_end, 0,
5075 0, hole_len, 0, hole_len,
5076 0, 0, 0);
5077 if (ret < 0)
5078 return ret;
5079
5080 /*
5081 * Search for the same key again in the root. Since it's
5082 * an extent item and we are holding the inode lock, the
5083 * key must still exist. If it doesn't just emit warning
5084 * and return an error to fall back to a transaction
5085 * commit.
5086 */
5087 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5088 if (ret < 0)
5089 return ret;
5090 if (WARN_ON(ret > 0))
5091 return -ENOENT;
5092 leaf = path->nodes[0];
5093 }
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005094
Filipe Manana7af59742020-04-07 11:37:44 +01005095 prev_extent_end = btrfs_file_extent_end(path);
Filipe Manana0e563152019-11-19 12:07:33 +00005096 path->slots[0]++;
5097 cond_resched();
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005098 }
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005099
Filipe Manana7af59742020-04-07 11:37:44 +01005100 if (prev_extent_end < i_size) {
Filipe Manana0e563152019-11-19 12:07:33 +00005101 u64 hole_len;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005102
Filipe Manana0e563152019-11-19 12:07:33 +00005103 btrfs_release_path(path);
Filipe Manana7af59742020-04-07 11:37:44 +01005104 hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
Filipe Manana0e563152019-11-19 12:07:33 +00005105 ret = btrfs_insert_file_extent(trans, root->log_root,
5106 ino, prev_extent_end, 0, 0,
5107 hole_len, 0, hole_len,
5108 0, 0, 0);
5109 if (ret < 0)
5110 return ret;
5111 }
5112
5113 return 0;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005114}
5115
Filipe Manana56f23fd2016-03-30 23:37:21 +01005116/*
5117 * When we are logging a new inode X, check if it doesn't have a reference that
5118 * matches the reference from some other inode Y created in a past transaction
5119 * and that was renamed in the current transaction. If we don't do this, then at
5120 * log replay time we can lose inode Y (and all its files if it's a directory):
5121 *
5122 * mkdir /mnt/x
5123 * echo "hello world" > /mnt/x/foobar
5124 * sync
5125 * mv /mnt/x /mnt/y
5126 * mkdir /mnt/x # or touch /mnt/x
5127 * xfs_io -c fsync /mnt/x
5128 * <power fail>
5129 * mount fs, trigger log replay
5130 *
5131 * After the log replay procedure, we would lose the first directory and all its
5132 * files (file foobar).
5133 * For the case where inode Y is not a directory we simply end up losing it:
5134 *
5135 * echo "123" > /mnt/foo
5136 * sync
5137 * mv /mnt/foo /mnt/bar
5138 * echo "abc" > /mnt/foo
5139 * xfs_io -c fsync /mnt/foo
5140 * <power fail>
5141 *
5142 * We also need this for cases where a snapshot entry is replaced by some other
5143 * entry (file or directory) otherwise we end up with an unreplayable log due to
5144 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
5145 * if it were a regular entry:
5146 *
5147 * mkdir /mnt/x
5148 * btrfs subvolume snapshot /mnt /mnt/x/snap
5149 * btrfs subvolume delete /mnt/x/snap
5150 * rmdir /mnt/x
5151 * mkdir /mnt/x
5152 * fsync /mnt/x or fsync some new file inside it
5153 * <power fail>
5154 *
5155 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
5156 * the same transaction.
5157 */
5158static int btrfs_check_ref_name_override(struct extent_buffer *eb,
5159 const int slot,
5160 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02005161 struct btrfs_inode *inode,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005162 u64 *other_ino, u64 *other_parent)
Filipe Manana56f23fd2016-03-30 23:37:21 +01005163{
5164 int ret;
5165 struct btrfs_path *search_path;
5166 char *name = NULL;
5167 u32 name_len = 0;
5168 u32 item_size = btrfs_item_size_nr(eb, slot);
5169 u32 cur_offset = 0;
5170 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
5171
5172 search_path = btrfs_alloc_path();
5173 if (!search_path)
5174 return -ENOMEM;
5175 search_path->search_commit_root = 1;
5176 search_path->skip_locking = 1;
5177
5178 while (cur_offset < item_size) {
5179 u64 parent;
5180 u32 this_name_len;
5181 u32 this_len;
5182 unsigned long name_ptr;
5183 struct btrfs_dir_item *di;
5184
5185 if (key->type == BTRFS_INODE_REF_KEY) {
5186 struct btrfs_inode_ref *iref;
5187
5188 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
5189 parent = key->offset;
5190 this_name_len = btrfs_inode_ref_name_len(eb, iref);
5191 name_ptr = (unsigned long)(iref + 1);
5192 this_len = sizeof(*iref) + this_name_len;
5193 } else {
5194 struct btrfs_inode_extref *extref;
5195
5196 extref = (struct btrfs_inode_extref *)(ptr +
5197 cur_offset);
5198 parent = btrfs_inode_extref_parent(eb, extref);
5199 this_name_len = btrfs_inode_extref_name_len(eb, extref);
5200 name_ptr = (unsigned long)&extref->name;
5201 this_len = sizeof(*extref) + this_name_len;
5202 }
5203
5204 if (this_name_len > name_len) {
5205 char *new_name;
5206
5207 new_name = krealloc(name, this_name_len, GFP_NOFS);
5208 if (!new_name) {
5209 ret = -ENOMEM;
5210 goto out;
5211 }
5212 name_len = this_name_len;
5213 name = new_name;
5214 }
5215
5216 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02005217 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
5218 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01005219 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005220 struct btrfs_key di_key;
5221
5222 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
5223 di, &di_key);
5224 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
Filipe Manana6b5fc432019-02-13 12:14:03 +00005225 if (di_key.objectid != key->objectid) {
5226 ret = 1;
5227 *other_ino = di_key.objectid;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005228 *other_parent = parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005229 } else {
5230 ret = 0;
5231 }
Filipe Manana44f714d2016-06-06 16:11:13 +01005232 } else {
5233 ret = -EAGAIN;
5234 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01005235 goto out;
5236 } else if (IS_ERR(di)) {
5237 ret = PTR_ERR(di);
5238 goto out;
5239 }
5240 btrfs_release_path(search_path);
5241
5242 cur_offset += this_len;
5243 }
5244 ret = 0;
5245out:
5246 btrfs_free_path(search_path);
5247 kfree(name);
5248 return ret;
5249}
5250
Filipe Manana6b5fc432019-02-13 12:14:03 +00005251struct btrfs_ino_list {
5252 u64 ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005253 u64 parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005254 struct list_head list;
5255};
5256
5257static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
5258 struct btrfs_root *root,
5259 struct btrfs_path *path,
5260 struct btrfs_log_ctx *ctx,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005261 u64 ino, u64 parent)
Filipe Manana6b5fc432019-02-13 12:14:03 +00005262{
5263 struct btrfs_ino_list *ino_elem;
5264 LIST_HEAD(inode_list);
5265 int ret = 0;
5266
5267 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
5268 if (!ino_elem)
5269 return -ENOMEM;
5270 ino_elem->ino = ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005271 ino_elem->parent = parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005272 list_add_tail(&ino_elem->list, &inode_list);
5273
5274 while (!list_empty(&inode_list)) {
5275 struct btrfs_fs_info *fs_info = root->fs_info;
5276 struct btrfs_key key;
5277 struct inode *inode;
5278
5279 ino_elem = list_first_entry(&inode_list, struct btrfs_ino_list,
5280 list);
5281 ino = ino_elem->ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005282 parent = ino_elem->parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005283 list_del(&ino_elem->list);
5284 kfree(ino_elem);
5285 if (ret)
5286 continue;
5287
5288 btrfs_release_path(path);
5289
David Sterba0202e832020-05-15 19:35:59 +02005290 inode = btrfs_iget(fs_info->sb, ino, root);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005291 /*
5292 * If the other inode that had a conflicting dir entry was
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005293 * deleted in the current transaction, we need to log its parent
5294 * directory.
Filipe Manana6b5fc432019-02-13 12:14:03 +00005295 */
5296 if (IS_ERR(inode)) {
5297 ret = PTR_ERR(inode);
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005298 if (ret == -ENOENT) {
David Sterba0202e832020-05-15 19:35:59 +02005299 inode = btrfs_iget(fs_info->sb, parent, root);
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005300 if (IS_ERR(inode)) {
5301 ret = PTR_ERR(inode);
5302 } else {
Filipe Manana90d04512021-09-16 11:32:10 +01005303 ret = btrfs_log_inode(trans,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005304 BTRFS_I(inode),
5305 LOG_OTHER_INODE_ALL,
Filipe Manana48778172020-08-11 12:43:58 +01005306 ctx);
Filipe Manana410f9542019-09-10 15:26:49 +01005307 btrfs_add_delayed_iput(inode);
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005308 }
5309 }
Filipe Manana6b5fc432019-02-13 12:14:03 +00005310 continue;
5311 }
5312 /*
Filipe Mananab5e4ff92020-01-15 13:21:35 +00005313 * If the inode was already logged skip it - otherwise we can
5314 * hit an infinite loop. Example:
5315 *
5316 * From the commit root (previous transaction) we have the
5317 * following inodes:
5318 *
5319 * inode 257 a directory
5320 * inode 258 with references "zz" and "zz_link" on inode 257
5321 * inode 259 with reference "a" on inode 257
5322 *
5323 * And in the current (uncommitted) transaction we have:
5324 *
5325 * inode 257 a directory, unchanged
5326 * inode 258 with references "a" and "a2" on inode 257
5327 * inode 259 with reference "zz_link" on inode 257
5328 * inode 261 with reference "zz" on inode 257
5329 *
5330 * When logging inode 261 the following infinite loop could
5331 * happen if we don't skip already logged inodes:
5332 *
5333 * - we detect inode 258 as a conflicting inode, with inode 261
5334 * on reference "zz", and log it;
5335 *
5336 * - we detect inode 259 as a conflicting inode, with inode 258
5337 * on reference "a", and log it;
5338 *
5339 * - we detect inode 258 as a conflicting inode, with inode 259
5340 * on reference "zz_link", and log it - again! After this we
5341 * repeat the above steps forever.
5342 */
5343 spin_lock(&BTRFS_I(inode)->lock);
5344 /*
5345 * Check the inode's logged_trans only instead of
5346 * btrfs_inode_in_log(). This is because the last_log_commit of
Filipe Manana1f295372021-07-29 15:30:21 +01005347 * the inode is not updated when we only log that it exists (see
5348 * btrfs_log_inode()).
Filipe Mananab5e4ff92020-01-15 13:21:35 +00005349 */
5350 if (BTRFS_I(inode)->logged_trans == trans->transid) {
5351 spin_unlock(&BTRFS_I(inode)->lock);
5352 btrfs_add_delayed_iput(inode);
5353 continue;
5354 }
5355 spin_unlock(&BTRFS_I(inode)->lock);
5356 /*
Filipe Manana6b5fc432019-02-13 12:14:03 +00005357 * We are safe logging the other inode without acquiring its
5358 * lock as long as we log with the LOG_INODE_EXISTS mode. We
5359 * are safe against concurrent renames of the other inode as
5360 * well because during a rename we pin the log and update the
5361 * log with the new name before we unpin it.
5362 */
Filipe Manana90d04512021-09-16 11:32:10 +01005363 ret = btrfs_log_inode(trans, BTRFS_I(inode), LOG_OTHER_INODE, ctx);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005364 if (ret) {
Filipe Manana410f9542019-09-10 15:26:49 +01005365 btrfs_add_delayed_iput(inode);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005366 continue;
5367 }
5368
5369 key.objectid = ino;
5370 key.type = BTRFS_INODE_REF_KEY;
5371 key.offset = 0;
5372 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5373 if (ret < 0) {
Filipe Manana410f9542019-09-10 15:26:49 +01005374 btrfs_add_delayed_iput(inode);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005375 continue;
5376 }
5377
5378 while (true) {
5379 struct extent_buffer *leaf = path->nodes[0];
5380 int slot = path->slots[0];
5381 u64 other_ino = 0;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005382 u64 other_parent = 0;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005383
5384 if (slot >= btrfs_header_nritems(leaf)) {
5385 ret = btrfs_next_leaf(root, path);
5386 if (ret < 0) {
5387 break;
5388 } else if (ret > 0) {
5389 ret = 0;
5390 break;
5391 }
5392 continue;
5393 }
5394
5395 btrfs_item_key_to_cpu(leaf, &key, slot);
5396 if (key.objectid != ino ||
5397 (key.type != BTRFS_INODE_REF_KEY &&
5398 key.type != BTRFS_INODE_EXTREF_KEY)) {
5399 ret = 0;
5400 break;
5401 }
5402
5403 ret = btrfs_check_ref_name_override(leaf, slot, &key,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005404 BTRFS_I(inode), &other_ino,
5405 &other_parent);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005406 if (ret < 0)
5407 break;
5408 if (ret > 0) {
5409 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
5410 if (!ino_elem) {
5411 ret = -ENOMEM;
5412 break;
5413 }
5414 ino_elem->ino = other_ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005415 ino_elem->parent = other_parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005416 list_add_tail(&ino_elem->list, &inode_list);
5417 ret = 0;
5418 }
5419 path->slots[0]++;
5420 }
Filipe Manana410f9542019-09-10 15:26:49 +01005421 btrfs_add_delayed_iput(inode);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005422 }
5423
5424 return ret;
5425}
5426
Filipe Mananada447002020-03-09 12:41:07 +00005427static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,
5428 struct btrfs_inode *inode,
5429 struct btrfs_key *min_key,
5430 const struct btrfs_key *max_key,
5431 struct btrfs_path *path,
5432 struct btrfs_path *dst_path,
5433 const u64 logged_isize,
5434 const bool recursive_logging,
5435 const int inode_only,
5436 struct btrfs_log_ctx *ctx,
5437 bool *need_log_inode_item)
5438{
5439 struct btrfs_root *root = inode->root;
5440 int ins_start_slot = 0;
5441 int ins_nr = 0;
5442 int ret;
5443
5444 while (1) {
5445 ret = btrfs_search_forward(root, min_key, path, trans->transid);
5446 if (ret < 0)
5447 return ret;
5448 if (ret > 0) {
5449 ret = 0;
5450 break;
5451 }
5452again:
5453 /* Note, ins_nr might be > 0 here, cleanup outside the loop */
5454 if (min_key->objectid != max_key->objectid)
5455 break;
5456 if (min_key->type > max_key->type)
5457 break;
5458
5459 if (min_key->type == BTRFS_INODE_ITEM_KEY)
5460 *need_log_inode_item = false;
5461
5462 if ((min_key->type == BTRFS_INODE_REF_KEY ||
5463 min_key->type == BTRFS_INODE_EXTREF_KEY) &&
5464 inode->generation == trans->transid &&
5465 !recursive_logging) {
5466 u64 other_ino = 0;
5467 u64 other_parent = 0;
5468
5469 ret = btrfs_check_ref_name_override(path->nodes[0],
5470 path->slots[0], min_key, inode,
5471 &other_ino, &other_parent);
5472 if (ret < 0) {
5473 return ret;
Filipe Manana289cffc2021-08-31 15:30:32 +01005474 } else if (ret > 0 &&
Filipe Mananada447002020-03-09 12:41:07 +00005475 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
5476 if (ins_nr > 0) {
5477 ins_nr++;
5478 } else {
5479 ins_nr = 1;
5480 ins_start_slot = path->slots[0];
5481 }
5482 ret = copy_items(trans, inode, dst_path, path,
5483 ins_start_slot, ins_nr,
5484 inode_only, logged_isize);
5485 if (ret < 0)
5486 return ret;
5487 ins_nr = 0;
5488
5489 ret = log_conflicting_inodes(trans, root, path,
5490 ctx, other_ino, other_parent);
5491 if (ret)
5492 return ret;
5493 btrfs_release_path(path);
5494 goto next_key;
5495 }
5496 }
5497
5498 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5499 if (min_key->type == BTRFS_XATTR_ITEM_KEY) {
5500 if (ins_nr == 0)
5501 goto next_slot;
5502 ret = copy_items(trans, inode, dst_path, path,
5503 ins_start_slot,
5504 ins_nr, inode_only, logged_isize);
5505 if (ret < 0)
5506 return ret;
5507 ins_nr = 0;
5508 goto next_slot;
5509 }
5510
5511 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5512 ins_nr++;
5513 goto next_slot;
5514 } else if (!ins_nr) {
5515 ins_start_slot = path->slots[0];
5516 ins_nr = 1;
5517 goto next_slot;
5518 }
5519
5520 ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
5521 ins_nr, inode_only, logged_isize);
5522 if (ret < 0)
5523 return ret;
5524 ins_nr = 1;
5525 ins_start_slot = path->slots[0];
5526next_slot:
5527 path->slots[0]++;
5528 if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
5529 btrfs_item_key_to_cpu(path->nodes[0], min_key,
5530 path->slots[0]);
5531 goto again;
5532 }
5533 if (ins_nr) {
5534 ret = copy_items(trans, inode, dst_path, path,
5535 ins_start_slot, ins_nr, inode_only,
5536 logged_isize);
5537 if (ret < 0)
5538 return ret;
5539 ins_nr = 0;
5540 }
5541 btrfs_release_path(path);
5542next_key:
5543 if (min_key->offset < (u64)-1) {
5544 min_key->offset++;
5545 } else if (min_key->type < max_key->type) {
5546 min_key->type++;
5547 min_key->offset = 0;
5548 } else {
5549 break;
5550 }
5551 }
5552 if (ins_nr)
5553 ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
5554 ins_nr, inode_only, logged_isize);
5555
5556 return ret;
5557}
5558
Chris Masone02119d2008-09-05 16:13:11 -04005559/* log a single inode in the tree log.
5560 * At least one parent directory for this inode must exist in the tree
5561 * or be logged already.
5562 *
5563 * Any items from this inode changed by the current transaction are copied
5564 * to the log tree. An extra reference is taken on any extents in this
5565 * file, allowing us to avoid a whole pile of corner cases around logging
5566 * blocks that have been removed from the tree.
5567 *
5568 * See LOG_INODE_ALL and related defines for a description of what inode_only
5569 * does.
5570 *
5571 * This handles both files and directories.
5572 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005573static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana90d04512021-09-16 11:32:10 +01005574 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005575 int inode_only,
Filipe Manana8407f552014-09-05 15:14:39 +01005576 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005577{
5578 struct btrfs_path *path;
5579 struct btrfs_path *dst_path;
5580 struct btrfs_key min_key;
5581 struct btrfs_key max_key;
Filipe Manana90d04512021-09-16 11:32:10 +01005582 struct btrfs_root *log = inode->root->log_root;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005583 int err = 0;
Filipe Manana8c8648d2020-07-02 12:31:59 +01005584 int ret = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005585 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005586 u64 ino = btrfs_ino(inode);
5587 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005588 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01005589 bool need_log_inode_item = true;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005590 bool xattrs_logged = false;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005591 bool recursive_logging = false;
Filipe Manana2ac691d2021-07-20 16:03:43 +01005592 bool inode_item_dropped = true;
Chris Masone02119d2008-09-05 16:13:11 -04005593
Chris Masone02119d2008-09-05 16:13:11 -04005594 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00005595 if (!path)
5596 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04005597 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00005598 if (!dst_path) {
5599 btrfs_free_path(path);
5600 return -ENOMEM;
5601 }
Chris Masone02119d2008-09-05 16:13:11 -04005602
Li Zefan33345d012011-04-20 10:31:50 +08005603 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04005604 min_key.type = BTRFS_INODE_ITEM_KEY;
5605 min_key.offset = 0;
5606
Li Zefan33345d012011-04-20 10:31:50 +08005607 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04005608
Chris Mason12fcfd22009-03-24 10:24:20 -04005609
Josef Bacik5dc562c2012-08-17 13:14:17 -04005610 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005611 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00005612 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005613 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08005614 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04005615 max_key.type = BTRFS_XATTR_ITEM_KEY;
5616 else
5617 max_key.type = (u8)-1;
5618 max_key.offset = (u64)-1;
5619
Filipe Manana2c2c4522015-01-13 16:40:04 +00005620 /*
Filipe Manana5aa7d1a2020-07-02 12:32:20 +01005621 * Only run delayed items if we are a directory. We want to make sure
5622 * all directory indexes hit the fs/subvolume tree so we can find them
5623 * and figure out which index ranges have to be logged.
Filipe Manana2c2c4522015-01-13 16:40:04 +00005624 */
Filipe Mananaf6df27d2021-08-31 15:30:40 +01005625 if (S_ISDIR(inode->vfs_inode.i_mode)) {
5626 err = btrfs_commit_inode_delayed_items(trans, inode);
5627 if (err)
5628 goto out;
Miao Xie16cdcec2011-04-22 18:12:22 +08005629 }
5630
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005631 if (inode_only == LOG_OTHER_INODE || inode_only == LOG_OTHER_INODE_ALL) {
5632 recursive_logging = true;
5633 if (inode_only == LOG_OTHER_INODE)
5634 inode_only = LOG_INODE_EXISTS;
5635 else
5636 inode_only = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005637 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08005638 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005639 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08005640 }
Chris Masone02119d2008-09-05 16:13:11 -04005641
Filipe Manana5e33a2b2016-02-25 23:19:38 +00005642 /*
Filipe Manana64d6b282021-01-27 10:34:59 +00005643 * This is for cases where logging a directory could result in losing a
5644 * a file after replaying the log. For example, if we move a file from a
5645 * directory A to a directory B, then fsync directory A, we have no way
5646 * to known the file was moved from A to B, so logging just A would
5647 * result in losing the file after a log replay.
5648 */
5649 if (S_ISDIR(inode->vfs_inode.i_mode) &&
5650 inode_only == LOG_INODE_ALL &&
5651 inode->last_unlink_trans >= trans->transid) {
5652 btrfs_set_log_full_commit(trans);
5653 err = 1;
5654 goto out_unlock;
5655 }
5656
5657 /*
Chris Masone02119d2008-09-05 16:13:11 -04005658 * a brute force approach to making sure we get the most uptodate
5659 * copies of everything.
5660 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005661 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04005662 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
5663
Filipe Mananaab123132021-01-27 10:34:56 +00005664 clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags);
Filipe Manana4f764e52015-02-23 19:53:35 +00005665 if (inode_only == LOG_INODE_EXISTS)
5666 max_key_type = BTRFS_XATTR_ITEM_KEY;
Filipe Manana88e221c2021-08-31 15:30:35 +01005667 ret = drop_inode_items(trans, log, path, inode, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04005668 } else {
Filipe Mananaa5c733a2021-08-31 15:30:38 +01005669 if (inode_only == LOG_INODE_EXISTS && inode_logged(trans, inode)) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005670 /*
5671 * Make sure the new inode item we write to the log has
5672 * the same isize as the current one (if it exists).
5673 * This is necessary to prevent data loss after log
5674 * replay, and also to prevent doing a wrong expanding
5675 * truncate - for e.g. create file, write 4K into offset
5676 * 0, fsync, write 4K into offset 4096, add hard link,
5677 * fsync some other file (to sync log), power fail - if
5678 * we use the inode's current i_size, after log replay
5679 * we get a 8Kb file, with the last 4Kb extent as a hole
5680 * (zeroes), as if an expanding truncate happened,
5681 * instead of getting a file of 4Kb only.
5682 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005683 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005684 if (err)
5685 goto out_unlock;
5686 }
Filipe Mananaa7429942015-02-13 16:56:14 +00005687 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005688 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00005689 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005690 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Manana88e221c2021-08-31 15:30:35 +01005691 ret = drop_inode_items(trans, log, path, inode,
5692 max_key.type);
Filipe Mananaa7429942015-02-13 16:56:14 +00005693 } else {
5694 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005695 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00005696 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005697 &inode->runtime_flags);
Filipe Manana4934a812021-08-31 15:30:37 +01005698 if (inode_logged(trans, inode))
5699 ret = truncate_inode_items(trans, log,
5700 inode, 0, 0);
Filipe Mananaa7429942015-02-13 16:56:14 +00005701 }
Filipe Manana4f764e52015-02-23 19:53:35 +00005702 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005703 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05005704 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005705 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04005706 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00005707 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Manana88e221c2021-08-31 15:30:35 +01005708 ret = drop_inode_items(trans, log, path, inode,
5709 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005710 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00005711 if (inode_only == LOG_INODE_ALL)
5712 fast_search = true;
Filipe Manana2ac691d2021-07-20 16:03:43 +01005713 inode_item_dropped = false;
Josef Bacika95249b2012-10-11 16:17:34 -04005714 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005715 }
Josef Bacika95249b2012-10-11 16:17:34 -04005716
Chris Masone02119d2008-09-05 16:13:11 -04005717 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005718 if (ret) {
5719 err = ret;
5720 goto out_unlock;
5721 }
Chris Masone02119d2008-09-05 16:13:11 -04005722
Filipe Mananada447002020-03-09 12:41:07 +00005723 err = copy_inode_items_to_log(trans, inode, &min_key, &max_key,
5724 path, dst_path, logged_isize,
Filipe Manana7af59742020-04-07 11:37:44 +01005725 recursive_logging, inode_only, ctx,
5726 &need_log_inode_item);
Filipe Mananada447002020-03-09 12:41:07 +00005727 if (err)
5728 goto out_unlock;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005729
Filipe Manana36283bf2015-06-20 00:44:51 +01005730 btrfs_release_path(path);
5731 btrfs_release_path(dst_path);
Filipe Manana90d04512021-09-16 11:32:10 +01005732 err = btrfs_log_all_xattrs(trans, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01005733 if (err)
5734 goto out_unlock;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005735 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005736 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5737 btrfs_release_path(path);
5738 btrfs_release_path(dst_path);
Filipe Manana90d04512021-09-16 11:32:10 +01005739 err = btrfs_log_holes(trans, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005740 if (err)
5741 goto out_unlock;
5742 }
Josef Bacika95249b2012-10-11 16:17:34 -04005743log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04005744 btrfs_release_path(path);
5745 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01005746 if (need_log_inode_item) {
Filipe Manana2ac691d2021-07-20 16:03:43 +01005747 err = log_inode_item(trans, log, dst_path, inode, inode_item_dropped);
Filipe Mananae4545de2015-06-17 12:49:23 +01005748 if (err)
5749 goto out_unlock;
Filipe Mananab590b832021-05-28 11:37:32 +01005750 /*
5751 * If we are doing a fast fsync and the inode was logged before
5752 * in this transaction, we don't need to log the xattrs because
5753 * they were logged before. If xattrs were added, changed or
5754 * deleted since the last time we logged the inode, then we have
5755 * already logged them because the inode had the runtime flag
5756 * BTRFS_INODE_COPY_EVERYTHING set.
5757 */
5758 if (!xattrs_logged && inode->logged_trans < trans->transid) {
Filipe Manana90d04512021-09-16 11:32:10 +01005759 err = btrfs_log_all_xattrs(trans, inode, path, dst_path);
Filipe Mananab590b832021-05-28 11:37:32 +01005760 if (err)
5761 goto out_unlock;
5762 btrfs_release_path(path);
5763 }
Filipe Mananae4545de2015-06-17 12:49:23 +01005764 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005765 if (fast_search) {
Filipe Manana90d04512021-09-16 11:32:10 +01005766 ret = btrfs_log_changed_extents(trans, inode, dst_path, ctx);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005767 if (ret) {
5768 err = ret;
5769 goto out_unlock;
5770 }
Josef Bacikd006a042013-11-12 20:54:09 -05005771 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06005772 struct extent_map *em, *n;
5773
Filipe Manana49dae1b2014-09-06 22:34:39 +01005774 write_lock(&em_tree->lock);
Filipe Manana48778172020-08-11 12:43:58 +01005775 list_for_each_entry_safe(em, n, &em_tree->modified_extents, list)
5776 list_del_init(&em->list);
Filipe Manana49dae1b2014-09-06 22:34:39 +01005777 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005778 }
5779
Nikolay Borisova59108a2017-01-18 00:31:48 +02005780 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
Filipe Manana90d04512021-09-16 11:32:10 +01005781 ret = log_directory_changes(trans, inode, path, dst_path, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005782 if (ret) {
5783 err = ret;
5784 goto out_unlock;
5785 }
Chris Masone02119d2008-09-05 16:13:11 -04005786 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005787
Filipe Manana130341b2021-08-31 15:30:34 +01005788 spin_lock(&inode->lock);
5789 inode->logged_trans = trans->transid;
Filipe Mananad1d832a2019-06-07 11:25:24 +01005790 /*
Filipe Manana130341b2021-08-31 15:30:34 +01005791 * Don't update last_log_commit if we logged that an inode exists.
5792 * We do this for three reasons:
5793 *
5794 * 1) We might have had buffered writes to this inode that were
5795 * flushed and had their ordered extents completed in this
5796 * transaction, but we did not previously log the inode with
5797 * LOG_INODE_ALL. Later the inode was evicted and after that
5798 * it was loaded again and this LOG_INODE_EXISTS log operation
5799 * happened. We must make sure that if an explicit fsync against
5800 * the inode is performed later, it logs the new extents, an
5801 * updated inode item, etc, and syncs the log. The same logic
5802 * applies to direct IO writes instead of buffered writes.
5803 *
5804 * 2) When we log the inode with LOG_INODE_EXISTS, its inode item
5805 * is logged with an i_size of 0 or whatever value was logged
5806 * before. If later the i_size of the inode is increased by a
5807 * truncate operation, the log is synced through an fsync of
5808 * some other inode and then finally an explicit fsync against
5809 * this inode is made, we must make sure this fsync logs the
5810 * inode with the new i_size, the hole between old i_size and
5811 * the new i_size, and syncs the log.
5812 *
5813 * 3) If we are logging that an ancestor inode exists as part of
5814 * logging a new name from a link or rename operation, don't update
5815 * its last_log_commit - otherwise if an explicit fsync is made
5816 * against an ancestor, the fsync considers the inode in the log
5817 * and doesn't sync the log, resulting in the ancestor missing after
5818 * a power failure unless the log was synced as part of an fsync
5819 * against any other unrelated inode.
Filipe Mananad1d832a2019-06-07 11:25:24 +01005820 */
Filipe Manana130341b2021-08-31 15:30:34 +01005821 if (inode_only != LOG_INODE_EXISTS)
5822 inode->last_log_commit = inode->last_sub_trans;
5823 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005824out_unlock:
Nikolay Borisova59108a2017-01-18 00:31:48 +02005825 mutex_unlock(&inode->log_mutex);
Filipe Mananaf6df27d2021-08-31 15:30:40 +01005826out:
Chris Masone02119d2008-09-05 16:13:11 -04005827 btrfs_free_path(path);
5828 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005829 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005830}
5831
Chris Mason12fcfd22009-03-24 10:24:20 -04005832/*
Filipe Mananaab123132021-01-27 10:34:56 +00005833 * Check if we need to log an inode. This is used in contexts where while
5834 * logging an inode we need to log another inode (either that it exists or in
5835 * full mode). This is used instead of btrfs_inode_in_log() because the later
5836 * requires the inode to be in the log and have the log transaction committed,
5837 * while here we do not care if the log transaction was already committed - our
5838 * caller will commit the log later - and we want to avoid logging an inode
5839 * multiple times when multiple tasks have joined the same log transaction.
5840 */
5841static bool need_log_inode(struct btrfs_trans_handle *trans,
5842 struct btrfs_inode *inode)
5843{
5844 /*
Filipe Manana8be2ba22021-07-29 18:52:46 +01005845 * If a directory was not modified, no dentries added or removed, we can
5846 * and should avoid logging it.
5847 */
5848 if (S_ISDIR(inode->vfs_inode.i_mode) && inode->last_trans < trans->transid)
5849 return false;
5850
5851 /*
Filipe Mananaab123132021-01-27 10:34:56 +00005852 * If this inode does not have new/updated/deleted xattrs since the last
5853 * time it was logged and is flagged as logged in the current transaction,
5854 * we can skip logging it. As for new/deleted names, those are updated in
5855 * the log by link/unlink/rename operations.
5856 * In case the inode was logged and then evicted and reloaded, its
5857 * logged_trans will be 0, in which case we have to fully log it since
5858 * logged_trans is a transient field, not persisted.
5859 */
5860 if (inode->logged_trans == trans->transid &&
5861 !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags))
5862 return false;
5863
5864 return true;
5865}
5866
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005867struct btrfs_dir_list {
5868 u64 ino;
5869 struct list_head list;
5870};
5871
5872/*
5873 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5874 * details about the why it is needed.
5875 * This is a recursive operation - if an existing dentry corresponds to a
5876 * directory, that directory's new entries are logged too (same behaviour as
5877 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5878 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5879 * complains about the following circular lock dependency / possible deadlock:
5880 *
5881 * CPU0 CPU1
5882 * ---- ----
5883 * lock(&type->i_mutex_dir_key#3/2);
5884 * lock(sb_internal#2);
5885 * lock(&type->i_mutex_dir_key#3/2);
5886 * lock(&sb->s_type->i_mutex_key#14);
5887 *
5888 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5889 * sb_start_intwrite() in btrfs_start_transaction().
5890 * Not locking i_mutex of the inodes is still safe because:
5891 *
5892 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5893 * that while logging the inode new references (names) are added or removed
5894 * from the inode, leaving the logged inode item with a link count that does
5895 * not match the number of logged inode reference items. This is fine because
5896 * at log replay time we compute the real number of links and correct the
5897 * link count in the inode item (see replay_one_buffer() and
5898 * link_to_fixup_dir());
5899 *
5900 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5901 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5902 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5903 * has a size that doesn't match the sum of the lengths of all the logged
5904 * names. This does not result in a problem because if a dir_item key is
5905 * logged but its matching dir_index key is not logged, at log replay time we
5906 * don't use it to replay the respective name (see replay_one_name()). On the
5907 * other hand if only the dir_index key ends up being logged, the respective
5908 * name is added to the fs/subvol tree with both the dir_item and dir_index
5909 * keys created (see replay_one_name()).
5910 * The directory's inode item with a wrong i_size is not a problem as well,
5911 * since we don't use it at log replay time to set the i_size in the inode
5912 * item of the fs/subvol tree (see overwrite_item()).
5913 */
5914static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5915 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005916 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005917 struct btrfs_log_ctx *ctx)
5918{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005919 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005920 struct btrfs_root *log = root->log_root;
5921 struct btrfs_path *path;
5922 LIST_HEAD(dir_list);
5923 struct btrfs_dir_list *dir_elem;
5924 int ret = 0;
5925
Filipe Mananac48792c2021-08-31 15:30:33 +01005926 /*
5927 * If we are logging a new name, as part of a link or rename operation,
5928 * don't bother logging new dentries, as we just want to log the names
5929 * of an inode and that any new parents exist.
5930 */
5931 if (ctx->logging_new_name)
5932 return 0;
5933
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005934 path = btrfs_alloc_path();
5935 if (!path)
5936 return -ENOMEM;
5937
5938 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5939 if (!dir_elem) {
5940 btrfs_free_path(path);
5941 return -ENOMEM;
5942 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005943 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005944 list_add_tail(&dir_elem->list, &dir_list);
5945
5946 while (!list_empty(&dir_list)) {
5947 struct extent_buffer *leaf;
5948 struct btrfs_key min_key;
5949 int nritems;
5950 int i;
5951
5952 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5953 list);
5954 if (ret)
5955 goto next_dir_inode;
5956
5957 min_key.objectid = dir_elem->ino;
5958 min_key.type = BTRFS_DIR_ITEM_KEY;
5959 min_key.offset = 0;
5960again:
5961 btrfs_release_path(path);
5962 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5963 if (ret < 0) {
5964 goto next_dir_inode;
5965 } else if (ret > 0) {
5966 ret = 0;
5967 goto next_dir_inode;
5968 }
5969
5970process_leaf:
5971 leaf = path->nodes[0];
5972 nritems = btrfs_header_nritems(leaf);
5973 for (i = path->slots[0]; i < nritems; i++) {
5974 struct btrfs_dir_item *di;
5975 struct btrfs_key di_key;
5976 struct inode *di_inode;
5977 struct btrfs_dir_list *new_dir_elem;
5978 int log_mode = LOG_INODE_EXISTS;
5979 int type;
5980
5981 btrfs_item_key_to_cpu(leaf, &min_key, i);
5982 if (min_key.objectid != dir_elem->ino ||
5983 min_key.type != BTRFS_DIR_ITEM_KEY)
5984 goto next_dir_inode;
5985
5986 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5987 type = btrfs_dir_type(leaf, di);
5988 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5989 type != BTRFS_FT_DIR)
5990 continue;
5991 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5992 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5993 continue;
5994
Robbie Koec125cf2016-10-28 10:48:26 +08005995 btrfs_release_path(path);
David Sterba0202e832020-05-15 19:35:59 +02005996 di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005997 if (IS_ERR(di_inode)) {
5998 ret = PTR_ERR(di_inode);
5999 goto next_dir_inode;
6000 }
6001
Filipe Manana0e44cb32021-01-27 10:34:58 +00006002 if (!need_log_inode(trans, BTRFS_I(di_inode))) {
Filipe Manana410f9542019-09-10 15:26:49 +01006003 btrfs_add_delayed_iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08006004 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006005 }
6006
6007 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01006008 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006009 log_mode = LOG_INODE_ALL;
Filipe Manana90d04512021-09-16 11:32:10 +01006010 ret = btrfs_log_inode(trans, BTRFS_I(di_inode),
Filipe Manana48778172020-08-11 12:43:58 +01006011 log_mode, ctx);
Filipe Manana410f9542019-09-10 15:26:49 +01006012 btrfs_add_delayed_iput(di_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006013 if (ret)
6014 goto next_dir_inode;
6015 if (ctx->log_new_dentries) {
6016 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
6017 GFP_NOFS);
6018 if (!new_dir_elem) {
6019 ret = -ENOMEM;
6020 goto next_dir_inode;
6021 }
6022 new_dir_elem->ino = di_key.objectid;
6023 list_add_tail(&new_dir_elem->list, &dir_list);
6024 }
6025 break;
6026 }
6027 if (i == nritems) {
6028 ret = btrfs_next_leaf(log, path);
6029 if (ret < 0) {
6030 goto next_dir_inode;
6031 } else if (ret > 0) {
6032 ret = 0;
6033 goto next_dir_inode;
6034 }
6035 goto process_leaf;
6036 }
6037 if (min_key.offset < (u64)-1) {
6038 min_key.offset++;
6039 goto again;
6040 }
6041next_dir_inode:
6042 list_del(&dir_elem->list);
6043 kfree(dir_elem);
6044 }
6045
6046 btrfs_free_path(path);
6047 return ret;
6048}
6049
Filipe Manana18aa0922015-08-05 16:49:08 +01006050static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02006051 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01006052 struct btrfs_log_ctx *ctx)
6053{
David Sterba3ffbd682018-06-29 10:56:42 +02006054 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana18aa0922015-08-05 16:49:08 +01006055 int ret;
6056 struct btrfs_path *path;
6057 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02006058 struct btrfs_root *root = inode->root;
6059 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01006060
6061 path = btrfs_alloc_path();
6062 if (!path)
6063 return -ENOMEM;
6064 path->skip_locking = 1;
6065 path->search_commit_root = 1;
6066
6067 key.objectid = ino;
6068 key.type = BTRFS_INODE_REF_KEY;
6069 key.offset = 0;
6070 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6071 if (ret < 0)
6072 goto out;
6073
6074 while (true) {
6075 struct extent_buffer *leaf = path->nodes[0];
6076 int slot = path->slots[0];
6077 u32 cur_offset = 0;
6078 u32 item_size;
6079 unsigned long ptr;
6080
6081 if (slot >= btrfs_header_nritems(leaf)) {
6082 ret = btrfs_next_leaf(root, path);
6083 if (ret < 0)
6084 goto out;
6085 else if (ret > 0)
6086 break;
6087 continue;
6088 }
6089
6090 btrfs_item_key_to_cpu(leaf, &key, slot);
6091 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
6092 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
6093 break;
6094
6095 item_size = btrfs_item_size_nr(leaf, slot);
6096 ptr = btrfs_item_ptr_offset(leaf, slot);
6097 while (cur_offset < item_size) {
6098 struct btrfs_key inode_key;
6099 struct inode *dir_inode;
6100
6101 inode_key.type = BTRFS_INODE_ITEM_KEY;
6102 inode_key.offset = 0;
6103
6104 if (key.type == BTRFS_INODE_EXTREF_KEY) {
6105 struct btrfs_inode_extref *extref;
6106
6107 extref = (struct btrfs_inode_extref *)
6108 (ptr + cur_offset);
6109 inode_key.objectid = btrfs_inode_extref_parent(
6110 leaf, extref);
6111 cur_offset += sizeof(*extref);
6112 cur_offset += btrfs_inode_extref_name_len(leaf,
6113 extref);
6114 } else {
6115 inode_key.objectid = key.offset;
6116 cur_offset = item_size;
6117 }
6118
David Sterba0202e832020-05-15 19:35:59 +02006119 dir_inode = btrfs_iget(fs_info->sb, inode_key.objectid,
6120 root);
Filipe Manana0f375ee2018-10-09 15:05:29 +01006121 /*
6122 * If the parent inode was deleted, return an error to
6123 * fallback to a transaction commit. This is to prevent
6124 * getting an inode that was moved from one parent A to
6125 * a parent B, got its former parent A deleted and then
6126 * it got fsync'ed, from existing at both parents after
6127 * a log replay (and the old parent still existing).
6128 * Example:
6129 *
6130 * mkdir /mnt/A
6131 * mkdir /mnt/B
6132 * touch /mnt/B/bar
6133 * sync
6134 * mv /mnt/B/bar /mnt/A/bar
6135 * mv -T /mnt/A /mnt/B
6136 * fsync /mnt/B/bar
6137 * <power fail>
6138 *
6139 * If we ignore the old parent B which got deleted,
6140 * after a log replay we would have file bar linked
6141 * at both parents and the old parent B would still
6142 * exist.
6143 */
6144 if (IS_ERR(dir_inode)) {
6145 ret = PTR_ERR(dir_inode);
6146 goto out;
6147 }
Filipe Manana18aa0922015-08-05 16:49:08 +01006148
Filipe Manana3e6a86a2021-01-27 10:34:57 +00006149 if (!need_log_inode(trans, BTRFS_I(dir_inode))) {
6150 btrfs_add_delayed_iput(dir_inode);
6151 continue;
6152 }
6153
Filipe Manana289cffc2021-08-31 15:30:32 +01006154 ctx->log_new_dentries = false;
Filipe Manana90d04512021-09-16 11:32:10 +01006155 ret = btrfs_log_inode(trans, BTRFS_I(dir_inode),
Filipe Manana48778172020-08-11 12:43:58 +01006156 LOG_INODE_ALL, ctx);
Filipe Manana289cffc2021-08-31 15:30:32 +01006157 if (!ret && ctx->log_new_dentries)
Filipe Manana657ed1a2016-04-06 17:11:56 +01006158 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01006159 BTRFS_I(dir_inode), ctx);
Filipe Manana410f9542019-09-10 15:26:49 +01006160 btrfs_add_delayed_iput(dir_inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01006161 if (ret)
6162 goto out;
6163 }
6164 path->slots[0]++;
6165 }
6166 ret = 0;
6167out:
6168 btrfs_free_path(path);
6169 return ret;
6170}
6171
Filipe Mananab8aa3302019-04-17 11:31:06 +01006172static int log_new_ancestors(struct btrfs_trans_handle *trans,
6173 struct btrfs_root *root,
6174 struct btrfs_path *path,
6175 struct btrfs_log_ctx *ctx)
6176{
6177 struct btrfs_key found_key;
6178
6179 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
6180
6181 while (true) {
6182 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananab8aa3302019-04-17 11:31:06 +01006183 struct extent_buffer *leaf = path->nodes[0];
6184 int slot = path->slots[0];
6185 struct btrfs_key search_key;
6186 struct inode *inode;
David Sterba0202e832020-05-15 19:35:59 +02006187 u64 ino;
Filipe Mananab8aa3302019-04-17 11:31:06 +01006188 int ret = 0;
6189
6190 btrfs_release_path(path);
6191
David Sterba0202e832020-05-15 19:35:59 +02006192 ino = found_key.offset;
6193
Filipe Mananab8aa3302019-04-17 11:31:06 +01006194 search_key.objectid = found_key.offset;
6195 search_key.type = BTRFS_INODE_ITEM_KEY;
6196 search_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02006197 inode = btrfs_iget(fs_info->sb, ino, root);
Filipe Mananab8aa3302019-04-17 11:31:06 +01006198 if (IS_ERR(inode))
6199 return PTR_ERR(inode);
6200
Filipe Mananaab123132021-01-27 10:34:56 +00006201 if (BTRFS_I(inode)->generation >= trans->transid &&
6202 need_log_inode(trans, BTRFS_I(inode)))
Filipe Manana90d04512021-09-16 11:32:10 +01006203 ret = btrfs_log_inode(trans, BTRFS_I(inode),
Filipe Manana48778172020-08-11 12:43:58 +01006204 LOG_INODE_EXISTS, ctx);
Filipe Manana410f9542019-09-10 15:26:49 +01006205 btrfs_add_delayed_iput(inode);
Filipe Mananab8aa3302019-04-17 11:31:06 +01006206 if (ret)
6207 return ret;
6208
6209 if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
6210 break;
6211
6212 search_key.type = BTRFS_INODE_REF_KEY;
6213 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
6214 if (ret < 0)
6215 return ret;
6216
6217 leaf = path->nodes[0];
6218 slot = path->slots[0];
6219 if (slot >= btrfs_header_nritems(leaf)) {
6220 ret = btrfs_next_leaf(root, path);
6221 if (ret < 0)
6222 return ret;
6223 else if (ret > 0)
6224 return -ENOENT;
6225 leaf = path->nodes[0];
6226 slot = path->slots[0];
6227 }
6228
6229 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6230 if (found_key.objectid != search_key.objectid ||
6231 found_key.type != BTRFS_INODE_REF_KEY)
6232 return -ENOENT;
6233 }
6234 return 0;
6235}
6236
6237static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
6238 struct btrfs_inode *inode,
6239 struct dentry *parent,
6240 struct btrfs_log_ctx *ctx)
6241{
6242 struct btrfs_root *root = inode->root;
Filipe Mananab8aa3302019-04-17 11:31:06 +01006243 struct dentry *old_parent = NULL;
6244 struct super_block *sb = inode->vfs_inode.i_sb;
6245 int ret = 0;
6246
6247 while (true) {
6248 if (!parent || d_really_is_negative(parent) ||
6249 sb != parent->d_sb)
6250 break;
6251
6252 inode = BTRFS_I(d_inode(parent));
6253 if (root != inode->root)
6254 break;
6255
Filipe Mananaab123132021-01-27 10:34:56 +00006256 if (inode->generation >= trans->transid &&
6257 need_log_inode(trans, inode)) {
Filipe Manana90d04512021-09-16 11:32:10 +01006258 ret = btrfs_log_inode(trans, inode,
Filipe Manana48778172020-08-11 12:43:58 +01006259 LOG_INODE_EXISTS, ctx);
Filipe Mananab8aa3302019-04-17 11:31:06 +01006260 if (ret)
6261 break;
6262 }
6263 if (IS_ROOT(parent))
6264 break;
6265
6266 parent = dget_parent(parent);
6267 dput(old_parent);
6268 old_parent = parent;
6269 }
6270 dput(old_parent);
6271
6272 return ret;
6273}
6274
6275static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
6276 struct btrfs_inode *inode,
6277 struct dentry *parent,
6278 struct btrfs_log_ctx *ctx)
6279{
6280 struct btrfs_root *root = inode->root;
6281 const u64 ino = btrfs_ino(inode);
6282 struct btrfs_path *path;
6283 struct btrfs_key search_key;
6284 int ret;
6285
6286 /*
6287 * For a single hard link case, go through a fast path that does not
6288 * need to iterate the fs/subvolume tree.
6289 */
6290 if (inode->vfs_inode.i_nlink < 2)
6291 return log_new_ancestors_fast(trans, inode, parent, ctx);
6292
6293 path = btrfs_alloc_path();
6294 if (!path)
6295 return -ENOMEM;
6296
6297 search_key.objectid = ino;
6298 search_key.type = BTRFS_INODE_REF_KEY;
6299 search_key.offset = 0;
6300again:
6301 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
6302 if (ret < 0)
6303 goto out;
6304 if (ret == 0)
6305 path->slots[0]++;
6306
6307 while (true) {
6308 struct extent_buffer *leaf = path->nodes[0];
6309 int slot = path->slots[0];
6310 struct btrfs_key found_key;
6311
6312 if (slot >= btrfs_header_nritems(leaf)) {
6313 ret = btrfs_next_leaf(root, path);
6314 if (ret < 0)
6315 goto out;
6316 else if (ret > 0)
6317 break;
6318 continue;
6319 }
6320
6321 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6322 if (found_key.objectid != ino ||
6323 found_key.type > BTRFS_INODE_EXTREF_KEY)
6324 break;
6325
6326 /*
6327 * Don't deal with extended references because they are rare
6328 * cases and too complex to deal with (we would need to keep
6329 * track of which subitem we are processing for each item in
6330 * this loop, etc). So just return some error to fallback to
6331 * a transaction commit.
6332 */
6333 if (found_key.type == BTRFS_INODE_EXTREF_KEY) {
6334 ret = -EMLINK;
6335 goto out;
6336 }
6337
6338 /*
6339 * Logging ancestors needs to do more searches on the fs/subvol
6340 * tree, so it releases the path as needed to avoid deadlocks.
6341 * Keep track of the last inode ref key and resume from that key
6342 * after logging all new ancestors for the current hard link.
6343 */
6344 memcpy(&search_key, &found_key, sizeof(search_key));
6345
6346 ret = log_new_ancestors(trans, root, path, ctx);
6347 if (ret)
6348 goto out;
6349 btrfs_release_path(path);
6350 goto again;
6351 }
6352 ret = 0;
6353out:
6354 btrfs_free_path(path);
6355 return ret;
6356}
6357
Chris Masone02119d2008-09-05 16:13:11 -04006358/*
6359 * helper function around btrfs_log_inode to make sure newly created
6360 * parent directories also end up in the log. A minimal inode and backref
6361 * only logging is done of any parent directories that are older than
6362 * the last committed transaction
6363 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006364static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006365 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01006366 struct dentry *parent,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07006367 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08006368 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04006369{
Nikolay Borisovf8822742018-02-27 17:37:17 +02006370 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006371 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason12fcfd22009-03-24 10:24:20 -04006372 int ret = 0;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006373 bool log_dentries = false;
Chris Mason12fcfd22009-03-24 10:24:20 -04006374
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006375 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04006376 ret = 1;
6377 goto end_no_trans;
6378 }
6379
Nikolay Borisovf8822742018-02-27 17:37:17 +02006380 if (btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04006381 ret = 1;
6382 goto end_no_trans;
6383 }
6384
Filipe Mananaf2d72f42018-10-08 11:12:55 +01006385 /*
6386 * Skip already logged inodes or inodes corresponding to tmpfiles
6387 * (since logging them is pointless, a link count of 0 means they
6388 * will never be accessible).
6389 */
Filipe Manana626e9f42021-04-27 11:27:20 +01006390 if ((btrfs_inode_in_log(inode, trans->transid) &&
6391 list_empty(&ctx->ordered_extents)) ||
Filipe Mananaf2d72f42018-10-08 11:12:55 +01006392 inode->vfs_inode.i_nlink == 0) {
Chris Mason257c62e2009-10-13 13:21:08 -04006393 ret = BTRFS_NO_LOG_SYNC;
6394 goto end_no_trans;
6395 }
6396
Miao Xie8b050d32014-02-20 18:08:58 +08006397 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006398 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08006399 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04006400
Filipe Manana90d04512021-09-16 11:32:10 +01006401 ret = btrfs_log_inode(trans, inode, inode_only, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006402 if (ret)
6403 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04006404
Chris Masonaf4176b2009-03-24 10:24:31 -04006405 /*
6406 * for regular files, if its inode is already on disk, we don't
6407 * have to worry about the parents at all. This is because
6408 * we can use the last_unlink_trans field to record renames
6409 * and other fun in this file.
6410 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006411 if (S_ISREG(inode->vfs_inode.i_mode) &&
Filipe Manana47d3db42020-11-25 12:19:26 +00006412 inode->generation < trans->transid &&
6413 inode->last_unlink_trans < trans->transid) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006414 ret = 0;
6415 goto end_trans;
6416 }
Chris Masonaf4176b2009-03-24 10:24:31 -04006417
Filipe Manana289cffc2021-08-31 15:30:32 +01006418 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006419 log_dentries = true;
6420
Filipe Manana18aa0922015-08-05 16:49:08 +01006421 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006422 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01006423 * inodes are fully logged. This is to prevent leaving dangling
6424 * directory index entries in directories that were our parents but are
6425 * not anymore. Not doing this results in old parent directory being
6426 * impossible to delete after log replay (rmdir will always fail with
6427 * error -ENOTEMPTY).
6428 *
6429 * Example 1:
6430 *
6431 * mkdir testdir
6432 * touch testdir/foo
6433 * ln testdir/foo testdir/bar
6434 * sync
6435 * unlink testdir/bar
6436 * xfs_io -c fsync testdir/foo
6437 * <power failure>
6438 * mount fs, triggers log replay
6439 *
6440 * If we don't log the parent directory (testdir), after log replay the
6441 * directory still has an entry pointing to the file inode using the bar
6442 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
6443 * the file inode has a link count of 1.
6444 *
6445 * Example 2:
6446 *
6447 * mkdir testdir
6448 * touch foo
6449 * ln foo testdir/foo2
6450 * ln foo testdir/foo3
6451 * sync
6452 * unlink testdir/foo3
6453 * xfs_io -c fsync foo
6454 * <power failure>
6455 * mount fs, triggers log replay
6456 *
6457 * Similar as the first example, after log replay the parent directory
6458 * testdir still has an entry pointing to the inode file with name foo3
6459 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
6460 * and has a link count of 2.
6461 */
Filipe Manana47d3db42020-11-25 12:19:26 +00006462 if (inode->last_unlink_trans >= trans->transid) {
Filipe Mananab8aa3302019-04-17 11:31:06 +01006463 ret = btrfs_log_all_parents(trans, inode, ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01006464 if (ret)
6465 goto end_trans;
6466 }
6467
Filipe Mananab8aa3302019-04-17 11:31:06 +01006468 ret = log_all_new_ancestors(trans, inode, parent, ctx);
6469 if (ret)
Filipe Manana41bd6062018-11-28 14:54:28 +00006470 goto end_trans;
Filipe Manana41bd6062018-11-28 14:54:28 +00006471
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006472 if (log_dentries)
Filipe Mananab8aa3302019-04-17 11:31:06 +01006473 ret = log_new_dir_dentries(trans, root, inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006474 else
6475 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006476end_trans:
6477 if (ret < 0) {
David Sterba90787762019-03-20 13:28:05 +01006478 btrfs_set_log_full_commit(trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006479 ret = 1;
6480 }
Miao Xie8b050d32014-02-20 18:08:58 +08006481
6482 if (ret)
6483 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04006484 btrfs_end_log_trans(root);
6485end_no_trans:
6486 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006487}
6488
6489/*
6490 * it is not safe to log dentry if the chunk root has added new
6491 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
6492 * If this returns 1, you must commit the transaction to safely get your
6493 * data on disk.
6494 */
6495int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Nikolay Borisove5b84f7a2018-02-27 17:37:18 +02006496 struct dentry *dentry,
Miao Xie8b050d32014-02-20 18:08:58 +08006497 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04006498{
Josef Bacik6a912212010-11-20 09:48:00 +00006499 struct dentry *parent = dget_parent(dentry);
6500 int ret;
6501
Nikolay Borisovf8822742018-02-27 17:37:17 +02006502 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
Filipe Manana48778172020-08-11 12:43:58 +01006503 LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00006504 dput(parent);
6505
6506 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006507}
6508
6509/*
6510 * should be called during mount to recover any replay any log trees
6511 * from the FS
6512 */
6513int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
6514{
6515 int ret;
6516 struct btrfs_path *path;
6517 struct btrfs_trans_handle *trans;
6518 struct btrfs_key key;
6519 struct btrfs_key found_key;
Chris Masone02119d2008-09-05 16:13:11 -04006520 struct btrfs_root *log;
6521 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
6522 struct walk_control wc = {
6523 .process_func = process_one_buffer,
David Sterba430a6622019-08-01 14:50:35 +02006524 .stage = LOG_WALK_PIN_ONLY,
Chris Masone02119d2008-09-05 16:13:11 -04006525 };
6526
Chris Masone02119d2008-09-05 16:13:11 -04006527 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006528 if (!path)
6529 return -ENOMEM;
6530
Josef Bacikafcdd122016-09-02 15:40:02 -04006531 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04006532
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006533 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006534 if (IS_ERR(trans)) {
6535 ret = PTR_ERR(trans);
6536 goto error;
6537 }
Chris Masone02119d2008-09-05 16:13:11 -04006538
6539 wc.trans = trans;
6540 wc.pin = 1;
6541
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006542 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006543 if (ret) {
Josef Bacikba51e2a2021-10-05 16:35:23 -04006544 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006545 goto error;
6546 }
Chris Masone02119d2008-09-05 16:13:11 -04006547
6548again:
6549 key.objectid = BTRFS_TREE_LOG_OBJECTID;
6550 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02006551 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04006552
Chris Masond3977122009-01-05 21:25:51 -05006553 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04006554 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006555
6556 if (ret < 0) {
Josef Bacikba51e2a2021-10-05 16:35:23 -04006557 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006558 goto error;
6559 }
Chris Masone02119d2008-09-05 16:13:11 -04006560 if (ret > 0) {
6561 if (path->slots[0] == 0)
6562 break;
6563 path->slots[0]--;
6564 }
6565 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
6566 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02006567 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006568 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
6569 break;
6570
Josef Bacik62a2c732020-01-24 09:32:21 -05006571 log = btrfs_read_tree_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006572 if (IS_ERR(log)) {
6573 ret = PTR_ERR(log);
Josef Bacikba51e2a2021-10-05 16:35:23 -04006574 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006575 goto error;
6576 }
Chris Masone02119d2008-09-05 16:13:11 -04006577
David Sterba56e93572020-05-15 19:35:55 +02006578 wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset,
6579 true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006580 if (IS_ERR(wc.replay_dest)) {
6581 ret = PTR_ERR(wc.replay_dest);
Josef Bacik9bc574d2019-12-06 09:37:17 -05006582
6583 /*
6584 * We didn't find the subvol, likely because it was
6585 * deleted. This is ok, simply skip this log and go to
6586 * the next one.
6587 *
6588 * We need to exclude the root because we can't have
6589 * other log replays overwriting this log as we'll read
6590 * it back in a few more times. This will keep our
6591 * block from being modified, and we'll just bail for
6592 * each subsequent pass.
6593 */
6594 if (ret == -ENOENT)
Nikolay Borisov9fce5702020-01-20 16:09:13 +02006595 ret = btrfs_pin_extent_for_log_replay(trans,
Josef Bacik9bc574d2019-12-06 09:37:17 -05006596 log->node->start,
6597 log->node->len);
Josef Bacik00246522020-01-24 09:33:01 -05006598 btrfs_put_root(log);
Josef Bacik9bc574d2019-12-06 09:37:17 -05006599
6600 if (!ret)
6601 goto next;
Josef Bacikba51e2a2021-10-05 16:35:23 -04006602 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006603 goto error;
6604 }
Chris Masone02119d2008-09-05 16:13:11 -04006605
Yan Zheng07d400a2009-01-06 11:42:00 -05006606 wc.replay_dest->log_root = log;
Josef Bacik2002ae12021-03-12 15:25:05 -05006607 ret = btrfs_record_root_in_trans(trans, wc.replay_dest);
6608 if (ret)
6609 /* The loop needs to continue due to the root refs */
Josef Bacikba51e2a2021-10-05 16:35:23 -04006610 btrfs_abort_transaction(trans, ret);
Josef Bacik2002ae12021-03-12 15:25:05 -05006611 else
6612 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04006613
Josef Bacikb50c6e22013-04-25 15:55:30 -04006614 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04006615 ret = fixup_inode_link_counts(trans, wc.replay_dest,
6616 path);
Josef Bacikba51e2a2021-10-05 16:35:23 -04006617 if (ret)
6618 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04006619 }
Chris Masone02119d2008-09-05 16:13:11 -04006620
Liu Bo900c9982018-01-25 11:02:56 -07006621 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
6622 struct btrfs_root *root = wc.replay_dest;
6623
6624 btrfs_release_path(path);
6625
6626 /*
6627 * We have just replayed everything, and the highest
6628 * objectid of fs roots probably has changed in case
6629 * some inode_item's got replayed.
6630 *
6631 * root->objectid_mutex is not acquired as log replay
6632 * could only happen during mount.
6633 */
Nikolay Borisov453e4872020-12-07 17:32:32 +02006634 ret = btrfs_init_root_free_objectid(root);
Josef Bacikba51e2a2021-10-05 16:35:23 -04006635 if (ret)
6636 btrfs_abort_transaction(trans, ret);
Liu Bo900c9982018-01-25 11:02:56 -07006637 }
6638
Yan Zheng07d400a2009-01-06 11:42:00 -05006639 wc.replay_dest->log_root = NULL;
Josef Bacik00246522020-01-24 09:33:01 -05006640 btrfs_put_root(wc.replay_dest);
Josef Bacik00246522020-01-24 09:33:01 -05006641 btrfs_put_root(log);
Chris Masone02119d2008-09-05 16:13:11 -04006642
Josef Bacikb50c6e22013-04-25 15:55:30 -04006643 if (ret)
6644 goto error;
Josef Bacik9bc574d2019-12-06 09:37:17 -05006645next:
Chris Masone02119d2008-09-05 16:13:11 -04006646 if (found_key.offset == 0)
6647 break;
Josef Bacik9bc574d2019-12-06 09:37:17 -05006648 key.offset = found_key.offset - 1;
Chris Masone02119d2008-09-05 16:13:11 -04006649 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006650 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006651
6652 /* step one is to pin it all, step two is to replay just inodes */
6653 if (wc.pin) {
6654 wc.pin = 0;
6655 wc.process_func = replay_one_buffer;
6656 wc.stage = LOG_WALK_REPLAY_INODES;
6657 goto again;
6658 }
6659 /* step three is to replay everything */
6660 if (wc.stage < LOG_WALK_REPLAY_ALL) {
6661 wc.stage++;
6662 goto again;
6663 }
6664
6665 btrfs_free_path(path);
6666
Josef Bacikabefa552013-04-24 16:40:05 -04006667 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006668 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04006669 if (ret)
6670 return ret;
6671
Chris Masone02119d2008-09-05 16:13:11 -04006672 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04006673 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Josef Bacik00246522020-01-24 09:33:01 -05006674 btrfs_put_root(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006675
Josef Bacikabefa552013-04-24 16:40:05 -04006676 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006677error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04006678 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006679 btrfs_end_transaction(wc.trans);
David Sterba1aeb6b52020-07-07 18:38:05 +02006680 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006681 btrfs_free_path(path);
6682 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006683}
Chris Mason12fcfd22009-03-24 10:24:20 -04006684
6685/*
6686 * there are some corner cases where we want to force a full
6687 * commit instead of allowing a directory to be logged.
6688 *
6689 * They revolve around files there were unlinked from the directory, and
6690 * this function updates the parent directory so that a full commit is
6691 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00006692 *
6693 * Must be called before the unlink operations (updates to the subvolume tree,
6694 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04006695 */
6696void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006697 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04006698 int for_rename)
6699{
6700 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006701 * when we're logging a file, if it hasn't been renamed
6702 * or unlinked, and its inode is fully committed on disk,
6703 * we don't have to worry about walking up the directory chain
6704 * to log its parents.
6705 *
6706 * So, we use the last_unlink_trans field to put this transid
6707 * into the file. When the file is logged we check it and
6708 * don't log the parents if the file is fully on disk.
6709 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006710 mutex_lock(&inode->log_mutex);
6711 inode->last_unlink_trans = trans->transid;
6712 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04006713
6714 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006715 * if this directory was already logged any new
6716 * names for this file/dir will get recorded
6717 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006718 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006719 return;
6720
6721 /*
6722 * if the inode we're about to unlink was logged,
6723 * the log will be properly updated for any new names
6724 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006725 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006726 return;
6727
6728 /*
6729 * when renaming files across directories, if the directory
6730 * there we're unlinking from gets fsync'd later on, there's
6731 * no way to find the destination directory later and fsync it
6732 * properly. So, we have to be conservative and force commits
6733 * so the new name gets discovered.
6734 */
6735 if (for_rename)
6736 goto record;
6737
6738 /* we can safely do the unlink without any special recording */
6739 return;
6740
6741record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006742 mutex_lock(&dir->log_mutex);
6743 dir->last_unlink_trans = trans->transid;
6744 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04006745}
6746
6747/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006748 * Make sure that if someone attempts to fsync the parent directory of a deleted
6749 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6750 * that after replaying the log tree of the parent directory's root we will not
6751 * see the snapshot anymore and at log replay time we will not see any log tree
6752 * corresponding to the deleted snapshot's root, which could lead to replaying
6753 * it after replaying the log tree of the parent directory (which would replay
6754 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00006755 *
6756 * Must be called before the actual snapshot destroy operation (updates to the
6757 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006758 */
6759void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02006760 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006761{
Nikolay Borisov43663552017-01-18 00:31:29 +02006762 mutex_lock(&dir->log_mutex);
6763 dir->last_unlink_trans = trans->transid;
6764 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006765}
6766
6767/*
Chris Mason12fcfd22009-03-24 10:24:20 -04006768 * Call this after adding a new name for a file and it will properly
6769 * update the log to reflect the new name.
Chris Mason12fcfd22009-03-24 10:24:20 -04006770 */
Filipe Manana75b463d2020-08-11 12:43:48 +01006771void btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006772 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Filipe Manana75b463d2020-08-11 12:43:48 +01006773 struct dentry *parent)
Chris Mason12fcfd22009-03-24 10:24:20 -04006774{
Filipe Manana75b463d2020-08-11 12:43:48 +01006775 struct btrfs_log_ctx ctx;
Chris Mason12fcfd22009-03-24 10:24:20 -04006776
6777 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006778 * this will force the logging code to walk the dentry chain
6779 * up for the file
6780 */
Filipe Manana9a6509c2018-02-28 15:55:40 +00006781 if (!S_ISDIR(inode->vfs_inode.i_mode))
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006782 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04006783
6784 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006785 * if this inode hasn't been logged and directory we're renaming it
6786 * from hasn't been logged, we don't need to log it
6787 */
Filipe Mananaecc64fa2021-07-27 11:24:43 +01006788 if (!inode_logged(trans, inode) &&
6789 (!old_dir || !inode_logged(trans, old_dir)))
Filipe Manana75b463d2020-08-11 12:43:48 +01006790 return;
Chris Mason12fcfd22009-03-24 10:24:20 -04006791
Filipe Manana54a40fc2021-05-12 16:27:16 +01006792 /*
6793 * If we are doing a rename (old_dir is not NULL) from a directory that
6794 * was previously logged, make sure the next log attempt on the directory
6795 * is not skipped and logs the inode again. This is because the log may
6796 * not currently be authoritative for a range including the old
6797 * BTRFS_DIR_ITEM_KEY and BTRFS_DIR_INDEX_KEY keys, so we want to make
6798 * sure after a log replay we do not end up with both the new and old
6799 * dentries around (in case the inode is a directory we would have a
6800 * directory with two hard links and 2 inode references for different
6801 * parents). The next log attempt of old_dir will happen at
6802 * btrfs_log_all_parents(), called through btrfs_log_inode_parent()
6803 * below, because we have previously set inode->last_unlink_trans to the
6804 * current transaction ID, either here or at btrfs_record_unlink_dir() in
6805 * case inode is a directory.
6806 */
6807 if (old_dir)
6808 old_dir->logged_trans = 0;
6809
Filipe Manana75b463d2020-08-11 12:43:48 +01006810 btrfs_init_log_ctx(&ctx, &inode->vfs_inode);
6811 ctx.logging_new_name = true;
6812 /*
6813 * We don't care about the return value. If we fail to log the new name
6814 * then we know the next attempt to sync the log will fallback to a full
6815 * transaction commit (due to a call to btrfs_set_log_full_commit()), so
6816 * we don't need to worry about getting a log committed that has an
6817 * inconsistent state after a rename operation.
6818 */
Filipe Manana48778172020-08-11 12:43:58 +01006819 btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04006820}
6821