blob: 71b3ddb0333dfcc6b80e33fbb3567b0c94d300fb [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,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100792 key->objectid, offset);
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,
924 struct btrfs_root *root,
925 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200926 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400927 struct btrfs_dir_item *di)
928{
929 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
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200957 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
958 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
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001122 ret = btrfs_unlink_inode(trans, root, 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) {
1184 return ret;
1185 } else if (!ret) {
Mark Fashehf1863732012-08-08 11:32:27 -07001186 ret = -ENOENT;
1187 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001188 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001189 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001190 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001191 btrfs_release_path(path);
1192
1193 ret = btrfs_unlink_inode(trans, root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001194 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001195 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001196 victim_name,
1197 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001198 if (!ret)
1199 ret = btrfs_run_delayed_items(
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001200 trans);
Mark Fashehf1863732012-08-08 11:32:27 -07001201 }
Mark Fashehf1863732012-08-08 11:32:27 -07001202 iput(victim_parent);
1203 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001204 if (ret)
1205 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001206 *search_done = 1;
1207 goto again;
1208 }
1209 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001210next:
1211 cur_offset += victim_name_len + sizeof(*extref);
1212 }
1213 *search_done = 1;
1214 }
1215 btrfs_release_path(path);
1216
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001217 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001218 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001219 ref_index, name, namelen, 0);
Filipe Manana52db7772021-10-01 13:52:32 +01001220 if (IS_ERR(di)) {
Filipe Manana8dcbc262021-10-01 13:52:33 +01001221 return PTR_ERR(di);
Filipe Manana52db7772021-10-01 13:52:32 +01001222 } else if (di) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001223 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001224 if (ret)
1225 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001226 }
1227 btrfs_release_path(path);
1228
Andrea Gelmini52042d82018-11-28 12:05:13 +01001229 /* look for a conflicting name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001230 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001231 name, namelen, 0);
Filipe Manana52db7772021-10-01 13:52:32 +01001232 if (IS_ERR(di)) {
1233 return PTR_ERR(di);
1234 } else if (di) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001235 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001236 if (ret)
1237 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001238 }
1239 btrfs_release_path(path);
1240
1241 return 0;
1242}
Chris Masone02119d2008-09-05 16:13:11 -04001243
Qu Wenruobae15d92017-11-08 08:54:26 +08001244static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1245 u32 *namelen, char **name, u64 *index,
1246 u64 *parent_objectid)
Mark Fashehf1863732012-08-08 11:32:27 -07001247{
1248 struct btrfs_inode_extref *extref;
1249
1250 extref = (struct btrfs_inode_extref *)ref_ptr;
1251
1252 *namelen = btrfs_inode_extref_name_len(eb, extref);
1253 *name = kmalloc(*namelen, GFP_NOFS);
1254 if (*name == NULL)
1255 return -ENOMEM;
1256
1257 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1258 *namelen);
1259
Filipe Manana1f250e92018-02-28 15:56:10 +00001260 if (index)
1261 *index = btrfs_inode_extref_index(eb, extref);
Mark Fashehf1863732012-08-08 11:32:27 -07001262 if (parent_objectid)
1263 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1264
1265 return 0;
1266}
1267
Qu Wenruobae15d92017-11-08 08:54:26 +08001268static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1269 u32 *namelen, char **name, u64 *index)
Mark Fashehf1863732012-08-08 11:32:27 -07001270{
1271 struct btrfs_inode_ref *ref;
1272
1273 ref = (struct btrfs_inode_ref *)ref_ptr;
1274
1275 *namelen = btrfs_inode_ref_name_len(eb, ref);
1276 *name = kmalloc(*namelen, GFP_NOFS);
1277 if (*name == NULL)
1278 return -ENOMEM;
1279
1280 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1281
Filipe Manana1f250e92018-02-28 15:56:10 +00001282 if (index)
1283 *index = btrfs_inode_ref_index(eb, ref);
Mark Fashehf1863732012-08-08 11:32:27 -07001284
1285 return 0;
1286}
1287
Chris Masone02119d2008-09-05 16:13:11 -04001288/*
Filipe Manana1f250e92018-02-28 15:56:10 +00001289 * Take an inode reference item from the log tree and iterate all names from the
1290 * inode reference item in the subvolume tree with the same key (if it exists).
1291 * For any name that is not in the inode reference item from the log tree, do a
1292 * proper unlink of that name (that is, remove its entry from the inode
1293 * reference item and both dir index keys).
1294 */
1295static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1296 struct btrfs_root *root,
1297 struct btrfs_path *path,
1298 struct btrfs_inode *inode,
1299 struct extent_buffer *log_eb,
1300 int log_slot,
1301 struct btrfs_key *key)
1302{
1303 int ret;
1304 unsigned long ref_ptr;
1305 unsigned long ref_end;
1306 struct extent_buffer *eb;
1307
1308again:
1309 btrfs_release_path(path);
1310 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1311 if (ret > 0) {
1312 ret = 0;
1313 goto out;
1314 }
1315 if (ret < 0)
1316 goto out;
1317
1318 eb = path->nodes[0];
1319 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1320 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1321 while (ref_ptr < ref_end) {
1322 char *name = NULL;
1323 int namelen;
1324 u64 parent_id;
1325
1326 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1327 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1328 NULL, &parent_id);
1329 } else {
1330 parent_id = key->offset;
1331 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1332 NULL);
1333 }
1334 if (ret)
1335 goto out;
1336
1337 if (key->type == BTRFS_INODE_EXTREF_KEY)
Nikolay Borisov6ff49c62019-08-27 14:46:29 +03001338 ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot,
1339 parent_id, name,
1340 namelen);
Filipe Manana1f250e92018-02-28 15:56:10 +00001341 else
Nikolay Borisov9bb84072019-08-27 14:46:28 +03001342 ret = !!btrfs_find_name_in_backref(log_eb, log_slot,
1343 name, namelen);
Filipe Manana1f250e92018-02-28 15:56:10 +00001344
1345 if (!ret) {
1346 struct inode *dir;
1347
1348 btrfs_release_path(path);
1349 dir = read_one_inode(root, parent_id);
1350 if (!dir) {
1351 ret = -ENOENT;
1352 kfree(name);
1353 goto out;
1354 }
1355 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1356 inode, name, namelen);
1357 kfree(name);
1358 iput(dir);
1359 if (ret)
1360 goto out;
1361 goto again;
1362 }
1363
1364 kfree(name);
1365 ref_ptr += namelen;
1366 if (key->type == BTRFS_INODE_EXTREF_KEY)
1367 ref_ptr += sizeof(struct btrfs_inode_extref);
1368 else
1369 ref_ptr += sizeof(struct btrfs_inode_ref);
1370 }
1371 ret = 0;
1372 out:
1373 btrfs_release_path(path);
1374 return ret;
1375}
1376
Filipe Manana0d836392018-07-20 10:59:06 +01001377static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
1378 const u8 ref_type, const char *name,
1379 const int namelen)
1380{
1381 struct btrfs_key key;
1382 struct btrfs_path *path;
1383 const u64 parent_id = btrfs_ino(BTRFS_I(dir));
1384 int ret;
1385
1386 path = btrfs_alloc_path();
1387 if (!path)
1388 return -ENOMEM;
1389
1390 key.objectid = btrfs_ino(BTRFS_I(inode));
1391 key.type = ref_type;
1392 if (key.type == BTRFS_INODE_REF_KEY)
1393 key.offset = parent_id;
1394 else
1395 key.offset = btrfs_extref_hash(parent_id, name, namelen);
1396
1397 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
1398 if (ret < 0)
1399 goto out;
1400 if (ret > 0) {
1401 ret = 0;
1402 goto out;
1403 }
1404 if (key.type == BTRFS_INODE_EXTREF_KEY)
Nikolay Borisov6ff49c62019-08-27 14:46:29 +03001405 ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
1406 path->slots[0], parent_id, name, namelen);
Filipe Manana0d836392018-07-20 10:59:06 +01001407 else
Nikolay Borisov9bb84072019-08-27 14:46:28 +03001408 ret = !!btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
1409 name, namelen);
Filipe Manana0d836392018-07-20 10:59:06 +01001410
1411out:
1412 btrfs_free_path(path);
1413 return ret;
1414}
1415
Filipe Manana6b5fc432019-02-13 12:14:03 +00001416static int add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1417 struct inode *dir, struct inode *inode, const char *name,
1418 int namelen, u64 ref_index)
1419{
1420 struct btrfs_dir_item *dir_item;
1421 struct btrfs_key key;
1422 struct btrfs_path *path;
1423 struct inode *other_inode = NULL;
1424 int ret;
1425
1426 path = btrfs_alloc_path();
1427 if (!path)
1428 return -ENOMEM;
1429
1430 dir_item = btrfs_lookup_dir_item(NULL, root, path,
1431 btrfs_ino(BTRFS_I(dir)),
1432 name, namelen, 0);
1433 if (!dir_item) {
1434 btrfs_release_path(path);
1435 goto add_link;
1436 } else if (IS_ERR(dir_item)) {
1437 ret = PTR_ERR(dir_item);
1438 goto out;
1439 }
1440
1441 /*
1442 * Our inode's dentry collides with the dentry of another inode which is
1443 * in the log but not yet processed since it has a higher inode number.
1444 * So delete that other dentry.
1445 */
1446 btrfs_dir_item_key_to_cpu(path->nodes[0], dir_item, &key);
1447 btrfs_release_path(path);
1448 other_inode = read_one_inode(root, key.objectid);
1449 if (!other_inode) {
1450 ret = -ENOENT;
1451 goto out;
1452 }
1453 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), BTRFS_I(other_inode),
1454 name, namelen);
1455 if (ret)
1456 goto out;
1457 /*
1458 * If we dropped the link count to 0, bump it so that later the iput()
1459 * on the inode will not free it. We will fixup the link count later.
1460 */
1461 if (other_inode->i_nlink == 0)
1462 inc_nlink(other_inode);
1463
1464 ret = btrfs_run_delayed_items(trans);
1465 if (ret)
1466 goto out;
1467add_link:
1468 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
1469 name, namelen, 0, ref_index);
1470out:
1471 iput(other_inode);
1472 btrfs_free_path(path);
1473
1474 return ret;
1475}
1476
Filipe Manana1f250e92018-02-28 15:56:10 +00001477/*
Chris Masone02119d2008-09-05 16:13:11 -04001478 * replay one inode back reference item found in the log tree.
1479 * eb, slot and key refer to the buffer and key found in the log tree.
1480 * root is the destination we are replaying into, and path is for temp
1481 * use by this function. (it should be released on return).
1482 */
1483static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1484 struct btrfs_root *root,
1485 struct btrfs_root *log,
1486 struct btrfs_path *path,
1487 struct extent_buffer *eb, int slot,
1488 struct btrfs_key *key)
1489{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001490 struct inode *dir = NULL;
1491 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001492 unsigned long ref_ptr;
1493 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001494 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001495 int namelen;
1496 int ret;
liuboc622ae62011-03-26 08:01:12 -04001497 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001498 int log_ref_ver = 0;
1499 u64 parent_objectid;
1500 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001501 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001502 int ref_struct_size;
1503
1504 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1505 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1506
1507 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1508 struct btrfs_inode_extref *r;
1509
1510 ref_struct_size = sizeof(struct btrfs_inode_extref);
1511 log_ref_ver = 1;
1512 r = (struct btrfs_inode_extref *)ref_ptr;
1513 parent_objectid = btrfs_inode_extref_parent(eb, r);
1514 } else {
1515 ref_struct_size = sizeof(struct btrfs_inode_ref);
1516 parent_objectid = key->offset;
1517 }
1518 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001519
Chris Masone02119d2008-09-05 16:13:11 -04001520 /*
1521 * it is possible that we didn't log all the parent directories
1522 * for a given inode. If we don't find the dir, just don't
1523 * copy the back ref in. The link count fixup code will take
1524 * care of the rest
1525 */
Mark Fashehf1863732012-08-08 11:32:27 -07001526 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001527 if (!dir) {
1528 ret = -ENOENT;
1529 goto out;
1530 }
Chris Masone02119d2008-09-05 16:13:11 -04001531
Mark Fashehf1863732012-08-08 11:32:27 -07001532 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001533 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001534 ret = -EIO;
1535 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001536 }
Chris Masone02119d2008-09-05 16:13:11 -04001537
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001538 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001539 if (log_ref_ver) {
Qu Wenruobae15d92017-11-08 08:54:26 +08001540 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1541 &ref_index, &parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001542 /*
1543 * parent object can change from one array
1544 * item to another.
1545 */
1546 if (!dir)
1547 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001548 if (!dir) {
1549 ret = -ENOENT;
1550 goto out;
1551 }
Mark Fashehf1863732012-08-08 11:32:27 -07001552 } else {
Qu Wenruobae15d92017-11-08 08:54:26 +08001553 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1554 &ref_index);
Mark Fashehf1863732012-08-08 11:32:27 -07001555 }
1556 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001557 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001558
Filipe Manana77a5b9e2021-10-01 13:52:30 +01001559 ret = inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1560 btrfs_ino(BTRFS_I(inode)), ref_index,
1561 name, namelen);
1562 if (ret < 0) {
1563 goto out;
1564 } else if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001565 /*
1566 * look for a conflicting back reference in the
1567 * metadata. if we find one we have to unlink that name
1568 * of the file before we add our new link. Later on, we
1569 * overwrite any existing back reference, and we don't
1570 * want to create dangling pointers in the directory.
1571 */
Chris Masone02119d2008-09-05 16:13:11 -04001572
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001573 if (!search_done) {
1574 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001575 BTRFS_I(dir),
David Sterbad75eefd2017-02-10 20:20:19 +01001576 BTRFS_I(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001577 inode_objectid,
1578 parent_objectid,
1579 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001580 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001581 if (ret) {
1582 if (ret == 1)
1583 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001584 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001585 }
Chris Masone02119d2008-09-05 16:13:11 -04001586 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001587
Filipe Manana0d836392018-07-20 10:59:06 +01001588 /*
1589 * If a reference item already exists for this inode
1590 * with the same parent and name, but different index,
1591 * drop it and the corresponding directory index entries
1592 * from the parent before adding the new reference item
1593 * and dir index entries, otherwise we would fail with
1594 * -EEXIST returned from btrfs_add_link() below.
1595 */
1596 ret = btrfs_inode_ref_exists(inode, dir, key->type,
1597 name, namelen);
1598 if (ret > 0) {
1599 ret = btrfs_unlink_inode(trans, root,
1600 BTRFS_I(dir),
1601 BTRFS_I(inode),
1602 name, namelen);
1603 /*
1604 * If we dropped the link count to 0, bump it so
1605 * that later the iput() on the inode will not
1606 * free it. We will fixup the link count later.
1607 */
1608 if (!ret && inode->i_nlink == 0)
1609 inc_nlink(inode);
1610 }
1611 if (ret < 0)
1612 goto out;
1613
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001614 /* insert our name */
Filipe Manana6b5fc432019-02-13 12:14:03 +00001615 ret = add_link(trans, root, dir, inode, name, namelen,
1616 ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001617 if (ret)
1618 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001619
Josef Bacikf96d4472021-05-19 11:26:25 -04001620 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
1621 if (ret)
1622 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001623 }
Filipe Manana77a5b9e2021-10-01 13:52:30 +01001624 /* Else, ret == 1, we already have a perfect match, we're done. */
liuboc622ae62011-03-26 08:01:12 -04001625
Mark Fashehf1863732012-08-08 11:32:27 -07001626 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001627 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001628 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001629 if (log_ref_ver) {
1630 iput(dir);
1631 dir = NULL;
1632 }
Chris Masone02119d2008-09-05 16:13:11 -04001633 }
Chris Masone02119d2008-09-05 16:13:11 -04001634
Filipe Manana1f250e92018-02-28 15:56:10 +00001635 /*
1636 * Before we overwrite the inode reference item in the subvolume tree
1637 * with the item from the log tree, we must unlink all names from the
1638 * parent directory that are in the subvolume's tree inode reference
1639 * item, otherwise we end up with an inconsistent subvolume tree where
1640 * dir index entries exist for a name but there is no inode reference
1641 * item with the same name.
1642 */
1643 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1644 key);
1645 if (ret)
1646 goto out;
1647
Chris Masone02119d2008-09-05 16:13:11 -04001648 /* finally write the back reference in the inode */
1649 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001650out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001651 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001652 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001653 iput(dir);
1654 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001655 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001656}
1657
Mark Fashehf1863732012-08-08 11:32:27 -07001658static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001659 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001660{
Mark Fashehf1863732012-08-08 11:32:27 -07001661 int ret = 0;
1662 int name_len;
1663 unsigned int nlink = 0;
1664 u32 item_size;
1665 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001666 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001667 u64 offset = 0;
1668 unsigned long ptr;
1669 struct btrfs_inode_extref *extref;
1670 struct extent_buffer *leaf;
1671
1672 while (1) {
1673 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1674 &extref, &offset);
1675 if (ret)
1676 break;
1677
1678 leaf = path->nodes[0];
1679 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1680 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001681 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001682
1683 while (cur_offset < item_size) {
1684 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1685 name_len = btrfs_inode_extref_name_len(leaf, extref);
1686
1687 nlink++;
1688
1689 cur_offset += name_len + sizeof(*extref);
1690 }
1691
1692 offset++;
1693 btrfs_release_path(path);
1694 }
1695 btrfs_release_path(path);
1696
Filipe Manana2c2c4522015-01-13 16:40:04 +00001697 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001698 return ret;
1699 return nlink;
1700}
1701
1702static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001703 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001704{
Chris Masone02119d2008-09-05 16:13:11 -04001705 int ret;
1706 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001707 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001708 unsigned long ptr;
1709 unsigned long ptr_end;
1710 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001711 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001712
Li Zefan33345d012011-04-20 10:31:50 +08001713 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001714 key.type = BTRFS_INODE_REF_KEY;
1715 key.offset = (u64)-1;
1716
Chris Masond3977122009-01-05 21:25:51 -05001717 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001718 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1719 if (ret < 0)
1720 break;
1721 if (ret > 0) {
1722 if (path->slots[0] == 0)
1723 break;
1724 path->slots[0]--;
1725 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001726process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001727 btrfs_item_key_to_cpu(path->nodes[0], &key,
1728 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001729 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001730 key.type != BTRFS_INODE_REF_KEY)
1731 break;
1732 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1733 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1734 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001735 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001736 struct btrfs_inode_ref *ref;
1737
1738 ref = (struct btrfs_inode_ref *)ptr;
1739 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1740 ref);
1741 ptr = (unsigned long)(ref + 1) + name_len;
1742 nlink++;
1743 }
1744
1745 if (key.offset == 0)
1746 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001747 if (path->slots[0] > 0) {
1748 path->slots[0]--;
1749 goto process_slot;
1750 }
Chris Masone02119d2008-09-05 16:13:11 -04001751 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001752 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001753 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001754 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001755
1756 return nlink;
1757}
1758
1759/*
1760 * There are a few corners where the link count of the file can't
1761 * be properly maintained during replay. So, instead of adding
1762 * lots of complexity to the log code, we just scan the backrefs
1763 * for any file that has been through replay.
1764 *
1765 * The scan will update the link count on the inode to reflect the
1766 * number of back refs found. If it goes down to zero, the iput
1767 * will free the inode.
1768 */
1769static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1770 struct btrfs_root *root,
1771 struct inode *inode)
1772{
1773 struct btrfs_path *path;
1774 int ret;
1775 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001776 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001777
1778 path = btrfs_alloc_path();
1779 if (!path)
1780 return -ENOMEM;
1781
Nikolay Borisovf329e312017-01-18 00:31:50 +02001782 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001783 if (ret < 0)
1784 goto out;
1785
1786 nlink = ret;
1787
Nikolay Borisov36283652017-01-18 00:31:49 +02001788 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001789 if (ret < 0)
1790 goto out;
1791
1792 nlink += ret;
1793
1794 ret = 0;
1795
Chris Masone02119d2008-09-05 16:13:11 -04001796 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001797 set_nlink(inode, nlink);
Josef Bacikf96d4472021-05-19 11:26:25 -04001798 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
1799 if (ret)
1800 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001801 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001802 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001803
Yan, Zhengc71bf092009-11-12 09:34:40 +00001804 if (inode->i_nlink == 0) {
1805 if (S_ISDIR(inode->i_mode)) {
1806 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001807 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001808 if (ret)
1809 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001810 }
Nikolay Borisovecdcf3c2020-10-22 18:40:46 +03001811 ret = btrfs_insert_orphan_item(trans, root, ino);
1812 if (ret == -EEXIST)
1813 ret = 0;
Chris Mason12fcfd22009-03-24 10:24:20 -04001814 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001815
Mark Fashehf1863732012-08-08 11:32:27 -07001816out:
1817 btrfs_free_path(path);
1818 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001819}
1820
1821static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1822 struct btrfs_root *root,
1823 struct btrfs_path *path)
1824{
1825 int ret;
1826 struct btrfs_key key;
1827 struct inode *inode;
1828
1829 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1830 key.type = BTRFS_ORPHAN_ITEM_KEY;
1831 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001832 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001833 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1834 if (ret < 0)
1835 break;
1836
1837 if (ret == 1) {
Josef Bacik011b28a2021-05-19 13:13:15 -04001838 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001839 if (path->slots[0] == 0)
1840 break;
1841 path->slots[0]--;
1842 }
1843
1844 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1845 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1846 key.type != BTRFS_ORPHAN_ITEM_KEY)
1847 break;
1848
1849 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001850 if (ret)
Josef Bacik011b28a2021-05-19 13:13:15 -04001851 break;
Chris Masone02119d2008-09-05 16:13:11 -04001852
David Sterbab3b4aa72011-04-21 01:20:15 +02001853 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001854 inode = read_one_inode(root, key.offset);
Josef Bacik011b28a2021-05-19 13:13:15 -04001855 if (!inode) {
1856 ret = -EIO;
1857 break;
1858 }
Chris Masone02119d2008-09-05 16:13:11 -04001859
1860 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001861 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001862 if (ret)
Josef Bacik011b28a2021-05-19 13:13:15 -04001863 break;
Chris Masone02119d2008-09-05 16:13:11 -04001864
Chris Mason12fcfd22009-03-24 10:24:20 -04001865 /*
1866 * fixup on a directory may create new entries,
1867 * make sure we always look for the highset possible
1868 * offset
1869 */
1870 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001871 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001872 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001873 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001874}
1875
1876
1877/*
1878 * record a given inode in the fixup dir so we can check its link
1879 * count when replay is done. The link count is incremented here
1880 * so the inode won't go away until we check it
1881 */
1882static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1883 struct btrfs_root *root,
1884 struct btrfs_path *path,
1885 u64 objectid)
1886{
1887 struct btrfs_key key;
1888 int ret = 0;
1889 struct inode *inode;
1890
1891 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001892 if (!inode)
1893 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001894
1895 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001896 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001897 key.offset = objectid;
1898
1899 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1900
David Sterbab3b4aa72011-04-21 01:20:15 +02001901 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001902 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001903 if (!inode->i_nlink)
1904 set_nlink(inode, 1);
1905 else
Zach Brown8b558c52013-10-16 12:10:34 -07001906 inc_nlink(inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02001907 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masone02119d2008-09-05 16:13:11 -04001908 } else if (ret == -EEXIST) {
1909 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001910 }
1911 iput(inode);
1912
1913 return ret;
1914}
1915
1916/*
1917 * when replaying the log for a directory, we only insert names
1918 * for inodes that actually exist. This means an fsync on a directory
1919 * does not implicitly fsync all the new files in it
1920 */
1921static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1922 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001923 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001924 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001925 struct btrfs_key *location)
1926{
1927 struct inode *inode;
1928 struct inode *dir;
1929 int ret;
1930
1931 inode = read_one_inode(root, location->objectid);
1932 if (!inode)
1933 return -ENOENT;
1934
1935 dir = read_one_inode(root, dirid);
1936 if (!dir) {
1937 iput(inode);
1938 return -EIO;
1939 }
Josef Bacikd5554382013-09-11 14:17:00 -04001940
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001941 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1942 name_len, 1, index);
Chris Masone02119d2008-09-05 16:13:11 -04001943
1944 /* FIXME, put inode into FIXUP list */
1945
1946 iput(inode);
1947 iput(dir);
1948 return ret;
1949}
1950
1951/*
1952 * take a single entry in a log directory item and replay it into
1953 * the subvolume.
1954 *
1955 * if a conflicting item exists in the subdirectory already,
1956 * the inode it points to is unlinked and put into the link count
1957 * fix up tree.
1958 *
1959 * If a name from the log points to a file or directory that does
1960 * not exist in the FS, it is skipped. fsyncs on directories
1961 * do not force down inodes inside that directory, just changes to the
1962 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001963 *
1964 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1965 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001966 */
1967static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1968 struct btrfs_root *root,
1969 struct btrfs_path *path,
1970 struct extent_buffer *eb,
1971 struct btrfs_dir_item *di,
1972 struct btrfs_key *key)
1973{
1974 char *name;
1975 int name_len;
1976 struct btrfs_dir_item *dst_di;
1977 struct btrfs_key found_key;
1978 struct btrfs_key log_key;
1979 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001980 u8 log_type;
Filipe Mananacfd31262021-10-01 13:48:18 +01001981 bool exists;
1982 int ret;
Josef Bacikd5554382013-09-11 14:17:00 -04001983 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001984 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001985
1986 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001987 if (!dir)
1988 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001989
1990 name_len = btrfs_dir_name_len(eb, di);
1991 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001992 if (!name) {
1993 ret = -ENOMEM;
1994 goto out;
1995 }
liubo2a29edc2011-01-26 06:22:08 +00001996
Chris Masone02119d2008-09-05 16:13:11 -04001997 log_type = btrfs_dir_type(eb, di);
1998 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1999 name_len);
2000
2001 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Filipe Mananacfd31262021-10-01 13:48:18 +01002002 ret = btrfs_lookup_inode(trans, root, path, &log_key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02002003 btrfs_release_path(path);
Filipe Mananacfd31262021-10-01 13:48:18 +01002004 if (ret < 0)
2005 goto out;
2006 exists = (ret == 0);
2007 ret = 0;
Chris Mason4bef0842008-09-08 11:18:08 -04002008
Chris Masone02119d2008-09-05 16:13:11 -04002009 if (key->type == BTRFS_DIR_ITEM_KEY) {
2010 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
2011 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05002012 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002013 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
2014 key->objectid,
2015 key->offset, name,
2016 name_len, 1);
2017 } else {
Josef Bacik36508602013-04-25 16:23:32 -04002018 /* Corruption */
2019 ret = -EINVAL;
2020 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002021 }
Filipe Mananae15ac642021-10-01 13:52:31 +01002022
Filipe Mananae15ac642021-10-01 13:52:31 +01002023 if (IS_ERR(dst_di)) {
2024 ret = PTR_ERR(dst_di);
2025 goto out;
2026 } else if (!dst_di) {
Chris Masone02119d2008-09-05 16:13:11 -04002027 /* we need a sequence number to insert, so we only
2028 * do inserts for the BTRFS_DIR_INDEX_KEY types
2029 */
2030 if (key->type != BTRFS_DIR_INDEX_KEY)
2031 goto out;
2032 goto insert;
2033 }
2034
2035 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
2036 /* the existing item matches the logged item */
2037 if (found_key.objectid == log_key.objectid &&
2038 found_key.type == log_key.type &&
2039 found_key.offset == log_key.offset &&
2040 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01002041 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04002042 goto out;
2043 }
2044
2045 /*
2046 * don't drop the conflicting directory entry if the inode
2047 * for the new entry doesn't exist
2048 */
Chris Mason4bef0842008-09-08 11:18:08 -04002049 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04002050 goto out;
2051
Nikolay Borisov207e7d92017-01-18 00:31:45 +02002052 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04002053 if (ret)
2054 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002055
2056 if (key->type == BTRFS_DIR_INDEX_KEY)
2057 goto insert;
2058out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002059 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04002060 if (!ret && update_size) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02002061 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02002062 ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
Josef Bacikd5554382013-09-11 14:17:00 -04002063 }
Chris Masone02119d2008-09-05 16:13:11 -04002064 kfree(name);
2065 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01002066 if (!ret && name_added)
2067 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04002068 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002069
2070insert:
Nikolay Borisov725af922019-08-30 17:44:49 +03002071 /*
2072 * Check if the inode reference exists in the log for the given name,
2073 * inode and parent inode
2074 */
2075 found_key.objectid = log_key.objectid;
2076 found_key.type = BTRFS_INODE_REF_KEY;
2077 found_key.offset = key->objectid;
2078 ret = backref_in_log(root->log_root, &found_key, 0, name, name_len);
2079 if (ret < 0) {
2080 goto out;
2081 } else if (ret) {
2082 /* The dentry will be added later. */
2083 ret = 0;
2084 update_size = false;
2085 goto out;
2086 }
2087
2088 found_key.objectid = log_key.objectid;
2089 found_key.type = BTRFS_INODE_EXTREF_KEY;
2090 found_key.offset = key->objectid;
2091 ret = backref_in_log(root->log_root, &found_key, key->objectid, name,
2092 name_len);
2093 if (ret < 0) {
2094 goto out;
2095 } else if (ret) {
Filipe Mananadf8d1162015-01-14 01:52:25 +00002096 /* The dentry will be added later. */
2097 ret = 0;
2098 update_size = false;
2099 goto out;
2100 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002101 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08002102 ret = insert_one_name(trans, root, key->objectid, key->offset,
2103 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00002104 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04002105 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002106 if (!ret)
2107 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04002108 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04002109 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002110 goto out;
2111}
2112
2113/*
2114 * find all the names in a directory item and reconcile them into
2115 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
2116 * one name in a directory item, but the same code gets used for
2117 * both directory index types
2118 */
2119static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
2120 struct btrfs_root *root,
2121 struct btrfs_path *path,
2122 struct extent_buffer *eb, int slot,
2123 struct btrfs_key *key)
2124{
Filipe Mananabb53eda2015-07-15 23:26:43 +01002125 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002126 u32 item_size = btrfs_item_size_nr(eb, slot);
2127 struct btrfs_dir_item *di;
2128 int name_len;
2129 unsigned long ptr;
2130 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002131 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04002132
2133 ptr = btrfs_item_ptr_offset(eb, slot);
2134 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002135 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002136 di = (struct btrfs_dir_item *)ptr;
2137 name_len = btrfs_dir_name_len(eb, di);
2138 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01002139 if (ret < 0)
2140 break;
Chris Masone02119d2008-09-05 16:13:11 -04002141 ptr = (unsigned long)(di + 1);
2142 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002143
2144 /*
2145 * If this entry refers to a non-directory (directories can not
2146 * have a link count > 1) and it was added in the transaction
2147 * that was not committed, make sure we fixup the link count of
2148 * the inode it the entry points to. Otherwise something like
2149 * the following would result in a directory pointing to an
2150 * inode with a wrong link that does not account for this dir
2151 * entry:
2152 *
2153 * mkdir testdir
2154 * touch testdir/foo
2155 * touch testdir/bar
2156 * sync
2157 *
2158 * ln testdir/bar testdir/bar_link
2159 * ln testdir/foo testdir/foo_link
2160 * xfs_io -c "fsync" testdir/bar
2161 *
2162 * <power failure>
2163 *
2164 * mount fs, log replay happens
2165 *
2166 * File foo would remain with a link count of 1 when it has two
2167 * entries pointing to it in the directory testdir. This would
2168 * make it impossible to ever delete the parent directory has
2169 * it would result in stale dentries that can never be deleted.
2170 */
2171 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
2172 struct btrfs_key di_key;
2173
2174 if (!fixup_path) {
2175 fixup_path = btrfs_alloc_path();
2176 if (!fixup_path) {
2177 ret = -ENOMEM;
2178 break;
2179 }
2180 }
2181
2182 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2183 ret = link_to_fixup_dir(trans, root, fixup_path,
2184 di_key.objectid);
2185 if (ret)
2186 break;
2187 }
2188 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002189 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01002190 btrfs_free_path(fixup_path);
2191 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002192}
2193
2194/*
2195 * directory replay has two parts. There are the standard directory
2196 * items in the log copied from the subvolume, and range items
2197 * created in the log while the subvolume was logged.
2198 *
2199 * The range items tell us which parts of the key space the log
2200 * is authoritative for. During replay, if a key in the subvolume
2201 * directory is in a logged range item, but not actually in the log
2202 * that means it was deleted from the directory before the fsync
2203 * and should be removed.
2204 */
2205static noinline int find_dir_range(struct btrfs_root *root,
2206 struct btrfs_path *path,
2207 u64 dirid, int key_type,
2208 u64 *start_ret, u64 *end_ret)
2209{
2210 struct btrfs_key key;
2211 u64 found_end;
2212 struct btrfs_dir_log_item *item;
2213 int ret;
2214 int nritems;
2215
2216 if (*start_ret == (u64)-1)
2217 return 1;
2218
2219 key.objectid = dirid;
2220 key.type = key_type;
2221 key.offset = *start_ret;
2222
2223 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2224 if (ret < 0)
2225 goto out;
2226 if (ret > 0) {
2227 if (path->slots[0] == 0)
2228 goto out;
2229 path->slots[0]--;
2230 }
2231 if (ret != 0)
2232 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2233
2234 if (key.type != key_type || key.objectid != dirid) {
2235 ret = 1;
2236 goto next;
2237 }
2238 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2239 struct btrfs_dir_log_item);
2240 found_end = btrfs_dir_log_end(path->nodes[0], item);
2241
2242 if (*start_ret >= key.offset && *start_ret <= found_end) {
2243 ret = 0;
2244 *start_ret = key.offset;
2245 *end_ret = found_end;
2246 goto out;
2247 }
2248 ret = 1;
2249next:
2250 /* check the next slot in the tree to see if it is a valid item */
2251 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08002252 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04002253 if (path->slots[0] >= nritems) {
2254 ret = btrfs_next_leaf(root, path);
2255 if (ret)
2256 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002257 }
2258
2259 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2260
2261 if (key.type != key_type || key.objectid != dirid) {
2262 ret = 1;
2263 goto out;
2264 }
2265 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2266 struct btrfs_dir_log_item);
2267 found_end = btrfs_dir_log_end(path->nodes[0], item);
2268 *start_ret = key.offset;
2269 *end_ret = found_end;
2270 ret = 0;
2271out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002272 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002273 return ret;
2274}
2275
2276/*
2277 * this looks for a given directory item in the log. If the directory
2278 * item is not in the log, the item is removed and the inode it points
2279 * to is unlinked
2280 */
2281static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2282 struct btrfs_root *root,
2283 struct btrfs_root *log,
2284 struct btrfs_path *path,
2285 struct btrfs_path *log_path,
2286 struct inode *dir,
2287 struct btrfs_key *dir_key)
2288{
2289 int ret;
2290 struct extent_buffer *eb;
2291 int slot;
2292 u32 item_size;
2293 struct btrfs_dir_item *di;
2294 struct btrfs_dir_item *log_di;
2295 int name_len;
2296 unsigned long ptr;
2297 unsigned long ptr_end;
2298 char *name;
2299 struct inode *inode;
2300 struct btrfs_key location;
2301
2302again:
2303 eb = path->nodes[0];
2304 slot = path->slots[0];
2305 item_size = btrfs_item_size_nr(eb, slot);
2306 ptr = btrfs_item_ptr_offset(eb, slot);
2307 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002308 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002309 di = (struct btrfs_dir_item *)ptr;
2310 name_len = btrfs_dir_name_len(eb, di);
2311 name = kmalloc(name_len, GFP_NOFS);
2312 if (!name) {
2313 ret = -ENOMEM;
2314 goto out;
2315 }
2316 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2317 name_len);
2318 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002319 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002320 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2321 dir_key->objectid,
2322 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002323 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002324 log_di = btrfs_lookup_dir_index_item(trans, log,
2325 log_path,
2326 dir_key->objectid,
2327 dir_key->offset,
2328 name, name_len, 0);
2329 }
Filipe Manana8dcbc262021-10-01 13:52:33 +01002330 if (!log_di) {
Chris Masone02119d2008-09-05 16:13:11 -04002331 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002332 btrfs_release_path(path);
2333 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002334 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002335 if (!inode) {
2336 kfree(name);
2337 return -EIO;
2338 }
Chris Masone02119d2008-09-05 16:13:11 -04002339
2340 ret = link_to_fixup_dir(trans, root,
2341 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002342 if (ret) {
2343 kfree(name);
2344 iput(inode);
2345 goto out;
2346 }
2347
Zach Brown8b558c52013-10-16 12:10:34 -07002348 inc_nlink(inode);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002349 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2350 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002351 if (!ret)
Nikolay Borisove5c304e62018-02-07 17:55:43 +02002352 ret = btrfs_run_delayed_items(trans);
Chris Masone02119d2008-09-05 16:13:11 -04002353 kfree(name);
2354 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002355 if (ret)
2356 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002357
2358 /* there might still be more names under this key
2359 * check and repeat if required
2360 */
2361 ret = btrfs_search_slot(NULL, root, dir_key, path,
2362 0, 0);
2363 if (ret == 0)
2364 goto again;
2365 ret = 0;
2366 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002367 } else if (IS_ERR(log_di)) {
2368 kfree(name);
2369 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002370 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002371 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002372 kfree(name);
2373
2374 ptr = (unsigned long)(di + 1);
2375 ptr += name_len;
2376 }
2377 ret = 0;
2378out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002379 btrfs_release_path(path);
2380 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002381 return ret;
2382}
2383
Filipe Manana4f764e52015-02-23 19:53:35 +00002384static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2385 struct btrfs_root *root,
2386 struct btrfs_root *log,
2387 struct btrfs_path *path,
2388 const u64 ino)
2389{
2390 struct btrfs_key search_key;
2391 struct btrfs_path *log_path;
2392 int i;
2393 int nritems;
2394 int ret;
2395
2396 log_path = btrfs_alloc_path();
2397 if (!log_path)
2398 return -ENOMEM;
2399
2400 search_key.objectid = ino;
2401 search_key.type = BTRFS_XATTR_ITEM_KEY;
2402 search_key.offset = 0;
2403again:
2404 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2405 if (ret < 0)
2406 goto out;
2407process_leaf:
2408 nritems = btrfs_header_nritems(path->nodes[0]);
2409 for (i = path->slots[0]; i < nritems; i++) {
2410 struct btrfs_key key;
2411 struct btrfs_dir_item *di;
2412 struct btrfs_dir_item *log_di;
2413 u32 total_size;
2414 u32 cur;
2415
2416 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2417 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2418 ret = 0;
2419 goto out;
2420 }
2421
2422 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2423 total_size = btrfs_item_size_nr(path->nodes[0], i);
2424 cur = 0;
2425 while (cur < total_size) {
2426 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2427 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2428 u32 this_len = sizeof(*di) + name_len + data_len;
2429 char *name;
2430
2431 name = kmalloc(name_len, GFP_NOFS);
2432 if (!name) {
2433 ret = -ENOMEM;
2434 goto out;
2435 }
2436 read_extent_buffer(path->nodes[0], name,
2437 (unsigned long)(di + 1), name_len);
2438
2439 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2440 name, name_len, 0);
2441 btrfs_release_path(log_path);
2442 if (!log_di) {
2443 /* Doesn't exist in log tree, so delete it. */
2444 btrfs_release_path(path);
2445 di = btrfs_lookup_xattr(trans, root, path, ino,
2446 name, name_len, -1);
2447 kfree(name);
2448 if (IS_ERR(di)) {
2449 ret = PTR_ERR(di);
2450 goto out;
2451 }
2452 ASSERT(di);
2453 ret = btrfs_delete_one_dir_name(trans, root,
2454 path, di);
2455 if (ret)
2456 goto out;
2457 btrfs_release_path(path);
2458 search_key = key;
2459 goto again;
2460 }
2461 kfree(name);
2462 if (IS_ERR(log_di)) {
2463 ret = PTR_ERR(log_di);
2464 goto out;
2465 }
2466 cur += this_len;
2467 di = (struct btrfs_dir_item *)((char *)di + this_len);
2468 }
2469 }
2470 ret = btrfs_next_leaf(root, path);
2471 if (ret > 0)
2472 ret = 0;
2473 else if (ret == 0)
2474 goto process_leaf;
2475out:
2476 btrfs_free_path(log_path);
2477 btrfs_release_path(path);
2478 return ret;
2479}
2480
2481
Chris Masone02119d2008-09-05 16:13:11 -04002482/*
2483 * deletion replay happens before we copy any new directory items
2484 * out of the log or out of backreferences from inodes. It
2485 * scans the log to find ranges of keys that log is authoritative for,
2486 * and then scans the directory to find items in those ranges that are
2487 * not present in the log.
2488 *
2489 * Anything we don't find in the log is unlinked and removed from the
2490 * directory.
2491 */
2492static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2493 struct btrfs_root *root,
2494 struct btrfs_root *log,
2495 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002496 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002497{
2498 u64 range_start;
2499 u64 range_end;
2500 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2501 int ret = 0;
2502 struct btrfs_key dir_key;
2503 struct btrfs_key found_key;
2504 struct btrfs_path *log_path;
2505 struct inode *dir;
2506
2507 dir_key.objectid = dirid;
2508 dir_key.type = BTRFS_DIR_ITEM_KEY;
2509 log_path = btrfs_alloc_path();
2510 if (!log_path)
2511 return -ENOMEM;
2512
2513 dir = read_one_inode(root, dirid);
2514 /* it isn't an error if the inode isn't there, that can happen
2515 * because we replay the deletes before we copy in the inode item
2516 * from the log
2517 */
2518 if (!dir) {
2519 btrfs_free_path(log_path);
2520 return 0;
2521 }
2522again:
2523 range_start = 0;
2524 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002525 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002526 if (del_all)
2527 range_end = (u64)-1;
2528 else {
2529 ret = find_dir_range(log, path, dirid, key_type,
2530 &range_start, &range_end);
2531 if (ret != 0)
2532 break;
2533 }
Chris Masone02119d2008-09-05 16:13:11 -04002534
2535 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002536 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002537 int nritems;
2538 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2539 0, 0);
2540 if (ret < 0)
2541 goto out;
2542
2543 nritems = btrfs_header_nritems(path->nodes[0]);
2544 if (path->slots[0] >= nritems) {
2545 ret = btrfs_next_leaf(root, path);
Liu Bob98def72018-04-03 01:59:48 +08002546 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002547 break;
Liu Bob98def72018-04-03 01:59:48 +08002548 else if (ret < 0)
2549 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002550 }
2551 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2552 path->slots[0]);
2553 if (found_key.objectid != dirid ||
2554 found_key.type != dir_key.type)
2555 goto next_type;
2556
2557 if (found_key.offset > range_end)
2558 break;
2559
2560 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002561 log_path, dir,
2562 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002563 if (ret)
2564 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002565 if (found_key.offset == (u64)-1)
2566 break;
2567 dir_key.offset = found_key.offset + 1;
2568 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002569 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002570 if (range_end == (u64)-1)
2571 break;
2572 range_start = range_end + 1;
2573 }
2574
2575next_type:
2576 ret = 0;
2577 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2578 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2579 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002580 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002581 goto again;
2582 }
2583out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002584 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002585 btrfs_free_path(log_path);
2586 iput(dir);
2587 return ret;
2588}
2589
2590/*
2591 * the process_func used to replay items from the log tree. This
2592 * gets called in two different stages. The first stage just looks
2593 * for inodes and makes sure they are all copied into the subvolume.
2594 *
2595 * The second stage copies all the other item types from the log into
2596 * the subvolume. The two stage approach is slower, but gets rid of
2597 * lots of complexity around inodes referencing other inodes that exist
2598 * only in the log (references come from either directory items or inode
2599 * back refs).
2600 */
2601static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +08002602 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -04002603{
2604 int nritems;
2605 struct btrfs_path *path;
2606 struct btrfs_root *root = wc->replay_dest;
2607 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002608 int i;
2609 int ret;
2610
Qu Wenruo581c1762018-03-29 09:08:11 +08002611 ret = btrfs_read_buffer(eb, gen, level, NULL);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002612 if (ret)
2613 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002614
2615 level = btrfs_header_level(eb);
2616
2617 if (level != 0)
2618 return 0;
2619
2620 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002621 if (!path)
2622 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002623
2624 nritems = btrfs_header_nritems(eb);
2625 for (i = 0; i < nritems; i++) {
2626 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002627
2628 /* inode keys are done during the first stage */
2629 if (key.type == BTRFS_INODE_ITEM_KEY &&
2630 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002631 struct btrfs_inode_item *inode_item;
2632 u32 mode;
2633
2634 inode_item = btrfs_item_ptr(eb, i,
2635 struct btrfs_inode_item);
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002636 /*
2637 * If we have a tmpfile (O_TMPFILE) that got fsync'ed
2638 * and never got linked before the fsync, skip it, as
2639 * replaying it is pointless since it would be deleted
2640 * later. We skip logging tmpfiles, but it's always
2641 * possible we are replaying a log created with a kernel
2642 * that used to log tmpfiles.
2643 */
2644 if (btrfs_inode_nlink(eb, inode_item) == 0) {
2645 wc->ignore_cur_inode = true;
2646 continue;
2647 } else {
2648 wc->ignore_cur_inode = false;
2649 }
Filipe Manana4f764e52015-02-23 19:53:35 +00002650 ret = replay_xattr_deletes(wc->trans, root, log,
2651 path, key.objectid);
2652 if (ret)
2653 break;
Chris Masone02119d2008-09-05 16:13:11 -04002654 mode = btrfs_inode_mode(eb, inode_item);
2655 if (S_ISDIR(mode)) {
2656 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002657 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002658 if (ret)
2659 break;
Chris Masone02119d2008-09-05 16:13:11 -04002660 }
2661 ret = overwrite_item(wc->trans, root, path,
2662 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002663 if (ret)
2664 break;
Chris Masone02119d2008-09-05 16:13:11 -04002665
Filipe Manana471d5572018-04-05 22:55:12 +01002666 /*
2667 * Before replaying extents, truncate the inode to its
2668 * size. We need to do it now and not after log replay
2669 * because before an fsync we can have prealloc extents
2670 * added beyond the inode's i_size. If we did it after,
2671 * through orphan cleanup for example, we would drop
2672 * those prealloc extents just after replaying them.
Chris Masone02119d2008-09-05 16:13:11 -04002673 */
2674 if (S_ISREG(mode)) {
Filipe Manana5893dfb2020-11-04 11:07:32 +00002675 struct btrfs_drop_extents_args drop_args = { 0 };
Filipe Manana471d5572018-04-05 22:55:12 +01002676 struct inode *inode;
2677 u64 from;
2678
2679 inode = read_one_inode(root, key.objectid);
2680 if (!inode) {
2681 ret = -EIO;
2682 break;
2683 }
2684 from = ALIGN(i_size_read(inode),
2685 root->fs_info->sectorsize);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002686 drop_args.start = from;
2687 drop_args.end = (u64)-1;
2688 drop_args.drop_cache = true;
2689 ret = btrfs_drop_extents(wc->trans, root,
2690 BTRFS_I(inode),
2691 &drop_args);
Filipe Manana471d5572018-04-05 22:55:12 +01002692 if (!ret) {
Filipe Manana2766ff62020-11-04 11:07:34 +00002693 inode_sub_bytes(inode,
2694 drop_args.bytes_found);
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002695 /* Update the inode's nbytes. */
Filipe Manana471d5572018-04-05 22:55:12 +01002696 ret = btrfs_update_inode(wc->trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02002697 root, BTRFS_I(inode));
Filipe Manana471d5572018-04-05 22:55:12 +01002698 }
2699 iput(inode);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002700 if (ret)
2701 break;
Chris Masone02119d2008-09-05 16:13:11 -04002702 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002703
Chris Masone02119d2008-09-05 16:13:11 -04002704 ret = link_to_fixup_dir(wc->trans, root,
2705 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002706 if (ret)
2707 break;
Chris Masone02119d2008-09-05 16:13:11 -04002708 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002709
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002710 if (wc->ignore_cur_inode)
2711 continue;
2712
Josef Bacikdd8e7212013-09-11 11:57:23 -04002713 if (key.type == BTRFS_DIR_INDEX_KEY &&
2714 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2715 ret = replay_one_dir_item(wc->trans, root, path,
2716 eb, i, &key);
2717 if (ret)
2718 break;
2719 }
2720
Chris Masone02119d2008-09-05 16:13:11 -04002721 if (wc->stage < LOG_WALK_REPLAY_ALL)
2722 continue;
2723
2724 /* these keys are simply copied */
2725 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2726 ret = overwrite_item(wc->trans, root, path,
2727 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002728 if (ret)
2729 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002730 } else if (key.type == BTRFS_INODE_REF_KEY ||
2731 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002732 ret = add_inode_ref(wc->trans, root, log, path,
2733 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002734 if (ret && ret != -ENOENT)
2735 break;
2736 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002737 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2738 ret = replay_one_extent(wc->trans, root, path,
2739 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002740 if (ret)
2741 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002742 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002743 ret = replay_one_dir_item(wc->trans, root, path,
2744 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002745 if (ret)
2746 break;
Chris Masone02119d2008-09-05 16:13:11 -04002747 }
2748 }
2749 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002750 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002751}
2752
Nikolay Borisov6787bb92020-01-20 16:09:10 +02002753/*
2754 * Correctly adjust the reserved bytes occupied by a log tree extent buffer
2755 */
2756static void unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start)
2757{
2758 struct btrfs_block_group *cache;
2759
2760 cache = btrfs_lookup_block_group(fs_info, start);
2761 if (!cache) {
2762 btrfs_err(fs_info, "unable to find block group for %llu", start);
2763 return;
2764 }
2765
2766 spin_lock(&cache->space_info->lock);
2767 spin_lock(&cache->lock);
2768 cache->reserved -= fs_info->nodesize;
2769 cache->space_info->bytes_reserved -= fs_info->nodesize;
2770 spin_unlock(&cache->lock);
2771 spin_unlock(&cache->space_info->lock);
2772
2773 btrfs_put_block_group(cache);
2774}
2775
Chris Masond3977122009-01-05 21:25:51 -05002776static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002777 struct btrfs_root *root,
2778 struct btrfs_path *path, int *level,
2779 struct walk_control *wc)
2780{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002781 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002782 u64 bytenr;
2783 u64 ptr_gen;
2784 struct extent_buffer *next;
2785 struct extent_buffer *cur;
Chris Masone02119d2008-09-05 16:13:11 -04002786 u32 blocksize;
2787 int ret = 0;
2788
Chris Masond3977122009-01-05 21:25:51 -05002789 while (*level > 0) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002790 struct btrfs_key first_key;
2791
Chris Masone02119d2008-09-05 16:13:11 -04002792 cur = path->nodes[*level];
2793
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302794 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002795
2796 if (path->slots[*level] >=
2797 btrfs_header_nritems(cur))
2798 break;
2799
2800 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2801 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08002802 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002803 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002804
Josef Bacik3fbaf252020-11-05 10:45:20 -05002805 next = btrfs_find_create_tree_block(fs_info, bytenr,
2806 btrfs_header_owner(cur),
2807 *level - 1);
Liu Boc871b0f2016-06-06 12:01:23 -07002808 if (IS_ERR(next))
2809 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002810
Chris Masone02119d2008-09-05 16:13:11 -04002811 if (*level == 1) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002812 ret = wc->process_func(root, next, wc, ptr_gen,
2813 *level - 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002814 if (ret) {
2815 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002816 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002817 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002818
Chris Masone02119d2008-09-05 16:13:11 -04002819 path->slots[*level]++;
2820 if (wc->free) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002821 ret = btrfs_read_buffer(next, ptr_gen,
2822 *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002823 if (ret) {
2824 free_extent_buffer(next);
2825 return ret;
2826 }
Chris Masone02119d2008-09-05 16:13:11 -04002827
Josef Bacik681ae502013-10-07 15:11:00 -04002828 if (trans) {
2829 btrfs_tree_lock(next);
David Sterba6a884d7d2019-03-20 14:30:02 +01002830 btrfs_clean_tree_block(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002831 btrfs_wait_tree_block_writeback(next);
2832 btrfs_tree_unlock(next);
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02002833 ret = btrfs_pin_reserved_extent(trans,
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002834 bytenr, blocksize);
2835 if (ret) {
2836 free_extent_buffer(next);
2837 return ret;
2838 }
Naohiro Aotad35751562021-02-04 19:21:54 +09002839 btrfs_redirty_list_add(
2840 trans->transaction, next);
Liu Bo18464302018-01-25 11:02:51 -07002841 } else {
2842 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2843 clear_extent_buffer_dirty(next);
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002844 unaccount_log_buffer(fs_info, bytenr);
Josef Bacik36508602013-04-25 16:23:32 -04002845 }
Chris Masone02119d2008-09-05 16:13:11 -04002846 }
2847 free_extent_buffer(next);
2848 continue;
2849 }
Qu Wenruo581c1762018-03-29 09:08:11 +08002850 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002851 if (ret) {
2852 free_extent_buffer(next);
2853 return ret;
2854 }
Chris Masone02119d2008-09-05 16:13:11 -04002855
Chris Masone02119d2008-09-05 16:13:11 -04002856 if (path->nodes[*level-1])
2857 free_extent_buffer(path->nodes[*level-1]);
2858 path->nodes[*level-1] = next;
2859 *level = btrfs_header_level(next);
2860 path->slots[*level] = 0;
2861 cond_resched();
2862 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002863 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002864
2865 cond_resched();
2866 return 0;
2867}
2868
Chris Masond3977122009-01-05 21:25:51 -05002869static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002870 struct btrfs_root *root,
2871 struct btrfs_path *path, int *level,
2872 struct walk_control *wc)
2873{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002875 int i;
2876 int slot;
2877 int ret;
2878
Chris Masond3977122009-01-05 21:25:51 -05002879 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002880 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002881 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002882 path->slots[i]++;
2883 *level = i;
2884 WARN_ON(*level == 0);
2885 return 0;
2886 } else {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002887 ret = wc->process_func(root, path->nodes[*level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002888 btrfs_header_generation(path->nodes[*level]),
2889 *level);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002890 if (ret)
2891 return ret;
2892
Chris Masone02119d2008-09-05 16:13:11 -04002893 if (wc->free) {
2894 struct extent_buffer *next;
2895
2896 next = path->nodes[*level];
2897
Josef Bacik681ae502013-10-07 15:11:00 -04002898 if (trans) {
2899 btrfs_tree_lock(next);
David Sterba6a884d7d2019-03-20 14:30:02 +01002900 btrfs_clean_tree_block(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002901 btrfs_wait_tree_block_writeback(next);
2902 btrfs_tree_unlock(next);
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02002903 ret = btrfs_pin_reserved_extent(trans,
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002904 path->nodes[*level]->start,
2905 path->nodes[*level]->len);
2906 if (ret)
2907 return ret;
Liu Bo18464302018-01-25 11:02:51 -07002908 } else {
2909 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2910 clear_extent_buffer_dirty(next);
Chris Masone02119d2008-09-05 16:13:11 -04002911
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002912 unaccount_log_buffer(fs_info,
2913 path->nodes[*level]->start);
2914 }
Chris Masone02119d2008-09-05 16:13:11 -04002915 }
2916 free_extent_buffer(path->nodes[*level]);
2917 path->nodes[*level] = NULL;
2918 *level = i + 1;
2919 }
2920 }
2921 return 1;
2922}
2923
2924/*
2925 * drop the reference count on the tree rooted at 'snap'. This traverses
2926 * the tree freeing any blocks that have a ref count of zero after being
2927 * decremented.
2928 */
2929static int walk_log_tree(struct btrfs_trans_handle *trans,
2930 struct btrfs_root *log, struct walk_control *wc)
2931{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002932 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002933 int ret = 0;
2934 int wret;
2935 int level;
2936 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002937 int orig_level;
2938
2939 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002940 if (!path)
2941 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002942
2943 level = btrfs_header_level(log->node);
2944 orig_level = level;
2945 path->nodes[level] = log->node;
David Sterba67439da2019-10-08 13:28:47 +02002946 atomic_inc(&log->node->refs);
Chris Masone02119d2008-09-05 16:13:11 -04002947 path->slots[level] = 0;
2948
Chris Masond3977122009-01-05 21:25:51 -05002949 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002950 wret = walk_down_log_tree(trans, log, path, &level, wc);
2951 if (wret > 0)
2952 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002953 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002954 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002955 goto out;
2956 }
Chris Masone02119d2008-09-05 16:13:11 -04002957
2958 wret = walk_up_log_tree(trans, log, path, &level, wc);
2959 if (wret > 0)
2960 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002961 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002962 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002963 goto out;
2964 }
Chris Masone02119d2008-09-05 16:13:11 -04002965 }
2966
2967 /* was the root node processed? if not, catch it here */
2968 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002969 ret = wc->process_func(log, path->nodes[orig_level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002970 btrfs_header_generation(path->nodes[orig_level]),
2971 orig_level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002972 if (ret)
2973 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002974 if (wc->free) {
2975 struct extent_buffer *next;
2976
2977 next = path->nodes[orig_level];
2978
Josef Bacik681ae502013-10-07 15:11:00 -04002979 if (trans) {
2980 btrfs_tree_lock(next);
David Sterba6a884d7d2019-03-20 14:30:02 +01002981 btrfs_clean_tree_block(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002982 btrfs_wait_tree_block_writeback(next);
2983 btrfs_tree_unlock(next);
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02002984 ret = btrfs_pin_reserved_extent(trans,
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002985 next->start, next->len);
2986 if (ret)
2987 goto out;
Liu Bo18464302018-01-25 11:02:51 -07002988 } else {
2989 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2990 clear_extent_buffer_dirty(next);
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002991 unaccount_log_buffer(fs_info, next->start);
Josef Bacik681ae502013-10-07 15:11:00 -04002992 }
Chris Masone02119d2008-09-05 16:13:11 -04002993 }
2994 }
2995
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002996out:
Chris Masone02119d2008-09-05 16:13:11 -04002997 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002998 return ret;
2999}
3000
Yan Zheng7237f182009-01-21 12:54:03 -05003001/*
3002 * helper function to update the item for a given subvolumes log root
3003 * in the tree of log roots
3004 */
3005static int update_log_root(struct btrfs_trans_handle *trans,
Josef Bacik4203e962019-09-30 16:27:25 -04003006 struct btrfs_root *log,
3007 struct btrfs_root_item *root_item)
Yan Zheng7237f182009-01-21 12:54:03 -05003008{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003009 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05003010 int ret;
3011
3012 if (log->log_transid == 1) {
3013 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003014 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Josef Bacik4203e962019-09-30 16:27:25 -04003015 &log->root_key, root_item);
Yan Zheng7237f182009-01-21 12:54:03 -05003016 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003017 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Josef Bacik4203e962019-09-30 16:27:25 -04003018 &log->root_key, root_item);
Yan Zheng7237f182009-01-21 12:54:03 -05003019 }
3020 return ret;
3021}
3022
Zhaolei60d53eb2015-08-17 18:44:46 +08003023static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04003024{
3025 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05003026 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04003027
Yan Zheng7237f182009-01-21 12:54:03 -05003028 /*
3029 * we only allow two pending log transactions at a time,
3030 * so we know that if ours is more than 2 older than the
3031 * current transaction, we're done
3032 */
Liu Bo49e83f52017-09-01 16:14:30 -06003033 for (;;) {
Yan Zheng7237f182009-01-21 12:54:03 -05003034 prepare_to_wait(&root->log_commit_wait[index],
3035 &wait, TASK_UNINTERRUPTIBLE);
Liu Bo49e83f52017-09-01 16:14:30 -06003036
3037 if (!(root->log_transid_committed < transid &&
3038 atomic_read(&root->log_commit[index])))
3039 break;
3040
Yan Zheng7237f182009-01-21 12:54:03 -05003041 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06003042 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05003043 mutex_lock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06003044 }
3045 finish_wait(&root->log_commit_wait[index], &wait);
Yan Zheng7237f182009-01-21 12:54:03 -05003046}
3047
Zhaolei60d53eb2015-08-17 18:44:46 +08003048static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05003049{
3050 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08003051
Liu Bo49e83f52017-09-01 16:14:30 -06003052 for (;;) {
3053 prepare_to_wait(&root->log_writer_wait, &wait,
3054 TASK_UNINTERRUPTIBLE);
3055 if (!atomic_read(&root->log_writers))
3056 break;
3057
Yan Zheng7237f182009-01-21 12:54:03 -05003058 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06003059 schedule();
Filipe Manana575849e2015-02-11 11:12:39 +00003060 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05003061 }
Liu Bo49e83f52017-09-01 16:14:30 -06003062 finish_wait(&root->log_writer_wait, &wait);
Chris Masone02119d2008-09-05 16:13:11 -04003063}
3064
Miao Xie8b050d32014-02-20 18:08:58 +08003065static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
3066 struct btrfs_log_ctx *ctx)
3067{
Miao Xie8b050d32014-02-20 18:08:58 +08003068 mutex_lock(&root->log_mutex);
3069 list_del_init(&ctx->list);
3070 mutex_unlock(&root->log_mutex);
3071}
3072
3073/*
3074 * Invoked in log mutex context, or be sure there is no other task which
3075 * can access the list.
3076 */
3077static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
3078 int index, int error)
3079{
3080 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07003081 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08003082
Chris Mason570dd452016-10-27 10:42:20 -07003083 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
3084 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08003085 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07003086 }
Miao Xie8b050d32014-02-20 18:08:58 +08003087}
3088
Chris Masone02119d2008-09-05 16:13:11 -04003089/*
3090 * btrfs_sync_log does sends a given tree log down to the disk and
3091 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04003092 * you know that any inodes previously logged are safely on disk only
3093 * if it returns 0.
3094 *
3095 * Any other return value means you need to call btrfs_commit_transaction.
3096 * Some of the edge cases for fsyncing directories that have had unlinks
3097 * or renames done in the past mean that sometimes the only safe
3098 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
3099 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04003100 */
3101int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08003102 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003103{
Yan Zheng7237f182009-01-21 12:54:03 -05003104 int index1;
3105 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003106 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04003107 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003108 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003109 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003110 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Josef Bacik4203e962019-09-30 16:27:25 -04003111 struct btrfs_root_item new_root_item;
Miao Xiebb14a592014-02-20 18:08:56 +08003112 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08003113 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00003114 struct blk_plug plug;
Filipe Manana47876f72020-11-25 12:19:28 +00003115 u64 log_root_start;
3116 u64 log_root_level;
Chris Masone02119d2008-09-05 16:13:11 -04003117
Yan Zheng7237f182009-01-21 12:54:03 -05003118 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08003119 log_transid = ctx->log_transid;
3120 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05003121 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003122 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04003123 }
Miao Xied1433de2014-02-20 18:08:59 +08003124
3125 index1 = log_transid % 2;
3126 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003127 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08003128 mutex_unlock(&root->log_mutex);
3129 return ctx->log_ret;
3130 }
3131 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003132 atomic_set(&root->log_commit[index1], 1);
3133
3134 /* wait for previous tree log sync to complete */
3135 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08003136 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08003137
Yan, Zheng86df7eb2009-10-14 09:24:59 -04003138 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06003139 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04003140 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003141 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08003142 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04003143 mutex_unlock(&root->log_mutex);
3144 schedule_timeout_uninterruptible(1);
3145 mutex_lock(&root->log_mutex);
3146 }
Zhaolei60d53eb2015-08-17 18:44:46 +08003147 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06003148 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04003149 break;
3150 }
Chris Masond0c803c2008-09-11 16:17:57 -04003151
Chris Mason12fcfd22009-03-24 10:24:20 -04003152 /* bail out if we need to do a full commit */
David Sterba4884b8e2019-03-20 13:25:34 +01003153 if (btrfs_need_log_full_commit(trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003154 ret = -EAGAIN;
3155 mutex_unlock(&root->log_mutex);
3156 goto out;
3157 }
3158
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003159 if (log_transid % 2 == 0)
3160 mark = EXTENT_DIRTY;
3161 else
3162 mark = EXTENT_NEW;
3163
Chris Mason690587d2009-10-13 13:29:19 -04003164 /* we start IO on all the marked extents here, but we don't actually
3165 * wait for them until later.
3166 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00003167 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003168 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Naohiro Aotab528f462021-02-05 23:58:36 +09003169 /*
3170 * -EAGAIN happens when someone, e.g., a concurrent transaction
3171 * commit, writes a dirty extent in this tree-log commit. This
3172 * concurrent write will create a hole writing out the extents,
3173 * and we cannot proceed on a zoned filesystem, requiring
3174 * sequential writing. While we can bail out to a full commit
3175 * here, but we can continue hoping the concurrent writing fills
3176 * the hole.
3177 */
3178 if (ret == -EAGAIN && btrfs_is_zoned(fs_info))
3179 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003180 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003181 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04003182 btrfs_abort_transaction(trans, ret);
David Sterba90787762019-03-20 13:28:05 +01003183 btrfs_set_log_full_commit(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003184 mutex_unlock(&root->log_mutex);
3185 goto out;
3186 }
Yan Zheng7237f182009-01-21 12:54:03 -05003187
Josef Bacik4203e962019-09-30 16:27:25 -04003188 /*
3189 * We _must_ update under the root->log_mutex in order to make sure we
3190 * have a consistent view of the log root we are trying to commit at
3191 * this moment.
3192 *
3193 * We _must_ copy this into a local copy, because we are not holding the
3194 * log_root_tree->log_mutex yet. This is important because when we
3195 * commit the log_root_tree we must have a consistent view of the
3196 * log_root_tree when we update the super block to point at the
3197 * log_root_tree bytenr. If we update the log_root_tree here we'll race
3198 * with the commit and possibly point at the new block which we may not
3199 * have written out.
3200 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003201 btrfs_set_root_node(&log->root_item, log->node);
Josef Bacik4203e962019-09-30 16:27:25 -04003202 memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
Yan Zheng7237f182009-01-21 12:54:03 -05003203
Yan Zheng7237f182009-01-21 12:54:03 -05003204 root->log_transid++;
3205 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04003206 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05003207 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003208 * IO has been started, blocks of the log tree have WRITTEN flag set
3209 * in their headers. new modifications of the log will be written to
3210 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05003211 */
3212 mutex_unlock(&root->log_mutex);
3213
Naohiro Aota3ddebf22021-02-04 19:22:20 +09003214 if (btrfs_is_zoned(fs_info)) {
Naohiro Aotae75f9fd2021-03-24 23:23:11 +09003215 mutex_lock(&fs_info->tree_root->log_mutex);
Naohiro Aota3ddebf22021-02-04 19:22:20 +09003216 if (!log_root_tree->node) {
3217 ret = btrfs_alloc_log_tree_node(trans, log_root_tree);
3218 if (ret) {
Filipe Mananaea32af42021-07-07 12:23:45 +01003219 mutex_unlock(&fs_info->tree_root->log_mutex);
Naohiro Aota3ddebf22021-02-04 19:22:20 +09003220 goto out;
3221 }
3222 }
Naohiro Aotae75f9fd2021-03-24 23:23:11 +09003223 mutex_unlock(&fs_info->tree_root->log_mutex);
Naohiro Aota3ddebf22021-02-04 19:22:20 +09003224 }
3225
Naohiro Aotae75f9fd2021-03-24 23:23:11 +09003226 btrfs_init_log_ctx(&root_log_ctx, NULL);
3227
3228 mutex_lock(&log_root_tree->log_mutex);
3229
Filipe Mananae3d3b412021-03-11 15:13:30 +00003230 index2 = log_root_tree->log_transid % 2;
3231 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3232 root_log_ctx.log_transid = log_root_tree->log_transid;
3233
Josef Bacik4203e962019-09-30 16:27:25 -04003234 /*
3235 * Now we are safe to update the log_root_tree because we're under the
3236 * log_mutex, and we're a current writer so we're holding the commit
3237 * open until we drop the log_mutex.
3238 */
3239 ret = update_log_root(trans, log, &new_root_item);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003240 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08003241 if (!list_empty(&root_log_ctx.list))
3242 list_del_init(&root_log_ctx.list);
3243
Miao Xiec6adc9c2013-05-28 10:05:39 +00003244 blk_finish_plug(&plug);
David Sterba90787762019-03-20 13:28:05 +01003245 btrfs_set_log_full_commit(trans);
Miao Xie995946d2014-04-02 19:51:06 +08003246
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003247 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003248 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003249 mutex_unlock(&log_root_tree->log_mutex);
3250 goto out;
3251 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003252 btrfs_wait_tree_log_extents(log, mark);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003253 mutex_unlock(&log_root_tree->log_mutex);
3254 ret = -EAGAIN;
3255 goto out;
3256 }
3257
Miao Xied1433de2014-02-20 18:08:59 +08003258 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08003259 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07003260 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08003261 mutex_unlock(&log_root_tree->log_mutex);
3262 ret = root_log_ctx.log_ret;
3263 goto out;
3264 }
Miao Xie8b050d32014-02-20 18:08:58 +08003265
Miao Xied1433de2014-02-20 18:08:59 +08003266 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05003267 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003268 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003269 ret = btrfs_wait_tree_log_extents(log, mark);
Zhaolei60d53eb2015-08-17 18:44:46 +08003270 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003271 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003272 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003273 if (!ret)
3274 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05003275 goto out;
3276 }
Miao Xied1433de2014-02-20 18:08:59 +08003277 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003278 atomic_set(&log_root_tree->log_commit[index2], 1);
3279
Chris Mason12fcfd22009-03-24 10:24:20 -04003280 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003281 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003282 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04003283 }
Yan Zheng7237f182009-01-21 12:54:03 -05003284
Chris Mason12fcfd22009-03-24 10:24:20 -04003285 /*
3286 * now that we've moved on to the tree of log tree roots,
3287 * check the full commit flag again
3288 */
David Sterba4884b8e2019-03-20 13:25:34 +01003289 if (btrfs_need_log_full_commit(trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003290 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003291 btrfs_wait_tree_log_extents(log, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04003292 mutex_unlock(&log_root_tree->log_mutex);
3293 ret = -EAGAIN;
3294 goto out_wake_log_root;
3295 }
Yan Zheng7237f182009-01-21 12:54:03 -05003296
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003297 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00003298 &log_root_tree->dirty_log_pages,
3299 EXTENT_DIRTY | EXTENT_NEW);
3300 blk_finish_plug(&plug);
Naohiro Aotab528f462021-02-05 23:58:36 +09003301 /*
3302 * As described above, -EAGAIN indicates a hole in the extents. We
3303 * cannot wait for these write outs since the waiting cause a
3304 * deadlock. Bail out to the full commit instead.
3305 */
3306 if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) {
3307 btrfs_set_log_full_commit(trans);
3308 btrfs_wait_tree_log_extents(log, mark);
3309 mutex_unlock(&log_root_tree->log_mutex);
3310 goto out_wake_log_root;
3311 } else if (ret) {
David Sterba90787762019-03-20 13:28:05 +01003312 btrfs_set_log_full_commit(trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003313 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003314 mutex_unlock(&log_root_tree->log_mutex);
3315 goto out_wake_log_root;
3316 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003317 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003318 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003319 ret = btrfs_wait_tree_log_extents(log_root_tree,
3320 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003321 if (ret) {
David Sterba90787762019-03-20 13:28:05 +01003322 btrfs_set_log_full_commit(trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003323 mutex_unlock(&log_root_tree->log_mutex);
3324 goto out_wake_log_root;
3325 }
Chris Masone02119d2008-09-05 16:13:11 -04003326
Filipe Manana47876f72020-11-25 12:19:28 +00003327 log_root_start = log_root_tree->node->start;
3328 log_root_level = btrfs_header_level(log_root_tree->node);
Yan Zheng7237f182009-01-21 12:54:03 -05003329 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05003330 mutex_unlock(&log_root_tree->log_mutex);
3331
3332 /*
Filipe Manana47876f72020-11-25 12:19:28 +00003333 * Here we are guaranteed that nobody is going to write the superblock
3334 * for the current transaction before us and that neither we do write
3335 * our superblock before the previous transaction finishes its commit
3336 * and writes its superblock, because:
3337 *
3338 * 1) We are holding a handle on the current transaction, so no body
3339 * can commit it until we release the handle;
3340 *
3341 * 2) Before writing our superblock we acquire the tree_log_mutex, so
3342 * if the previous transaction is still committing, and hasn't yet
3343 * written its superblock, we wait for it to do it, because a
3344 * transaction commit acquires the tree_log_mutex when the commit
3345 * begins and releases it only after writing its superblock.
Yan Zheng7237f182009-01-21 12:54:03 -05003346 */
Filipe Manana47876f72020-11-25 12:19:28 +00003347 mutex_lock(&fs_info->tree_log_mutex);
Josef Bacik165ea852021-05-19 17:15:53 -04003348
3349 /*
3350 * The previous transaction writeout phase could have failed, and thus
3351 * marked the fs in an error state. We must not commit here, as we
3352 * could have updated our generation in the super_for_commit and
3353 * writing the super here would result in transid mismatches. If there
3354 * is an error here just bail.
3355 */
Josef Bacik84961532021-10-05 16:35:25 -04003356 if (BTRFS_FS_ERROR(fs_info)) {
Josef Bacik165ea852021-05-19 17:15:53 -04003357 ret = -EIO;
3358 btrfs_set_log_full_commit(trans);
3359 btrfs_abort_transaction(trans, ret);
3360 mutex_unlock(&fs_info->tree_log_mutex);
3361 goto out_wake_log_root;
3362 }
3363
Filipe Manana47876f72020-11-25 12:19:28 +00003364 btrfs_set_super_log_root(fs_info->super_for_commit, log_root_start);
3365 btrfs_set_super_log_root_level(fs_info->super_for_commit, log_root_level);
David Sterbaeece6a92017-02-10 19:04:32 +01003366 ret = write_all_supers(fs_info, 1);
Filipe Manana47876f72020-11-25 12:19:28 +00003367 mutex_unlock(&fs_info->tree_log_mutex);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003368 if (ret) {
David Sterba90787762019-03-20 13:28:05 +01003369 btrfs_set_log_full_commit(trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003370 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003371 goto out_wake_log_root;
3372 }
Yan Zheng7237f182009-01-21 12:54:03 -05003373
Filipe Mananae1a6d262021-07-20 16:03:41 +01003374 /*
3375 * We know there can only be one task here, since we have not yet set
3376 * root->log_commit[index1] to 0 and any task attempting to sync the
3377 * log must wait for the previous log transaction to commit if it's
3378 * still in progress or wait for the current log transaction commit if
3379 * someone else already started it. We use <= and not < because the
3380 * first log transaction has an ID of 0.
3381 */
3382 ASSERT(root->last_log_commit <= log_transid);
3383 root->last_log_commit = log_transid;
Chris Mason257c62e2009-10-13 13:21:08 -04003384
Chris Mason12fcfd22009-03-24 10:24:20 -04003385out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07003386 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003387 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3388
Miao Xied1433de2014-02-20 18:08:59 +08003389 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003390 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003391 mutex_unlock(&log_root_tree->log_mutex);
3392
David Sterba33a9eca2015-10-10 18:35:10 +02003393 /*
David Sterba093258e2018-02-26 16:15:17 +01003394 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3395 * all the updates above are seen by the woken threads. It might not be
3396 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003397 */
David Sterba093258e2018-02-26 16:15:17 +01003398 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04003399out:
Miao Xied1433de2014-02-20 18:08:59 +08003400 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07003401 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08003402 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003403 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003404 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003405
David Sterba33a9eca2015-10-10 18:35:10 +02003406 /*
David Sterba093258e2018-02-26 16:15:17 +01003407 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3408 * all the updates above are seen by the woken threads. It might not be
3409 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003410 */
David Sterba093258e2018-02-26 16:15:17 +01003411 cond_wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003412 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003413}
3414
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003415static void free_log_tree(struct btrfs_trans_handle *trans,
3416 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003417{
3418 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04003419 struct walk_control wc = {
3420 .free = 1,
3421 .process_func = process_one_buffer
3422 };
3423
Naohiro Aota3ddebf22021-02-04 19:22:20 +09003424 if (log->node) {
3425 ret = walk_log_tree(trans, log, &wc);
3426 if (ret) {
3427 if (trans)
3428 btrfs_abort_transaction(trans, ret);
3429 else
3430 btrfs_handle_fs_error(log->fs_info, ret, NULL);
3431 }
Jeff Mahoney374b0e22018-09-06 16:59:33 -04003432 }
Chris Masone02119d2008-09-05 16:13:11 -04003433
Filipe Manana59b07132018-11-09 10:43:08 +00003434 clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
3435 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Filipe Mananae289f032020-05-18 12:14:50 +01003436 extent_io_tree_release(&log->log_csum_range);
Naohiro Aotad35751562021-02-04 19:21:54 +09003437
3438 if (trans && log->node)
3439 btrfs_redirty_list_add(trans->transaction, log->node);
Josef Bacik00246522020-01-24 09:33:01 -05003440 btrfs_put_root(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003441}
3442
3443/*
3444 * free all the extents used by the tree log. This should be called
3445 * at commit time of the full transaction
3446 */
3447int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3448{
3449 if (root->log_root) {
3450 free_log_tree(trans, root->log_root);
3451 root->log_root = NULL;
Filipe Mananae7a79812020-06-15 10:38:44 +01003452 clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003453 }
3454 return 0;
3455}
3456
3457int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3458 struct btrfs_fs_info *fs_info)
3459{
3460 if (fs_info->log_root_tree) {
3461 free_log_tree(trans, fs_info->log_root_tree);
3462 fs_info->log_root_tree = NULL;
Filipe Manana47876f72020-11-25 12:19:28 +00003463 clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &fs_info->tree_root->state);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003464 }
Chris Masone02119d2008-09-05 16:13:11 -04003465 return 0;
3466}
3467
3468/*
Filipe Manana6e8e7772021-07-27 11:24:44 +01003469 * Check if an inode was logged in the current transaction. This may often
3470 * return some false positives, because logged_trans is an in memory only field,
3471 * not persisted anywhere. This is meant to be used in contexts where a false
3472 * positive has no functional consequences.
Filipe Manana803f0f62019-06-19 13:05:39 +01003473 */
3474static bool inode_logged(struct btrfs_trans_handle *trans,
3475 struct btrfs_inode *inode)
3476{
3477 if (inode->logged_trans == trans->transid)
3478 return true;
3479
Filipe Manana1e0860f2021-08-31 15:30:31 +01003480 if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state))
3481 return false;
3482
Filipe Manana6e8e7772021-07-27 11:24:44 +01003483 /*
3484 * The inode's logged_trans is always 0 when we load it (because it is
3485 * not persisted in the inode item or elsewhere). So if it is 0, the
Filipe Mananad135a532021-07-29 15:29:01 +01003486 * inode was last modified in the current transaction then the inode may
3487 * have been logged before in the current transaction, then evicted and
3488 * loaded again in the current transaction - or may have never been logged
3489 * in the current transaction, but since we can not be sure, we have to
3490 * assume it was, otherwise our callers can leave an inconsistent log.
Filipe Manana6e8e7772021-07-27 11:24:44 +01003491 */
3492 if (inode->logged_trans == 0 &&
3493 inode->last_trans == trans->transid &&
Filipe Manana803f0f62019-06-19 13:05:39 +01003494 !test_bit(BTRFS_FS_LOG_RECOVERING, &trans->fs_info->flags))
3495 return true;
3496
3497 return false;
3498}
3499
3500/*
Chris Masone02119d2008-09-05 16:13:11 -04003501 * If both a file and directory are logged, and unlinks or renames are
3502 * mixed in, we have a few interesting corners:
3503 *
3504 * create file X in dir Y
3505 * link file X to X.link in dir Y
3506 * fsync file X
3507 * unlink file X but leave X.link
3508 * fsync dir Y
3509 *
3510 * After a crash we would expect only X.link to exist. But file X
3511 * didn't get fsync'd again so the log has back refs for X and X.link.
3512 *
3513 * We solve this by removing directory entries and inode backrefs from the
3514 * log when a file that was logged in the current transaction is
3515 * unlinked. Any later fsync will include the updated log entries, and
3516 * we'll be able to reconstruct the proper directory items from backrefs.
3517 *
3518 * This optimizations allows us to avoid relogging the entire inode
3519 * or the entire directory.
3520 */
Josef Bacik9a35fc92021-10-05 16:35:24 -04003521void btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3522 struct btrfs_root *root,
3523 const char *name, int name_len,
3524 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003525{
3526 struct btrfs_root *log;
3527 struct btrfs_dir_item *di;
3528 struct btrfs_path *path;
3529 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003530 int err = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003531 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003532
Filipe Manana803f0f62019-06-19 13:05:39 +01003533 if (!inode_logged(trans, dir))
Josef Bacik9a35fc92021-10-05 16:35:24 -04003534 return;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003535
Chris Masone02119d2008-09-05 16:13:11 -04003536 ret = join_running_log_trans(root);
3537 if (ret)
Josef Bacik9a35fc92021-10-05 16:35:24 -04003538 return;
Chris Masone02119d2008-09-05 16:13:11 -04003539
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003540 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003541
3542 log = root->log_root;
3543 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003544 if (!path) {
3545 err = -ENOMEM;
3546 goto out_unlock;
3547 }
liubo2a29edc2011-01-26 06:22:08 +00003548
Li Zefan33345d012011-04-20 10:31:50 +08003549 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003550 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003551 if (IS_ERR(di)) {
3552 err = PTR_ERR(di);
3553 goto fail;
3554 }
3555 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003556 ret = btrfs_delete_one_dir_name(trans, log, path, di);
Josef Bacik36508602013-04-25 16:23:32 -04003557 if (ret) {
3558 err = ret;
3559 goto fail;
3560 }
Chris Masone02119d2008-09-05 16:13:11 -04003561 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003562 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003563 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003564 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003565 if (IS_ERR(di)) {
3566 err = PTR_ERR(di);
3567 goto fail;
3568 }
3569 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003570 ret = btrfs_delete_one_dir_name(trans, log, path, di);
Josef Bacik36508602013-04-25 16:23:32 -04003571 if (ret) {
3572 err = ret;
3573 goto fail;
3574 }
Chris Masone02119d2008-09-05 16:13:11 -04003575 }
3576
Filipe Mananaddffcf62021-01-27 10:34:54 +00003577 /*
3578 * We do not need to update the size field of the directory's inode item
3579 * because on log replay we update the field to reflect all existing
3580 * entries in the directory (see overwrite_item()).
Chris Masone02119d2008-09-05 16:13:11 -04003581 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003582fail:
Chris Masone02119d2008-09-05 16:13:11 -04003583 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003584out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003585 mutex_unlock(&dir->log_mutex);
Josef Bacik9a35fc92021-10-05 16:35:24 -04003586 if (err < 0)
David Sterba90787762019-03-20 13:28:05 +01003587 btrfs_set_log_full_commit(trans);
Chris Mason12fcfd22009-03-24 10:24:20 -04003588 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003589}
3590
3591/* see comments for btrfs_del_dir_entries_in_log */
Josef Bacik9a35fc92021-10-05 16:35:24 -04003592void btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3593 struct btrfs_root *root,
3594 const char *name, int name_len,
3595 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003596{
3597 struct btrfs_root *log;
3598 u64 index;
3599 int ret;
3600
Filipe Manana803f0f62019-06-19 13:05:39 +01003601 if (!inode_logged(trans, inode))
Josef Bacik9a35fc92021-10-05 16:35:24 -04003602 return;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003603
Chris Masone02119d2008-09-05 16:13:11 -04003604 ret = join_running_log_trans(root);
3605 if (ret)
Josef Bacik9a35fc92021-10-05 16:35:24 -04003606 return;
Chris Masone02119d2008-09-05 16:13:11 -04003607 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003608 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003609
Nikolay Borisova491abb2017-01-18 00:31:33 +02003610 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003611 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003612 mutex_unlock(&inode->log_mutex);
Josef Bacik9a35fc92021-10-05 16:35:24 -04003613 if (ret < 0 && ret != -ENOENT)
David Sterba90787762019-03-20 13:28:05 +01003614 btrfs_set_log_full_commit(trans);
Chris Mason12fcfd22009-03-24 10:24:20 -04003615 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003616}
3617
3618/*
3619 * creates a range item in the log for 'dirid'. first_offset and
3620 * last_offset tell us which parts of the key space the log should
3621 * be considered authoritative for.
3622 */
3623static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3624 struct btrfs_root *log,
3625 struct btrfs_path *path,
3626 int key_type, u64 dirid,
3627 u64 first_offset, u64 last_offset)
3628{
3629 int ret;
3630 struct btrfs_key key;
3631 struct btrfs_dir_log_item *item;
3632
3633 key.objectid = dirid;
3634 key.offset = first_offset;
3635 if (key_type == BTRFS_DIR_ITEM_KEY)
3636 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3637 else
3638 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3639 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003640 if (ret)
3641 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003642
3643 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3644 struct btrfs_dir_log_item);
3645 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3646 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003647 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003648 return 0;
3649}
3650
Filipe Manana086dcbf2021-09-16 11:32:13 +01003651static int flush_dir_items_batch(struct btrfs_trans_handle *trans,
3652 struct btrfs_root *log,
3653 struct extent_buffer *src,
3654 struct btrfs_path *dst_path,
3655 int start_slot,
3656 int count)
3657{
3658 char *ins_data = NULL;
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003659 struct btrfs_item_batch batch;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003660 struct extent_buffer *dst;
Filipe Mananada1b8112021-09-24 12:28:15 +01003661 unsigned long src_offset;
3662 unsigned long dst_offset;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003663 struct btrfs_key key;
3664 u32 item_size;
3665 int ret;
3666 int i;
3667
3668 ASSERT(count > 0);
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003669 batch.nr = count;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003670
3671 if (count == 1) {
3672 btrfs_item_key_to_cpu(src, &key, start_slot);
3673 item_size = btrfs_item_size_nr(src, start_slot);
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003674 batch.keys = &key;
3675 batch.data_sizes = &item_size;
3676 batch.total_data_size = item_size;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003677 } else {
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003678 struct btrfs_key *ins_keys;
3679 u32 *ins_sizes;
3680
Filipe Manana086dcbf2021-09-16 11:32:13 +01003681 ins_data = kmalloc(count * sizeof(u32) +
3682 count * sizeof(struct btrfs_key), GFP_NOFS);
3683 if (!ins_data)
3684 return -ENOMEM;
3685
3686 ins_sizes = (u32 *)ins_data;
3687 ins_keys = (struct btrfs_key *)(ins_data + count * sizeof(u32));
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003688 batch.keys = ins_keys;
3689 batch.data_sizes = ins_sizes;
3690 batch.total_data_size = 0;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003691
3692 for (i = 0; i < count; i++) {
3693 const int slot = start_slot + i;
3694
3695 btrfs_item_key_to_cpu(src, &ins_keys[i], slot);
3696 ins_sizes[i] = btrfs_item_size_nr(src, slot);
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003697 batch.total_data_size += ins_sizes[i];
Filipe Manana086dcbf2021-09-16 11:32:13 +01003698 }
3699 }
3700
Filipe Mananab7ef5f32021-09-24 12:28:13 +01003701 ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
Filipe Manana086dcbf2021-09-16 11:32:13 +01003702 if (ret)
3703 goto out;
3704
3705 dst = dst_path->nodes[0];
Filipe Mananada1b8112021-09-24 12:28:15 +01003706 /*
3707 * Copy all the items in bulk, in a single copy operation. Item data is
3708 * organized such that it's placed at the end of a leaf and from right
3709 * to left. For example, the data for the second item ends at an offset
3710 * that matches the offset where the data for the first item starts, the
3711 * data for the third item ends at an offset that matches the offset
3712 * where the data of the second items starts, and so on.
3713 * Therefore our source and destination start offsets for copy match the
3714 * offsets of the last items (highest slots).
3715 */
3716 dst_offset = btrfs_item_ptr_offset(dst, dst_path->slots[0] + count - 1);
3717 src_offset = btrfs_item_ptr_offset(src, start_slot + count - 1);
3718 copy_extent_buffer(dst, src, dst_offset, src_offset, batch.total_data_size);
Filipe Manana086dcbf2021-09-16 11:32:13 +01003719 btrfs_release_path(dst_path);
3720out:
3721 kfree(ins_data);
3722
3723 return ret;
3724}
3725
Filipe Mananaeb10d852021-09-16 11:32:12 +01003726static int process_dir_items_leaf(struct btrfs_trans_handle *trans,
3727 struct btrfs_inode *inode,
3728 struct btrfs_path *path,
3729 struct btrfs_path *dst_path,
3730 int key_type,
3731 struct btrfs_log_ctx *ctx)
3732{
3733 struct btrfs_root *log = inode->root->log_root;
3734 struct extent_buffer *src = path->nodes[0];
3735 const int nritems = btrfs_header_nritems(src);
3736 const u64 ino = btrfs_ino(inode);
Filipe Manana086dcbf2021-09-16 11:32:13 +01003737 const bool inode_logged_before = inode_logged(trans, inode);
Filipe Mananadc287222021-09-16 11:32:14 +01003738 u64 last_logged_key_offset;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003739 bool last_found = false;
3740 int batch_start = 0;
3741 int batch_size = 0;
Filipe Mananaeb10d852021-09-16 11:32:12 +01003742 int i;
3743
Filipe Mananadc287222021-09-16 11:32:14 +01003744 if (key_type == BTRFS_DIR_ITEM_KEY)
3745 last_logged_key_offset = inode->last_dir_item_offset;
3746 else
3747 last_logged_key_offset = inode->last_dir_index_offset;
3748
Filipe Mananaeb10d852021-09-16 11:32:12 +01003749 for (i = path->slots[0]; i < nritems; i++) {
3750 struct btrfs_key key;
Filipe Mananaeb10d852021-09-16 11:32:12 +01003751 int ret;
3752
3753 btrfs_item_key_to_cpu(src, &key, i);
3754
Filipe Manana086dcbf2021-09-16 11:32:13 +01003755 if (key.objectid != ino || key.type != key_type) {
3756 last_found = true;
3757 break;
3758 }
Filipe Mananaeb10d852021-09-16 11:32:12 +01003759
Filipe Mananadc287222021-09-16 11:32:14 +01003760 ctx->last_dir_item_offset = key.offset;
Filipe Mananaeb10d852021-09-16 11:32:12 +01003761 /*
3762 * We must make sure that when we log a directory entry, the
3763 * corresponding inode, after log replay, has a matching link
3764 * count. For example:
3765 *
3766 * touch foo
3767 * mkdir mydir
3768 * sync
3769 * ln foo mydir/bar
3770 * xfs_io -c "fsync" mydir
3771 * <crash>
3772 * <mount fs and log replay>
3773 *
3774 * Would result in a fsync log that when replayed, our file inode
3775 * would have a link count of 1, but we get two directory entries
3776 * pointing to the same inode. After removing one of the names,
3777 * it would not be possible to remove the other name, which
3778 * resulted always in stale file handle errors, and would not be
3779 * possible to rmdir the parent directory, since its i_size could
3780 * never be decremented to the value BTRFS_EMPTY_DIR_SIZE,
3781 * resulting in -ENOTEMPTY errors.
3782 */
Filipe Manana086dcbf2021-09-16 11:32:13 +01003783 if (!ctx->log_new_dentries) {
3784 struct btrfs_dir_item *di;
3785 struct btrfs_key di_key;
3786
3787 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3788 btrfs_dir_item_key_to_cpu(src, di, &di_key);
3789 if ((btrfs_dir_transid(src, di) == trans->transid ||
3790 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3791 di_key.type != BTRFS_ROOT_ITEM_KEY)
3792 ctx->log_new_dentries = true;
3793 }
3794
3795 if (!inode_logged_before)
3796 goto add_to_batch;
Filipe Mananadc287222021-09-16 11:32:14 +01003797
3798 /*
3799 * If we were logged before and have logged dir items, we can skip
3800 * checking if any item with a key offset larger than the last one
3801 * we logged is in the log tree, saving time and avoiding adding
3802 * contention on the log tree.
3803 */
3804 if (key.offset > last_logged_key_offset)
3805 goto add_to_batch;
Filipe Manana086dcbf2021-09-16 11:32:13 +01003806 /*
3807 * Check if the key was already logged before. If not we can add
3808 * it to a batch for bulk insertion.
3809 */
3810 ret = btrfs_search_slot(NULL, log, &key, dst_path, 0, 0);
3811 if (ret < 0) {
3812 return ret;
3813 } else if (ret > 0) {
3814 btrfs_release_path(dst_path);
3815 goto add_to_batch;
3816 }
3817
3818 /*
3819 * Item exists in the log. Overwrite the item in the log if it
3820 * has different content or do nothing if it has exactly the same
3821 * content. And then flush the current batch if any - do it after
3822 * overwriting the current item, or we would deadlock otherwise,
3823 * since we are holding a path for the existing item.
3824 */
3825 ret = do_overwrite_item(trans, log, dst_path, src, i, &key);
3826 if (ret < 0)
3827 return ret;
3828
3829 if (batch_size > 0) {
3830 ret = flush_dir_items_batch(trans, log, src, dst_path,
3831 batch_start, batch_size);
3832 if (ret < 0)
3833 return ret;
3834 batch_size = 0;
3835 }
3836 continue;
3837add_to_batch:
3838 if (batch_size == 0)
3839 batch_start = i;
3840 batch_size++;
Filipe Mananaeb10d852021-09-16 11:32:12 +01003841 }
3842
Filipe Manana086dcbf2021-09-16 11:32:13 +01003843 if (batch_size > 0) {
3844 int ret;
3845
3846 ret = flush_dir_items_batch(trans, log, src, dst_path,
3847 batch_start, batch_size);
3848 if (ret < 0)
3849 return ret;
3850 }
3851
3852 return last_found ? 1 : 0;
Filipe Mananaeb10d852021-09-16 11:32:12 +01003853}
3854
Chris Masone02119d2008-09-05 16:13:11 -04003855/*
3856 * log all the items included in the current transaction for a given
3857 * directory. This also creates the range items in the log tree required
3858 * to replay anything deleted before the fsync
3859 */
3860static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Filipe Manana90d04512021-09-16 11:32:10 +01003861 struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003862 struct btrfs_path *path,
3863 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003864 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003865 u64 min_offset, u64 *last_offset_ret)
3866{
3867 struct btrfs_key min_key;
Filipe Manana90d04512021-09-16 11:32:10 +01003868 struct btrfs_root *root = inode->root;
Chris Masone02119d2008-09-05 16:13:11 -04003869 struct btrfs_root *log = root->log_root;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003870 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003871 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04003872 u64 first_offset = min_offset;
3873 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003874 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003875
Li Zefan33345d012011-04-20 10:31:50 +08003876 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003877 min_key.type = key_type;
3878 min_key.offset = min_offset;
3879
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003880 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003881
3882 /*
3883 * we didn't find anything from this transaction, see if there
3884 * is anything at all
3885 */
Li Zefan33345d012011-04-20 10:31:50 +08003886 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3887 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003888 min_key.type = key_type;
3889 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003890 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003891 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3892 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003893 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003894 return ret;
3895 }
Li Zefan33345d012011-04-20 10:31:50 +08003896 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003897
3898 /* if ret == 0 there are items for this type,
3899 * create a range to tell us the last key of this type.
3900 * otherwise, there are no items in this directory after
3901 * *min_offset, and we create a range to indicate that.
3902 */
3903 if (ret == 0) {
3904 struct btrfs_key tmp;
3905 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3906 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003907 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003908 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003909 }
3910 goto done;
3911 }
3912
3913 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003914 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003915 if (ret == 0) {
3916 struct btrfs_key tmp;
3917 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3918 if (key_type == tmp.type) {
3919 first_offset = tmp.offset;
3920 ret = overwrite_item(trans, log, dst_path,
3921 path->nodes[0], path->slots[0],
3922 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003923 if (ret) {
3924 err = ret;
3925 goto done;
3926 }
Chris Masone02119d2008-09-05 16:13:11 -04003927 }
3928 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003929 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003930
Josef Bacik2cc83342019-03-06 17:13:04 -05003931 /*
3932 * Find the first key from this transaction again. See the note for
3933 * log_new_dir_dentries, if we're logging a directory recursively we
3934 * won't be holding its i_mutex, which means we can modify the directory
3935 * while we're logging it. If we remove an entry between our first
3936 * search and this search we'll not find the key again and can just
3937 * bail.
3938 */
Filipe Mananabb56f022020-09-14 15:27:50 +01003939search:
Chris Masone02119d2008-09-05 16:13:11 -04003940 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Josef Bacik2cc83342019-03-06 17:13:04 -05003941 if (ret != 0)
Chris Masone02119d2008-09-05 16:13:11 -04003942 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003943
3944 /*
3945 * we have a block from this transaction, log every item in it
3946 * from our directory
3947 */
Chris Masond3977122009-01-05 21:25:51 -05003948 while (1) {
Filipe Mananaeb10d852021-09-16 11:32:12 +01003949 ret = process_dir_items_leaf(trans, inode, path, dst_path,
3950 key_type, ctx);
3951 if (ret != 0) {
3952 if (ret < 0)
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003953 err = ret;
Filipe Mananaeb10d852021-09-16 11:32:12 +01003954 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003955 }
Filipe Mananaeb10d852021-09-16 11:32:12 +01003956 path->slots[0] = btrfs_header_nritems(path->nodes[0]);
Chris Masone02119d2008-09-05 16:13:11 -04003957
3958 /*
3959 * look ahead to the next item and see if it is also
3960 * from this directory and from this transaction
3961 */
3962 ret = btrfs_next_leaf(root, path);
Liu Bo80c0b422018-04-03 01:59:47 +08003963 if (ret) {
3964 if (ret == 1)
3965 last_offset = (u64)-1;
3966 else
3967 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003968 goto done;
3969 }
Filipe Mananaeb10d852021-09-16 11:32:12 +01003970 btrfs_item_key_to_cpu(path->nodes[0], &min_key, path->slots[0]);
3971 if (min_key.objectid != ino || min_key.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003972 last_offset = (u64)-1;
3973 goto done;
3974 }
3975 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3976 ret = overwrite_item(trans, log, dst_path,
3977 path->nodes[0], path->slots[0],
Filipe Mananaeb10d852021-09-16 11:32:12 +01003978 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003979 if (ret)
3980 err = ret;
3981 else
Filipe Mananaeb10d852021-09-16 11:32:12 +01003982 last_offset = min_key.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003983 goto done;
3984 }
Filipe Mananaeb10d852021-09-16 11:32:12 +01003985 if (need_resched()) {
3986 btrfs_release_path(path);
3987 cond_resched();
3988 goto search;
3989 }
Chris Masone02119d2008-09-05 16:13:11 -04003990 }
3991done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003992 btrfs_release_path(path);
3993 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003994
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003995 if (err == 0) {
3996 *last_offset_ret = last_offset;
3997 /*
3998 * insert the log range keys to indicate where the log
3999 * is valid
4000 */
4001 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08004002 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004003 if (ret)
4004 err = ret;
4005 }
4006 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004007}
4008
4009/*
4010 * logging directories is very similar to logging inodes, We find all the items
4011 * from the current transaction and write them to the log.
4012 *
4013 * The recovery code scans the directory in the subvolume, and if it finds a
4014 * key in the range logged that is not present in the log tree, then it means
4015 * that dir entry was unlinked during the transaction.
4016 *
4017 * In order for that scan to work, we must include one key smaller than
4018 * the smallest logged by this transaction and one key larger than the largest
4019 * key logged by this transaction.
4020 */
4021static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Filipe Manana90d04512021-09-16 11:32:10 +01004022 struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04004023 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004024 struct btrfs_path *dst_path,
4025 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004026{
4027 u64 min_key;
4028 u64 max_key;
4029 int ret;
4030 int key_type = BTRFS_DIR_ITEM_KEY;
4031
Filipe Mananadc287222021-09-16 11:32:14 +01004032 /*
4033 * If this is the first time we are being logged in the current
4034 * transaction, or we were logged before but the inode was evicted and
4035 * reloaded later, in which case its logged_trans is 0, reset the values
4036 * of the last logged key offsets. Note that we don't use the helper
4037 * function inode_logged() here - that is because the function returns
4038 * true after an inode eviction, assuming the worst case as it can not
4039 * know for sure if the inode was logged before. So we can not skip key
4040 * searches in the case the inode was evicted, because it may not have
4041 * been logged in this transaction and may have been logged in a past
4042 * transaction, so we need to reset the last dir item and index offsets
4043 * to (u64)-1.
4044 */
4045 if (inode->logged_trans != trans->transid) {
4046 inode->last_dir_item_offset = (u64)-1;
4047 inode->last_dir_index_offset = (u64)-1;
4048 }
Chris Masone02119d2008-09-05 16:13:11 -04004049again:
4050 min_key = 0;
4051 max_key = 0;
Filipe Mananadc287222021-09-16 11:32:14 +01004052 if (key_type == BTRFS_DIR_ITEM_KEY)
4053 ctx->last_dir_item_offset = inode->last_dir_item_offset;
4054 else
4055 ctx->last_dir_item_offset = inode->last_dir_index_offset;
4056
Chris Masond3977122009-01-05 21:25:51 -05004057 while (1) {
Filipe Manana90d04512021-09-16 11:32:10 +01004058 ret = log_dir_items(trans, inode, path, dst_path, key_type,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02004059 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004060 if (ret)
4061 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004062 if (max_key == (u64)-1)
4063 break;
4064 min_key = max_key + 1;
4065 }
4066
4067 if (key_type == BTRFS_DIR_ITEM_KEY) {
Filipe Mananadc287222021-09-16 11:32:14 +01004068 inode->last_dir_item_offset = ctx->last_dir_item_offset;
Chris Masone02119d2008-09-05 16:13:11 -04004069 key_type = BTRFS_DIR_INDEX_KEY;
4070 goto again;
Filipe Mananadc287222021-09-16 11:32:14 +01004071 } else {
4072 inode->last_dir_index_offset = ctx->last_dir_item_offset;
Chris Masone02119d2008-09-05 16:13:11 -04004073 }
4074 return 0;
4075}
4076
4077/*
4078 * a helper function to drop items from the log before we relog an
4079 * inode. max_key_type indicates the highest item type to remove.
4080 * This cannot be run for file data extents because it does not
4081 * free the extents they point to.
4082 */
Filipe Manana88e221c2021-08-31 15:30:35 +01004083static int drop_inode_items(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04004084 struct btrfs_root *log,
4085 struct btrfs_path *path,
Filipe Manana88e221c2021-08-31 15:30:35 +01004086 struct btrfs_inode *inode,
4087 int max_key_type)
Chris Masone02119d2008-09-05 16:13:11 -04004088{
4089 int ret;
4090 struct btrfs_key key;
4091 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04004092 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004093
Filipe Manana88e221c2021-08-31 15:30:35 +01004094 if (!inode_logged(trans, inode))
4095 return 0;
4096
4097 key.objectid = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04004098 key.type = max_key_type;
4099 key.offset = (u64)-1;
4100
Chris Masond3977122009-01-05 21:25:51 -05004101 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004102 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04004103 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004104 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04004105 break;
4106
4107 if (path->slots[0] == 0)
4108 break;
4109
4110 path->slots[0]--;
4111 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4112 path->slots[0]);
4113
Filipe Manana88e221c2021-08-31 15:30:35 +01004114 if (found_key.objectid != key.objectid)
Chris Masone02119d2008-09-05 16:13:11 -04004115 break;
4116
Josef Bacik18ec90d2012-09-28 11:56:28 -04004117 found_key.offset = 0;
4118 found_key.type = 0;
Qu Wenruoe3b83362020-04-17 15:08:21 +08004119 ret = btrfs_bin_search(path->nodes[0], &found_key, &start_slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00004120 if (ret < 0)
4121 break;
Josef Bacik18ec90d2012-09-28 11:56:28 -04004122
4123 ret = btrfs_del_items(trans, log, path, start_slot,
4124 path->slots[0] - start_slot + 1);
4125 /*
4126 * If start slot isn't 0 then we don't need to re-search, we've
4127 * found the last guy with the objectid in this tree.
4128 */
4129 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00004130 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02004131 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004132 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004133 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04004134 if (ret > 0)
4135 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004136 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004137}
4138
Filipe Manana8a2b3da2021-08-31 15:30:36 +01004139static int truncate_inode_items(struct btrfs_trans_handle *trans,
4140 struct btrfs_root *log_root,
4141 struct btrfs_inode *inode,
4142 u64 new_size, u32 min_type)
4143{
4144 int ret;
4145
4146 do {
4147 ret = btrfs_truncate_inode_items(trans, log_root, inode,
4148 new_size, min_type, NULL);
4149 } while (ret == -EAGAIN);
4150
4151 return ret;
4152}
4153
Josef Bacik94edf4a2012-09-25 14:56:25 -04004154static void fill_inode_item(struct btrfs_trans_handle *trans,
4155 struct extent_buffer *leaf,
4156 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004157 struct inode *inode, int log_inode_only,
4158 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004159{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04004160 struct btrfs_map_token token;
Boris Burkov77eea052021-06-30 13:01:48 -07004161 u64 flags;
Josef Bacik94edf4a2012-09-25 14:56:25 -04004162
David Sterbac82f8232019-08-09 17:48:21 +02004163 btrfs_init_map_token(&token, leaf);
Josef Bacik94edf4a2012-09-25 14:56:25 -04004164
4165 if (log_inode_only) {
4166 /* set the generation to zero so the recover code
4167 * can tell the difference between an logging
4168 * just to say 'this inode exists' and a logging
4169 * to say 'update this inode with these values'
4170 */
David Sterbacc4c13d2020-04-29 02:15:56 +02004171 btrfs_set_token_inode_generation(&token, item, 0);
4172 btrfs_set_token_inode_size(&token, item, logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04004173 } else {
David Sterbacc4c13d2020-04-29 02:15:56 +02004174 btrfs_set_token_inode_generation(&token, item,
4175 BTRFS_I(inode)->generation);
4176 btrfs_set_token_inode_size(&token, item, inode->i_size);
Josef Bacik94edf4a2012-09-25 14:56:25 -04004177 }
4178
David Sterbacc4c13d2020-04-29 02:15:56 +02004179 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
4180 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
4181 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
4182 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04004183
David Sterbacc4c13d2020-04-29 02:15:56 +02004184 btrfs_set_token_timespec_sec(&token, &item->atime,
4185 inode->i_atime.tv_sec);
4186 btrfs_set_token_timespec_nsec(&token, &item->atime,
4187 inode->i_atime.tv_nsec);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04004188
David Sterbacc4c13d2020-04-29 02:15:56 +02004189 btrfs_set_token_timespec_sec(&token, &item->mtime,
4190 inode->i_mtime.tv_sec);
4191 btrfs_set_token_timespec_nsec(&token, &item->mtime,
4192 inode->i_mtime.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->ctime,
4195 inode->i_ctime.tv_sec);
4196 btrfs_set_token_timespec_nsec(&token, &item->ctime,
4197 inode->i_ctime.tv_nsec);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04004198
Filipe Mananae593e542021-01-27 10:34:55 +00004199 /*
4200 * We do not need to set the nbytes field, in fact during a fast fsync
4201 * its value may not even be correct, since a fast fsync does not wait
4202 * for ordered extent completion, which is where we update nbytes, it
4203 * only waits for writeback to complete. During log replay as we find
4204 * file extent items and replay them, we adjust the nbytes field of the
4205 * inode item in subvolume tree as needed (see overwrite_item()).
4206 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04004207
David Sterbacc4c13d2020-04-29 02:15:56 +02004208 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
4209 btrfs_set_token_inode_transid(&token, item, trans->transid);
4210 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
Boris Burkov77eea052021-06-30 13:01:48 -07004211 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
4212 BTRFS_I(inode)->ro_flags);
4213 btrfs_set_token_inode_flags(&token, item, flags);
David Sterbacc4c13d2020-04-29 02:15:56 +02004214 btrfs_set_token_inode_block_group(&token, item, 0);
Josef Bacik94edf4a2012-09-25 14:56:25 -04004215}
4216
Josef Bacika95249b2012-10-11 16:17:34 -04004217static int log_inode_item(struct btrfs_trans_handle *trans,
4218 struct btrfs_root *log, struct btrfs_path *path,
Filipe Manana2ac691d2021-07-20 16:03:43 +01004219 struct btrfs_inode *inode, bool inode_item_dropped)
Josef Bacika95249b2012-10-11 16:17:34 -04004220{
4221 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04004222 int ret;
4223
Filipe Manana2ac691d2021-07-20 16:03:43 +01004224 /*
4225 * If we are doing a fast fsync and the inode was logged before in the
4226 * current transaction, then we know the inode was previously logged and
4227 * it exists in the log tree. For performance reasons, in this case use
4228 * btrfs_search_slot() directly with ins_len set to 0 so that we never
4229 * attempt a write lock on the leaf's parent, which adds unnecessary lock
4230 * contention in case there are concurrent fsyncs for other inodes of the
4231 * same subvolume. Using btrfs_insert_empty_item() when the inode item
4232 * already exists can also result in unnecessarily splitting a leaf.
4233 */
4234 if (!inode_item_dropped && inode->logged_trans == trans->transid) {
4235 ret = btrfs_search_slot(trans, log, &inode->location, path, 0, 1);
4236 ASSERT(ret <= 0);
4237 if (ret > 0)
4238 ret = -ENOENT;
4239 } else {
4240 /*
4241 * This means it is the first fsync in the current transaction,
4242 * so the inode item is not in the log and we need to insert it.
4243 * We can never get -EEXIST because we are only called for a fast
4244 * fsync and in case an inode eviction happens after the inode was
4245 * logged before in the current transaction, when we load again
4246 * the inode, we set BTRFS_INODE_NEEDS_FULL_SYNC on its runtime
4247 * flags and set ->logged_trans to 0.
4248 */
4249 ret = btrfs_insert_empty_item(trans, log, path, &inode->location,
4250 sizeof(*inode_item));
4251 ASSERT(ret != -EEXIST);
4252 }
4253 if (ret)
Josef Bacika95249b2012-10-11 16:17:34 -04004254 return ret;
4255 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4256 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02004257 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
4258 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04004259 btrfs_release_path(path);
4260 return 0;
4261}
4262
Filipe Manana40e046a2019-12-05 16:58:30 +00004263static int log_csums(struct btrfs_trans_handle *trans,
Filipe Manana3ebac172020-07-15 12:30:43 +01004264 struct btrfs_inode *inode,
Filipe Manana40e046a2019-12-05 16:58:30 +00004265 struct btrfs_root *log_root,
4266 struct btrfs_ordered_sum *sums)
4267{
Filipe Mananae289f032020-05-18 12:14:50 +01004268 const u64 lock_end = sums->bytenr + sums->len - 1;
4269 struct extent_state *cached_state = NULL;
Filipe Manana40e046a2019-12-05 16:58:30 +00004270 int ret;
4271
4272 /*
Filipe Manana3ebac172020-07-15 12:30:43 +01004273 * If this inode was not used for reflink operations in the current
4274 * transaction with new extents, then do the fast path, no need to
4275 * worry about logging checksum items with overlapping ranges.
4276 */
4277 if (inode->last_reflink_trans < trans->transid)
4278 return btrfs_csum_file_blocks(trans, log_root, sums);
4279
4280 /*
Filipe Mananae289f032020-05-18 12:14:50 +01004281 * Serialize logging for checksums. This is to avoid racing with the
4282 * same checksum being logged by another task that is logging another
4283 * file which happens to refer to the same extent as well. Such races
4284 * can leave checksum items in the log with overlapping ranges.
4285 */
4286 ret = lock_extent_bits(&log_root->log_csum_range, sums->bytenr,
4287 lock_end, &cached_state);
4288 if (ret)
4289 return ret;
4290 /*
Filipe Manana40e046a2019-12-05 16:58:30 +00004291 * Due to extent cloning, we might have logged a csum item that covers a
4292 * subrange of a cloned extent, and later we can end up logging a csum
4293 * item for a larger subrange of the same extent or the entire range.
4294 * This would leave csum items in the log tree that cover the same range
4295 * and break the searches for checksums in the log tree, resulting in
4296 * some checksums missing in the fs/subvolume tree. So just delete (or
4297 * trim and adjust) any existing csum items in the log for this range.
4298 */
4299 ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len);
Filipe Mananae289f032020-05-18 12:14:50 +01004300 if (!ret)
4301 ret = btrfs_csum_file_blocks(trans, log_root, sums);
Filipe Manana40e046a2019-12-05 16:58:30 +00004302
Filipe Mananae289f032020-05-18 12:14:50 +01004303 unlock_extent_cached(&log_root->log_csum_range, sums->bytenr, lock_end,
4304 &cached_state);
4305
4306 return ret;
Filipe Manana40e046a2019-12-05 16:58:30 +00004307}
4308
Chris Mason31ff1cd2008-09-11 16:17:57 -04004309static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004310 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04004311 struct btrfs_path *dst_path,
Filipe Manana0e563152019-11-19 12:07:33 +00004312 struct btrfs_path *src_path,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004313 int start_slot, int nr, int inode_only,
4314 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04004315{
David Sterba3ffbd682018-06-29 10:56:42 +02004316 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004317 unsigned long src_offset;
4318 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004319 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004320 struct btrfs_file_extent_item *extent;
4321 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04004322 struct extent_buffer *src = src_path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004323 int ret;
4324 struct btrfs_key *ins_keys;
4325 u32 *ins_sizes;
Filipe Mananab7ef5f32021-09-24 12:28:13 +01004326 struct btrfs_item_batch batch;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004327 char *ins_data;
4328 int i;
Chris Masond20f7042008-12-08 16:58:54 -05004329 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004330 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Chris Masond20f7042008-12-08 16:58:54 -05004331
4332 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04004333
4334 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
4335 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00004336 if (!ins_data)
4337 return -ENOMEM;
4338
Chris Mason31ff1cd2008-09-11 16:17:57 -04004339 ins_sizes = (u32 *)ins_data;
4340 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
Filipe Mananab7ef5f32021-09-24 12:28:13 +01004341 batch.keys = ins_keys;
4342 batch.data_sizes = ins_sizes;
4343 batch.total_data_size = 0;
4344 batch.nr = nr;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004345
4346 for (i = 0; i < nr; i++) {
4347 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
Filipe Mananab7ef5f32021-09-24 12:28:13 +01004348 batch.total_data_size += ins_sizes[i];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004349 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
4350 }
Filipe Mananab7ef5f32021-09-24 12:28:13 +01004351 ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004352 if (ret) {
4353 kfree(ins_data);
4354 return ret;
4355 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004356
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004357 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004358 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
4359 dst_path->slots[0]);
4360
4361 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
4362
Josef Bacik94edf4a2012-09-25 14:56:25 -04004363 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004364 inode_item = btrfs_item_ptr(dst_path->nodes[0],
4365 dst_path->slots[0],
4366 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04004367 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01004368 &inode->vfs_inode,
4369 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004370 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04004371 } else {
4372 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
4373 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04004374 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04004375
Chris Mason31ff1cd2008-09-11 16:17:57 -04004376 /* take a reference on file data extents so that truncates
4377 * or deletes of this inode don't have to relog the inode
4378 * again
4379 */
David Sterba962a2982014-06-04 18:41:45 +02004380 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06004381 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004382 int found_type;
4383 extent = btrfs_item_ptr(src, start_slot + i,
4384 struct btrfs_file_extent_item);
4385
liubo8e531cd2011-05-06 10:36:09 +08004386 if (btrfs_file_extent_generation(src, extent) < trans->transid)
4387 continue;
4388
Chris Mason31ff1cd2008-09-11 16:17:57 -04004389 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04004390 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004391 u64 ds, dl, cs, cl;
4392 ds = btrfs_file_extent_disk_bytenr(src,
4393 extent);
4394 /* ds == 0 is a hole */
4395 if (ds == 0)
4396 continue;
4397
4398 dl = btrfs_file_extent_disk_num_bytes(src,
4399 extent);
4400 cs = btrfs_file_extent_offset(src, extent);
4401 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07004402 extent);
Chris Mason580afd72008-12-08 19:15:39 -05004403 if (btrfs_file_extent_compression(src,
4404 extent)) {
4405 cs = 0;
4406 cl = dl;
4407 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004408
4409 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004410 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004411 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01004412 &ordered_sums, 0);
Filipe Manana4f264332020-07-29 10:17:50 +01004413 if (ret)
4414 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004415 }
4416 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004417 }
4418
4419 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004420 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04004421 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05004422
4423 /*
4424 * we have to do this after the loop above to avoid changing the
4425 * log tree while trying to change the log tree.
4426 */
Chris Masond3977122009-01-05 21:25:51 -05004427 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05004428 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4429 struct btrfs_ordered_sum,
4430 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004431 if (!ret)
Filipe Manana3ebac172020-07-15 12:30:43 +01004432 ret = log_csums(trans, inode, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05004433 list_del(&sums->list);
4434 kfree(sums);
4435 }
Josef Bacik16e75492013-10-22 12:18:51 -04004436
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004437 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004438}
4439
Sami Tolvanen4f0f5862021-04-08 11:28:34 -07004440static int extent_cmp(void *priv, const struct list_head *a,
4441 const struct list_head *b)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004442{
David Sterba214cc182021-07-26 14:15:26 +02004443 const struct extent_map *em1, *em2;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004444
4445 em1 = list_entry(a, struct extent_map, list);
4446 em2 = list_entry(b, struct extent_map, list);
4447
4448 if (em1->start < em2->start)
4449 return -1;
4450 else if (em1->start > em2->start)
4451 return 1;
4452 return 0;
4453}
4454
Josef Bacike7175a62018-05-23 11:58:34 -04004455static int log_extent_csums(struct btrfs_trans_handle *trans,
4456 struct btrfs_inode *inode,
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004457 struct btrfs_root *log_root,
Filipe Manana48778172020-08-11 12:43:58 +01004458 const struct extent_map *em,
4459 struct btrfs_log_ctx *ctx)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004460{
Filipe Manana48778172020-08-11 12:43:58 +01004461 struct btrfs_ordered_extent *ordered;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004462 u64 csum_offset;
4463 u64 csum_len;
Filipe Manana48778172020-08-11 12:43:58 +01004464 u64 mod_start = em->mod_start;
4465 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004466 LIST_HEAD(ordered_sums);
4467 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004468
Josef Bacike7175a62018-05-23 11:58:34 -04004469 if (inode->flags & BTRFS_INODE_NODATASUM ||
4470 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
Filipe Manana8407f552014-09-05 15:14:39 +01004471 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04004472 return 0;
4473
Filipe Manana48778172020-08-11 12:43:58 +01004474 list_for_each_entry(ordered, &ctx->ordered_extents, log_list) {
4475 const u64 ordered_end = ordered->file_offset + ordered->num_bytes;
4476 const u64 mod_end = mod_start + mod_len;
4477 struct btrfs_ordered_sum *sums;
4478
4479 if (mod_len == 0)
4480 break;
4481
4482 if (ordered_end <= mod_start)
4483 continue;
4484 if (mod_end <= ordered->file_offset)
4485 break;
4486
4487 /*
4488 * We are going to copy all the csums on this ordered extent, so
4489 * go ahead and adjust mod_start and mod_len in case this ordered
4490 * extent has already been logged.
4491 */
4492 if (ordered->file_offset > mod_start) {
4493 if (ordered_end >= mod_end)
4494 mod_len = ordered->file_offset - mod_start;
4495 /*
4496 * If we have this case
4497 *
4498 * |--------- logged extent ---------|
4499 * |----- ordered extent ----|
4500 *
4501 * Just don't mess with mod_start and mod_len, we'll
4502 * just end up logging more csums than we need and it
4503 * will be ok.
4504 */
4505 } else {
4506 if (ordered_end < mod_end) {
4507 mod_len = mod_end - ordered_end;
4508 mod_start = ordered_end;
4509 } else {
4510 mod_len = 0;
4511 }
4512 }
4513
4514 /*
4515 * To keep us from looping for the above case of an ordered
4516 * extent that falls inside of the logged extent.
4517 */
4518 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags))
4519 continue;
4520
4521 list_for_each_entry(sums, &ordered->list, list) {
4522 ret = log_csums(trans, inode, log_root, sums);
4523 if (ret)
4524 return ret;
4525 }
4526 }
4527
4528 /* We're done, found all csums in the ordered extents. */
4529 if (mod_len == 0)
4530 return 0;
4531
Josef Bacike7175a62018-05-23 11:58:34 -04004532 /* If we're compressed we have to save the entire range of csums. */
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004533 if (em->compress_type) {
4534 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004535 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004536 } else {
Filipe Manana48778172020-08-11 12:43:58 +01004537 csum_offset = mod_start - em->start;
4538 csum_len = mod_len;
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004539 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004540
Josef Bacik70c8a912012-10-11 16:54:30 -04004541 /* block start is already adjusted for the file extent offset. */
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004542 ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004543 em->block_start + csum_offset,
4544 em->block_start + csum_offset +
4545 csum_len - 1, &ordered_sums, 0);
4546 if (ret)
4547 return ret;
4548
4549 while (!list_empty(&ordered_sums)) {
4550 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4551 struct btrfs_ordered_sum,
4552 list);
4553 if (!ret)
Filipe Manana3ebac172020-07-15 12:30:43 +01004554 ret = log_csums(trans, inode, log_root, sums);
Josef Bacik70c8a912012-10-11 16:54:30 -04004555 list_del(&sums->list);
4556 kfree(sums);
4557 }
4558
4559 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004560}
4561
Filipe Manana8407f552014-09-05 15:14:39 +01004562static int log_one_extent(struct btrfs_trans_handle *trans,
Filipe Manana90d04512021-09-16 11:32:10 +01004563 struct btrfs_inode *inode,
Filipe Manana8407f552014-09-05 15:14:39 +01004564 const struct extent_map *em,
4565 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004566 struct btrfs_log_ctx *ctx)
4567{
Filipe Manana5893dfb2020-11-04 11:07:32 +00004568 struct btrfs_drop_extents_args drop_args = { 0 };
Filipe Manana90d04512021-09-16 11:32:10 +01004569 struct btrfs_root *log = inode->root->log_root;
Filipe Manana8407f552014-09-05 15:14:39 +01004570 struct btrfs_file_extent_item *fi;
4571 struct extent_buffer *leaf;
4572 struct btrfs_map_token token;
4573 struct btrfs_key key;
4574 u64 extent_offset = em->start - em->orig_start;
4575 u64 block_len;
4576 int ret;
Filipe Manana8407f552014-09-05 15:14:39 +01004577
Filipe Manana48778172020-08-11 12:43:58 +01004578 ret = log_extent_csums(trans, inode, log, em, ctx);
Filipe Manana8407f552014-09-05 15:14:39 +01004579 if (ret)
4580 return ret;
4581
Filipe Manana5328b2a2021-08-31 15:30:39 +01004582 /*
4583 * If this is the first time we are logging the inode in the current
4584 * transaction, we can avoid btrfs_drop_extents(), which is expensive
4585 * because it does a deletion search, which always acquires write locks
4586 * for extent buffers at levels 2, 1 and 0. This not only wastes time
4587 * but also adds significant contention in a log tree, since log trees
4588 * are small, with a root at level 2 or 3 at most, due to their short
4589 * life span.
4590 */
4591 if (inode_logged(trans, inode)) {
4592 drop_args.path = path;
4593 drop_args.start = em->start;
4594 drop_args.end = em->start + em->len;
4595 drop_args.replace_extent = true;
4596 drop_args.extent_item_size = sizeof(*fi);
4597 ret = btrfs_drop_extents(trans, log, inode, &drop_args);
4598 if (ret)
4599 return ret;
4600 }
Filipe Manana8407f552014-09-05 15:14:39 +01004601
Filipe Manana5893dfb2020-11-04 11:07:32 +00004602 if (!drop_args.extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004603 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004604 key.type = BTRFS_EXTENT_DATA_KEY;
4605 key.offset = em->start;
4606
4607 ret = btrfs_insert_empty_item(trans, log, path, &key,
4608 sizeof(*fi));
4609 if (ret)
4610 return ret;
4611 }
4612 leaf = path->nodes[0];
David Sterbac82f8232019-08-09 17:48:21 +02004613 btrfs_init_map_token(&token, leaf);
Filipe Manana8407f552014-09-05 15:14:39 +01004614 fi = btrfs_item_ptr(leaf, path->slots[0],
4615 struct btrfs_file_extent_item);
4616
David Sterbacc4c13d2020-04-29 02:15:56 +02004617 btrfs_set_token_file_extent_generation(&token, fi, trans->transid);
Filipe Manana8407f552014-09-05 15:14:39 +01004618 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
David Sterbacc4c13d2020-04-29 02:15:56 +02004619 btrfs_set_token_file_extent_type(&token, fi,
4620 BTRFS_FILE_EXTENT_PREALLOC);
Filipe Manana8407f552014-09-05 15:14:39 +01004621 else
David Sterbacc4c13d2020-04-29 02:15:56 +02004622 btrfs_set_token_file_extent_type(&token, fi,
4623 BTRFS_FILE_EXTENT_REG);
Filipe Manana8407f552014-09-05 15:14:39 +01004624
4625 block_len = max(em->block_len, em->orig_block_len);
4626 if (em->compress_type != BTRFS_COMPRESS_NONE) {
David Sterbacc4c13d2020-04-29 02:15:56 +02004627 btrfs_set_token_file_extent_disk_bytenr(&token, fi,
4628 em->block_start);
4629 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
Filipe Manana8407f552014-09-05 15:14:39 +01004630 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
David Sterbacc4c13d2020-04-29 02:15:56 +02004631 btrfs_set_token_file_extent_disk_bytenr(&token, fi,
Filipe Manana8407f552014-09-05 15:14:39 +01004632 em->block_start -
David Sterbacc4c13d2020-04-29 02:15:56 +02004633 extent_offset);
4634 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
Filipe Manana8407f552014-09-05 15:14:39 +01004635 } else {
David Sterbacc4c13d2020-04-29 02:15:56 +02004636 btrfs_set_token_file_extent_disk_bytenr(&token, fi, 0);
4637 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, 0);
Filipe Manana8407f552014-09-05 15:14:39 +01004638 }
4639
David Sterbacc4c13d2020-04-29 02:15:56 +02004640 btrfs_set_token_file_extent_offset(&token, fi, extent_offset);
4641 btrfs_set_token_file_extent_num_bytes(&token, fi, em->len);
4642 btrfs_set_token_file_extent_ram_bytes(&token, fi, em->ram_bytes);
4643 btrfs_set_token_file_extent_compression(&token, fi, em->compress_type);
4644 btrfs_set_token_file_extent_encryption(&token, fi, 0);
4645 btrfs_set_token_file_extent_other_encoding(&token, fi, 0);
Filipe Manana8407f552014-09-05 15:14:39 +01004646 btrfs_mark_buffer_dirty(leaf);
4647
4648 btrfs_release_path(path);
4649
4650 return ret;
4651}
4652
Filipe Manana31d11b82018-05-09 16:01:46 +01004653/*
4654 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4655 * lose them after doing a fast fsync and replaying the log. We scan the
4656 * subvolume's root instead of iterating the inode's extent map tree because
4657 * otherwise we can log incorrect extent items based on extent map conversion.
4658 * That can happen due to the fact that extent maps are merged when they
4659 * are not in the extent map tree's list of modified extents.
4660 */
4661static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4662 struct btrfs_inode *inode,
4663 struct btrfs_path *path)
4664{
4665 struct btrfs_root *root = inode->root;
4666 struct btrfs_key key;
4667 const u64 i_size = i_size_read(&inode->vfs_inode);
4668 const u64 ino = btrfs_ino(inode);
4669 struct btrfs_path *dst_path = NULL;
Filipe Manana0e563152019-11-19 12:07:33 +00004670 bool dropped_extents = false;
Filipe Mananaf135cea2020-04-23 16:30:53 +01004671 u64 truncate_offset = i_size;
4672 struct extent_buffer *leaf;
4673 int slot;
Filipe Manana31d11b82018-05-09 16:01:46 +01004674 int ins_nr = 0;
4675 int start_slot;
4676 int ret;
4677
4678 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4679 return 0;
4680
4681 key.objectid = ino;
4682 key.type = BTRFS_EXTENT_DATA_KEY;
4683 key.offset = i_size;
4684 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4685 if (ret < 0)
4686 goto out;
4687
Filipe Mananaf135cea2020-04-23 16:30:53 +01004688 /*
4689 * We must check if there is a prealloc extent that starts before the
4690 * i_size and crosses the i_size boundary. This is to ensure later we
4691 * truncate down to the end of that extent and not to the i_size, as
4692 * otherwise we end up losing part of the prealloc extent after a log
4693 * replay and with an implicit hole if there is another prealloc extent
4694 * that starts at an offset beyond i_size.
4695 */
4696 ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
4697 if (ret < 0)
4698 goto out;
4699
4700 if (ret == 0) {
4701 struct btrfs_file_extent_item *ei;
4702
4703 leaf = path->nodes[0];
4704 slot = path->slots[0];
4705 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4706
4707 if (btrfs_file_extent_type(leaf, ei) ==
4708 BTRFS_FILE_EXTENT_PREALLOC) {
4709 u64 extent_end;
4710
4711 btrfs_item_key_to_cpu(leaf, &key, slot);
4712 extent_end = key.offset +
4713 btrfs_file_extent_num_bytes(leaf, ei);
4714
4715 if (extent_end > i_size)
4716 truncate_offset = extent_end;
4717 }
4718 } else {
4719 ret = 0;
4720 }
4721
Filipe Manana31d11b82018-05-09 16:01:46 +01004722 while (true) {
Filipe Mananaf135cea2020-04-23 16:30:53 +01004723 leaf = path->nodes[0];
4724 slot = path->slots[0];
Filipe Manana31d11b82018-05-09 16:01:46 +01004725
4726 if (slot >= btrfs_header_nritems(leaf)) {
4727 if (ins_nr > 0) {
4728 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana0e563152019-11-19 12:07:33 +00004729 start_slot, ins_nr, 1, 0);
Filipe Manana31d11b82018-05-09 16:01:46 +01004730 if (ret < 0)
4731 goto out;
4732 ins_nr = 0;
4733 }
4734 ret = btrfs_next_leaf(root, path);
4735 if (ret < 0)
4736 goto out;
4737 if (ret > 0) {
4738 ret = 0;
4739 break;
4740 }
4741 continue;
4742 }
4743
4744 btrfs_item_key_to_cpu(leaf, &key, slot);
4745 if (key.objectid > ino)
4746 break;
4747 if (WARN_ON_ONCE(key.objectid < ino) ||
4748 key.type < BTRFS_EXTENT_DATA_KEY ||
4749 key.offset < i_size) {
4750 path->slots[0]++;
4751 continue;
4752 }
Filipe Manana0e563152019-11-19 12:07:33 +00004753 if (!dropped_extents) {
Filipe Manana31d11b82018-05-09 16:01:46 +01004754 /*
4755 * Avoid logging extent items logged in past fsync calls
4756 * and leading to duplicate keys in the log tree.
4757 */
Filipe Manana8a2b3da2021-08-31 15:30:36 +01004758 ret = truncate_inode_items(trans, root->log_root, inode,
4759 truncate_offset,
4760 BTRFS_EXTENT_DATA_KEY);
Filipe Manana31d11b82018-05-09 16:01:46 +01004761 if (ret)
4762 goto out;
Filipe Manana0e563152019-11-19 12:07:33 +00004763 dropped_extents = true;
Filipe Manana31d11b82018-05-09 16:01:46 +01004764 }
4765 if (ins_nr == 0)
4766 start_slot = slot;
4767 ins_nr++;
4768 path->slots[0]++;
4769 if (!dst_path) {
4770 dst_path = btrfs_alloc_path();
4771 if (!dst_path) {
4772 ret = -ENOMEM;
4773 goto out;
4774 }
4775 }
4776 }
Filipe Manana0bc2d3c2020-04-21 11:25:31 +01004777 if (ins_nr > 0)
Filipe Manana0e563152019-11-19 12:07:33 +00004778 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana31d11b82018-05-09 16:01:46 +01004779 start_slot, ins_nr, 1, 0);
Filipe Manana31d11b82018-05-09 16:01:46 +01004780out:
4781 btrfs_release_path(path);
4782 btrfs_free_path(dst_path);
4783 return ret;
4784}
4785
Josef Bacik5dc562c2012-08-17 13:14:17 -04004786static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004787 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004788 struct btrfs_path *path,
Filipe Manana48778172020-08-11 12:43:58 +01004789 struct btrfs_log_ctx *ctx)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004790{
Filipe Manana48778172020-08-11 12:43:58 +01004791 struct btrfs_ordered_extent *ordered;
4792 struct btrfs_ordered_extent *tmp;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004793 struct extent_map *em, *n;
4794 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004795 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004796 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004797 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004798
4799 INIT_LIST_HEAD(&extents);
4800
Josef Bacik5dc562c2012-08-17 13:14:17 -04004801 write_lock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004802
4803 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4804 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004805 /*
4806 * Just an arbitrary number, this can be really CPU intensive
4807 * once we start getting a lot of extents, and really once we
4808 * have a bunch of extents we just want to commit since it will
4809 * be faster.
4810 */
4811 if (++num > 32768) {
4812 list_del_init(&tree->modified_extents);
4813 ret = -EFBIG;
4814 goto process;
4815 }
4816
Filipe Manana5f96bfb2020-11-25 12:19:24 +00004817 if (em->generation < trans->transid)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004818 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004819
Filipe Manana31d11b82018-05-09 16:01:46 +01004820 /* We log prealloc extents beyond eof later. */
4821 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4822 em->start >= i_size_read(&inode->vfs_inode))
4823 continue;
4824
Josef Bacikff44c6e2012-09-14 12:59:20 -04004825 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004826 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004827 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004828 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004829 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004830 }
4831
4832 list_sort(NULL, &extents, extent_cmp);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004833process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004834 while (!list_empty(&extents)) {
4835 em = list_entry(extents.next, struct extent_map, list);
4836
4837 list_del_init(&em->list);
4838
4839 /*
4840 * If we had an error we just need to delete everybody from our
4841 * private list.
4842 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004843 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004844 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004845 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004846 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004847 }
4848
4849 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004850
Filipe Manana90d04512021-09-16 11:32:10 +01004851 ret = log_one_extent(trans, inode, em, path, ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004852 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004853 clear_em_logging(tree, em);
4854 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004855 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004856 WARN_ON(!list_empty(&extents));
4857 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004858
Josef Bacik5dc562c2012-08-17 13:14:17 -04004859 btrfs_release_path(path);
Filipe Manana31d11b82018-05-09 16:01:46 +01004860 if (!ret)
4861 ret = btrfs_log_prealloc_extents(trans, inode, path);
Filipe Manana48778172020-08-11 12:43:58 +01004862 if (ret)
4863 return ret;
Filipe Manana31d11b82018-05-09 16:01:46 +01004864
Filipe Manana48778172020-08-11 12:43:58 +01004865 /*
4866 * We have logged all extents successfully, now make sure the commit of
4867 * the current transaction waits for the ordered extents to complete
4868 * before it commits and wipes out the log trees, otherwise we would
4869 * lose data if an ordered extents completes after the transaction
4870 * commits and a power failure happens after the transaction commit.
4871 */
4872 list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
4873 list_del_init(&ordered->log_list);
4874 set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
4875
4876 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4877 spin_lock_irq(&inode->ordered_tree.lock);
4878 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4879 set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
4880 atomic_inc(&trans->transaction->pending_ordered);
4881 }
4882 spin_unlock_irq(&inode->ordered_tree.lock);
4883 }
4884 btrfs_put_ordered_extent(ordered);
4885 }
4886
4887 return 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004888}
4889
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004890static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004891 struct btrfs_path *path, u64 *size_ret)
4892{
4893 struct btrfs_key key;
4894 int ret;
4895
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004896 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004897 key.type = BTRFS_INODE_ITEM_KEY;
4898 key.offset = 0;
4899
4900 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4901 if (ret < 0) {
4902 return ret;
4903 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004904 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004905 } else {
4906 struct btrfs_inode_item *item;
4907
4908 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4909 struct btrfs_inode_item);
4910 *size_ret = btrfs_inode_size(path->nodes[0], item);
Filipe Mananabf504112019-03-04 14:06:12 +00004911 /*
4912 * If the in-memory inode's i_size is smaller then the inode
4913 * size stored in the btree, return the inode's i_size, so
4914 * that we get a correct inode size after replaying the log
4915 * when before a power failure we had a shrinking truncate
4916 * followed by addition of a new name (rename / new hard link).
4917 * Otherwise return the inode size from the btree, to avoid
4918 * data loss when replaying a log due to previously doing a
4919 * write that expands the inode's size and logging a new name
4920 * immediately after.
4921 */
4922 if (*size_ret > inode->vfs_inode.i_size)
4923 *size_ret = inode->vfs_inode.i_size;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004924 }
4925
4926 btrfs_release_path(path);
4927 return 0;
4928}
4929
Filipe Manana36283bf2015-06-20 00:44:51 +01004930/*
4931 * At the moment we always log all xattrs. This is to figure out at log replay
4932 * time which xattrs must have their deletion replayed. If a xattr is missing
4933 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4934 * because if a xattr is deleted, the inode is fsynced and a power failure
4935 * happens, causing the log to be replayed the next time the fs is mounted,
4936 * we want the xattr to not exist anymore (same behaviour as other filesystems
4937 * with a journal, ext3/4, xfs, f2fs, etc).
4938 */
4939static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004940 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004941 struct btrfs_path *path,
4942 struct btrfs_path *dst_path)
4943{
Filipe Manana90d04512021-09-16 11:32:10 +01004944 struct btrfs_root *root = inode->root;
Filipe Manana36283bf2015-06-20 00:44:51 +01004945 int ret;
4946 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004947 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004948 int ins_nr = 0;
4949 int start_slot = 0;
Filipe Mananaf2f121a2020-11-13 11:21:49 +00004950 bool found_xattrs = false;
4951
4952 if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags))
4953 return 0;
Filipe Manana36283bf2015-06-20 00:44:51 +01004954
4955 key.objectid = ino;
4956 key.type = BTRFS_XATTR_ITEM_KEY;
4957 key.offset = 0;
4958
4959 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4960 if (ret < 0)
4961 return ret;
4962
4963 while (true) {
4964 int slot = path->slots[0];
4965 struct extent_buffer *leaf = path->nodes[0];
4966 int nritems = btrfs_header_nritems(leaf);
4967
4968 if (slot >= nritems) {
4969 if (ins_nr > 0) {
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004970 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana0e563152019-11-19 12:07:33 +00004971 start_slot, ins_nr, 1, 0);
Filipe Manana36283bf2015-06-20 00:44:51 +01004972 if (ret < 0)
4973 return ret;
4974 ins_nr = 0;
4975 }
4976 ret = btrfs_next_leaf(root, path);
4977 if (ret < 0)
4978 return ret;
4979 else if (ret > 0)
4980 break;
4981 continue;
4982 }
4983
4984 btrfs_item_key_to_cpu(leaf, &key, slot);
4985 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4986 break;
4987
4988 if (ins_nr == 0)
4989 start_slot = slot;
4990 ins_nr++;
4991 path->slots[0]++;
Filipe Mananaf2f121a2020-11-13 11:21:49 +00004992 found_xattrs = true;
Filipe Manana36283bf2015-06-20 00:44:51 +01004993 cond_resched();
4994 }
4995 if (ins_nr > 0) {
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004996 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana0e563152019-11-19 12:07:33 +00004997 start_slot, ins_nr, 1, 0);
Filipe Manana36283bf2015-06-20 00:44:51 +01004998 if (ret < 0)
4999 return ret;
5000 }
5001
Filipe Mananaf2f121a2020-11-13 11:21:49 +00005002 if (!found_xattrs)
5003 set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags);
5004
Filipe Manana36283bf2015-06-20 00:44:51 +01005005 return 0;
5006}
5007
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005008/*
Filipe Manana0e563152019-11-19 12:07:33 +00005009 * When using the NO_HOLES feature if we punched a hole that causes the
5010 * deletion of entire leafs or all the extent items of the first leaf (the one
5011 * that contains the inode item and references) we may end up not processing
5012 * any extents, because there are no leafs with a generation matching the
5013 * current transaction that have extent items for our inode. So we need to find
5014 * if any holes exist and then log them. We also need to log holes after any
5015 * truncate operation that changes the inode's size.
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005016 */
Filipe Manana0e563152019-11-19 12:07:33 +00005017static int btrfs_log_holes(struct btrfs_trans_handle *trans,
Filipe Manana0e563152019-11-19 12:07:33 +00005018 struct btrfs_inode *inode,
Filipe Manana7af59742020-04-07 11:37:44 +01005019 struct btrfs_path *path)
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005020{
Filipe Manana90d04512021-09-16 11:32:10 +01005021 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005022 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005023 struct btrfs_key key;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02005024 const u64 ino = btrfs_ino(inode);
5025 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Manana7af59742020-04-07 11:37:44 +01005026 u64 prev_extent_end = 0;
Filipe Manana0e563152019-11-19 12:07:33 +00005027 int ret;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005028
Filipe Manana0e563152019-11-19 12:07:33 +00005029 if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005030 return 0;
5031
5032 key.objectid = ino;
5033 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana7af59742020-04-07 11:37:44 +01005034 key.offset = 0;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005035
5036 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005037 if (ret < 0)
5038 return ret;
5039
Filipe Manana0e563152019-11-19 12:07:33 +00005040 while (true) {
Filipe Manana0e563152019-11-19 12:07:33 +00005041 struct extent_buffer *leaf = path->nodes[0];
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005042
Filipe Manana0e563152019-11-19 12:07:33 +00005043 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5044 ret = btrfs_next_leaf(root, path);
5045 if (ret < 0)
5046 return ret;
5047 if (ret > 0) {
5048 ret = 0;
5049 break;
5050 }
5051 leaf = path->nodes[0];
5052 }
5053
5054 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5055 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
5056 break;
5057
5058 /* We have a hole, log it. */
5059 if (prev_extent_end < key.offset) {
Filipe Manana7af59742020-04-07 11:37:44 +01005060 const u64 hole_len = key.offset - prev_extent_end;
Filipe Manana0e563152019-11-19 12:07:33 +00005061
5062 /*
5063 * Release the path to avoid deadlocks with other code
5064 * paths that search the root while holding locks on
5065 * leafs from the log root.
5066 */
5067 btrfs_release_path(path);
5068 ret = btrfs_insert_file_extent(trans, root->log_root,
5069 ino, prev_extent_end, 0,
5070 0, hole_len, 0, hole_len,
5071 0, 0, 0);
5072 if (ret < 0)
5073 return ret;
5074
5075 /*
5076 * Search for the same key again in the root. Since it's
5077 * an extent item and we are holding the inode lock, the
5078 * key must still exist. If it doesn't just emit warning
5079 * and return an error to fall back to a transaction
5080 * commit.
5081 */
5082 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5083 if (ret < 0)
5084 return ret;
5085 if (WARN_ON(ret > 0))
5086 return -ENOENT;
5087 leaf = path->nodes[0];
5088 }
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005089
Filipe Manana7af59742020-04-07 11:37:44 +01005090 prev_extent_end = btrfs_file_extent_end(path);
Filipe Manana0e563152019-11-19 12:07:33 +00005091 path->slots[0]++;
5092 cond_resched();
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005093 }
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005094
Filipe Manana7af59742020-04-07 11:37:44 +01005095 if (prev_extent_end < i_size) {
Filipe Manana0e563152019-11-19 12:07:33 +00005096 u64 hole_len;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005097
Filipe Manana0e563152019-11-19 12:07:33 +00005098 btrfs_release_path(path);
Filipe Manana7af59742020-04-07 11:37:44 +01005099 hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
Filipe Manana0e563152019-11-19 12:07:33 +00005100 ret = btrfs_insert_file_extent(trans, root->log_root,
5101 ino, prev_extent_end, 0, 0,
5102 hole_len, 0, hole_len,
5103 0, 0, 0);
5104 if (ret < 0)
5105 return ret;
5106 }
5107
5108 return 0;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005109}
5110
Filipe Manana56f23fd2016-03-30 23:37:21 +01005111/*
5112 * When we are logging a new inode X, check if it doesn't have a reference that
5113 * matches the reference from some other inode Y created in a past transaction
5114 * and that was renamed in the current transaction. If we don't do this, then at
5115 * log replay time we can lose inode Y (and all its files if it's a directory):
5116 *
5117 * mkdir /mnt/x
5118 * echo "hello world" > /mnt/x/foobar
5119 * sync
5120 * mv /mnt/x /mnt/y
5121 * mkdir /mnt/x # or touch /mnt/x
5122 * xfs_io -c fsync /mnt/x
5123 * <power fail>
5124 * mount fs, trigger log replay
5125 *
5126 * After the log replay procedure, we would lose the first directory and all its
5127 * files (file foobar).
5128 * For the case where inode Y is not a directory we simply end up losing it:
5129 *
5130 * echo "123" > /mnt/foo
5131 * sync
5132 * mv /mnt/foo /mnt/bar
5133 * echo "abc" > /mnt/foo
5134 * xfs_io -c fsync /mnt/foo
5135 * <power fail>
5136 *
5137 * We also need this for cases where a snapshot entry is replaced by some other
5138 * entry (file or directory) otherwise we end up with an unreplayable log due to
5139 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
5140 * if it were a regular entry:
5141 *
5142 * mkdir /mnt/x
5143 * btrfs subvolume snapshot /mnt /mnt/x/snap
5144 * btrfs subvolume delete /mnt/x/snap
5145 * rmdir /mnt/x
5146 * mkdir /mnt/x
5147 * fsync /mnt/x or fsync some new file inside it
5148 * <power fail>
5149 *
5150 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
5151 * the same transaction.
5152 */
5153static int btrfs_check_ref_name_override(struct extent_buffer *eb,
5154 const int slot,
5155 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02005156 struct btrfs_inode *inode,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005157 u64 *other_ino, u64 *other_parent)
Filipe Manana56f23fd2016-03-30 23:37:21 +01005158{
5159 int ret;
5160 struct btrfs_path *search_path;
5161 char *name = NULL;
5162 u32 name_len = 0;
5163 u32 item_size = btrfs_item_size_nr(eb, slot);
5164 u32 cur_offset = 0;
5165 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
5166
5167 search_path = btrfs_alloc_path();
5168 if (!search_path)
5169 return -ENOMEM;
5170 search_path->search_commit_root = 1;
5171 search_path->skip_locking = 1;
5172
5173 while (cur_offset < item_size) {
5174 u64 parent;
5175 u32 this_name_len;
5176 u32 this_len;
5177 unsigned long name_ptr;
5178 struct btrfs_dir_item *di;
5179
5180 if (key->type == BTRFS_INODE_REF_KEY) {
5181 struct btrfs_inode_ref *iref;
5182
5183 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
5184 parent = key->offset;
5185 this_name_len = btrfs_inode_ref_name_len(eb, iref);
5186 name_ptr = (unsigned long)(iref + 1);
5187 this_len = sizeof(*iref) + this_name_len;
5188 } else {
5189 struct btrfs_inode_extref *extref;
5190
5191 extref = (struct btrfs_inode_extref *)(ptr +
5192 cur_offset);
5193 parent = btrfs_inode_extref_parent(eb, extref);
5194 this_name_len = btrfs_inode_extref_name_len(eb, extref);
5195 name_ptr = (unsigned long)&extref->name;
5196 this_len = sizeof(*extref) + this_name_len;
5197 }
5198
5199 if (this_name_len > name_len) {
5200 char *new_name;
5201
5202 new_name = krealloc(name, this_name_len, GFP_NOFS);
5203 if (!new_name) {
5204 ret = -ENOMEM;
5205 goto out;
5206 }
5207 name_len = this_name_len;
5208 name = new_name;
5209 }
5210
5211 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02005212 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
5213 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01005214 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005215 struct btrfs_key di_key;
5216
5217 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
5218 di, &di_key);
5219 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
Filipe Manana6b5fc432019-02-13 12:14:03 +00005220 if (di_key.objectid != key->objectid) {
5221 ret = 1;
5222 *other_ino = di_key.objectid;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005223 *other_parent = parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005224 } else {
5225 ret = 0;
5226 }
Filipe Manana44f714d2016-06-06 16:11:13 +01005227 } else {
5228 ret = -EAGAIN;
5229 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01005230 goto out;
5231 } else if (IS_ERR(di)) {
5232 ret = PTR_ERR(di);
5233 goto out;
5234 }
5235 btrfs_release_path(search_path);
5236
5237 cur_offset += this_len;
5238 }
5239 ret = 0;
5240out:
5241 btrfs_free_path(search_path);
5242 kfree(name);
5243 return ret;
5244}
5245
Filipe Manana6b5fc432019-02-13 12:14:03 +00005246struct btrfs_ino_list {
5247 u64 ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005248 u64 parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005249 struct list_head list;
5250};
5251
5252static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
5253 struct btrfs_root *root,
5254 struct btrfs_path *path,
5255 struct btrfs_log_ctx *ctx,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005256 u64 ino, u64 parent)
Filipe Manana6b5fc432019-02-13 12:14:03 +00005257{
5258 struct btrfs_ino_list *ino_elem;
5259 LIST_HEAD(inode_list);
5260 int ret = 0;
5261
5262 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
5263 if (!ino_elem)
5264 return -ENOMEM;
5265 ino_elem->ino = ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005266 ino_elem->parent = parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005267 list_add_tail(&ino_elem->list, &inode_list);
5268
5269 while (!list_empty(&inode_list)) {
5270 struct btrfs_fs_info *fs_info = root->fs_info;
5271 struct btrfs_key key;
5272 struct inode *inode;
5273
5274 ino_elem = list_first_entry(&inode_list, struct btrfs_ino_list,
5275 list);
5276 ino = ino_elem->ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005277 parent = ino_elem->parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005278 list_del(&ino_elem->list);
5279 kfree(ino_elem);
5280 if (ret)
5281 continue;
5282
5283 btrfs_release_path(path);
5284
David Sterba0202e832020-05-15 19:35:59 +02005285 inode = btrfs_iget(fs_info->sb, ino, root);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005286 /*
5287 * If the other inode that had a conflicting dir entry was
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005288 * deleted in the current transaction, we need to log its parent
5289 * directory.
Filipe Manana6b5fc432019-02-13 12:14:03 +00005290 */
5291 if (IS_ERR(inode)) {
5292 ret = PTR_ERR(inode);
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005293 if (ret == -ENOENT) {
David Sterba0202e832020-05-15 19:35:59 +02005294 inode = btrfs_iget(fs_info->sb, parent, root);
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005295 if (IS_ERR(inode)) {
5296 ret = PTR_ERR(inode);
5297 } else {
Filipe Manana90d04512021-09-16 11:32:10 +01005298 ret = btrfs_log_inode(trans,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005299 BTRFS_I(inode),
5300 LOG_OTHER_INODE_ALL,
Filipe Manana48778172020-08-11 12:43:58 +01005301 ctx);
Filipe Manana410f9542019-09-10 15:26:49 +01005302 btrfs_add_delayed_iput(inode);
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005303 }
5304 }
Filipe Manana6b5fc432019-02-13 12:14:03 +00005305 continue;
5306 }
5307 /*
Filipe Mananab5e4ff92020-01-15 13:21:35 +00005308 * If the inode was already logged skip it - otherwise we can
5309 * hit an infinite loop. Example:
5310 *
5311 * From the commit root (previous transaction) we have the
5312 * following inodes:
5313 *
5314 * inode 257 a directory
5315 * inode 258 with references "zz" and "zz_link" on inode 257
5316 * inode 259 with reference "a" on inode 257
5317 *
5318 * And in the current (uncommitted) transaction we have:
5319 *
5320 * inode 257 a directory, unchanged
5321 * inode 258 with references "a" and "a2" on inode 257
5322 * inode 259 with reference "zz_link" on inode 257
5323 * inode 261 with reference "zz" on inode 257
5324 *
5325 * When logging inode 261 the following infinite loop could
5326 * happen if we don't skip already logged inodes:
5327 *
5328 * - we detect inode 258 as a conflicting inode, with inode 261
5329 * on reference "zz", and log it;
5330 *
5331 * - we detect inode 259 as a conflicting inode, with inode 258
5332 * on reference "a", and log it;
5333 *
5334 * - we detect inode 258 as a conflicting inode, with inode 259
5335 * on reference "zz_link", and log it - again! After this we
5336 * repeat the above steps forever.
5337 */
5338 spin_lock(&BTRFS_I(inode)->lock);
5339 /*
5340 * Check the inode's logged_trans only instead of
5341 * btrfs_inode_in_log(). This is because the last_log_commit of
Filipe Manana1f295372021-07-29 15:30:21 +01005342 * the inode is not updated when we only log that it exists (see
5343 * btrfs_log_inode()).
Filipe Mananab5e4ff92020-01-15 13:21:35 +00005344 */
5345 if (BTRFS_I(inode)->logged_trans == trans->transid) {
5346 spin_unlock(&BTRFS_I(inode)->lock);
5347 btrfs_add_delayed_iput(inode);
5348 continue;
5349 }
5350 spin_unlock(&BTRFS_I(inode)->lock);
5351 /*
Filipe Manana6b5fc432019-02-13 12:14:03 +00005352 * We are safe logging the other inode without acquiring its
5353 * lock as long as we log with the LOG_INODE_EXISTS mode. We
5354 * are safe against concurrent renames of the other inode as
5355 * well because during a rename we pin the log and update the
5356 * log with the new name before we unpin it.
5357 */
Filipe Manana90d04512021-09-16 11:32:10 +01005358 ret = btrfs_log_inode(trans, BTRFS_I(inode), LOG_OTHER_INODE, ctx);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005359 if (ret) {
Filipe Manana410f9542019-09-10 15:26:49 +01005360 btrfs_add_delayed_iput(inode);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005361 continue;
5362 }
5363
5364 key.objectid = ino;
5365 key.type = BTRFS_INODE_REF_KEY;
5366 key.offset = 0;
5367 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5368 if (ret < 0) {
Filipe Manana410f9542019-09-10 15:26:49 +01005369 btrfs_add_delayed_iput(inode);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005370 continue;
5371 }
5372
5373 while (true) {
5374 struct extent_buffer *leaf = path->nodes[0];
5375 int slot = path->slots[0];
5376 u64 other_ino = 0;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005377 u64 other_parent = 0;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005378
5379 if (slot >= btrfs_header_nritems(leaf)) {
5380 ret = btrfs_next_leaf(root, path);
5381 if (ret < 0) {
5382 break;
5383 } else if (ret > 0) {
5384 ret = 0;
5385 break;
5386 }
5387 continue;
5388 }
5389
5390 btrfs_item_key_to_cpu(leaf, &key, slot);
5391 if (key.objectid != ino ||
5392 (key.type != BTRFS_INODE_REF_KEY &&
5393 key.type != BTRFS_INODE_EXTREF_KEY)) {
5394 ret = 0;
5395 break;
5396 }
5397
5398 ret = btrfs_check_ref_name_override(leaf, slot, &key,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005399 BTRFS_I(inode), &other_ino,
5400 &other_parent);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005401 if (ret < 0)
5402 break;
5403 if (ret > 0) {
5404 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
5405 if (!ino_elem) {
5406 ret = -ENOMEM;
5407 break;
5408 }
5409 ino_elem->ino = other_ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005410 ino_elem->parent = other_parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005411 list_add_tail(&ino_elem->list, &inode_list);
5412 ret = 0;
5413 }
5414 path->slots[0]++;
5415 }
Filipe Manana410f9542019-09-10 15:26:49 +01005416 btrfs_add_delayed_iput(inode);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005417 }
5418
5419 return ret;
5420}
5421
Filipe Mananada447002020-03-09 12:41:07 +00005422static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,
5423 struct btrfs_inode *inode,
5424 struct btrfs_key *min_key,
5425 const struct btrfs_key *max_key,
5426 struct btrfs_path *path,
5427 struct btrfs_path *dst_path,
5428 const u64 logged_isize,
5429 const bool recursive_logging,
5430 const int inode_only,
5431 struct btrfs_log_ctx *ctx,
5432 bool *need_log_inode_item)
5433{
5434 struct btrfs_root *root = inode->root;
5435 int ins_start_slot = 0;
5436 int ins_nr = 0;
5437 int ret;
5438
5439 while (1) {
5440 ret = btrfs_search_forward(root, min_key, path, trans->transid);
5441 if (ret < 0)
5442 return ret;
5443 if (ret > 0) {
5444 ret = 0;
5445 break;
5446 }
5447again:
5448 /* Note, ins_nr might be > 0 here, cleanup outside the loop */
5449 if (min_key->objectid != max_key->objectid)
5450 break;
5451 if (min_key->type > max_key->type)
5452 break;
5453
5454 if (min_key->type == BTRFS_INODE_ITEM_KEY)
5455 *need_log_inode_item = false;
5456
5457 if ((min_key->type == BTRFS_INODE_REF_KEY ||
5458 min_key->type == BTRFS_INODE_EXTREF_KEY) &&
5459 inode->generation == trans->transid &&
5460 !recursive_logging) {
5461 u64 other_ino = 0;
5462 u64 other_parent = 0;
5463
5464 ret = btrfs_check_ref_name_override(path->nodes[0],
5465 path->slots[0], min_key, inode,
5466 &other_ino, &other_parent);
5467 if (ret < 0) {
5468 return ret;
Filipe Manana289cffc2021-08-31 15:30:32 +01005469 } else if (ret > 0 &&
Filipe Mananada447002020-03-09 12:41:07 +00005470 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
5471 if (ins_nr > 0) {
5472 ins_nr++;
5473 } else {
5474 ins_nr = 1;
5475 ins_start_slot = path->slots[0];
5476 }
5477 ret = copy_items(trans, inode, dst_path, path,
5478 ins_start_slot, ins_nr,
5479 inode_only, logged_isize);
5480 if (ret < 0)
5481 return ret;
5482 ins_nr = 0;
5483
5484 ret = log_conflicting_inodes(trans, root, path,
5485 ctx, other_ino, other_parent);
5486 if (ret)
5487 return ret;
5488 btrfs_release_path(path);
5489 goto next_key;
5490 }
5491 }
5492
5493 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5494 if (min_key->type == BTRFS_XATTR_ITEM_KEY) {
5495 if (ins_nr == 0)
5496 goto next_slot;
5497 ret = copy_items(trans, inode, dst_path, path,
5498 ins_start_slot,
5499 ins_nr, inode_only, logged_isize);
5500 if (ret < 0)
5501 return ret;
5502 ins_nr = 0;
5503 goto next_slot;
5504 }
5505
5506 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5507 ins_nr++;
5508 goto next_slot;
5509 } else if (!ins_nr) {
5510 ins_start_slot = path->slots[0];
5511 ins_nr = 1;
5512 goto next_slot;
5513 }
5514
5515 ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
5516 ins_nr, inode_only, logged_isize);
5517 if (ret < 0)
5518 return ret;
5519 ins_nr = 1;
5520 ins_start_slot = path->slots[0];
5521next_slot:
5522 path->slots[0]++;
5523 if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
5524 btrfs_item_key_to_cpu(path->nodes[0], min_key,
5525 path->slots[0]);
5526 goto again;
5527 }
5528 if (ins_nr) {
5529 ret = copy_items(trans, inode, dst_path, path,
5530 ins_start_slot, ins_nr, inode_only,
5531 logged_isize);
5532 if (ret < 0)
5533 return ret;
5534 ins_nr = 0;
5535 }
5536 btrfs_release_path(path);
5537next_key:
5538 if (min_key->offset < (u64)-1) {
5539 min_key->offset++;
5540 } else if (min_key->type < max_key->type) {
5541 min_key->type++;
5542 min_key->offset = 0;
5543 } else {
5544 break;
5545 }
5546 }
5547 if (ins_nr)
5548 ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
5549 ins_nr, inode_only, logged_isize);
5550
5551 return ret;
5552}
5553
Chris Masone02119d2008-09-05 16:13:11 -04005554/* log a single inode in the tree log.
5555 * At least one parent directory for this inode must exist in the tree
5556 * or be logged already.
5557 *
5558 * Any items from this inode changed by the current transaction are copied
5559 * to the log tree. An extra reference is taken on any extents in this
5560 * file, allowing us to avoid a whole pile of corner cases around logging
5561 * blocks that have been removed from the tree.
5562 *
5563 * See LOG_INODE_ALL and related defines for a description of what inode_only
5564 * does.
5565 *
5566 * This handles both files and directories.
5567 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005568static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana90d04512021-09-16 11:32:10 +01005569 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005570 int inode_only,
Filipe Manana8407f552014-09-05 15:14:39 +01005571 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005572{
5573 struct btrfs_path *path;
5574 struct btrfs_path *dst_path;
5575 struct btrfs_key min_key;
5576 struct btrfs_key max_key;
Filipe Manana90d04512021-09-16 11:32:10 +01005577 struct btrfs_root *log = inode->root->log_root;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005578 int err = 0;
Filipe Manana8c8648d2020-07-02 12:31:59 +01005579 int ret = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005580 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005581 u64 ino = btrfs_ino(inode);
5582 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005583 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01005584 bool need_log_inode_item = true;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005585 bool xattrs_logged = false;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005586 bool recursive_logging = false;
Filipe Manana2ac691d2021-07-20 16:03:43 +01005587 bool inode_item_dropped = true;
Chris Masone02119d2008-09-05 16:13:11 -04005588
Chris Masone02119d2008-09-05 16:13:11 -04005589 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00005590 if (!path)
5591 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04005592 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00005593 if (!dst_path) {
5594 btrfs_free_path(path);
5595 return -ENOMEM;
5596 }
Chris Masone02119d2008-09-05 16:13:11 -04005597
Li Zefan33345d012011-04-20 10:31:50 +08005598 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04005599 min_key.type = BTRFS_INODE_ITEM_KEY;
5600 min_key.offset = 0;
5601
Li Zefan33345d012011-04-20 10:31:50 +08005602 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04005603
Chris Mason12fcfd22009-03-24 10:24:20 -04005604
Josef Bacik5dc562c2012-08-17 13:14:17 -04005605 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005606 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00005607 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005608 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08005609 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04005610 max_key.type = BTRFS_XATTR_ITEM_KEY;
5611 else
5612 max_key.type = (u8)-1;
5613 max_key.offset = (u64)-1;
5614
Filipe Manana2c2c4522015-01-13 16:40:04 +00005615 /*
Filipe Manana5aa7d1a2020-07-02 12:32:20 +01005616 * Only run delayed items if we are a directory. We want to make sure
5617 * all directory indexes hit the fs/subvolume tree so we can find them
5618 * and figure out which index ranges have to be logged.
Filipe Manana2c2c4522015-01-13 16:40:04 +00005619 */
Filipe Mananaf6df27d2021-08-31 15:30:40 +01005620 if (S_ISDIR(inode->vfs_inode.i_mode)) {
5621 err = btrfs_commit_inode_delayed_items(trans, inode);
5622 if (err)
5623 goto out;
Miao Xie16cdcec2011-04-22 18:12:22 +08005624 }
5625
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005626 if (inode_only == LOG_OTHER_INODE || inode_only == LOG_OTHER_INODE_ALL) {
5627 recursive_logging = true;
5628 if (inode_only == LOG_OTHER_INODE)
5629 inode_only = LOG_INODE_EXISTS;
5630 else
5631 inode_only = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005632 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08005633 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005634 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08005635 }
Chris Masone02119d2008-09-05 16:13:11 -04005636
Filipe Manana5e33a2b2016-02-25 23:19:38 +00005637 /*
Filipe Manana64d6b282021-01-27 10:34:59 +00005638 * This is for cases where logging a directory could result in losing a
5639 * a file after replaying the log. For example, if we move a file from a
5640 * directory A to a directory B, then fsync directory A, we have no way
5641 * to known the file was moved from A to B, so logging just A would
5642 * result in losing the file after a log replay.
5643 */
5644 if (S_ISDIR(inode->vfs_inode.i_mode) &&
5645 inode_only == LOG_INODE_ALL &&
5646 inode->last_unlink_trans >= trans->transid) {
5647 btrfs_set_log_full_commit(trans);
5648 err = 1;
5649 goto out_unlock;
5650 }
5651
5652 /*
Chris Masone02119d2008-09-05 16:13:11 -04005653 * a brute force approach to making sure we get the most uptodate
5654 * copies of everything.
5655 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005656 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04005657 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
5658
Filipe Mananaab123132021-01-27 10:34:56 +00005659 clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags);
Filipe Manana4f764e52015-02-23 19:53:35 +00005660 if (inode_only == LOG_INODE_EXISTS)
5661 max_key_type = BTRFS_XATTR_ITEM_KEY;
Filipe Manana88e221c2021-08-31 15:30:35 +01005662 ret = drop_inode_items(trans, log, path, inode, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04005663 } else {
Filipe Mananaa5c733a2021-08-31 15:30:38 +01005664 if (inode_only == LOG_INODE_EXISTS && inode_logged(trans, inode)) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005665 /*
5666 * Make sure the new inode item we write to the log has
5667 * the same isize as the current one (if it exists).
5668 * This is necessary to prevent data loss after log
5669 * replay, and also to prevent doing a wrong expanding
5670 * truncate - for e.g. create file, write 4K into offset
5671 * 0, fsync, write 4K into offset 4096, add hard link,
5672 * fsync some other file (to sync log), power fail - if
5673 * we use the inode's current i_size, after log replay
5674 * we get a 8Kb file, with the last 4Kb extent as a hole
5675 * (zeroes), as if an expanding truncate happened,
5676 * instead of getting a file of 4Kb only.
5677 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005678 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005679 if (err)
5680 goto out_unlock;
5681 }
Filipe Mananaa7429942015-02-13 16:56:14 +00005682 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005683 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00005684 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005685 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Manana88e221c2021-08-31 15:30:35 +01005686 ret = drop_inode_items(trans, log, path, inode,
5687 max_key.type);
Filipe Mananaa7429942015-02-13 16:56:14 +00005688 } else {
5689 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005690 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00005691 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005692 &inode->runtime_flags);
Filipe Manana4934a812021-08-31 15:30:37 +01005693 if (inode_logged(trans, inode))
5694 ret = truncate_inode_items(trans, log,
5695 inode, 0, 0);
Filipe Mananaa7429942015-02-13 16:56:14 +00005696 }
Filipe Manana4f764e52015-02-23 19:53:35 +00005697 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005698 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05005699 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005700 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04005701 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00005702 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Manana88e221c2021-08-31 15:30:35 +01005703 ret = drop_inode_items(trans, log, path, inode,
5704 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005705 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00005706 if (inode_only == LOG_INODE_ALL)
5707 fast_search = true;
Filipe Manana2ac691d2021-07-20 16:03:43 +01005708 inode_item_dropped = false;
Josef Bacika95249b2012-10-11 16:17:34 -04005709 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005710 }
Josef Bacika95249b2012-10-11 16:17:34 -04005711
Chris Masone02119d2008-09-05 16:13:11 -04005712 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005713 if (ret) {
5714 err = ret;
5715 goto out_unlock;
5716 }
Chris Masone02119d2008-09-05 16:13:11 -04005717
Filipe Mananada447002020-03-09 12:41:07 +00005718 err = copy_inode_items_to_log(trans, inode, &min_key, &max_key,
5719 path, dst_path, logged_isize,
Filipe Manana7af59742020-04-07 11:37:44 +01005720 recursive_logging, inode_only, ctx,
5721 &need_log_inode_item);
Filipe Mananada447002020-03-09 12:41:07 +00005722 if (err)
5723 goto out_unlock;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005724
Filipe Manana36283bf2015-06-20 00:44:51 +01005725 btrfs_release_path(path);
5726 btrfs_release_path(dst_path);
Filipe Manana90d04512021-09-16 11:32:10 +01005727 err = btrfs_log_all_xattrs(trans, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01005728 if (err)
5729 goto out_unlock;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005730 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005731 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5732 btrfs_release_path(path);
5733 btrfs_release_path(dst_path);
Filipe Manana90d04512021-09-16 11:32:10 +01005734 err = btrfs_log_holes(trans, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005735 if (err)
5736 goto out_unlock;
5737 }
Josef Bacika95249b2012-10-11 16:17:34 -04005738log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04005739 btrfs_release_path(path);
5740 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01005741 if (need_log_inode_item) {
Filipe Manana2ac691d2021-07-20 16:03:43 +01005742 err = log_inode_item(trans, log, dst_path, inode, inode_item_dropped);
Filipe Mananae4545de2015-06-17 12:49:23 +01005743 if (err)
5744 goto out_unlock;
Filipe Mananab590b832021-05-28 11:37:32 +01005745 /*
5746 * If we are doing a fast fsync and the inode was logged before
5747 * in this transaction, we don't need to log the xattrs because
5748 * they were logged before. If xattrs were added, changed or
5749 * deleted since the last time we logged the inode, then we have
5750 * already logged them because the inode had the runtime flag
5751 * BTRFS_INODE_COPY_EVERYTHING set.
5752 */
5753 if (!xattrs_logged && inode->logged_trans < trans->transid) {
Filipe Manana90d04512021-09-16 11:32:10 +01005754 err = btrfs_log_all_xattrs(trans, inode, path, dst_path);
Filipe Mananab590b832021-05-28 11:37:32 +01005755 if (err)
5756 goto out_unlock;
5757 btrfs_release_path(path);
5758 }
Filipe Mananae4545de2015-06-17 12:49:23 +01005759 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005760 if (fast_search) {
Filipe Manana90d04512021-09-16 11:32:10 +01005761 ret = btrfs_log_changed_extents(trans, inode, dst_path, ctx);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005762 if (ret) {
5763 err = ret;
5764 goto out_unlock;
5765 }
Josef Bacikd006a042013-11-12 20:54:09 -05005766 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06005767 struct extent_map *em, *n;
5768
Filipe Manana49dae1b2014-09-06 22:34:39 +01005769 write_lock(&em_tree->lock);
Filipe Manana48778172020-08-11 12:43:58 +01005770 list_for_each_entry_safe(em, n, &em_tree->modified_extents, list)
5771 list_del_init(&em->list);
Filipe Manana49dae1b2014-09-06 22:34:39 +01005772 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005773 }
5774
Nikolay Borisova59108a2017-01-18 00:31:48 +02005775 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
Filipe Manana90d04512021-09-16 11:32:10 +01005776 ret = log_directory_changes(trans, inode, path, dst_path, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005777 if (ret) {
5778 err = ret;
5779 goto out_unlock;
5780 }
Chris Masone02119d2008-09-05 16:13:11 -04005781 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005782
Filipe Manana130341b2021-08-31 15:30:34 +01005783 spin_lock(&inode->lock);
5784 inode->logged_trans = trans->transid;
Filipe Mananad1d832a2019-06-07 11:25:24 +01005785 /*
Filipe Manana130341b2021-08-31 15:30:34 +01005786 * Don't update last_log_commit if we logged that an inode exists.
5787 * We do this for three reasons:
5788 *
5789 * 1) We might have had buffered writes to this inode that were
5790 * flushed and had their ordered extents completed in this
5791 * transaction, but we did not previously log the inode with
5792 * LOG_INODE_ALL. Later the inode was evicted and after that
5793 * it was loaded again and this LOG_INODE_EXISTS log operation
5794 * happened. We must make sure that if an explicit fsync against
5795 * the inode is performed later, it logs the new extents, an
5796 * updated inode item, etc, and syncs the log. The same logic
5797 * applies to direct IO writes instead of buffered writes.
5798 *
5799 * 2) When we log the inode with LOG_INODE_EXISTS, its inode item
5800 * is logged with an i_size of 0 or whatever value was logged
5801 * before. If later the i_size of the inode is increased by a
5802 * truncate operation, the log is synced through an fsync of
5803 * some other inode and then finally an explicit fsync against
5804 * this inode is made, we must make sure this fsync logs the
5805 * inode with the new i_size, the hole between old i_size and
5806 * the new i_size, and syncs the log.
5807 *
5808 * 3) If we are logging that an ancestor inode exists as part of
5809 * logging a new name from a link or rename operation, don't update
5810 * its last_log_commit - otherwise if an explicit fsync is made
5811 * against an ancestor, the fsync considers the inode in the log
5812 * and doesn't sync the log, resulting in the ancestor missing after
5813 * a power failure unless the log was synced as part of an fsync
5814 * against any other unrelated inode.
Filipe Mananad1d832a2019-06-07 11:25:24 +01005815 */
Filipe Manana130341b2021-08-31 15:30:34 +01005816 if (inode_only != LOG_INODE_EXISTS)
5817 inode->last_log_commit = inode->last_sub_trans;
5818 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005819out_unlock:
Nikolay Borisova59108a2017-01-18 00:31:48 +02005820 mutex_unlock(&inode->log_mutex);
Filipe Mananaf6df27d2021-08-31 15:30:40 +01005821out:
Chris Masone02119d2008-09-05 16:13:11 -04005822 btrfs_free_path(path);
5823 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005824 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005825}
5826
Chris Mason12fcfd22009-03-24 10:24:20 -04005827/*
Filipe Mananaab123132021-01-27 10:34:56 +00005828 * Check if we need to log an inode. This is used in contexts where while
5829 * logging an inode we need to log another inode (either that it exists or in
5830 * full mode). This is used instead of btrfs_inode_in_log() because the later
5831 * requires the inode to be in the log and have the log transaction committed,
5832 * while here we do not care if the log transaction was already committed - our
5833 * caller will commit the log later - and we want to avoid logging an inode
5834 * multiple times when multiple tasks have joined the same log transaction.
5835 */
5836static bool need_log_inode(struct btrfs_trans_handle *trans,
5837 struct btrfs_inode *inode)
5838{
5839 /*
Filipe Manana8be2ba22021-07-29 18:52:46 +01005840 * If a directory was not modified, no dentries added or removed, we can
5841 * and should avoid logging it.
5842 */
5843 if (S_ISDIR(inode->vfs_inode.i_mode) && inode->last_trans < trans->transid)
5844 return false;
5845
5846 /*
Filipe Mananaab123132021-01-27 10:34:56 +00005847 * If this inode does not have new/updated/deleted xattrs since the last
5848 * time it was logged and is flagged as logged in the current transaction,
5849 * we can skip logging it. As for new/deleted names, those are updated in
5850 * the log by link/unlink/rename operations.
5851 * In case the inode was logged and then evicted and reloaded, its
5852 * logged_trans will be 0, in which case we have to fully log it since
5853 * logged_trans is a transient field, not persisted.
5854 */
5855 if (inode->logged_trans == trans->transid &&
5856 !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags))
5857 return false;
5858
5859 return true;
5860}
5861
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005862struct btrfs_dir_list {
5863 u64 ino;
5864 struct list_head list;
5865};
5866
5867/*
5868 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5869 * details about the why it is needed.
5870 * This is a recursive operation - if an existing dentry corresponds to a
5871 * directory, that directory's new entries are logged too (same behaviour as
5872 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5873 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5874 * complains about the following circular lock dependency / possible deadlock:
5875 *
5876 * CPU0 CPU1
5877 * ---- ----
5878 * lock(&type->i_mutex_dir_key#3/2);
5879 * lock(sb_internal#2);
5880 * lock(&type->i_mutex_dir_key#3/2);
5881 * lock(&sb->s_type->i_mutex_key#14);
5882 *
5883 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5884 * sb_start_intwrite() in btrfs_start_transaction().
5885 * Not locking i_mutex of the inodes is still safe because:
5886 *
5887 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5888 * that while logging the inode new references (names) are added or removed
5889 * from the inode, leaving the logged inode item with a link count that does
5890 * not match the number of logged inode reference items. This is fine because
5891 * at log replay time we compute the real number of links and correct the
5892 * link count in the inode item (see replay_one_buffer() and
5893 * link_to_fixup_dir());
5894 *
5895 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5896 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5897 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5898 * has a size that doesn't match the sum of the lengths of all the logged
5899 * names. This does not result in a problem because if a dir_item key is
5900 * logged but its matching dir_index key is not logged, at log replay time we
5901 * don't use it to replay the respective name (see replay_one_name()). On the
5902 * other hand if only the dir_index key ends up being logged, the respective
5903 * name is added to the fs/subvol tree with both the dir_item and dir_index
5904 * keys created (see replay_one_name()).
5905 * The directory's inode item with a wrong i_size is not a problem as well,
5906 * since we don't use it at log replay time to set the i_size in the inode
5907 * item of the fs/subvol tree (see overwrite_item()).
5908 */
5909static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5910 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005911 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005912 struct btrfs_log_ctx *ctx)
5913{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005914 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005915 struct btrfs_root *log = root->log_root;
5916 struct btrfs_path *path;
5917 LIST_HEAD(dir_list);
5918 struct btrfs_dir_list *dir_elem;
5919 int ret = 0;
5920
Filipe Mananac48792c2021-08-31 15:30:33 +01005921 /*
5922 * If we are logging a new name, as part of a link or rename operation,
5923 * don't bother logging new dentries, as we just want to log the names
5924 * of an inode and that any new parents exist.
5925 */
5926 if (ctx->logging_new_name)
5927 return 0;
5928
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005929 path = btrfs_alloc_path();
5930 if (!path)
5931 return -ENOMEM;
5932
5933 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5934 if (!dir_elem) {
5935 btrfs_free_path(path);
5936 return -ENOMEM;
5937 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005938 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005939 list_add_tail(&dir_elem->list, &dir_list);
5940
5941 while (!list_empty(&dir_list)) {
5942 struct extent_buffer *leaf;
5943 struct btrfs_key min_key;
5944 int nritems;
5945 int i;
5946
5947 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5948 list);
5949 if (ret)
5950 goto next_dir_inode;
5951
5952 min_key.objectid = dir_elem->ino;
5953 min_key.type = BTRFS_DIR_ITEM_KEY;
5954 min_key.offset = 0;
5955again:
5956 btrfs_release_path(path);
5957 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5958 if (ret < 0) {
5959 goto next_dir_inode;
5960 } else if (ret > 0) {
5961 ret = 0;
5962 goto next_dir_inode;
5963 }
5964
5965process_leaf:
5966 leaf = path->nodes[0];
5967 nritems = btrfs_header_nritems(leaf);
5968 for (i = path->slots[0]; i < nritems; i++) {
5969 struct btrfs_dir_item *di;
5970 struct btrfs_key di_key;
5971 struct inode *di_inode;
5972 struct btrfs_dir_list *new_dir_elem;
5973 int log_mode = LOG_INODE_EXISTS;
5974 int type;
5975
5976 btrfs_item_key_to_cpu(leaf, &min_key, i);
5977 if (min_key.objectid != dir_elem->ino ||
5978 min_key.type != BTRFS_DIR_ITEM_KEY)
5979 goto next_dir_inode;
5980
5981 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5982 type = btrfs_dir_type(leaf, di);
5983 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5984 type != BTRFS_FT_DIR)
5985 continue;
5986 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5987 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5988 continue;
5989
Robbie Koec125cf2016-10-28 10:48:26 +08005990 btrfs_release_path(path);
David Sterba0202e832020-05-15 19:35:59 +02005991 di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005992 if (IS_ERR(di_inode)) {
5993 ret = PTR_ERR(di_inode);
5994 goto next_dir_inode;
5995 }
5996
Filipe Manana0e44cb32021-01-27 10:34:58 +00005997 if (!need_log_inode(trans, BTRFS_I(di_inode))) {
Filipe Manana410f9542019-09-10 15:26:49 +01005998 btrfs_add_delayed_iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005999 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006000 }
6001
6002 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01006003 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006004 log_mode = LOG_INODE_ALL;
Filipe Manana90d04512021-09-16 11:32:10 +01006005 ret = btrfs_log_inode(trans, BTRFS_I(di_inode),
Filipe Manana48778172020-08-11 12:43:58 +01006006 log_mode, ctx);
Filipe Manana410f9542019-09-10 15:26:49 +01006007 btrfs_add_delayed_iput(di_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006008 if (ret)
6009 goto next_dir_inode;
6010 if (ctx->log_new_dentries) {
6011 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
6012 GFP_NOFS);
6013 if (!new_dir_elem) {
6014 ret = -ENOMEM;
6015 goto next_dir_inode;
6016 }
6017 new_dir_elem->ino = di_key.objectid;
6018 list_add_tail(&new_dir_elem->list, &dir_list);
6019 }
6020 break;
6021 }
6022 if (i == nritems) {
6023 ret = btrfs_next_leaf(log, path);
6024 if (ret < 0) {
6025 goto next_dir_inode;
6026 } else if (ret > 0) {
6027 ret = 0;
6028 goto next_dir_inode;
6029 }
6030 goto process_leaf;
6031 }
6032 if (min_key.offset < (u64)-1) {
6033 min_key.offset++;
6034 goto again;
6035 }
6036next_dir_inode:
6037 list_del(&dir_elem->list);
6038 kfree(dir_elem);
6039 }
6040
6041 btrfs_free_path(path);
6042 return ret;
6043}
6044
Filipe Manana18aa0922015-08-05 16:49:08 +01006045static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02006046 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01006047 struct btrfs_log_ctx *ctx)
6048{
David Sterba3ffbd682018-06-29 10:56:42 +02006049 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana18aa0922015-08-05 16:49:08 +01006050 int ret;
6051 struct btrfs_path *path;
6052 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02006053 struct btrfs_root *root = inode->root;
6054 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01006055
6056 path = btrfs_alloc_path();
6057 if (!path)
6058 return -ENOMEM;
6059 path->skip_locking = 1;
6060 path->search_commit_root = 1;
6061
6062 key.objectid = ino;
6063 key.type = BTRFS_INODE_REF_KEY;
6064 key.offset = 0;
6065 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6066 if (ret < 0)
6067 goto out;
6068
6069 while (true) {
6070 struct extent_buffer *leaf = path->nodes[0];
6071 int slot = path->slots[0];
6072 u32 cur_offset = 0;
6073 u32 item_size;
6074 unsigned long ptr;
6075
6076 if (slot >= btrfs_header_nritems(leaf)) {
6077 ret = btrfs_next_leaf(root, path);
6078 if (ret < 0)
6079 goto out;
6080 else if (ret > 0)
6081 break;
6082 continue;
6083 }
6084
6085 btrfs_item_key_to_cpu(leaf, &key, slot);
6086 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
6087 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
6088 break;
6089
6090 item_size = btrfs_item_size_nr(leaf, slot);
6091 ptr = btrfs_item_ptr_offset(leaf, slot);
6092 while (cur_offset < item_size) {
6093 struct btrfs_key inode_key;
6094 struct inode *dir_inode;
6095
6096 inode_key.type = BTRFS_INODE_ITEM_KEY;
6097 inode_key.offset = 0;
6098
6099 if (key.type == BTRFS_INODE_EXTREF_KEY) {
6100 struct btrfs_inode_extref *extref;
6101
6102 extref = (struct btrfs_inode_extref *)
6103 (ptr + cur_offset);
6104 inode_key.objectid = btrfs_inode_extref_parent(
6105 leaf, extref);
6106 cur_offset += sizeof(*extref);
6107 cur_offset += btrfs_inode_extref_name_len(leaf,
6108 extref);
6109 } else {
6110 inode_key.objectid = key.offset;
6111 cur_offset = item_size;
6112 }
6113
David Sterba0202e832020-05-15 19:35:59 +02006114 dir_inode = btrfs_iget(fs_info->sb, inode_key.objectid,
6115 root);
Filipe Manana0f375ee2018-10-09 15:05:29 +01006116 /*
6117 * If the parent inode was deleted, return an error to
6118 * fallback to a transaction commit. This is to prevent
6119 * getting an inode that was moved from one parent A to
6120 * a parent B, got its former parent A deleted and then
6121 * it got fsync'ed, from existing at both parents after
6122 * a log replay (and the old parent still existing).
6123 * Example:
6124 *
6125 * mkdir /mnt/A
6126 * mkdir /mnt/B
6127 * touch /mnt/B/bar
6128 * sync
6129 * mv /mnt/B/bar /mnt/A/bar
6130 * mv -T /mnt/A /mnt/B
6131 * fsync /mnt/B/bar
6132 * <power fail>
6133 *
6134 * If we ignore the old parent B which got deleted,
6135 * after a log replay we would have file bar linked
6136 * at both parents and the old parent B would still
6137 * exist.
6138 */
6139 if (IS_ERR(dir_inode)) {
6140 ret = PTR_ERR(dir_inode);
6141 goto out;
6142 }
Filipe Manana18aa0922015-08-05 16:49:08 +01006143
Filipe Manana3e6a86a2021-01-27 10:34:57 +00006144 if (!need_log_inode(trans, BTRFS_I(dir_inode))) {
6145 btrfs_add_delayed_iput(dir_inode);
6146 continue;
6147 }
6148
Filipe Manana289cffc2021-08-31 15:30:32 +01006149 ctx->log_new_dentries = false;
Filipe Manana90d04512021-09-16 11:32:10 +01006150 ret = btrfs_log_inode(trans, BTRFS_I(dir_inode),
Filipe Manana48778172020-08-11 12:43:58 +01006151 LOG_INODE_ALL, ctx);
Filipe Manana289cffc2021-08-31 15:30:32 +01006152 if (!ret && ctx->log_new_dentries)
Filipe Manana657ed1a2016-04-06 17:11:56 +01006153 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01006154 BTRFS_I(dir_inode), ctx);
Filipe Manana410f9542019-09-10 15:26:49 +01006155 btrfs_add_delayed_iput(dir_inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01006156 if (ret)
6157 goto out;
6158 }
6159 path->slots[0]++;
6160 }
6161 ret = 0;
6162out:
6163 btrfs_free_path(path);
6164 return ret;
6165}
6166
Filipe Mananab8aa3302019-04-17 11:31:06 +01006167static int log_new_ancestors(struct btrfs_trans_handle *trans,
6168 struct btrfs_root *root,
6169 struct btrfs_path *path,
6170 struct btrfs_log_ctx *ctx)
6171{
6172 struct btrfs_key found_key;
6173
6174 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
6175
6176 while (true) {
6177 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananab8aa3302019-04-17 11:31:06 +01006178 struct extent_buffer *leaf = path->nodes[0];
6179 int slot = path->slots[0];
6180 struct btrfs_key search_key;
6181 struct inode *inode;
David Sterba0202e832020-05-15 19:35:59 +02006182 u64 ino;
Filipe Mananab8aa3302019-04-17 11:31:06 +01006183 int ret = 0;
6184
6185 btrfs_release_path(path);
6186
David Sterba0202e832020-05-15 19:35:59 +02006187 ino = found_key.offset;
6188
Filipe Mananab8aa3302019-04-17 11:31:06 +01006189 search_key.objectid = found_key.offset;
6190 search_key.type = BTRFS_INODE_ITEM_KEY;
6191 search_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02006192 inode = btrfs_iget(fs_info->sb, ino, root);
Filipe Mananab8aa3302019-04-17 11:31:06 +01006193 if (IS_ERR(inode))
6194 return PTR_ERR(inode);
6195
Filipe Mananaab123132021-01-27 10:34:56 +00006196 if (BTRFS_I(inode)->generation >= trans->transid &&
6197 need_log_inode(trans, BTRFS_I(inode)))
Filipe Manana90d04512021-09-16 11:32:10 +01006198 ret = btrfs_log_inode(trans, BTRFS_I(inode),
Filipe Manana48778172020-08-11 12:43:58 +01006199 LOG_INODE_EXISTS, ctx);
Filipe Manana410f9542019-09-10 15:26:49 +01006200 btrfs_add_delayed_iput(inode);
Filipe Mananab8aa3302019-04-17 11:31:06 +01006201 if (ret)
6202 return ret;
6203
6204 if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
6205 break;
6206
6207 search_key.type = BTRFS_INODE_REF_KEY;
6208 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
6209 if (ret < 0)
6210 return ret;
6211
6212 leaf = path->nodes[0];
6213 slot = path->slots[0];
6214 if (slot >= btrfs_header_nritems(leaf)) {
6215 ret = btrfs_next_leaf(root, path);
6216 if (ret < 0)
6217 return ret;
6218 else if (ret > 0)
6219 return -ENOENT;
6220 leaf = path->nodes[0];
6221 slot = path->slots[0];
6222 }
6223
6224 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6225 if (found_key.objectid != search_key.objectid ||
6226 found_key.type != BTRFS_INODE_REF_KEY)
6227 return -ENOENT;
6228 }
6229 return 0;
6230}
6231
6232static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
6233 struct btrfs_inode *inode,
6234 struct dentry *parent,
6235 struct btrfs_log_ctx *ctx)
6236{
6237 struct btrfs_root *root = inode->root;
Filipe Mananab8aa3302019-04-17 11:31:06 +01006238 struct dentry *old_parent = NULL;
6239 struct super_block *sb = inode->vfs_inode.i_sb;
6240 int ret = 0;
6241
6242 while (true) {
6243 if (!parent || d_really_is_negative(parent) ||
6244 sb != parent->d_sb)
6245 break;
6246
6247 inode = BTRFS_I(d_inode(parent));
6248 if (root != inode->root)
6249 break;
6250
Filipe Mananaab123132021-01-27 10:34:56 +00006251 if (inode->generation >= trans->transid &&
6252 need_log_inode(trans, inode)) {
Filipe Manana90d04512021-09-16 11:32:10 +01006253 ret = btrfs_log_inode(trans, inode,
Filipe Manana48778172020-08-11 12:43:58 +01006254 LOG_INODE_EXISTS, ctx);
Filipe Mananab8aa3302019-04-17 11:31:06 +01006255 if (ret)
6256 break;
6257 }
6258 if (IS_ROOT(parent))
6259 break;
6260
6261 parent = dget_parent(parent);
6262 dput(old_parent);
6263 old_parent = parent;
6264 }
6265 dput(old_parent);
6266
6267 return ret;
6268}
6269
6270static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
6271 struct btrfs_inode *inode,
6272 struct dentry *parent,
6273 struct btrfs_log_ctx *ctx)
6274{
6275 struct btrfs_root *root = inode->root;
6276 const u64 ino = btrfs_ino(inode);
6277 struct btrfs_path *path;
6278 struct btrfs_key search_key;
6279 int ret;
6280
6281 /*
6282 * For a single hard link case, go through a fast path that does not
6283 * need to iterate the fs/subvolume tree.
6284 */
6285 if (inode->vfs_inode.i_nlink < 2)
6286 return log_new_ancestors_fast(trans, inode, parent, ctx);
6287
6288 path = btrfs_alloc_path();
6289 if (!path)
6290 return -ENOMEM;
6291
6292 search_key.objectid = ino;
6293 search_key.type = BTRFS_INODE_REF_KEY;
6294 search_key.offset = 0;
6295again:
6296 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
6297 if (ret < 0)
6298 goto out;
6299 if (ret == 0)
6300 path->slots[0]++;
6301
6302 while (true) {
6303 struct extent_buffer *leaf = path->nodes[0];
6304 int slot = path->slots[0];
6305 struct btrfs_key found_key;
6306
6307 if (slot >= btrfs_header_nritems(leaf)) {
6308 ret = btrfs_next_leaf(root, path);
6309 if (ret < 0)
6310 goto out;
6311 else if (ret > 0)
6312 break;
6313 continue;
6314 }
6315
6316 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6317 if (found_key.objectid != ino ||
6318 found_key.type > BTRFS_INODE_EXTREF_KEY)
6319 break;
6320
6321 /*
6322 * Don't deal with extended references because they are rare
6323 * cases and too complex to deal with (we would need to keep
6324 * track of which subitem we are processing for each item in
6325 * this loop, etc). So just return some error to fallback to
6326 * a transaction commit.
6327 */
6328 if (found_key.type == BTRFS_INODE_EXTREF_KEY) {
6329 ret = -EMLINK;
6330 goto out;
6331 }
6332
6333 /*
6334 * Logging ancestors needs to do more searches on the fs/subvol
6335 * tree, so it releases the path as needed to avoid deadlocks.
6336 * Keep track of the last inode ref key and resume from that key
6337 * after logging all new ancestors for the current hard link.
6338 */
6339 memcpy(&search_key, &found_key, sizeof(search_key));
6340
6341 ret = log_new_ancestors(trans, root, path, ctx);
6342 if (ret)
6343 goto out;
6344 btrfs_release_path(path);
6345 goto again;
6346 }
6347 ret = 0;
6348out:
6349 btrfs_free_path(path);
6350 return ret;
6351}
6352
Chris Masone02119d2008-09-05 16:13:11 -04006353/*
6354 * helper function around btrfs_log_inode to make sure newly created
6355 * parent directories also end up in the log. A minimal inode and backref
6356 * only logging is done of any parent directories that are older than
6357 * the last committed transaction
6358 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006359static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006360 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01006361 struct dentry *parent,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07006362 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08006363 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04006364{
Nikolay Borisovf8822742018-02-27 17:37:17 +02006365 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006366 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason12fcfd22009-03-24 10:24:20 -04006367 int ret = 0;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006368 bool log_dentries = false;
Chris Mason12fcfd22009-03-24 10:24:20 -04006369
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006370 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04006371 ret = 1;
6372 goto end_no_trans;
6373 }
6374
Nikolay Borisovf8822742018-02-27 17:37:17 +02006375 if (btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04006376 ret = 1;
6377 goto end_no_trans;
6378 }
6379
Filipe Mananaf2d72f42018-10-08 11:12:55 +01006380 /*
6381 * Skip already logged inodes or inodes corresponding to tmpfiles
6382 * (since logging them is pointless, a link count of 0 means they
6383 * will never be accessible).
6384 */
Filipe Manana626e9f42021-04-27 11:27:20 +01006385 if ((btrfs_inode_in_log(inode, trans->transid) &&
6386 list_empty(&ctx->ordered_extents)) ||
Filipe Mananaf2d72f42018-10-08 11:12:55 +01006387 inode->vfs_inode.i_nlink == 0) {
Chris Mason257c62e2009-10-13 13:21:08 -04006388 ret = BTRFS_NO_LOG_SYNC;
6389 goto end_no_trans;
6390 }
6391
Miao Xie8b050d32014-02-20 18:08:58 +08006392 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006393 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08006394 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04006395
Filipe Manana90d04512021-09-16 11:32:10 +01006396 ret = btrfs_log_inode(trans, inode, inode_only, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006397 if (ret)
6398 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04006399
Chris Masonaf4176b2009-03-24 10:24:31 -04006400 /*
6401 * for regular files, if its inode is already on disk, we don't
6402 * have to worry about the parents at all. This is because
6403 * we can use the last_unlink_trans field to record renames
6404 * and other fun in this file.
6405 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006406 if (S_ISREG(inode->vfs_inode.i_mode) &&
Filipe Manana47d3db42020-11-25 12:19:26 +00006407 inode->generation < trans->transid &&
6408 inode->last_unlink_trans < trans->transid) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006409 ret = 0;
6410 goto end_trans;
6411 }
Chris Masonaf4176b2009-03-24 10:24:31 -04006412
Filipe Manana289cffc2021-08-31 15:30:32 +01006413 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006414 log_dentries = true;
6415
Filipe Manana18aa0922015-08-05 16:49:08 +01006416 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006417 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01006418 * inodes are fully logged. This is to prevent leaving dangling
6419 * directory index entries in directories that were our parents but are
6420 * not anymore. Not doing this results in old parent directory being
6421 * impossible to delete after log replay (rmdir will always fail with
6422 * error -ENOTEMPTY).
6423 *
6424 * Example 1:
6425 *
6426 * mkdir testdir
6427 * touch testdir/foo
6428 * ln testdir/foo testdir/bar
6429 * sync
6430 * unlink testdir/bar
6431 * xfs_io -c fsync testdir/foo
6432 * <power failure>
6433 * mount fs, triggers log replay
6434 *
6435 * If we don't log the parent directory (testdir), after log replay the
6436 * directory still has an entry pointing to the file inode using the bar
6437 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
6438 * the file inode has a link count of 1.
6439 *
6440 * Example 2:
6441 *
6442 * mkdir testdir
6443 * touch foo
6444 * ln foo testdir/foo2
6445 * ln foo testdir/foo3
6446 * sync
6447 * unlink testdir/foo3
6448 * xfs_io -c fsync foo
6449 * <power failure>
6450 * mount fs, triggers log replay
6451 *
6452 * Similar as the first example, after log replay the parent directory
6453 * testdir still has an entry pointing to the inode file with name foo3
6454 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
6455 * and has a link count of 2.
6456 */
Filipe Manana47d3db42020-11-25 12:19:26 +00006457 if (inode->last_unlink_trans >= trans->transid) {
Filipe Mananab8aa3302019-04-17 11:31:06 +01006458 ret = btrfs_log_all_parents(trans, inode, ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01006459 if (ret)
6460 goto end_trans;
6461 }
6462
Filipe Mananab8aa3302019-04-17 11:31:06 +01006463 ret = log_all_new_ancestors(trans, inode, parent, ctx);
6464 if (ret)
Filipe Manana41bd6062018-11-28 14:54:28 +00006465 goto end_trans;
Filipe Manana41bd6062018-11-28 14:54:28 +00006466
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006467 if (log_dentries)
Filipe Mananab8aa3302019-04-17 11:31:06 +01006468 ret = log_new_dir_dentries(trans, root, inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006469 else
6470 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006471end_trans:
6472 if (ret < 0) {
David Sterba90787762019-03-20 13:28:05 +01006473 btrfs_set_log_full_commit(trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006474 ret = 1;
6475 }
Miao Xie8b050d32014-02-20 18:08:58 +08006476
6477 if (ret)
6478 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04006479 btrfs_end_log_trans(root);
6480end_no_trans:
6481 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006482}
6483
6484/*
6485 * it is not safe to log dentry if the chunk root has added new
6486 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
6487 * If this returns 1, you must commit the transaction to safely get your
6488 * data on disk.
6489 */
6490int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Nikolay Borisove5b84f7a2018-02-27 17:37:18 +02006491 struct dentry *dentry,
Miao Xie8b050d32014-02-20 18:08:58 +08006492 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04006493{
Josef Bacik6a912212010-11-20 09:48:00 +00006494 struct dentry *parent = dget_parent(dentry);
6495 int ret;
6496
Nikolay Borisovf8822742018-02-27 17:37:17 +02006497 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
Filipe Manana48778172020-08-11 12:43:58 +01006498 LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00006499 dput(parent);
6500
6501 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006502}
6503
6504/*
6505 * should be called during mount to recover any replay any log trees
6506 * from the FS
6507 */
6508int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
6509{
6510 int ret;
6511 struct btrfs_path *path;
6512 struct btrfs_trans_handle *trans;
6513 struct btrfs_key key;
6514 struct btrfs_key found_key;
Chris Masone02119d2008-09-05 16:13:11 -04006515 struct btrfs_root *log;
6516 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
6517 struct walk_control wc = {
6518 .process_func = process_one_buffer,
David Sterba430a6622019-08-01 14:50:35 +02006519 .stage = LOG_WALK_PIN_ONLY,
Chris Masone02119d2008-09-05 16:13:11 -04006520 };
6521
Chris Masone02119d2008-09-05 16:13:11 -04006522 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006523 if (!path)
6524 return -ENOMEM;
6525
Josef Bacikafcdd122016-09-02 15:40:02 -04006526 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04006527
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006528 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006529 if (IS_ERR(trans)) {
6530 ret = PTR_ERR(trans);
6531 goto error;
6532 }
Chris Masone02119d2008-09-05 16:13:11 -04006533
6534 wc.trans = trans;
6535 wc.pin = 1;
6536
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006537 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006538 if (ret) {
Josef Bacikba51e2a2021-10-05 16:35:23 -04006539 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006540 goto error;
6541 }
Chris Masone02119d2008-09-05 16:13:11 -04006542
6543again:
6544 key.objectid = BTRFS_TREE_LOG_OBJECTID;
6545 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02006546 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04006547
Chris Masond3977122009-01-05 21:25:51 -05006548 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04006549 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006550
6551 if (ret < 0) {
Josef Bacikba51e2a2021-10-05 16:35:23 -04006552 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006553 goto error;
6554 }
Chris Masone02119d2008-09-05 16:13:11 -04006555 if (ret > 0) {
6556 if (path->slots[0] == 0)
6557 break;
6558 path->slots[0]--;
6559 }
6560 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
6561 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02006562 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006563 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
6564 break;
6565
Josef Bacik62a2c732020-01-24 09:32:21 -05006566 log = btrfs_read_tree_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006567 if (IS_ERR(log)) {
6568 ret = PTR_ERR(log);
Josef Bacikba51e2a2021-10-05 16:35:23 -04006569 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006570 goto error;
6571 }
Chris Masone02119d2008-09-05 16:13:11 -04006572
David Sterba56e93572020-05-15 19:35:55 +02006573 wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset,
6574 true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006575 if (IS_ERR(wc.replay_dest)) {
6576 ret = PTR_ERR(wc.replay_dest);
Josef Bacik9bc574d2019-12-06 09:37:17 -05006577
6578 /*
6579 * We didn't find the subvol, likely because it was
6580 * deleted. This is ok, simply skip this log and go to
6581 * the next one.
6582 *
6583 * We need to exclude the root because we can't have
6584 * other log replays overwriting this log as we'll read
6585 * it back in a few more times. This will keep our
6586 * block from being modified, and we'll just bail for
6587 * each subsequent pass.
6588 */
6589 if (ret == -ENOENT)
Nikolay Borisov9fce5702020-01-20 16:09:13 +02006590 ret = btrfs_pin_extent_for_log_replay(trans,
Josef Bacik9bc574d2019-12-06 09:37:17 -05006591 log->node->start,
6592 log->node->len);
Josef Bacik00246522020-01-24 09:33:01 -05006593 btrfs_put_root(log);
Josef Bacik9bc574d2019-12-06 09:37:17 -05006594
6595 if (!ret)
6596 goto next;
Josef Bacikba51e2a2021-10-05 16:35:23 -04006597 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006598 goto error;
6599 }
Chris Masone02119d2008-09-05 16:13:11 -04006600
Yan Zheng07d400a2009-01-06 11:42:00 -05006601 wc.replay_dest->log_root = log;
Josef Bacik2002ae12021-03-12 15:25:05 -05006602 ret = btrfs_record_root_in_trans(trans, wc.replay_dest);
6603 if (ret)
6604 /* The loop needs to continue due to the root refs */
Josef Bacikba51e2a2021-10-05 16:35:23 -04006605 btrfs_abort_transaction(trans, ret);
Josef Bacik2002ae12021-03-12 15:25:05 -05006606 else
6607 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04006608
Josef Bacikb50c6e22013-04-25 15:55:30 -04006609 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04006610 ret = fixup_inode_link_counts(trans, wc.replay_dest,
6611 path);
Josef Bacikba51e2a2021-10-05 16:35:23 -04006612 if (ret)
6613 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04006614 }
Chris Masone02119d2008-09-05 16:13:11 -04006615
Liu Bo900c9982018-01-25 11:02:56 -07006616 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
6617 struct btrfs_root *root = wc.replay_dest;
6618
6619 btrfs_release_path(path);
6620
6621 /*
6622 * We have just replayed everything, and the highest
6623 * objectid of fs roots probably has changed in case
6624 * some inode_item's got replayed.
6625 *
6626 * root->objectid_mutex is not acquired as log replay
6627 * could only happen during mount.
6628 */
Nikolay Borisov453e4872020-12-07 17:32:32 +02006629 ret = btrfs_init_root_free_objectid(root);
Josef Bacikba51e2a2021-10-05 16:35:23 -04006630 if (ret)
6631 btrfs_abort_transaction(trans, ret);
Liu Bo900c9982018-01-25 11:02:56 -07006632 }
6633
Yan Zheng07d400a2009-01-06 11:42:00 -05006634 wc.replay_dest->log_root = NULL;
Josef Bacik00246522020-01-24 09:33:01 -05006635 btrfs_put_root(wc.replay_dest);
Josef Bacik00246522020-01-24 09:33:01 -05006636 btrfs_put_root(log);
Chris Masone02119d2008-09-05 16:13:11 -04006637
Josef Bacikb50c6e22013-04-25 15:55:30 -04006638 if (ret)
6639 goto error;
Josef Bacik9bc574d2019-12-06 09:37:17 -05006640next:
Chris Masone02119d2008-09-05 16:13:11 -04006641 if (found_key.offset == 0)
6642 break;
Josef Bacik9bc574d2019-12-06 09:37:17 -05006643 key.offset = found_key.offset - 1;
Chris Masone02119d2008-09-05 16:13:11 -04006644 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006645 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006646
6647 /* step one is to pin it all, step two is to replay just inodes */
6648 if (wc.pin) {
6649 wc.pin = 0;
6650 wc.process_func = replay_one_buffer;
6651 wc.stage = LOG_WALK_REPLAY_INODES;
6652 goto again;
6653 }
6654 /* step three is to replay everything */
6655 if (wc.stage < LOG_WALK_REPLAY_ALL) {
6656 wc.stage++;
6657 goto again;
6658 }
6659
6660 btrfs_free_path(path);
6661
Josef Bacikabefa552013-04-24 16:40:05 -04006662 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006663 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04006664 if (ret)
6665 return ret;
6666
Chris Masone02119d2008-09-05 16:13:11 -04006667 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04006668 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Josef Bacik00246522020-01-24 09:33:01 -05006669 btrfs_put_root(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006670
Josef Bacikabefa552013-04-24 16:40:05 -04006671 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006672error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04006673 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006674 btrfs_end_transaction(wc.trans);
David Sterba1aeb6b52020-07-07 18:38:05 +02006675 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006676 btrfs_free_path(path);
6677 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006678}
Chris Mason12fcfd22009-03-24 10:24:20 -04006679
6680/*
6681 * there are some corner cases where we want to force a full
6682 * commit instead of allowing a directory to be logged.
6683 *
6684 * They revolve around files there were unlinked from the directory, and
6685 * this function updates the parent directory so that a full commit is
6686 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00006687 *
6688 * Must be called before the unlink operations (updates to the subvolume tree,
6689 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04006690 */
6691void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006692 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04006693 int for_rename)
6694{
6695 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006696 * when we're logging a file, if it hasn't been renamed
6697 * or unlinked, and its inode is fully committed on disk,
6698 * we don't have to worry about walking up the directory chain
6699 * to log its parents.
6700 *
6701 * So, we use the last_unlink_trans field to put this transid
6702 * into the file. When the file is logged we check it and
6703 * don't log the parents if the file is fully on disk.
6704 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006705 mutex_lock(&inode->log_mutex);
6706 inode->last_unlink_trans = trans->transid;
6707 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04006708
6709 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006710 * if this directory was already logged any new
6711 * names for this file/dir will get recorded
6712 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006713 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006714 return;
6715
6716 /*
6717 * if the inode we're about to unlink was logged,
6718 * the log will be properly updated for any new names
6719 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006720 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006721 return;
6722
6723 /*
6724 * when renaming files across directories, if the directory
6725 * there we're unlinking from gets fsync'd later on, there's
6726 * no way to find the destination directory later and fsync it
6727 * properly. So, we have to be conservative and force commits
6728 * so the new name gets discovered.
6729 */
6730 if (for_rename)
6731 goto record;
6732
6733 /* we can safely do the unlink without any special recording */
6734 return;
6735
6736record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006737 mutex_lock(&dir->log_mutex);
6738 dir->last_unlink_trans = trans->transid;
6739 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04006740}
6741
6742/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006743 * Make sure that if someone attempts to fsync the parent directory of a deleted
6744 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6745 * that after replaying the log tree of the parent directory's root we will not
6746 * see the snapshot anymore and at log replay time we will not see any log tree
6747 * corresponding to the deleted snapshot's root, which could lead to replaying
6748 * it after replaying the log tree of the parent directory (which would replay
6749 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00006750 *
6751 * Must be called before the actual snapshot destroy operation (updates to the
6752 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006753 */
6754void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02006755 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006756{
Nikolay Borisov43663552017-01-18 00:31:29 +02006757 mutex_lock(&dir->log_mutex);
6758 dir->last_unlink_trans = trans->transid;
6759 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006760}
6761
6762/*
Chris Mason12fcfd22009-03-24 10:24:20 -04006763 * Call this after adding a new name for a file and it will properly
6764 * update the log to reflect the new name.
Chris Mason12fcfd22009-03-24 10:24:20 -04006765 */
Filipe Manana75b463d2020-08-11 12:43:48 +01006766void btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006767 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Filipe Manana75b463d2020-08-11 12:43:48 +01006768 struct dentry *parent)
Chris Mason12fcfd22009-03-24 10:24:20 -04006769{
Filipe Manana75b463d2020-08-11 12:43:48 +01006770 struct btrfs_log_ctx ctx;
Chris Mason12fcfd22009-03-24 10:24:20 -04006771
6772 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006773 * this will force the logging code to walk the dentry chain
6774 * up for the file
6775 */
Filipe Manana9a6509c2018-02-28 15:55:40 +00006776 if (!S_ISDIR(inode->vfs_inode.i_mode))
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006777 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04006778
6779 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006780 * if this inode hasn't been logged and directory we're renaming it
6781 * from hasn't been logged, we don't need to log it
6782 */
Filipe Mananaecc64fa2021-07-27 11:24:43 +01006783 if (!inode_logged(trans, inode) &&
6784 (!old_dir || !inode_logged(trans, old_dir)))
Filipe Manana75b463d2020-08-11 12:43:48 +01006785 return;
Chris Mason12fcfd22009-03-24 10:24:20 -04006786
Filipe Manana54a40fc2021-05-12 16:27:16 +01006787 /*
6788 * If we are doing a rename (old_dir is not NULL) from a directory that
6789 * was previously logged, make sure the next log attempt on the directory
6790 * is not skipped and logs the inode again. This is because the log may
6791 * not currently be authoritative for a range including the old
6792 * BTRFS_DIR_ITEM_KEY and BTRFS_DIR_INDEX_KEY keys, so we want to make
6793 * sure after a log replay we do not end up with both the new and old
6794 * dentries around (in case the inode is a directory we would have a
6795 * directory with two hard links and 2 inode references for different
6796 * parents). The next log attempt of old_dir will happen at
6797 * btrfs_log_all_parents(), called through btrfs_log_inode_parent()
6798 * below, because we have previously set inode->last_unlink_trans to the
6799 * current transaction ID, either here or at btrfs_record_unlink_dir() in
6800 * case inode is a directory.
6801 */
6802 if (old_dir)
6803 old_dir->logged_trans = 0;
6804
Filipe Manana75b463d2020-08-11 12:43:48 +01006805 btrfs_init_log_ctx(&ctx, &inode->vfs_inode);
6806 ctx.logging_new_name = true;
6807 /*
6808 * We don't care about the return value. If we fail to log the new name
6809 * then we know the next attempt to sync the log will fallback to a full
6810 * transaction commit (due to a call to btrfs_set_log_full_commit()), so
6811 * we don't need to worry about getting a log committed that has an
6812 * inconsistent state after a rename operation.
6813 */
Filipe Manana48778172020-08-11 12:43:58 +01006814 btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04006815}
6816