blob: b1f97be57bb3540d449465189fd851d4a613db5d [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"
Liu Bo900c9982018-01-25 11:02:56 -070020#include "inode-map.h"
Nikolay Borisov6787bb92020-01-20 16:09:10 +020021#include "block-group.h"
22#include "space-info.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,
Nikolay Borisova59108a2017-01-18 00:31:48 +020097 struct btrfs_root *root, 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);
Chris Masone02119d2008-09-05 16:13:11 -0400108
109/*
110 * tree logging is a special write ahead log used to make sure that
111 * fsyncs and O_SYNCs can happen without doing full tree commits.
112 *
113 * Full tree commits are expensive because they require commonly
114 * modified blocks to be recowed, creating many dirty pages in the
115 * extent tree an 4x-6x higher write load than ext3.
116 *
117 * Instead of doing a tree commit on every fsync, we use the
118 * key ranges and transaction ids to find items for a given file or directory
119 * that have changed in this transaction. Those items are copied into
120 * a special tree (one per subvolume root), that tree is written to disk
121 * and then the fsync is considered complete.
122 *
123 * After a crash, items are copied out of the log-tree back into the
124 * subvolume tree. Any file data extents found are recorded in the extent
125 * allocation tree, and the log-tree freed.
126 *
127 * The log tree is read three times, once to pin down all the extents it is
128 * using in ram and once, once to create all the inodes logged in the tree
129 * and once to do all the other items.
130 */
131
132/*
Chris Masone02119d2008-09-05 16:13:11 -0400133 * start a sub transaction and setup the log tree
134 * this increments the log tree writer count to make the people
135 * syncing the tree wait for us to finish
136 */
137static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800138 struct btrfs_root *root,
139 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400140{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400141 struct btrfs_fs_info *fs_info = root->fs_info;
Zhaolei34eb2a52015-08-17 18:44:45 +0800142 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500143
144 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800145
Yan Zheng7237f182009-01-21 12:54:03 -0500146 if (root->log_root) {
David Sterba4884b8e2019-03-20 13:25:34 +0100147 if (btrfs_need_log_full_commit(trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800148 ret = -EAGAIN;
149 goto out;
150 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800151
Josef Bacikff782e02009-10-08 15:30:04 -0400152 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800153 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800154 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400155 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800156 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400157 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800158 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400159 mutex_lock(&fs_info->tree_log_mutex);
160 if (!fs_info->log_root_tree)
161 ret = btrfs_init_log_root_tree(trans, fs_info);
162 mutex_unlock(&fs_info->tree_log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800163 if (ret)
164 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400165
Chris Masone02119d2008-09-05 16:13:11 -0400166 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400167 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800168 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800169
Filipe Mananae7a79812020-06-15 10:38:44 +0100170 set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800171 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
172 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400173 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800174
Miao Xie2ecb7922012-09-06 04:04:27 -0600175 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500176 atomic_inc(&root->log_writers);
Filipe Manana75b463d2020-08-11 12:43:48 +0100177 if (ctx && !ctx->logging_new_name) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800178 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800179 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800180 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800181 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800182
Miao Xiee87ac132014-02-20 18:08:53 +0800183out:
Yan Zheng7237f182009-01-21 12:54:03 -0500184 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800185 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400186}
187
188/*
189 * returns 0 if there was a log transaction running and we were able
190 * to join, or returns -ENOENT if there were not transactions
191 * in progress
192 */
193static int join_running_log_trans(struct btrfs_root *root)
194{
195 int ret = -ENOENT;
196
Filipe Mananae7a79812020-06-15 10:38:44 +0100197 if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
198 return ret;
199
Yan Zheng7237f182009-01-21 12:54:03 -0500200 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400201 if (root->log_root) {
202 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500203 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400204 }
Yan Zheng7237f182009-01-21 12:54:03 -0500205 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400206 return ret;
207}
208
209/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400210 * This either makes the current running log transaction wait
211 * until you call btrfs_end_log_trans() or it makes any future
212 * log transactions wait until you call btrfs_end_log_trans()
213 */
zhong jiang45128b02018-08-17 00:37:15 +0800214void btrfs_pin_log_trans(struct btrfs_root *root)
Chris Mason12fcfd22009-03-24 10:24:20 -0400215{
Chris Mason12fcfd22009-03-24 10:24:20 -0400216 atomic_inc(&root->log_writers);
Chris Mason12fcfd22009-03-24 10:24:20 -0400217}
218
219/*
Chris Masone02119d2008-09-05 16:13:11 -0400220 * indicate we're done making changes to the log tree
221 * and wake up anyone waiting to do a sync
222 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100223void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400224{
Yan Zheng7237f182009-01-21 12:54:03 -0500225 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +0100226 /* atomic_dec_and_test implies a barrier */
227 cond_wake_up_nomb(&root->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -0500228 }
Chris Masone02119d2008-09-05 16:13:11 -0400229}
230
David Sterba247462a2019-03-21 20:21:05 +0100231static int btrfs_write_tree_block(struct extent_buffer *buf)
232{
233 return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start,
234 buf->start + buf->len - 1);
235}
236
237static void btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
238{
239 filemap_fdatawait_range(buf->pages[0]->mapping,
240 buf->start, buf->start + buf->len - 1);
241}
Chris Masone02119d2008-09-05 16:13:11 -0400242
243/*
244 * the walk control struct is used to pass state down the chain when
245 * processing the log tree. The stage field tells us which part
246 * of the log tree processing we are currently doing. The others
247 * are state fields used for that specific part
248 */
249struct walk_control {
250 /* should we free the extent on disk when done? This is used
251 * at transaction commit time while freeing a log tree
252 */
253 int free;
254
255 /* should we write out the extent buffer? This is used
256 * while flushing the log tree to disk during a sync
257 */
258 int write;
259
260 /* should we wait for the extent buffer io to finish? Also used
261 * while flushing the log tree to disk for a sync
262 */
263 int wait;
264
265 /* pin only walk, we record which extents on disk belong to the
266 * log trees
267 */
268 int pin;
269
270 /* what stage of the replay code we're currently in */
271 int stage;
272
Filipe Mananaf2d72f42018-10-08 11:12:55 +0100273 /*
274 * Ignore any items from the inode currently being processed. Needs
275 * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
276 * the LOG_WALK_REPLAY_INODES stage.
277 */
278 bool ignore_cur_inode;
279
Chris Masone02119d2008-09-05 16:13:11 -0400280 /* the root we are currently replaying */
281 struct btrfs_root *replay_dest;
282
283 /* the trans handle for the current replay */
284 struct btrfs_trans_handle *trans;
285
286 /* the function that gets used to process blocks we find in the
287 * tree. Note the extent_buffer might not be up to date when it is
288 * passed in, and it must be checked or read if you need the data
289 * inside it
290 */
291 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800292 struct walk_control *wc, u64 gen, int level);
Chris Masone02119d2008-09-05 16:13:11 -0400293};
294
295/*
296 * process_func used to pin down extents, write them or wait on them
297 */
298static int process_one_buffer(struct btrfs_root *log,
299 struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800300 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -0400301{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400302 struct btrfs_fs_info *fs_info = log->fs_info;
Josef Bacikb50c6e22013-04-25 15:55:30 -0400303 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400304
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400305 /*
306 * If this fs is mixed then we need to be able to process the leaves to
307 * pin down any logged extents, so we have to read the block.
308 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400309 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Qu Wenruo581c1762018-03-29 09:08:11 +0800310 ret = btrfs_read_buffer(eb, gen, level, NULL);
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400311 if (ret)
312 return ret;
313 }
314
Josef Bacikb50c6e22013-04-25 15:55:30 -0400315 if (wc->pin)
Nikolay Borisov9fce5702020-01-20 16:09:13 +0200316 ret = btrfs_pin_extent_for_log_replay(wc->trans, eb->start,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400317 eb->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400318
319 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400320 if (wc->pin && btrfs_header_level(eb) == 0)
David Sterbabcdc4282019-03-20 12:14:33 +0100321 ret = btrfs_exclude_logged_extents(eb);
Chris Masone02119d2008-09-05 16:13:11 -0400322 if (wc->write)
323 btrfs_write_tree_block(eb);
324 if (wc->wait)
325 btrfs_wait_tree_block_writeback(eb);
326 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400327 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400328}
329
330/*
331 * Item overwrite used by replay and tree logging. eb, slot and key all refer
332 * to the src data we are copying out.
333 *
334 * root is the tree we are copying into, and path is a scratch
335 * path for use in this function (it should be released on entry and
336 * will be released on exit).
337 *
338 * If the key is already in the destination tree the existing item is
339 * overwritten. If the existing item isn't big enough, it is extended.
340 * If it is too large, it is truncated.
341 *
342 * If the key isn't in the destination yet, a new item is inserted.
343 */
344static noinline int overwrite_item(struct btrfs_trans_handle *trans,
345 struct btrfs_root *root,
346 struct btrfs_path *path,
347 struct extent_buffer *eb, int slot,
348 struct btrfs_key *key)
349{
350 int ret;
351 u32 item_size;
352 u64 saved_i_size = 0;
353 int save_old_i_size = 0;
354 unsigned long src_ptr;
355 unsigned long dst_ptr;
356 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000357 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400358
359 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
360 overwrite_root = 1;
361
362 item_size = btrfs_item_size_nr(eb, slot);
363 src_ptr = btrfs_item_ptr_offset(eb, slot);
364
365 /* look for the key in the destination tree */
366 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000367 if (ret < 0)
368 return ret;
369
Chris Masone02119d2008-09-05 16:13:11 -0400370 if (ret == 0) {
371 char *src_copy;
372 char *dst_copy;
373 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
374 path->slots[0]);
375 if (dst_size != item_size)
376 goto insert;
377
378 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200379 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400380 return 0;
381 }
382 dst_copy = kmalloc(item_size, GFP_NOFS);
383 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000384 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200385 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000386 kfree(dst_copy);
387 kfree(src_copy);
388 return -ENOMEM;
389 }
Chris Masone02119d2008-09-05 16:13:11 -0400390
391 read_extent_buffer(eb, src_copy, src_ptr, item_size);
392
393 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
394 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
395 item_size);
396 ret = memcmp(dst_copy, src_copy, item_size);
397
398 kfree(dst_copy);
399 kfree(src_copy);
400 /*
401 * they have the same contents, just return, this saves
402 * us from cowing blocks in the destination tree and doing
403 * extra writes that may not have been done by a previous
404 * sync
405 */
406 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200407 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400408 return 0;
409 }
410
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000411 /*
412 * We need to load the old nbytes into the inode so when we
413 * replay the extents we've logged we get the right nbytes.
414 */
415 if (inode_item) {
416 struct btrfs_inode_item *item;
417 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400418 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000419
420 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
421 struct btrfs_inode_item);
422 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
423 item = btrfs_item_ptr(eb, slot,
424 struct btrfs_inode_item);
425 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400426
427 /*
428 * If this is a directory we need to reset the i_size to
429 * 0 so that we can set it up properly when replaying
430 * the rest of the items in this log.
431 */
432 mode = btrfs_inode_mode(eb, item);
433 if (S_ISDIR(mode))
434 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000435 }
436 } else if (inode_item) {
437 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400438 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000439
440 /*
441 * New inode, set nbytes to 0 so that the nbytes comes out
442 * properly when we replay the extents.
443 */
444 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
445 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400446
447 /*
448 * If this is a directory we need to reset the i_size to 0 so
449 * that we can set it up properly when replaying the rest of
450 * the items in this log.
451 */
452 mode = btrfs_inode_mode(eb, item);
453 if (S_ISDIR(mode))
454 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400455 }
456insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200457 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400458 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000459 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400460 ret = btrfs_insert_empty_item(trans, root, path,
461 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000462 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400463
464 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000465 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400466 u32 found_size;
467 found_size = btrfs_item_size_nr(path->nodes[0],
468 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100469 if (found_size > item_size)
David Sterba78ac4f92019-03-20 14:49:12 +0100470 btrfs_truncate_item(path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100471 else if (found_size < item_size)
David Sterbac71dd882019-03-20 14:51:10 +0100472 btrfs_extend_item(path, item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400473 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400474 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400475 }
476 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
477 path->slots[0]);
478
479 /* don't overwrite an existing inode if the generation number
480 * was logged as zero. This is done when the tree logging code
481 * is just logging an inode to make sure it exists after recovery.
482 *
483 * Also, don't overwrite i_size on directories during replay.
484 * log replay inserts and removes directory items based on the
485 * state of the tree found in the subvolume, and i_size is modified
486 * as it goes
487 */
488 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
489 struct btrfs_inode_item *src_item;
490 struct btrfs_inode_item *dst_item;
491
492 src_item = (struct btrfs_inode_item *)src_ptr;
493 dst_item = (struct btrfs_inode_item *)dst_ptr;
494
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000495 if (btrfs_inode_generation(eb, src_item) == 0) {
496 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000497 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000498
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000499 /*
500 * For regular files an ino_size == 0 is used only when
501 * logging that an inode exists, as part of a directory
502 * fsync, and the inode wasn't fsynced before. In this
503 * case don't set the size of the inode in the fs/subvol
504 * tree, otherwise we would be throwing valid data away.
505 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000506 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000507 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
David Sterba60d48e22020-04-29 15:29:53 +0200508 ino_size != 0)
509 btrfs_set_inode_size(dst_eb, dst_item, ino_size);
Chris Masone02119d2008-09-05 16:13:11 -0400510 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000511 }
Chris Masone02119d2008-09-05 16:13:11 -0400512
513 if (overwrite_root &&
514 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
515 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
516 save_old_i_size = 1;
517 saved_i_size = btrfs_inode_size(path->nodes[0],
518 dst_item);
519 }
520 }
521
522 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
523 src_ptr, item_size);
524
525 if (save_old_i_size) {
526 struct btrfs_inode_item *dst_item;
527 dst_item = (struct btrfs_inode_item *)dst_ptr;
528 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
529 }
530
531 /* make sure the generation is filled in */
532 if (key->type == BTRFS_INODE_ITEM_KEY) {
533 struct btrfs_inode_item *dst_item;
534 dst_item = (struct btrfs_inode_item *)dst_ptr;
535 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
536 btrfs_set_inode_generation(path->nodes[0], dst_item,
537 trans->transid);
538 }
539 }
540no_copy:
541 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200542 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400543 return 0;
544}
545
546/*
547 * simple helper to read an inode off the disk from a given root
548 * This can only be called for subvolume roots and not for the log
549 */
550static noinline struct inode *read_one_inode(struct btrfs_root *root,
551 u64 objectid)
552{
553 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400554
David Sterba0202e832020-05-15 19:35:59 +0200555 inode = btrfs_iget(root->fs_info->sb, objectid, root);
Al Viro2e19f1f2018-07-29 23:04:45 +0100556 if (IS_ERR(inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400557 inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -0400558 return inode;
559}
560
561/* replays a single extent in 'eb' at 'slot' with 'key' into the
562 * subvolume 'root'. path is released on entry and should be released
563 * on exit.
564 *
565 * extents in the log tree have not been allocated out of the extent
566 * tree yet. So, this completes the allocation, taking a reference
567 * as required if the extent already exists or creating a new extent
568 * if it isn't in the extent allocation tree yet.
569 *
570 * The extent is inserted into the file, dropping any existing extents
571 * from the file that overlap the new one.
572 */
573static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
574 struct btrfs_root *root,
575 struct btrfs_path *path,
576 struct extent_buffer *eb, int slot,
577 struct btrfs_key *key)
578{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400579 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400580 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400581 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400582 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000583 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400584 struct btrfs_file_extent_item *item;
585 struct inode *inode = NULL;
586 unsigned long size;
587 int ret = 0;
588
589 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
590 found_type = btrfs_file_extent_type(eb, item);
591
Yan Zhengd899e052008-10-30 14:25:28 -0400592 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000593 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
594 nbytes = btrfs_file_extent_num_bytes(eb, item);
595 extent_end = start + nbytes;
596
597 /*
598 * We don't add to the inodes nbytes if we are prealloc or a
599 * hole.
600 */
601 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
602 nbytes = 0;
603 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +0800604 size = btrfs_file_extent_ram_bytes(eb, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000605 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400606 extent_end = ALIGN(start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400607 fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400608 } else {
609 ret = 0;
610 goto out;
611 }
612
613 inode = read_one_inode(root, key->objectid);
614 if (!inode) {
615 ret = -EIO;
616 goto out;
617 }
618
619 /*
620 * first check to see if we already have this extent in the
621 * file. This must be done before the btrfs_drop_extents run
622 * so we don't try to drop this extent.
623 */
David Sterbaf85b7372017-01-20 14:54:07 +0100624 ret = btrfs_lookup_file_extent(trans, root, path,
625 btrfs_ino(BTRFS_I(inode)), start, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400626
Yan Zhengd899e052008-10-30 14:25:28 -0400627 if (ret == 0 &&
628 (found_type == BTRFS_FILE_EXTENT_REG ||
629 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400630 struct btrfs_file_extent_item cmp1;
631 struct btrfs_file_extent_item cmp2;
632 struct btrfs_file_extent_item *existing;
633 struct extent_buffer *leaf;
634
635 leaf = path->nodes[0];
636 existing = btrfs_item_ptr(leaf, path->slots[0],
637 struct btrfs_file_extent_item);
638
639 read_extent_buffer(eb, &cmp1, (unsigned long)item,
640 sizeof(cmp1));
641 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
642 sizeof(cmp2));
643
644 /*
645 * we already have a pointer to this exact extent,
646 * we don't have to do anything
647 */
648 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200649 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400650 goto out;
651 }
652 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200653 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400654
655 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400656 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400657 if (ret)
658 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400659
Yan Zheng07d400a2009-01-06 11:42:00 -0500660 if (found_type == BTRFS_FILE_EXTENT_REG ||
661 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400662 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500663 unsigned long dest_offset;
664 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400665
Filipe Manana3168021c2017-02-01 14:58:02 +0000666 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
667 btrfs_fs_incompat(fs_info, NO_HOLES))
668 goto update_inode;
669
Yan Zheng07d400a2009-01-06 11:42:00 -0500670 ret = btrfs_insert_empty_item(trans, root, path, key,
671 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400672 if (ret)
673 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500674 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
675 path->slots[0]);
676 copy_extent_buffer(path->nodes[0], eb, dest_offset,
677 (unsigned long)item, sizeof(*item));
678
679 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
680 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
681 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400682 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500683
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800684 /*
685 * Manually record dirty extent, as here we did a shallow
686 * file extent item copy and skip normal backref update,
687 * but modifying extent tree all by ourselves.
688 * So need to manually record dirty extent for qgroup,
689 * as the owner of the file extent changed from log tree
690 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
691 */
Lu Fengqia95f3aa2018-07-18 16:28:03 +0800692 ret = btrfs_qgroup_trace_extent(trans,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800693 btrfs_file_extent_disk_bytenr(eb, item),
694 btrfs_file_extent_disk_num_bytes(eb, item),
695 GFP_NOFS);
696 if (ret < 0)
697 goto out;
698
Yan Zheng07d400a2009-01-06 11:42:00 -0500699 if (ins.objectid > 0) {
Qu Wenruo82fa1132019-04-04 14:45:35 +0800700 struct btrfs_ref ref = { 0 };
Yan Zheng07d400a2009-01-06 11:42:00 -0500701 u64 csum_start;
702 u64 csum_end;
703 LIST_HEAD(ordered_sums);
Qu Wenruo82fa1132019-04-04 14:45:35 +0800704
Yan Zheng07d400a2009-01-06 11:42:00 -0500705 /*
706 * is this extent already allocated in the extent
707 * allocation tree? If so, just add a reference
708 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400709 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500710 ins.offset);
711 if (ret == 0) {
Qu Wenruo82fa1132019-04-04 14:45:35 +0800712 btrfs_init_generic_ref(&ref,
713 BTRFS_ADD_DELAYED_REF,
714 ins.objectid, ins.offset, 0);
715 btrfs_init_data_ref(&ref,
716 root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100717 key->objectid, offset);
Qu Wenruo82fa1132019-04-04 14:45:35 +0800718 ret = btrfs_inc_extent_ref(trans, &ref);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400719 if (ret)
720 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500721 } else {
722 /*
723 * insert the extent pointer in the extent
724 * allocation tree
725 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400726 ret = btrfs_alloc_logged_file_extent(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400727 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400728 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400729 if (ret)
730 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500731 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200732 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500733
734 if (btrfs_file_extent_compression(eb, item)) {
735 csum_start = ins.objectid;
736 csum_end = csum_start + ins.offset;
737 } else {
738 csum_start = ins.objectid +
739 btrfs_file_extent_offset(eb, item);
740 csum_end = csum_start +
741 btrfs_file_extent_num_bytes(eb, item);
742 }
743
744 ret = btrfs_lookup_csums_range(root->log_root,
745 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100746 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400747 if (ret)
748 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100749 /*
750 * Now delete all existing cums in the csum root that
751 * cover our range. We do this because we can have an
752 * extent that is completely referenced by one file
753 * extent item and partially referenced by another
754 * file extent item (like after using the clone or
755 * extent_same ioctls). In this case if we end up doing
756 * the replay of the one that partially references the
757 * extent first, and we do not do the csum deletion
758 * below, we can get 2 csum items in the csum tree that
759 * overlap each other. For example, imagine our log has
760 * the two following file extent items:
761 *
762 * key (257 EXTENT_DATA 409600)
763 * extent data disk byte 12845056 nr 102400
764 * extent data offset 20480 nr 20480 ram 102400
765 *
766 * key (257 EXTENT_DATA 819200)
767 * extent data disk byte 12845056 nr 102400
768 * extent data offset 0 nr 102400 ram 102400
769 *
770 * Where the second one fully references the 100K extent
771 * that starts at disk byte 12845056, and the log tree
772 * has a single csum item that covers the entire range
773 * of the extent:
774 *
775 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
776 *
777 * After the first file extent item is replayed, the
778 * csum tree gets the following csum item:
779 *
780 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
781 *
782 * Which covers the 20K sub-range starting at offset 20K
783 * of our extent. Now when we replay the second file
784 * extent item, if we do not delete existing csum items
785 * that cover any of its blocks, we end up getting two
786 * csum items in our csum tree that overlap each other:
787 *
788 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
789 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
790 *
791 * Which is a problem, because after this anyone trying
792 * to lookup up for the checksum of any block of our
793 * extent starting at an offset of 40K or higher, will
794 * end up looking at the second csum item only, which
795 * does not contain the checksum for any block starting
796 * at offset 40K or higher of our extent.
797 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500798 while (!list_empty(&ordered_sums)) {
799 struct btrfs_ordered_sum *sums;
800 sums = list_entry(ordered_sums.next,
801 struct btrfs_ordered_sum,
802 list);
Josef Bacik36508602013-04-25 16:23:32 -0400803 if (!ret)
Filipe Manana40e046a2019-12-05 16:58:30 +0000804 ret = btrfs_del_csums(trans,
805 fs_info->csum_root,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400806 sums->bytenr,
807 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100808 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400809 ret = btrfs_csum_file_blocks(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400810 fs_info->csum_root, sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500811 list_del(&sums->list);
812 kfree(sums);
813 }
Josef Bacik36508602013-04-25 16:23:32 -0400814 if (ret)
815 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500816 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200817 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500818 }
819 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
820 /* inline extents are easy, we just overwrite them */
821 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400822 if (ret)
823 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500824 }
825
Josef Bacik9ddc9592020-01-17 09:02:22 -0500826 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start,
827 extent_end - start);
828 if (ret)
829 goto out;
830
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000831 inode_add_bytes(inode, nbytes);
Filipe Manana3168021c2017-02-01 14:58:02 +0000832update_inode:
Tsutomu Itohb9959292012-06-25 21:25:22 -0600833 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400834out:
835 if (inode)
836 iput(inode);
837 return ret;
838}
839
840/*
841 * when cleaning up conflicts between the directory names in the
842 * subvolume, directory names in the log and directory names in the
843 * inode back references, we may have to unlink inodes from directories.
844 *
845 * This is a helper function to do the unlink of a specific directory
846 * item
847 */
848static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
849 struct btrfs_root *root,
850 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200851 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400852 struct btrfs_dir_item *di)
853{
854 struct inode *inode;
855 char *name;
856 int name_len;
857 struct extent_buffer *leaf;
858 struct btrfs_key location;
859 int ret;
860
861 leaf = path->nodes[0];
862
863 btrfs_dir_item_key_to_cpu(leaf, di, &location);
864 name_len = btrfs_dir_name_len(leaf, di);
865 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000866 if (!name)
867 return -ENOMEM;
868
Chris Masone02119d2008-09-05 16:13:11 -0400869 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200870 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400871
872 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000873 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400874 ret = -EIO;
875 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000876 }
Chris Masone02119d2008-09-05 16:13:11 -0400877
Yan Zhengec051c02009-01-05 15:43:42 -0500878 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400879 if (ret)
880 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400881
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200882 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
883 name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400884 if (ret)
885 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100886 else
Nikolay Borisove5c304e62018-02-07 17:55:43 +0200887 ret = btrfs_run_delayed_items(trans);
Josef Bacik36508602013-04-25 16:23:32 -0400888out:
889 kfree(name);
890 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400891 return ret;
892}
893
894/*
895 * helper function to see if a given name and sequence number found
896 * in an inode back reference are already in a directory and correctly
897 * point to this inode
898 */
899static noinline int inode_in_dir(struct btrfs_root *root,
900 struct btrfs_path *path,
901 u64 dirid, u64 objectid, u64 index,
902 const char *name, int name_len)
903{
904 struct btrfs_dir_item *di;
905 struct btrfs_key location;
906 int match = 0;
907
908 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
909 index, name, name_len, 0);
910 if (di && !IS_ERR(di)) {
911 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
912 if (location.objectid != objectid)
913 goto out;
914 } else
915 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200916 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400917
918 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
919 if (di && !IS_ERR(di)) {
920 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
921 if (location.objectid != objectid)
922 goto out;
923 } else
924 goto out;
925 match = 1;
926out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200927 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400928 return match;
929}
930
931/*
932 * helper function to check a log tree for a named back reference in
933 * an inode. This is used to decide if a back reference that is
934 * found in the subvolume conflicts with what we find in the log.
935 *
936 * inode backreferences may have multiple refs in a single item,
937 * during replay we process one reference at a time, and we don't
938 * want to delete valid links to a file from the subvolume if that
939 * link is also in the log.
940 */
941static noinline int backref_in_log(struct btrfs_root *log,
942 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700943 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000944 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400945{
946 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400947 int ret;
Chris Masone02119d2008-09-05 16:13:11 -0400948
949 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000950 if (!path)
951 return -ENOMEM;
952
Chris Masone02119d2008-09-05 16:13:11 -0400953 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
Nikolay Borisovd3316c82019-09-25 14:03:03 +0300954 if (ret < 0) {
955 goto out;
956 } else if (ret == 1) {
Nikolay Borisov89cbf5f6b2019-08-30 17:44:47 +0300957 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400958 goto out;
Nikolay Borisov89cbf5f6b2019-08-30 17:44:47 +0300959 }
Chris Masone02119d2008-09-05 16:13:11 -0400960
Nikolay Borisov89cbf5f6b2019-08-30 17:44:47 +0300961 if (key->type == BTRFS_INODE_EXTREF_KEY)
962 ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
963 path->slots[0],
964 ref_objectid,
965 name, namelen);
966 else
967 ret = !!btrfs_find_name_in_backref(path->nodes[0],
Filipe Manana1f250e92018-02-28 15:56:10 +0000968 path->slots[0],
Nikolay Borisov89cbf5f6b2019-08-30 17:44:47 +0300969 name, namelen);
Chris Masone02119d2008-09-05 16:13:11 -0400970out:
971 btrfs_free_path(path);
Nikolay Borisov89cbf5f6b2019-08-30 17:44:47 +0300972 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400973}
974
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700975static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
976 struct btrfs_root *root,
977 struct btrfs_path *path,
978 struct btrfs_root *log_root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +0200979 struct btrfs_inode *dir,
980 struct btrfs_inode *inode,
Mark Fashehf1863732012-08-08 11:32:27 -0700981 u64 inode_objectid, u64 parent_objectid,
982 u64 ref_index, char *name, int namelen,
983 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700984{
985 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700986 char *victim_name;
987 int victim_name_len;
988 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700989 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700990 struct btrfs_key search_key;
991 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700992
Mark Fashehf1863732012-08-08 11:32:27 -0700993again:
994 /* Search old style refs */
995 search_key.objectid = inode_objectid;
996 search_key.type = BTRFS_INODE_REF_KEY;
997 search_key.offset = parent_objectid;
998 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700999 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001000 struct btrfs_inode_ref *victim_ref;
1001 unsigned long ptr;
1002 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001003
1004 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001005
1006 /* are we trying to overwrite a back ref for the root directory
1007 * if so, just jump out, we're done
1008 */
Mark Fashehf1863732012-08-08 11:32:27 -07001009 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001010 return 1;
1011
1012 /* check all the names in this back reference to see
1013 * if they are in the log. if so, we allow them to stay
1014 * otherwise they must be unlinked as a conflict
1015 */
1016 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1017 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1018 while (ptr < ptr_end) {
1019 victim_ref = (struct btrfs_inode_ref *)ptr;
1020 victim_name_len = btrfs_inode_ref_name_len(leaf,
1021 victim_ref);
1022 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001023 if (!victim_name)
1024 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001025
1026 read_extent_buffer(leaf, victim_name,
1027 (unsigned long)(victim_ref + 1),
1028 victim_name_len);
1029
Nikolay Borisovd3316c82019-09-25 14:03:03 +03001030 ret = backref_in_log(log_root, &search_key,
1031 parent_objectid, victim_name,
1032 victim_name_len);
1033 if (ret < 0) {
1034 kfree(victim_name);
1035 return ret;
1036 } else if (!ret) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001037 inc_nlink(&inode->vfs_inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001038 btrfs_release_path(path);
1039
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001040 ret = btrfs_unlink_inode(trans, root, dir, inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001041 victim_name, victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001042 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001043 if (ret)
1044 return ret;
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001045 ret = btrfs_run_delayed_items(trans);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001046 if (ret)
1047 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001048 *search_done = 1;
1049 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001050 }
1051 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001052
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001053 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1054 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001055
1056 /*
1057 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001058 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001059 */
1060 *search_done = 1;
1061 }
1062 btrfs_release_path(path);
1063
Mark Fashehf1863732012-08-08 11:32:27 -07001064 /* Same search but for extended refs */
1065 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1066 inode_objectid, parent_objectid, 0,
1067 0);
1068 if (!IS_ERR_OR_NULL(extref)) {
1069 u32 item_size;
1070 u32 cur_offset = 0;
1071 unsigned long base;
1072 struct inode *victim_parent;
1073
1074 leaf = path->nodes[0];
1075
1076 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1077 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1078
1079 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001080 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001081
1082 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1083
1084 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1085 goto next;
1086
1087 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001088 if (!victim_name)
1089 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001090 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1091 victim_name_len);
1092
1093 search_key.objectid = inode_objectid;
1094 search_key.type = BTRFS_INODE_EXTREF_KEY;
1095 search_key.offset = btrfs_extref_hash(parent_objectid,
1096 victim_name,
1097 victim_name_len);
Nikolay Borisovd3316c82019-09-25 14:03:03 +03001098 ret = backref_in_log(log_root, &search_key,
1099 parent_objectid, victim_name,
1100 victim_name_len);
1101 if (ret < 0) {
1102 return ret;
1103 } else if (!ret) {
Mark Fashehf1863732012-08-08 11:32:27 -07001104 ret = -ENOENT;
1105 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001106 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001107 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001108 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001109 btrfs_release_path(path);
1110
1111 ret = btrfs_unlink_inode(trans, root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001112 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001113 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001114 victim_name,
1115 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001116 if (!ret)
1117 ret = btrfs_run_delayed_items(
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001118 trans);
Mark Fashehf1863732012-08-08 11:32:27 -07001119 }
Mark Fashehf1863732012-08-08 11:32:27 -07001120 iput(victim_parent);
1121 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001122 if (ret)
1123 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001124 *search_done = 1;
1125 goto again;
1126 }
1127 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001128next:
1129 cur_offset += victim_name_len + sizeof(*extref);
1130 }
1131 *search_done = 1;
1132 }
1133 btrfs_release_path(path);
1134
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001135 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001136 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001137 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001138 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001139 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001140 if (ret)
1141 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001142 }
1143 btrfs_release_path(path);
1144
Andrea Gelmini52042d82018-11-28 12:05:13 +01001145 /* look for a conflicting name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001146 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001147 name, namelen, 0);
1148 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001149 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001150 if (ret)
1151 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001152 }
1153 btrfs_release_path(path);
1154
1155 return 0;
1156}
Chris Masone02119d2008-09-05 16:13:11 -04001157
Qu Wenruobae15d92017-11-08 08:54:26 +08001158static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1159 u32 *namelen, char **name, u64 *index,
1160 u64 *parent_objectid)
Mark Fashehf1863732012-08-08 11:32:27 -07001161{
1162 struct btrfs_inode_extref *extref;
1163
1164 extref = (struct btrfs_inode_extref *)ref_ptr;
1165
1166 *namelen = btrfs_inode_extref_name_len(eb, extref);
1167 *name = kmalloc(*namelen, GFP_NOFS);
1168 if (*name == NULL)
1169 return -ENOMEM;
1170
1171 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1172 *namelen);
1173
Filipe Manana1f250e92018-02-28 15:56:10 +00001174 if (index)
1175 *index = btrfs_inode_extref_index(eb, extref);
Mark Fashehf1863732012-08-08 11:32:27 -07001176 if (parent_objectid)
1177 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1178
1179 return 0;
1180}
1181
Qu Wenruobae15d92017-11-08 08:54:26 +08001182static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1183 u32 *namelen, char **name, u64 *index)
Mark Fashehf1863732012-08-08 11:32:27 -07001184{
1185 struct btrfs_inode_ref *ref;
1186
1187 ref = (struct btrfs_inode_ref *)ref_ptr;
1188
1189 *namelen = btrfs_inode_ref_name_len(eb, ref);
1190 *name = kmalloc(*namelen, GFP_NOFS);
1191 if (*name == NULL)
1192 return -ENOMEM;
1193
1194 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1195
Filipe Manana1f250e92018-02-28 15:56:10 +00001196 if (index)
1197 *index = btrfs_inode_ref_index(eb, ref);
Mark Fashehf1863732012-08-08 11:32:27 -07001198
1199 return 0;
1200}
1201
Chris Masone02119d2008-09-05 16:13:11 -04001202/*
Filipe Manana1f250e92018-02-28 15:56:10 +00001203 * Take an inode reference item from the log tree and iterate all names from the
1204 * inode reference item in the subvolume tree with the same key (if it exists).
1205 * For any name that is not in the inode reference item from the log tree, do a
1206 * proper unlink of that name (that is, remove its entry from the inode
1207 * reference item and both dir index keys).
1208 */
1209static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1210 struct btrfs_root *root,
1211 struct btrfs_path *path,
1212 struct btrfs_inode *inode,
1213 struct extent_buffer *log_eb,
1214 int log_slot,
1215 struct btrfs_key *key)
1216{
1217 int ret;
1218 unsigned long ref_ptr;
1219 unsigned long ref_end;
1220 struct extent_buffer *eb;
1221
1222again:
1223 btrfs_release_path(path);
1224 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1225 if (ret > 0) {
1226 ret = 0;
1227 goto out;
1228 }
1229 if (ret < 0)
1230 goto out;
1231
1232 eb = path->nodes[0];
1233 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1234 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1235 while (ref_ptr < ref_end) {
1236 char *name = NULL;
1237 int namelen;
1238 u64 parent_id;
1239
1240 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1241 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1242 NULL, &parent_id);
1243 } else {
1244 parent_id = key->offset;
1245 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1246 NULL);
1247 }
1248 if (ret)
1249 goto out;
1250
1251 if (key->type == BTRFS_INODE_EXTREF_KEY)
Nikolay Borisov6ff49c62019-08-27 14:46:29 +03001252 ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot,
1253 parent_id, name,
1254 namelen);
Filipe Manana1f250e92018-02-28 15:56:10 +00001255 else
Nikolay Borisov9bb84072019-08-27 14:46:28 +03001256 ret = !!btrfs_find_name_in_backref(log_eb, log_slot,
1257 name, namelen);
Filipe Manana1f250e92018-02-28 15:56:10 +00001258
1259 if (!ret) {
1260 struct inode *dir;
1261
1262 btrfs_release_path(path);
1263 dir = read_one_inode(root, parent_id);
1264 if (!dir) {
1265 ret = -ENOENT;
1266 kfree(name);
1267 goto out;
1268 }
1269 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1270 inode, name, namelen);
1271 kfree(name);
1272 iput(dir);
1273 if (ret)
1274 goto out;
1275 goto again;
1276 }
1277
1278 kfree(name);
1279 ref_ptr += namelen;
1280 if (key->type == BTRFS_INODE_EXTREF_KEY)
1281 ref_ptr += sizeof(struct btrfs_inode_extref);
1282 else
1283 ref_ptr += sizeof(struct btrfs_inode_ref);
1284 }
1285 ret = 0;
1286 out:
1287 btrfs_release_path(path);
1288 return ret;
1289}
1290
Filipe Manana0d836392018-07-20 10:59:06 +01001291static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
1292 const u8 ref_type, const char *name,
1293 const int namelen)
1294{
1295 struct btrfs_key key;
1296 struct btrfs_path *path;
1297 const u64 parent_id = btrfs_ino(BTRFS_I(dir));
1298 int ret;
1299
1300 path = btrfs_alloc_path();
1301 if (!path)
1302 return -ENOMEM;
1303
1304 key.objectid = btrfs_ino(BTRFS_I(inode));
1305 key.type = ref_type;
1306 if (key.type == BTRFS_INODE_REF_KEY)
1307 key.offset = parent_id;
1308 else
1309 key.offset = btrfs_extref_hash(parent_id, name, namelen);
1310
1311 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
1312 if (ret < 0)
1313 goto out;
1314 if (ret > 0) {
1315 ret = 0;
1316 goto out;
1317 }
1318 if (key.type == BTRFS_INODE_EXTREF_KEY)
Nikolay Borisov6ff49c62019-08-27 14:46:29 +03001319 ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
1320 path->slots[0], parent_id, name, namelen);
Filipe Manana0d836392018-07-20 10:59:06 +01001321 else
Nikolay Borisov9bb84072019-08-27 14:46:28 +03001322 ret = !!btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
1323 name, namelen);
Filipe Manana0d836392018-07-20 10:59:06 +01001324
1325out:
1326 btrfs_free_path(path);
1327 return ret;
1328}
1329
Filipe Manana6b5fc432019-02-13 12:14:03 +00001330static int add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1331 struct inode *dir, struct inode *inode, const char *name,
1332 int namelen, u64 ref_index)
1333{
1334 struct btrfs_dir_item *dir_item;
1335 struct btrfs_key key;
1336 struct btrfs_path *path;
1337 struct inode *other_inode = NULL;
1338 int ret;
1339
1340 path = btrfs_alloc_path();
1341 if (!path)
1342 return -ENOMEM;
1343
1344 dir_item = btrfs_lookup_dir_item(NULL, root, path,
1345 btrfs_ino(BTRFS_I(dir)),
1346 name, namelen, 0);
1347 if (!dir_item) {
1348 btrfs_release_path(path);
1349 goto add_link;
1350 } else if (IS_ERR(dir_item)) {
1351 ret = PTR_ERR(dir_item);
1352 goto out;
1353 }
1354
1355 /*
1356 * Our inode's dentry collides with the dentry of another inode which is
1357 * in the log but not yet processed since it has a higher inode number.
1358 * So delete that other dentry.
1359 */
1360 btrfs_dir_item_key_to_cpu(path->nodes[0], dir_item, &key);
1361 btrfs_release_path(path);
1362 other_inode = read_one_inode(root, key.objectid);
1363 if (!other_inode) {
1364 ret = -ENOENT;
1365 goto out;
1366 }
1367 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), BTRFS_I(other_inode),
1368 name, namelen);
1369 if (ret)
1370 goto out;
1371 /*
1372 * If we dropped the link count to 0, bump it so that later the iput()
1373 * on the inode will not free it. We will fixup the link count later.
1374 */
1375 if (other_inode->i_nlink == 0)
1376 inc_nlink(other_inode);
1377
1378 ret = btrfs_run_delayed_items(trans);
1379 if (ret)
1380 goto out;
1381add_link:
1382 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
1383 name, namelen, 0, ref_index);
1384out:
1385 iput(other_inode);
1386 btrfs_free_path(path);
1387
1388 return ret;
1389}
1390
Filipe Manana1f250e92018-02-28 15:56:10 +00001391/*
Chris Masone02119d2008-09-05 16:13:11 -04001392 * replay one inode back reference item found in the log tree.
1393 * eb, slot and key refer to the buffer and key found in the log tree.
1394 * root is the destination we are replaying into, and path is for temp
1395 * use by this function. (it should be released on return).
1396 */
1397static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1398 struct btrfs_root *root,
1399 struct btrfs_root *log,
1400 struct btrfs_path *path,
1401 struct extent_buffer *eb, int slot,
1402 struct btrfs_key *key)
1403{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001404 struct inode *dir = NULL;
1405 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001406 unsigned long ref_ptr;
1407 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001408 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001409 int namelen;
1410 int ret;
liuboc622ae62011-03-26 08:01:12 -04001411 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001412 int log_ref_ver = 0;
1413 u64 parent_objectid;
1414 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001415 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001416 int ref_struct_size;
1417
1418 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1419 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1420
1421 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1422 struct btrfs_inode_extref *r;
1423
1424 ref_struct_size = sizeof(struct btrfs_inode_extref);
1425 log_ref_ver = 1;
1426 r = (struct btrfs_inode_extref *)ref_ptr;
1427 parent_objectid = btrfs_inode_extref_parent(eb, r);
1428 } else {
1429 ref_struct_size = sizeof(struct btrfs_inode_ref);
1430 parent_objectid = key->offset;
1431 }
1432 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001433
Chris Masone02119d2008-09-05 16:13:11 -04001434 /*
1435 * it is possible that we didn't log all the parent directories
1436 * for a given inode. If we don't find the dir, just don't
1437 * copy the back ref in. The link count fixup code will take
1438 * care of the rest
1439 */
Mark Fashehf1863732012-08-08 11:32:27 -07001440 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001441 if (!dir) {
1442 ret = -ENOENT;
1443 goto out;
1444 }
Chris Masone02119d2008-09-05 16:13:11 -04001445
Mark Fashehf1863732012-08-08 11:32:27 -07001446 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001447 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001448 ret = -EIO;
1449 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001450 }
Chris Masone02119d2008-09-05 16:13:11 -04001451
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001452 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001453 if (log_ref_ver) {
Qu Wenruobae15d92017-11-08 08:54:26 +08001454 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1455 &ref_index, &parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001456 /*
1457 * parent object can change from one array
1458 * item to another.
1459 */
1460 if (!dir)
1461 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001462 if (!dir) {
1463 ret = -ENOENT;
1464 goto out;
1465 }
Mark Fashehf1863732012-08-08 11:32:27 -07001466 } else {
Qu Wenruobae15d92017-11-08 08:54:26 +08001467 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1468 &ref_index);
Mark Fashehf1863732012-08-08 11:32:27 -07001469 }
1470 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001471 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001472
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001473 /* if we already have a perfect match, we're done */
David Sterbaf85b7372017-01-20 14:54:07 +01001474 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1475 btrfs_ino(BTRFS_I(inode)), ref_index,
1476 name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001477 /*
1478 * look for a conflicting back reference in the
1479 * metadata. if we find one we have to unlink that name
1480 * of the file before we add our new link. Later on, we
1481 * overwrite any existing back reference, and we don't
1482 * want to create dangling pointers in the directory.
1483 */
Chris Masone02119d2008-09-05 16:13:11 -04001484
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001485 if (!search_done) {
1486 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001487 BTRFS_I(dir),
David Sterbad75eefd2017-02-10 20:20:19 +01001488 BTRFS_I(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001489 inode_objectid,
1490 parent_objectid,
1491 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001492 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001493 if (ret) {
1494 if (ret == 1)
1495 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001496 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001497 }
Chris Masone02119d2008-09-05 16:13:11 -04001498 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001499
Filipe Manana0d836392018-07-20 10:59:06 +01001500 /*
1501 * If a reference item already exists for this inode
1502 * with the same parent and name, but different index,
1503 * drop it and the corresponding directory index entries
1504 * from the parent before adding the new reference item
1505 * and dir index entries, otherwise we would fail with
1506 * -EEXIST returned from btrfs_add_link() below.
1507 */
1508 ret = btrfs_inode_ref_exists(inode, dir, key->type,
1509 name, namelen);
1510 if (ret > 0) {
1511 ret = btrfs_unlink_inode(trans, root,
1512 BTRFS_I(dir),
1513 BTRFS_I(inode),
1514 name, namelen);
1515 /*
1516 * If we dropped the link count to 0, bump it so
1517 * that later the iput() on the inode will not
1518 * free it. We will fixup the link count later.
1519 */
1520 if (!ret && inode->i_nlink == 0)
1521 inc_nlink(inode);
1522 }
1523 if (ret < 0)
1524 goto out;
1525
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001526 /* insert our name */
Filipe Manana6b5fc432019-02-13 12:14:03 +00001527 ret = add_link(trans, root, dir, inode, name, namelen,
1528 ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001529 if (ret)
1530 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001531
1532 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001533 }
liuboc622ae62011-03-26 08:01:12 -04001534
Mark Fashehf1863732012-08-08 11:32:27 -07001535 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001536 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001537 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001538 if (log_ref_ver) {
1539 iput(dir);
1540 dir = NULL;
1541 }
Chris Masone02119d2008-09-05 16:13:11 -04001542 }
Chris Masone02119d2008-09-05 16:13:11 -04001543
Filipe Manana1f250e92018-02-28 15:56:10 +00001544 /*
1545 * Before we overwrite the inode reference item in the subvolume tree
1546 * with the item from the log tree, we must unlink all names from the
1547 * parent directory that are in the subvolume's tree inode reference
1548 * item, otherwise we end up with an inconsistent subvolume tree where
1549 * dir index entries exist for a name but there is no inode reference
1550 * item with the same name.
1551 */
1552 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1553 key);
1554 if (ret)
1555 goto out;
1556
Chris Masone02119d2008-09-05 16:13:11 -04001557 /* finally write the back reference in the inode */
1558 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001559out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001560 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001561 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001562 iput(dir);
1563 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001564 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001565}
1566
Mark Fashehf1863732012-08-08 11:32:27 -07001567static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001568 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001569{
Mark Fashehf1863732012-08-08 11:32:27 -07001570 int ret = 0;
1571 int name_len;
1572 unsigned int nlink = 0;
1573 u32 item_size;
1574 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001575 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001576 u64 offset = 0;
1577 unsigned long ptr;
1578 struct btrfs_inode_extref *extref;
1579 struct extent_buffer *leaf;
1580
1581 while (1) {
1582 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1583 &extref, &offset);
1584 if (ret)
1585 break;
1586
1587 leaf = path->nodes[0];
1588 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1589 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001590 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001591
1592 while (cur_offset < item_size) {
1593 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1594 name_len = btrfs_inode_extref_name_len(leaf, extref);
1595
1596 nlink++;
1597
1598 cur_offset += name_len + sizeof(*extref);
1599 }
1600
1601 offset++;
1602 btrfs_release_path(path);
1603 }
1604 btrfs_release_path(path);
1605
Filipe Manana2c2c4522015-01-13 16:40:04 +00001606 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001607 return ret;
1608 return nlink;
1609}
1610
1611static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001612 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001613{
Chris Masone02119d2008-09-05 16:13:11 -04001614 int ret;
1615 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001616 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001617 unsigned long ptr;
1618 unsigned long ptr_end;
1619 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001620 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001621
Li Zefan33345d012011-04-20 10:31:50 +08001622 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001623 key.type = BTRFS_INODE_REF_KEY;
1624 key.offset = (u64)-1;
1625
Chris Masond3977122009-01-05 21:25:51 -05001626 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001627 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1628 if (ret < 0)
1629 break;
1630 if (ret > 0) {
1631 if (path->slots[0] == 0)
1632 break;
1633 path->slots[0]--;
1634 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001635process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001636 btrfs_item_key_to_cpu(path->nodes[0], &key,
1637 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001638 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001639 key.type != BTRFS_INODE_REF_KEY)
1640 break;
1641 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1642 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1643 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001644 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001645 struct btrfs_inode_ref *ref;
1646
1647 ref = (struct btrfs_inode_ref *)ptr;
1648 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1649 ref);
1650 ptr = (unsigned long)(ref + 1) + name_len;
1651 nlink++;
1652 }
1653
1654 if (key.offset == 0)
1655 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001656 if (path->slots[0] > 0) {
1657 path->slots[0]--;
1658 goto process_slot;
1659 }
Chris Masone02119d2008-09-05 16:13:11 -04001660 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001661 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001662 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001663 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001664
1665 return nlink;
1666}
1667
1668/*
1669 * There are a few corners where the link count of the file can't
1670 * be properly maintained during replay. So, instead of adding
1671 * lots of complexity to the log code, we just scan the backrefs
1672 * for any file that has been through replay.
1673 *
1674 * The scan will update the link count on the inode to reflect the
1675 * number of back refs found. If it goes down to zero, the iput
1676 * will free the inode.
1677 */
1678static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1679 struct btrfs_root *root,
1680 struct inode *inode)
1681{
1682 struct btrfs_path *path;
1683 int ret;
1684 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001685 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001686
1687 path = btrfs_alloc_path();
1688 if (!path)
1689 return -ENOMEM;
1690
Nikolay Borisovf329e312017-01-18 00:31:50 +02001691 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001692 if (ret < 0)
1693 goto out;
1694
1695 nlink = ret;
1696
Nikolay Borisov36283652017-01-18 00:31:49 +02001697 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001698 if (ret < 0)
1699 goto out;
1700
1701 nlink += ret;
1702
1703 ret = 0;
1704
Chris Masone02119d2008-09-05 16:13:11 -04001705 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001706 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001707 btrfs_update_inode(trans, root, inode);
1708 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001709 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001710
Yan, Zhengc71bf092009-11-12 09:34:40 +00001711 if (inode->i_nlink == 0) {
1712 if (S_ISDIR(inode->i_mode)) {
1713 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001714 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001715 if (ret)
1716 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001717 }
Nikolay Borisovecdcf3c2020-10-22 18:40:46 +03001718 ret = btrfs_insert_orphan_item(trans, root, ino);
1719 if (ret == -EEXIST)
1720 ret = 0;
Chris Mason12fcfd22009-03-24 10:24:20 -04001721 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001722
Mark Fashehf1863732012-08-08 11:32:27 -07001723out:
1724 btrfs_free_path(path);
1725 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001726}
1727
1728static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1729 struct btrfs_root *root,
1730 struct btrfs_path *path)
1731{
1732 int ret;
1733 struct btrfs_key key;
1734 struct inode *inode;
1735
1736 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1737 key.type = BTRFS_ORPHAN_ITEM_KEY;
1738 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001739 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001740 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1741 if (ret < 0)
1742 break;
1743
1744 if (ret == 1) {
1745 if (path->slots[0] == 0)
1746 break;
1747 path->slots[0]--;
1748 }
1749
1750 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1751 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1752 key.type != BTRFS_ORPHAN_ITEM_KEY)
1753 break;
1754
1755 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001756 if (ret)
1757 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001758
David Sterbab3b4aa72011-04-21 01:20:15 +02001759 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001760 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001761 if (!inode)
1762 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001763
1764 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001765 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001766 if (ret)
1767 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001768
Chris Mason12fcfd22009-03-24 10:24:20 -04001769 /*
1770 * fixup on a directory may create new entries,
1771 * make sure we always look for the highset possible
1772 * offset
1773 */
1774 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001775 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001776 ret = 0;
1777out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001778 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001779 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001780}
1781
1782
1783/*
1784 * record a given inode in the fixup dir so we can check its link
1785 * count when replay is done. The link count is incremented here
1786 * so the inode won't go away until we check it
1787 */
1788static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1789 struct btrfs_root *root,
1790 struct btrfs_path *path,
1791 u64 objectid)
1792{
1793 struct btrfs_key key;
1794 int ret = 0;
1795 struct inode *inode;
1796
1797 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001798 if (!inode)
1799 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001800
1801 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001802 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001803 key.offset = objectid;
1804
1805 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1806
David Sterbab3b4aa72011-04-21 01:20:15 +02001807 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001808 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001809 if (!inode->i_nlink)
1810 set_nlink(inode, 1);
1811 else
Zach Brown8b558c52013-10-16 12:10:34 -07001812 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001813 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001814 } else if (ret == -EEXIST) {
1815 ret = 0;
1816 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001817 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001818 }
1819 iput(inode);
1820
1821 return ret;
1822}
1823
1824/*
1825 * when replaying the log for a directory, we only insert names
1826 * for inodes that actually exist. This means an fsync on a directory
1827 * does not implicitly fsync all the new files in it
1828 */
1829static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1830 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001831 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001832 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001833 struct btrfs_key *location)
1834{
1835 struct inode *inode;
1836 struct inode *dir;
1837 int ret;
1838
1839 inode = read_one_inode(root, location->objectid);
1840 if (!inode)
1841 return -ENOENT;
1842
1843 dir = read_one_inode(root, dirid);
1844 if (!dir) {
1845 iput(inode);
1846 return -EIO;
1847 }
Josef Bacikd5554382013-09-11 14:17:00 -04001848
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001849 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1850 name_len, 1, index);
Chris Masone02119d2008-09-05 16:13:11 -04001851
1852 /* FIXME, put inode into FIXUP list */
1853
1854 iput(inode);
1855 iput(dir);
1856 return ret;
1857}
1858
1859/*
1860 * take a single entry in a log directory item and replay it into
1861 * the subvolume.
1862 *
1863 * if a conflicting item exists in the subdirectory already,
1864 * the inode it points to is unlinked and put into the link count
1865 * fix up tree.
1866 *
1867 * If a name from the log points to a file or directory that does
1868 * not exist in the FS, it is skipped. fsyncs on directories
1869 * do not force down inodes inside that directory, just changes to the
1870 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001871 *
1872 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1873 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001874 */
1875static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1876 struct btrfs_root *root,
1877 struct btrfs_path *path,
1878 struct extent_buffer *eb,
1879 struct btrfs_dir_item *di,
1880 struct btrfs_key *key)
1881{
1882 char *name;
1883 int name_len;
1884 struct btrfs_dir_item *dst_di;
1885 struct btrfs_key found_key;
1886 struct btrfs_key log_key;
1887 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001888 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001889 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001890 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001891 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001892 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001893
1894 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001895 if (!dir)
1896 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001897
1898 name_len = btrfs_dir_name_len(eb, di);
1899 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001900 if (!name) {
1901 ret = -ENOMEM;
1902 goto out;
1903 }
liubo2a29edc2011-01-26 06:22:08 +00001904
Chris Masone02119d2008-09-05 16:13:11 -04001905 log_type = btrfs_dir_type(eb, di);
1906 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1907 name_len);
1908
1909 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001910 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1911 if (exists == 0)
1912 exists = 1;
1913 else
1914 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001915 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001916
Chris Masone02119d2008-09-05 16:13:11 -04001917 if (key->type == BTRFS_DIR_ITEM_KEY) {
1918 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1919 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001920 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001921 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1922 key->objectid,
1923 key->offset, name,
1924 name_len, 1);
1925 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001926 /* Corruption */
1927 ret = -EINVAL;
1928 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001929 }
David Sterbac7040052011-04-19 18:00:01 +02001930 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001931 /* we need a sequence number to insert, so we only
1932 * do inserts for the BTRFS_DIR_INDEX_KEY types
1933 */
1934 if (key->type != BTRFS_DIR_INDEX_KEY)
1935 goto out;
1936 goto insert;
1937 }
1938
1939 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1940 /* the existing item matches the logged item */
1941 if (found_key.objectid == log_key.objectid &&
1942 found_key.type == log_key.type &&
1943 found_key.offset == log_key.offset &&
1944 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001945 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001946 goto out;
1947 }
1948
1949 /*
1950 * don't drop the conflicting directory entry if the inode
1951 * for the new entry doesn't exist
1952 */
Chris Mason4bef0842008-09-08 11:18:08 -04001953 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001954 goto out;
1955
Nikolay Borisov207e7d92017-01-18 00:31:45 +02001956 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001957 if (ret)
1958 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001959
1960 if (key->type == BTRFS_DIR_INDEX_KEY)
1961 goto insert;
1962out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001963 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001964 if (!ret && update_size) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02001965 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
Josef Bacikd5554382013-09-11 14:17:00 -04001966 ret = btrfs_update_inode(trans, root, dir);
1967 }
Chris Masone02119d2008-09-05 16:13:11 -04001968 kfree(name);
1969 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001970 if (!ret && name_added)
1971 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001972 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001973
1974insert:
Nikolay Borisov725af922019-08-30 17:44:49 +03001975 /*
1976 * Check if the inode reference exists in the log for the given name,
1977 * inode and parent inode
1978 */
1979 found_key.objectid = log_key.objectid;
1980 found_key.type = BTRFS_INODE_REF_KEY;
1981 found_key.offset = key->objectid;
1982 ret = backref_in_log(root->log_root, &found_key, 0, name, name_len);
1983 if (ret < 0) {
1984 goto out;
1985 } else if (ret) {
1986 /* The dentry will be added later. */
1987 ret = 0;
1988 update_size = false;
1989 goto out;
1990 }
1991
1992 found_key.objectid = log_key.objectid;
1993 found_key.type = BTRFS_INODE_EXTREF_KEY;
1994 found_key.offset = key->objectid;
1995 ret = backref_in_log(root->log_root, &found_key, key->objectid, name,
1996 name_len);
1997 if (ret < 0) {
1998 goto out;
1999 } else if (ret) {
Filipe Mananadf8d1162015-01-14 01:52:25 +00002000 /* The dentry will be added later. */
2001 ret = 0;
2002 update_size = false;
2003 goto out;
2004 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002005 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08002006 ret = insert_one_name(trans, root, key->objectid, key->offset,
2007 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00002008 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04002009 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002010 if (!ret)
2011 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04002012 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04002013 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002014 goto out;
2015}
2016
2017/*
2018 * find all the names in a directory item and reconcile them into
2019 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
2020 * one name in a directory item, but the same code gets used for
2021 * both directory index types
2022 */
2023static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
2024 struct btrfs_root *root,
2025 struct btrfs_path *path,
2026 struct extent_buffer *eb, int slot,
2027 struct btrfs_key *key)
2028{
Filipe Mananabb53eda2015-07-15 23:26:43 +01002029 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002030 u32 item_size = btrfs_item_size_nr(eb, slot);
2031 struct btrfs_dir_item *di;
2032 int name_len;
2033 unsigned long ptr;
2034 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002035 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04002036
2037 ptr = btrfs_item_ptr_offset(eb, slot);
2038 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002039 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002040 di = (struct btrfs_dir_item *)ptr;
2041 name_len = btrfs_dir_name_len(eb, di);
2042 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01002043 if (ret < 0)
2044 break;
Chris Masone02119d2008-09-05 16:13:11 -04002045 ptr = (unsigned long)(di + 1);
2046 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002047
2048 /*
2049 * If this entry refers to a non-directory (directories can not
2050 * have a link count > 1) and it was added in the transaction
2051 * that was not committed, make sure we fixup the link count of
2052 * the inode it the entry points to. Otherwise something like
2053 * the following would result in a directory pointing to an
2054 * inode with a wrong link that does not account for this dir
2055 * entry:
2056 *
2057 * mkdir testdir
2058 * touch testdir/foo
2059 * touch testdir/bar
2060 * sync
2061 *
2062 * ln testdir/bar testdir/bar_link
2063 * ln testdir/foo testdir/foo_link
2064 * xfs_io -c "fsync" testdir/bar
2065 *
2066 * <power failure>
2067 *
2068 * mount fs, log replay happens
2069 *
2070 * File foo would remain with a link count of 1 when it has two
2071 * entries pointing to it in the directory testdir. This would
2072 * make it impossible to ever delete the parent directory has
2073 * it would result in stale dentries that can never be deleted.
2074 */
2075 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
2076 struct btrfs_key di_key;
2077
2078 if (!fixup_path) {
2079 fixup_path = btrfs_alloc_path();
2080 if (!fixup_path) {
2081 ret = -ENOMEM;
2082 break;
2083 }
2084 }
2085
2086 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2087 ret = link_to_fixup_dir(trans, root, fixup_path,
2088 di_key.objectid);
2089 if (ret)
2090 break;
2091 }
2092 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002093 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01002094 btrfs_free_path(fixup_path);
2095 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002096}
2097
2098/*
2099 * directory replay has two parts. There are the standard directory
2100 * items in the log copied from the subvolume, and range items
2101 * created in the log while the subvolume was logged.
2102 *
2103 * The range items tell us which parts of the key space the log
2104 * is authoritative for. During replay, if a key in the subvolume
2105 * directory is in a logged range item, but not actually in the log
2106 * that means it was deleted from the directory before the fsync
2107 * and should be removed.
2108 */
2109static noinline int find_dir_range(struct btrfs_root *root,
2110 struct btrfs_path *path,
2111 u64 dirid, int key_type,
2112 u64 *start_ret, u64 *end_ret)
2113{
2114 struct btrfs_key key;
2115 u64 found_end;
2116 struct btrfs_dir_log_item *item;
2117 int ret;
2118 int nritems;
2119
2120 if (*start_ret == (u64)-1)
2121 return 1;
2122
2123 key.objectid = dirid;
2124 key.type = key_type;
2125 key.offset = *start_ret;
2126
2127 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2128 if (ret < 0)
2129 goto out;
2130 if (ret > 0) {
2131 if (path->slots[0] == 0)
2132 goto out;
2133 path->slots[0]--;
2134 }
2135 if (ret != 0)
2136 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2137
2138 if (key.type != key_type || key.objectid != dirid) {
2139 ret = 1;
2140 goto next;
2141 }
2142 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2143 struct btrfs_dir_log_item);
2144 found_end = btrfs_dir_log_end(path->nodes[0], item);
2145
2146 if (*start_ret >= key.offset && *start_ret <= found_end) {
2147 ret = 0;
2148 *start_ret = key.offset;
2149 *end_ret = found_end;
2150 goto out;
2151 }
2152 ret = 1;
2153next:
2154 /* check the next slot in the tree to see if it is a valid item */
2155 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08002156 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04002157 if (path->slots[0] >= nritems) {
2158 ret = btrfs_next_leaf(root, path);
2159 if (ret)
2160 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002161 }
2162
2163 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2164
2165 if (key.type != key_type || key.objectid != dirid) {
2166 ret = 1;
2167 goto out;
2168 }
2169 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2170 struct btrfs_dir_log_item);
2171 found_end = btrfs_dir_log_end(path->nodes[0], item);
2172 *start_ret = key.offset;
2173 *end_ret = found_end;
2174 ret = 0;
2175out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002176 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002177 return ret;
2178}
2179
2180/*
2181 * this looks for a given directory item in the log. If the directory
2182 * item is not in the log, the item is removed and the inode it points
2183 * to is unlinked
2184 */
2185static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2186 struct btrfs_root *root,
2187 struct btrfs_root *log,
2188 struct btrfs_path *path,
2189 struct btrfs_path *log_path,
2190 struct inode *dir,
2191 struct btrfs_key *dir_key)
2192{
2193 int ret;
2194 struct extent_buffer *eb;
2195 int slot;
2196 u32 item_size;
2197 struct btrfs_dir_item *di;
2198 struct btrfs_dir_item *log_di;
2199 int name_len;
2200 unsigned long ptr;
2201 unsigned long ptr_end;
2202 char *name;
2203 struct inode *inode;
2204 struct btrfs_key location;
2205
2206again:
2207 eb = path->nodes[0];
2208 slot = path->slots[0];
2209 item_size = btrfs_item_size_nr(eb, slot);
2210 ptr = btrfs_item_ptr_offset(eb, slot);
2211 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002212 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002213 di = (struct btrfs_dir_item *)ptr;
2214 name_len = btrfs_dir_name_len(eb, di);
2215 name = kmalloc(name_len, GFP_NOFS);
2216 if (!name) {
2217 ret = -ENOMEM;
2218 goto out;
2219 }
2220 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2221 name_len);
2222 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002223 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002224 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2225 dir_key->objectid,
2226 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002227 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002228 log_di = btrfs_lookup_dir_index_item(trans, log,
2229 log_path,
2230 dir_key->objectid,
2231 dir_key->offset,
2232 name, name_len, 0);
2233 }
Al Viro8d9e2202018-07-29 23:04:46 +01002234 if (!log_di || log_di == ERR_PTR(-ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002235 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002236 btrfs_release_path(path);
2237 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002238 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002239 if (!inode) {
2240 kfree(name);
2241 return -EIO;
2242 }
Chris Masone02119d2008-09-05 16:13:11 -04002243
2244 ret = link_to_fixup_dir(trans, root,
2245 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002246 if (ret) {
2247 kfree(name);
2248 iput(inode);
2249 goto out;
2250 }
2251
Zach Brown8b558c52013-10-16 12:10:34 -07002252 inc_nlink(inode);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002253 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2254 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002255 if (!ret)
Nikolay Borisove5c304e62018-02-07 17:55:43 +02002256 ret = btrfs_run_delayed_items(trans);
Chris Masone02119d2008-09-05 16:13:11 -04002257 kfree(name);
2258 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002259 if (ret)
2260 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002261
2262 /* there might still be more names under this key
2263 * check and repeat if required
2264 */
2265 ret = btrfs_search_slot(NULL, root, dir_key, path,
2266 0, 0);
2267 if (ret == 0)
2268 goto again;
2269 ret = 0;
2270 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002271 } else if (IS_ERR(log_di)) {
2272 kfree(name);
2273 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002274 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002275 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002276 kfree(name);
2277
2278 ptr = (unsigned long)(di + 1);
2279 ptr += name_len;
2280 }
2281 ret = 0;
2282out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002283 btrfs_release_path(path);
2284 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002285 return ret;
2286}
2287
Filipe Manana4f764e52015-02-23 19:53:35 +00002288static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2289 struct btrfs_root *root,
2290 struct btrfs_root *log,
2291 struct btrfs_path *path,
2292 const u64 ino)
2293{
2294 struct btrfs_key search_key;
2295 struct btrfs_path *log_path;
2296 int i;
2297 int nritems;
2298 int ret;
2299
2300 log_path = btrfs_alloc_path();
2301 if (!log_path)
2302 return -ENOMEM;
2303
2304 search_key.objectid = ino;
2305 search_key.type = BTRFS_XATTR_ITEM_KEY;
2306 search_key.offset = 0;
2307again:
2308 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2309 if (ret < 0)
2310 goto out;
2311process_leaf:
2312 nritems = btrfs_header_nritems(path->nodes[0]);
2313 for (i = path->slots[0]; i < nritems; i++) {
2314 struct btrfs_key key;
2315 struct btrfs_dir_item *di;
2316 struct btrfs_dir_item *log_di;
2317 u32 total_size;
2318 u32 cur;
2319
2320 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2321 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2322 ret = 0;
2323 goto out;
2324 }
2325
2326 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2327 total_size = btrfs_item_size_nr(path->nodes[0], i);
2328 cur = 0;
2329 while (cur < total_size) {
2330 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2331 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2332 u32 this_len = sizeof(*di) + name_len + data_len;
2333 char *name;
2334
2335 name = kmalloc(name_len, GFP_NOFS);
2336 if (!name) {
2337 ret = -ENOMEM;
2338 goto out;
2339 }
2340 read_extent_buffer(path->nodes[0], name,
2341 (unsigned long)(di + 1), name_len);
2342
2343 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2344 name, name_len, 0);
2345 btrfs_release_path(log_path);
2346 if (!log_di) {
2347 /* Doesn't exist in log tree, so delete it. */
2348 btrfs_release_path(path);
2349 di = btrfs_lookup_xattr(trans, root, path, ino,
2350 name, name_len, -1);
2351 kfree(name);
2352 if (IS_ERR(di)) {
2353 ret = PTR_ERR(di);
2354 goto out;
2355 }
2356 ASSERT(di);
2357 ret = btrfs_delete_one_dir_name(trans, root,
2358 path, di);
2359 if (ret)
2360 goto out;
2361 btrfs_release_path(path);
2362 search_key = key;
2363 goto again;
2364 }
2365 kfree(name);
2366 if (IS_ERR(log_di)) {
2367 ret = PTR_ERR(log_di);
2368 goto out;
2369 }
2370 cur += this_len;
2371 di = (struct btrfs_dir_item *)((char *)di + this_len);
2372 }
2373 }
2374 ret = btrfs_next_leaf(root, path);
2375 if (ret > 0)
2376 ret = 0;
2377 else if (ret == 0)
2378 goto process_leaf;
2379out:
2380 btrfs_free_path(log_path);
2381 btrfs_release_path(path);
2382 return ret;
2383}
2384
2385
Chris Masone02119d2008-09-05 16:13:11 -04002386/*
2387 * deletion replay happens before we copy any new directory items
2388 * out of the log or out of backreferences from inodes. It
2389 * scans the log to find ranges of keys that log is authoritative for,
2390 * and then scans the directory to find items in those ranges that are
2391 * not present in the log.
2392 *
2393 * Anything we don't find in the log is unlinked and removed from the
2394 * directory.
2395 */
2396static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2397 struct btrfs_root *root,
2398 struct btrfs_root *log,
2399 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002400 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002401{
2402 u64 range_start;
2403 u64 range_end;
2404 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2405 int ret = 0;
2406 struct btrfs_key dir_key;
2407 struct btrfs_key found_key;
2408 struct btrfs_path *log_path;
2409 struct inode *dir;
2410
2411 dir_key.objectid = dirid;
2412 dir_key.type = BTRFS_DIR_ITEM_KEY;
2413 log_path = btrfs_alloc_path();
2414 if (!log_path)
2415 return -ENOMEM;
2416
2417 dir = read_one_inode(root, dirid);
2418 /* it isn't an error if the inode isn't there, that can happen
2419 * because we replay the deletes before we copy in the inode item
2420 * from the log
2421 */
2422 if (!dir) {
2423 btrfs_free_path(log_path);
2424 return 0;
2425 }
2426again:
2427 range_start = 0;
2428 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002429 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002430 if (del_all)
2431 range_end = (u64)-1;
2432 else {
2433 ret = find_dir_range(log, path, dirid, key_type,
2434 &range_start, &range_end);
2435 if (ret != 0)
2436 break;
2437 }
Chris Masone02119d2008-09-05 16:13:11 -04002438
2439 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002440 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002441 int nritems;
2442 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2443 0, 0);
2444 if (ret < 0)
2445 goto out;
2446
2447 nritems = btrfs_header_nritems(path->nodes[0]);
2448 if (path->slots[0] >= nritems) {
2449 ret = btrfs_next_leaf(root, path);
Liu Bob98def72018-04-03 01:59:48 +08002450 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002451 break;
Liu Bob98def72018-04-03 01:59:48 +08002452 else if (ret < 0)
2453 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002454 }
2455 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2456 path->slots[0]);
2457 if (found_key.objectid != dirid ||
2458 found_key.type != dir_key.type)
2459 goto next_type;
2460
2461 if (found_key.offset > range_end)
2462 break;
2463
2464 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002465 log_path, dir,
2466 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002467 if (ret)
2468 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002469 if (found_key.offset == (u64)-1)
2470 break;
2471 dir_key.offset = found_key.offset + 1;
2472 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002473 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002474 if (range_end == (u64)-1)
2475 break;
2476 range_start = range_end + 1;
2477 }
2478
2479next_type:
2480 ret = 0;
2481 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2482 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2483 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002484 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002485 goto again;
2486 }
2487out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002488 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002489 btrfs_free_path(log_path);
2490 iput(dir);
2491 return ret;
2492}
2493
2494/*
2495 * the process_func used to replay items from the log tree. This
2496 * gets called in two different stages. The first stage just looks
2497 * for inodes and makes sure they are all copied into the subvolume.
2498 *
2499 * The second stage copies all the other item types from the log into
2500 * the subvolume. The two stage approach is slower, but gets rid of
2501 * lots of complexity around inodes referencing other inodes that exist
2502 * only in the log (references come from either directory items or inode
2503 * back refs).
2504 */
2505static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +08002506 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -04002507{
2508 int nritems;
2509 struct btrfs_path *path;
2510 struct btrfs_root *root = wc->replay_dest;
2511 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002512 int i;
2513 int ret;
2514
Qu Wenruo581c1762018-03-29 09:08:11 +08002515 ret = btrfs_read_buffer(eb, gen, level, NULL);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002516 if (ret)
2517 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002518
2519 level = btrfs_header_level(eb);
2520
2521 if (level != 0)
2522 return 0;
2523
2524 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002525 if (!path)
2526 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002527
2528 nritems = btrfs_header_nritems(eb);
2529 for (i = 0; i < nritems; i++) {
2530 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002531
2532 /* inode keys are done during the first stage */
2533 if (key.type == BTRFS_INODE_ITEM_KEY &&
2534 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002535 struct btrfs_inode_item *inode_item;
2536 u32 mode;
2537
2538 inode_item = btrfs_item_ptr(eb, i,
2539 struct btrfs_inode_item);
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002540 /*
2541 * If we have a tmpfile (O_TMPFILE) that got fsync'ed
2542 * and never got linked before the fsync, skip it, as
2543 * replaying it is pointless since it would be deleted
2544 * later. We skip logging tmpfiles, but it's always
2545 * possible we are replaying a log created with a kernel
2546 * that used to log tmpfiles.
2547 */
2548 if (btrfs_inode_nlink(eb, inode_item) == 0) {
2549 wc->ignore_cur_inode = true;
2550 continue;
2551 } else {
2552 wc->ignore_cur_inode = false;
2553 }
Filipe Manana4f764e52015-02-23 19:53:35 +00002554 ret = replay_xattr_deletes(wc->trans, root, log,
2555 path, key.objectid);
2556 if (ret)
2557 break;
Chris Masone02119d2008-09-05 16:13:11 -04002558 mode = btrfs_inode_mode(eb, inode_item);
2559 if (S_ISDIR(mode)) {
2560 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002561 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002562 if (ret)
2563 break;
Chris Masone02119d2008-09-05 16:13:11 -04002564 }
2565 ret = overwrite_item(wc->trans, root, path,
2566 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002567 if (ret)
2568 break;
Chris Masone02119d2008-09-05 16:13:11 -04002569
Filipe Manana471d5572018-04-05 22:55:12 +01002570 /*
2571 * Before replaying extents, truncate the inode to its
2572 * size. We need to do it now and not after log replay
2573 * because before an fsync we can have prealloc extents
2574 * added beyond the inode's i_size. If we did it after,
2575 * through orphan cleanup for example, we would drop
2576 * those prealloc extents just after replaying them.
Chris Masone02119d2008-09-05 16:13:11 -04002577 */
2578 if (S_ISREG(mode)) {
Filipe Manana471d5572018-04-05 22:55:12 +01002579 struct inode *inode;
2580 u64 from;
2581
2582 inode = read_one_inode(root, key.objectid);
2583 if (!inode) {
2584 ret = -EIO;
2585 break;
2586 }
2587 from = ALIGN(i_size_read(inode),
2588 root->fs_info->sectorsize);
2589 ret = btrfs_drop_extents(wc->trans, root, inode,
2590 from, (u64)-1, 1);
Filipe Manana471d5572018-04-05 22:55:12 +01002591 if (!ret) {
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002592 /* Update the inode's nbytes. */
Filipe Manana471d5572018-04-05 22:55:12 +01002593 ret = btrfs_update_inode(wc->trans,
2594 root, inode);
2595 }
2596 iput(inode);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002597 if (ret)
2598 break;
Chris Masone02119d2008-09-05 16:13:11 -04002599 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002600
Chris Masone02119d2008-09-05 16:13:11 -04002601 ret = link_to_fixup_dir(wc->trans, root,
2602 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002603 if (ret)
2604 break;
Chris Masone02119d2008-09-05 16:13:11 -04002605 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002606
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002607 if (wc->ignore_cur_inode)
2608 continue;
2609
Josef Bacikdd8e7212013-09-11 11:57:23 -04002610 if (key.type == BTRFS_DIR_INDEX_KEY &&
2611 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2612 ret = replay_one_dir_item(wc->trans, root, path,
2613 eb, i, &key);
2614 if (ret)
2615 break;
2616 }
2617
Chris Masone02119d2008-09-05 16:13:11 -04002618 if (wc->stage < LOG_WALK_REPLAY_ALL)
2619 continue;
2620
2621 /* these keys are simply copied */
2622 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2623 ret = overwrite_item(wc->trans, root, path,
2624 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002625 if (ret)
2626 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002627 } else if (key.type == BTRFS_INODE_REF_KEY ||
2628 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002629 ret = add_inode_ref(wc->trans, root, log, path,
2630 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002631 if (ret && ret != -ENOENT)
2632 break;
2633 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002634 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2635 ret = replay_one_extent(wc->trans, root, path,
2636 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002637 if (ret)
2638 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002639 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002640 ret = replay_one_dir_item(wc->trans, root, path,
2641 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002642 if (ret)
2643 break;
Chris Masone02119d2008-09-05 16:13:11 -04002644 }
2645 }
2646 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002647 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002648}
2649
Nikolay Borisov6787bb92020-01-20 16:09:10 +02002650/*
2651 * Correctly adjust the reserved bytes occupied by a log tree extent buffer
2652 */
2653static void unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start)
2654{
2655 struct btrfs_block_group *cache;
2656
2657 cache = btrfs_lookup_block_group(fs_info, start);
2658 if (!cache) {
2659 btrfs_err(fs_info, "unable to find block group for %llu", start);
2660 return;
2661 }
2662
2663 spin_lock(&cache->space_info->lock);
2664 spin_lock(&cache->lock);
2665 cache->reserved -= fs_info->nodesize;
2666 cache->space_info->bytes_reserved -= fs_info->nodesize;
2667 spin_unlock(&cache->lock);
2668 spin_unlock(&cache->space_info->lock);
2669
2670 btrfs_put_block_group(cache);
2671}
2672
Chris Masond3977122009-01-05 21:25:51 -05002673static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002674 struct btrfs_root *root,
2675 struct btrfs_path *path, int *level,
2676 struct walk_control *wc)
2677{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002678 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002679 u64 bytenr;
2680 u64 ptr_gen;
2681 struct extent_buffer *next;
2682 struct extent_buffer *cur;
Chris Masone02119d2008-09-05 16:13:11 -04002683 u32 blocksize;
2684 int ret = 0;
2685
Chris Masond3977122009-01-05 21:25:51 -05002686 while (*level > 0) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002687 struct btrfs_key first_key;
2688
Chris Masone02119d2008-09-05 16:13:11 -04002689 cur = path->nodes[*level];
2690
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302691 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002692
2693 if (path->slots[*level] >=
2694 btrfs_header_nritems(cur))
2695 break;
2696
2697 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2698 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08002699 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002700 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002701
Josef Bacik3fbaf252020-11-05 10:45:20 -05002702 next = btrfs_find_create_tree_block(fs_info, bytenr,
2703 btrfs_header_owner(cur),
2704 *level - 1);
Liu Boc871b0f2016-06-06 12:01:23 -07002705 if (IS_ERR(next))
2706 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002707
Chris Masone02119d2008-09-05 16:13:11 -04002708 if (*level == 1) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002709 ret = wc->process_func(root, next, wc, ptr_gen,
2710 *level - 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002711 if (ret) {
2712 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002713 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002714 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002715
Chris Masone02119d2008-09-05 16:13:11 -04002716 path->slots[*level]++;
2717 if (wc->free) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002718 ret = btrfs_read_buffer(next, ptr_gen,
2719 *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002720 if (ret) {
2721 free_extent_buffer(next);
2722 return ret;
2723 }
Chris Masone02119d2008-09-05 16:13:11 -04002724
Josef Bacik681ae502013-10-07 15:11:00 -04002725 if (trans) {
2726 btrfs_tree_lock(next);
David Sterba6a884d7d2019-03-20 14:30:02 +01002727 btrfs_clean_tree_block(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002728 btrfs_wait_tree_block_writeback(next);
2729 btrfs_tree_unlock(next);
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02002730 ret = btrfs_pin_reserved_extent(trans,
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002731 bytenr, blocksize);
2732 if (ret) {
2733 free_extent_buffer(next);
2734 return ret;
2735 }
Liu Bo18464302018-01-25 11:02:51 -07002736 } else {
2737 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2738 clear_extent_buffer_dirty(next);
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002739 unaccount_log_buffer(fs_info, bytenr);
Josef Bacik36508602013-04-25 16:23:32 -04002740 }
Chris Masone02119d2008-09-05 16:13:11 -04002741 }
2742 free_extent_buffer(next);
2743 continue;
2744 }
Qu Wenruo581c1762018-03-29 09:08:11 +08002745 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002746 if (ret) {
2747 free_extent_buffer(next);
2748 return ret;
2749 }
Chris Masone02119d2008-09-05 16:13:11 -04002750
Chris Masone02119d2008-09-05 16:13:11 -04002751 if (path->nodes[*level-1])
2752 free_extent_buffer(path->nodes[*level-1]);
2753 path->nodes[*level-1] = next;
2754 *level = btrfs_header_level(next);
2755 path->slots[*level] = 0;
2756 cond_resched();
2757 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002758 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002759
2760 cond_resched();
2761 return 0;
2762}
2763
Chris Masond3977122009-01-05 21:25:51 -05002764static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002765 struct btrfs_root *root,
2766 struct btrfs_path *path, int *level,
2767 struct walk_control *wc)
2768{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002769 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002770 int i;
2771 int slot;
2772 int ret;
2773
Chris Masond3977122009-01-05 21:25:51 -05002774 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002775 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002776 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002777 path->slots[i]++;
2778 *level = i;
2779 WARN_ON(*level == 0);
2780 return 0;
2781 } else {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002782 ret = wc->process_func(root, path->nodes[*level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002783 btrfs_header_generation(path->nodes[*level]),
2784 *level);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002785 if (ret)
2786 return ret;
2787
Chris Masone02119d2008-09-05 16:13:11 -04002788 if (wc->free) {
2789 struct extent_buffer *next;
2790
2791 next = path->nodes[*level];
2792
Josef Bacik681ae502013-10-07 15:11:00 -04002793 if (trans) {
2794 btrfs_tree_lock(next);
David Sterba6a884d7d2019-03-20 14:30:02 +01002795 btrfs_clean_tree_block(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002796 btrfs_wait_tree_block_writeback(next);
2797 btrfs_tree_unlock(next);
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02002798 ret = btrfs_pin_reserved_extent(trans,
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002799 path->nodes[*level]->start,
2800 path->nodes[*level]->len);
2801 if (ret)
2802 return ret;
Liu Bo18464302018-01-25 11:02:51 -07002803 } else {
2804 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2805 clear_extent_buffer_dirty(next);
Chris Masone02119d2008-09-05 16:13:11 -04002806
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002807 unaccount_log_buffer(fs_info,
2808 path->nodes[*level]->start);
2809 }
Chris Masone02119d2008-09-05 16:13:11 -04002810 }
2811 free_extent_buffer(path->nodes[*level]);
2812 path->nodes[*level] = NULL;
2813 *level = i + 1;
2814 }
2815 }
2816 return 1;
2817}
2818
2819/*
2820 * drop the reference count on the tree rooted at 'snap'. This traverses
2821 * the tree freeing any blocks that have a ref count of zero after being
2822 * decremented.
2823 */
2824static int walk_log_tree(struct btrfs_trans_handle *trans,
2825 struct btrfs_root *log, struct walk_control *wc)
2826{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002827 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002828 int ret = 0;
2829 int wret;
2830 int level;
2831 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002832 int orig_level;
2833
2834 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002835 if (!path)
2836 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002837
2838 level = btrfs_header_level(log->node);
2839 orig_level = level;
2840 path->nodes[level] = log->node;
David Sterba67439da2019-10-08 13:28:47 +02002841 atomic_inc(&log->node->refs);
Chris Masone02119d2008-09-05 16:13:11 -04002842 path->slots[level] = 0;
2843
Chris Masond3977122009-01-05 21:25:51 -05002844 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002845 wret = walk_down_log_tree(trans, log, path, &level, wc);
2846 if (wret > 0)
2847 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002848 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002849 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002850 goto out;
2851 }
Chris Masone02119d2008-09-05 16:13:11 -04002852
2853 wret = walk_up_log_tree(trans, log, path, &level, wc);
2854 if (wret > 0)
2855 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002856 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002857 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002858 goto out;
2859 }
Chris Masone02119d2008-09-05 16:13:11 -04002860 }
2861
2862 /* was the root node processed? if not, catch it here */
2863 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002864 ret = wc->process_func(log, path->nodes[orig_level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002865 btrfs_header_generation(path->nodes[orig_level]),
2866 orig_level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002867 if (ret)
2868 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002869 if (wc->free) {
2870 struct extent_buffer *next;
2871
2872 next = path->nodes[orig_level];
2873
Josef Bacik681ae502013-10-07 15:11:00 -04002874 if (trans) {
2875 btrfs_tree_lock(next);
David Sterba6a884d7d2019-03-20 14:30:02 +01002876 btrfs_clean_tree_block(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002877 btrfs_wait_tree_block_writeback(next);
2878 btrfs_tree_unlock(next);
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02002879 ret = btrfs_pin_reserved_extent(trans,
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002880 next->start, next->len);
2881 if (ret)
2882 goto out;
Liu Bo18464302018-01-25 11:02:51 -07002883 } else {
2884 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2885 clear_extent_buffer_dirty(next);
Nikolay Borisov10e958d2020-01-20 16:09:11 +02002886 unaccount_log_buffer(fs_info, next->start);
Josef Bacik681ae502013-10-07 15:11:00 -04002887 }
Chris Masone02119d2008-09-05 16:13:11 -04002888 }
2889 }
2890
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002891out:
Chris Masone02119d2008-09-05 16:13:11 -04002892 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002893 return ret;
2894}
2895
Yan Zheng7237f182009-01-21 12:54:03 -05002896/*
2897 * helper function to update the item for a given subvolumes log root
2898 * in the tree of log roots
2899 */
2900static int update_log_root(struct btrfs_trans_handle *trans,
Josef Bacik4203e962019-09-30 16:27:25 -04002901 struct btrfs_root *log,
2902 struct btrfs_root_item *root_item)
Yan Zheng7237f182009-01-21 12:54:03 -05002903{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002904 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05002905 int ret;
2906
2907 if (log->log_transid == 1) {
2908 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002909 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Josef Bacik4203e962019-09-30 16:27:25 -04002910 &log->root_key, root_item);
Yan Zheng7237f182009-01-21 12:54:03 -05002911 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002912 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Josef Bacik4203e962019-09-30 16:27:25 -04002913 &log->root_key, root_item);
Yan Zheng7237f182009-01-21 12:54:03 -05002914 }
2915 return ret;
2916}
2917
Zhaolei60d53eb2015-08-17 18:44:46 +08002918static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002919{
2920 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002921 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002922
Yan Zheng7237f182009-01-21 12:54:03 -05002923 /*
2924 * we only allow two pending log transactions at a time,
2925 * so we know that if ours is more than 2 older than the
2926 * current transaction, we're done
2927 */
Liu Bo49e83f52017-09-01 16:14:30 -06002928 for (;;) {
Yan Zheng7237f182009-01-21 12:54:03 -05002929 prepare_to_wait(&root->log_commit_wait[index],
2930 &wait, TASK_UNINTERRUPTIBLE);
Liu Bo49e83f52017-09-01 16:14:30 -06002931
2932 if (!(root->log_transid_committed < transid &&
2933 atomic_read(&root->log_commit[index])))
2934 break;
2935
Yan Zheng7237f182009-01-21 12:54:03 -05002936 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002937 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002938 mutex_lock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002939 }
2940 finish_wait(&root->log_commit_wait[index], &wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002941}
2942
Zhaolei60d53eb2015-08-17 18:44:46 +08002943static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002944{
2945 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002946
Liu Bo49e83f52017-09-01 16:14:30 -06002947 for (;;) {
2948 prepare_to_wait(&root->log_writer_wait, &wait,
2949 TASK_UNINTERRUPTIBLE);
2950 if (!atomic_read(&root->log_writers))
2951 break;
2952
Yan Zheng7237f182009-01-21 12:54:03 -05002953 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002954 schedule();
Filipe Manana575849e2015-02-11 11:12:39 +00002955 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002956 }
Liu Bo49e83f52017-09-01 16:14:30 -06002957 finish_wait(&root->log_writer_wait, &wait);
Chris Masone02119d2008-09-05 16:13:11 -04002958}
2959
Miao Xie8b050d32014-02-20 18:08:58 +08002960static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2961 struct btrfs_log_ctx *ctx)
2962{
2963 if (!ctx)
2964 return;
2965
2966 mutex_lock(&root->log_mutex);
2967 list_del_init(&ctx->list);
2968 mutex_unlock(&root->log_mutex);
2969}
2970
2971/*
2972 * Invoked in log mutex context, or be sure there is no other task which
2973 * can access the list.
2974 */
2975static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2976 int index, int error)
2977{
2978 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002979 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002980
Chris Mason570dd452016-10-27 10:42:20 -07002981 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2982 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002983 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002984 }
Miao Xie8b050d32014-02-20 18:08:58 +08002985
2986 INIT_LIST_HEAD(&root->log_ctxs[index]);
2987}
2988
Chris Masone02119d2008-09-05 16:13:11 -04002989/*
2990 * btrfs_sync_log does sends a given tree log down to the disk and
2991 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002992 * you know that any inodes previously logged are safely on disk only
2993 * if it returns 0.
2994 *
2995 * Any other return value means you need to call btrfs_commit_transaction.
2996 * Some of the edge cases for fsyncing directories that have had unlinks
2997 * or renames done in the past mean that sometimes the only safe
2998 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2999 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04003000 */
3001int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08003002 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003003{
Yan Zheng7237f182009-01-21 12:54:03 -05003004 int index1;
3005 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003006 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04003007 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003008 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003009 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003010 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Josef Bacik4203e962019-09-30 16:27:25 -04003011 struct btrfs_root_item new_root_item;
Miao Xiebb14a592014-02-20 18:08:56 +08003012 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08003013 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00003014 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04003015
Yan Zheng7237f182009-01-21 12:54:03 -05003016 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08003017 log_transid = ctx->log_transid;
3018 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05003019 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003020 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04003021 }
Miao Xied1433de2014-02-20 18:08:59 +08003022
3023 index1 = log_transid % 2;
3024 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003025 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08003026 mutex_unlock(&root->log_mutex);
3027 return ctx->log_ret;
3028 }
3029 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003030 atomic_set(&root->log_commit[index1], 1);
3031
3032 /* wait for previous tree log sync to complete */
3033 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08003034 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08003035
Yan, Zheng86df7eb2009-10-14 09:24:59 -04003036 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06003037 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04003038 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003039 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08003040 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04003041 mutex_unlock(&root->log_mutex);
3042 schedule_timeout_uninterruptible(1);
3043 mutex_lock(&root->log_mutex);
3044 }
Zhaolei60d53eb2015-08-17 18:44:46 +08003045 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06003046 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04003047 break;
3048 }
Chris Masond0c803c2008-09-11 16:17:57 -04003049
Chris Mason12fcfd22009-03-24 10:24:20 -04003050 /* bail out if we need to do a full commit */
David Sterba4884b8e2019-03-20 13:25:34 +01003051 if (btrfs_need_log_full_commit(trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003052 ret = -EAGAIN;
3053 mutex_unlock(&root->log_mutex);
3054 goto out;
3055 }
3056
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003057 if (log_transid % 2 == 0)
3058 mark = EXTENT_DIRTY;
3059 else
3060 mark = EXTENT_NEW;
3061
Chris Mason690587d2009-10-13 13:29:19 -04003062 /* we start IO on all the marked extents here, but we don't actually
3063 * wait for them until later.
3064 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00003065 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003066 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003067 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003068 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04003069 btrfs_abort_transaction(trans, ret);
David Sterba90787762019-03-20 13:28:05 +01003070 btrfs_set_log_full_commit(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003071 mutex_unlock(&root->log_mutex);
3072 goto out;
3073 }
Yan Zheng7237f182009-01-21 12:54:03 -05003074
Josef Bacik4203e962019-09-30 16:27:25 -04003075 /*
3076 * We _must_ update under the root->log_mutex in order to make sure we
3077 * have a consistent view of the log root we are trying to commit at
3078 * this moment.
3079 *
3080 * We _must_ copy this into a local copy, because we are not holding the
3081 * log_root_tree->log_mutex yet. This is important because when we
3082 * commit the log_root_tree we must have a consistent view of the
3083 * log_root_tree when we update the super block to point at the
3084 * log_root_tree bytenr. If we update the log_root_tree here we'll race
3085 * with the commit and possibly point at the new block which we may not
3086 * have written out.
3087 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003088 btrfs_set_root_node(&log->root_item, log->node);
Josef Bacik4203e962019-09-30 16:27:25 -04003089 memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
Yan Zheng7237f182009-01-21 12:54:03 -05003090
Yan Zheng7237f182009-01-21 12:54:03 -05003091 root->log_transid++;
3092 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04003093 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05003094 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003095 * IO has been started, blocks of the log tree have WRITTEN flag set
3096 * in their headers. new modifications of the log will be written to
3097 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05003098 */
3099 mutex_unlock(&root->log_mutex);
3100
Filipe Manana28a23592016-08-23 21:13:51 +01003101 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08003102
Yan Zheng7237f182009-01-21 12:54:03 -05003103 mutex_lock(&log_root_tree->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08003104
3105 index2 = log_root_tree->log_transid % 2;
3106 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3107 root_log_ctx.log_transid = log_root_tree->log_transid;
3108
Josef Bacik4203e962019-09-30 16:27:25 -04003109 /*
3110 * Now we are safe to update the log_root_tree because we're under the
3111 * log_mutex, and we're a current writer so we're holding the commit
3112 * open until we drop the log_mutex.
3113 */
3114 ret = update_log_root(trans, log, &new_root_item);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003115 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08003116 if (!list_empty(&root_log_ctx.list))
3117 list_del_init(&root_log_ctx.list);
3118
Miao Xiec6adc9c2013-05-28 10:05:39 +00003119 blk_finish_plug(&plug);
David Sterba90787762019-03-20 13:28:05 +01003120 btrfs_set_log_full_commit(trans);
Miao Xie995946d2014-04-02 19:51:06 +08003121
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003122 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003123 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003124 mutex_unlock(&log_root_tree->log_mutex);
3125 goto out;
3126 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003127 btrfs_wait_tree_log_extents(log, mark);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003128 mutex_unlock(&log_root_tree->log_mutex);
3129 ret = -EAGAIN;
3130 goto out;
3131 }
3132
Miao Xied1433de2014-02-20 18:08:59 +08003133 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08003134 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07003135 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08003136 mutex_unlock(&log_root_tree->log_mutex);
3137 ret = root_log_ctx.log_ret;
3138 goto out;
3139 }
Miao Xie8b050d32014-02-20 18:08:58 +08003140
Miao Xied1433de2014-02-20 18:08:59 +08003141 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05003142 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003143 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003144 ret = btrfs_wait_tree_log_extents(log, mark);
Zhaolei60d53eb2015-08-17 18:44:46 +08003145 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003146 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003147 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003148 if (!ret)
3149 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05003150 goto out;
3151 }
Miao Xied1433de2014-02-20 18:08:59 +08003152 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003153 atomic_set(&log_root_tree->log_commit[index2], 1);
3154
Chris Mason12fcfd22009-03-24 10:24:20 -04003155 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003156 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003157 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04003158 }
Yan Zheng7237f182009-01-21 12:54:03 -05003159
Chris Mason12fcfd22009-03-24 10:24:20 -04003160 /*
3161 * now that we've moved on to the tree of log tree roots,
3162 * check the full commit flag again
3163 */
David Sterba4884b8e2019-03-20 13:25:34 +01003164 if (btrfs_need_log_full_commit(trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003165 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003166 btrfs_wait_tree_log_extents(log, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04003167 mutex_unlock(&log_root_tree->log_mutex);
3168 ret = -EAGAIN;
3169 goto out_wake_log_root;
3170 }
Yan Zheng7237f182009-01-21 12:54:03 -05003171
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003172 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00003173 &log_root_tree->dirty_log_pages,
3174 EXTENT_DIRTY | EXTENT_NEW);
3175 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003176 if (ret) {
David Sterba90787762019-03-20 13:28:05 +01003177 btrfs_set_log_full_commit(trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003178 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003179 mutex_unlock(&log_root_tree->log_mutex);
3180 goto out_wake_log_root;
3181 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003182 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003183 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003184 ret = btrfs_wait_tree_log_extents(log_root_tree,
3185 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003186 if (ret) {
David Sterba90787762019-03-20 13:28:05 +01003187 btrfs_set_log_full_commit(trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003188 mutex_unlock(&log_root_tree->log_mutex);
3189 goto out_wake_log_root;
3190 }
Chris Masone02119d2008-09-05 16:13:11 -04003191
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003192 btrfs_set_super_log_root(fs_info->super_for_commit,
3193 log_root_tree->node->start);
3194 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3195 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04003196
Yan Zheng7237f182009-01-21 12:54:03 -05003197 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05003198 mutex_unlock(&log_root_tree->log_mutex);
3199
3200 /*
Andrea Gelmini52042d82018-11-28 12:05:13 +01003201 * Nobody else is going to jump in and write the ctree
Yan Zheng7237f182009-01-21 12:54:03 -05003202 * super here because the log_commit atomic below is protecting
3203 * us. We must be called with a transaction handle pinning
3204 * the running transaction open, so a full commit can't hop
3205 * in and cause problems either.
3206 */
David Sterbaeece6a92017-02-10 19:04:32 +01003207 ret = write_all_supers(fs_info, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003208 if (ret) {
David Sterba90787762019-03-20 13:28:05 +01003209 btrfs_set_log_full_commit(trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003210 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003211 goto out_wake_log_root;
3212 }
Yan Zheng7237f182009-01-21 12:54:03 -05003213
Chris Mason257c62e2009-10-13 13:21:08 -04003214 mutex_lock(&root->log_mutex);
3215 if (root->last_log_commit < log_transid)
3216 root->last_log_commit = log_transid;
3217 mutex_unlock(&root->log_mutex);
3218
Chris Mason12fcfd22009-03-24 10:24:20 -04003219out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07003220 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003221 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3222
Miao Xied1433de2014-02-20 18:08:59 +08003223 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003224 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003225 mutex_unlock(&log_root_tree->log_mutex);
3226
David Sterba33a9eca2015-10-10 18:35:10 +02003227 /*
David Sterba093258e2018-02-26 16:15:17 +01003228 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3229 * all the updates above are seen by the woken threads. It might not be
3230 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003231 */
David Sterba093258e2018-02-26 16:15:17 +01003232 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04003233out:
Miao Xied1433de2014-02-20 18:08:59 +08003234 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07003235 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08003236 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003237 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003238 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003239
David Sterba33a9eca2015-10-10 18:35:10 +02003240 /*
David Sterba093258e2018-02-26 16:15:17 +01003241 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3242 * all the updates above are seen by the woken threads. It might not be
3243 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003244 */
David Sterba093258e2018-02-26 16:15:17 +01003245 cond_wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003246 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003247}
3248
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003249static void free_log_tree(struct btrfs_trans_handle *trans,
3250 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003251{
3252 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04003253 struct walk_control wc = {
3254 .free = 1,
3255 .process_func = process_one_buffer
3256 };
3257
Josef Bacik681ae502013-10-07 15:11:00 -04003258 ret = walk_log_tree(trans, log, &wc);
Jeff Mahoney374b0e22018-09-06 16:59:33 -04003259 if (ret) {
3260 if (trans)
3261 btrfs_abort_transaction(trans, ret);
3262 else
3263 btrfs_handle_fs_error(log->fs_info, ret, NULL);
3264 }
Chris Masone02119d2008-09-05 16:13:11 -04003265
Filipe Manana59b07132018-11-09 10:43:08 +00003266 clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
3267 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Filipe Mananae289f032020-05-18 12:14:50 +01003268 extent_io_tree_release(&log->log_csum_range);
Josef Bacik00246522020-01-24 09:33:01 -05003269 btrfs_put_root(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003270}
3271
3272/*
3273 * free all the extents used by the tree log. This should be called
3274 * at commit time of the full transaction
3275 */
3276int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3277{
3278 if (root->log_root) {
3279 free_log_tree(trans, root->log_root);
3280 root->log_root = NULL;
Filipe Mananae7a79812020-06-15 10:38:44 +01003281 clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003282 }
3283 return 0;
3284}
3285
3286int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3287 struct btrfs_fs_info *fs_info)
3288{
3289 if (fs_info->log_root_tree) {
3290 free_log_tree(trans, fs_info->log_root_tree);
3291 fs_info->log_root_tree = NULL;
3292 }
Chris Masone02119d2008-09-05 16:13:11 -04003293 return 0;
3294}
3295
3296/*
Filipe Manana803f0f62019-06-19 13:05:39 +01003297 * Check if an inode was logged in the current transaction. We can't always rely
3298 * on an inode's logged_trans value, because it's an in-memory only field and
3299 * therefore not persisted. This means that its value is lost if the inode gets
3300 * evicted and loaded again from disk (in which case it has a value of 0, and
3301 * certainly it is smaller then any possible transaction ID), when that happens
3302 * the full_sync flag is set in the inode's runtime flags, so on that case we
3303 * assume eviction happened and ignore the logged_trans value, assuming the
3304 * worst case, that the inode was logged before in the current transaction.
3305 */
3306static bool inode_logged(struct btrfs_trans_handle *trans,
3307 struct btrfs_inode *inode)
3308{
3309 if (inode->logged_trans == trans->transid)
3310 return true;
3311
3312 if (inode->last_trans == trans->transid &&
3313 test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) &&
3314 !test_bit(BTRFS_FS_LOG_RECOVERING, &trans->fs_info->flags))
3315 return true;
3316
3317 return false;
3318}
3319
3320/*
Chris Masone02119d2008-09-05 16:13:11 -04003321 * If both a file and directory are logged, and unlinks or renames are
3322 * mixed in, we have a few interesting corners:
3323 *
3324 * create file X in dir Y
3325 * link file X to X.link in dir Y
3326 * fsync file X
3327 * unlink file X but leave X.link
3328 * fsync dir Y
3329 *
3330 * After a crash we would expect only X.link to exist. But file X
3331 * didn't get fsync'd again so the log has back refs for X and X.link.
3332 *
3333 * We solve this by removing directory entries and inode backrefs from the
3334 * log when a file that was logged in the current transaction is
3335 * unlinked. Any later fsync will include the updated log entries, and
3336 * we'll be able to reconstruct the proper directory items from backrefs.
3337 *
3338 * This optimizations allows us to avoid relogging the entire inode
3339 * or the entire directory.
3340 */
3341int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3342 struct btrfs_root *root,
3343 const char *name, int name_len,
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003344 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003345{
3346 struct btrfs_root *log;
3347 struct btrfs_dir_item *di;
3348 struct btrfs_path *path;
3349 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003350 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003351 int bytes_del = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003352 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003353
Filipe Manana803f0f62019-06-19 13:05:39 +01003354 if (!inode_logged(trans, dir))
Chris Mason3a5f1d42008-09-11 15:53:37 -04003355 return 0;
3356
Chris Masone02119d2008-09-05 16:13:11 -04003357 ret = join_running_log_trans(root);
3358 if (ret)
3359 return 0;
3360
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003361 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003362
3363 log = root->log_root;
3364 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003365 if (!path) {
3366 err = -ENOMEM;
3367 goto out_unlock;
3368 }
liubo2a29edc2011-01-26 06:22:08 +00003369
Li Zefan33345d012011-04-20 10:31:50 +08003370 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003371 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003372 if (IS_ERR(di)) {
3373 err = PTR_ERR(di);
3374 goto fail;
3375 }
3376 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003377 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3378 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003379 if (ret) {
3380 err = ret;
3381 goto fail;
3382 }
Chris Masone02119d2008-09-05 16:13:11 -04003383 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003384 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003385 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003386 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003387 if (IS_ERR(di)) {
3388 err = PTR_ERR(di);
3389 goto fail;
3390 }
3391 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003392 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3393 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003394 if (ret) {
3395 err = ret;
3396 goto fail;
3397 }
Chris Masone02119d2008-09-05 16:13:11 -04003398 }
3399
3400 /* update the directory size in the log to reflect the names
3401 * we have removed
3402 */
3403 if (bytes_del) {
3404 struct btrfs_key key;
3405
Li Zefan33345d012011-04-20 10:31:50 +08003406 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003407 key.offset = 0;
3408 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003409 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003410
3411 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003412 if (ret < 0) {
3413 err = ret;
3414 goto fail;
3415 }
Chris Masone02119d2008-09-05 16:13:11 -04003416 if (ret == 0) {
3417 struct btrfs_inode_item *item;
3418 u64 i_size;
3419
3420 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3421 struct btrfs_inode_item);
3422 i_size = btrfs_inode_size(path->nodes[0], item);
3423 if (i_size > bytes_del)
3424 i_size -= bytes_del;
3425 else
3426 i_size = 0;
3427 btrfs_set_inode_size(path->nodes[0], item, i_size);
3428 btrfs_mark_buffer_dirty(path->nodes[0]);
3429 } else
3430 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003431 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003432 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003433fail:
Chris Masone02119d2008-09-05 16:13:11 -04003434 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003435out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003436 mutex_unlock(&dir->log_mutex);
Josef Bacikfb2fecb2020-08-10 17:31:16 -04003437 if (err == -ENOSPC) {
David Sterba90787762019-03-20 13:28:05 +01003438 btrfs_set_log_full_commit(trans);
Josef Bacikfb2fecb2020-08-10 17:31:16 -04003439 err = 0;
3440 } else if (err < 0 && err != -ENOENT) {
3441 /* ENOENT can be returned if the entry hasn't been fsynced yet */
3442 btrfs_abort_transaction(trans, err);
3443 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003444
Chris Mason12fcfd22009-03-24 10:24:20 -04003445 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003446
Andi Kleen411fc6b2010-10-29 15:14:31 -04003447 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003448}
3449
3450/* see comments for btrfs_del_dir_entries_in_log */
3451int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3452 struct btrfs_root *root,
3453 const char *name, int name_len,
Nikolay Borisova491abb2017-01-18 00:31:33 +02003454 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003455{
3456 struct btrfs_root *log;
3457 u64 index;
3458 int ret;
3459
Filipe Manana803f0f62019-06-19 13:05:39 +01003460 if (!inode_logged(trans, inode))
Chris Mason3a5f1d42008-09-11 15:53:37 -04003461 return 0;
3462
Chris Masone02119d2008-09-05 16:13:11 -04003463 ret = join_running_log_trans(root);
3464 if (ret)
3465 return 0;
3466 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003467 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003468
Nikolay Borisova491abb2017-01-18 00:31:33 +02003469 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003470 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003471 mutex_unlock(&inode->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003472 if (ret == -ENOSPC) {
David Sterba90787762019-03-20 13:28:05 +01003473 btrfs_set_log_full_commit(trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003474 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003475 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003476 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003477 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003478
Chris Masone02119d2008-09-05 16:13:11 -04003479 return ret;
3480}
3481
3482/*
3483 * creates a range item in the log for 'dirid'. first_offset and
3484 * last_offset tell us which parts of the key space the log should
3485 * be considered authoritative for.
3486 */
3487static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3488 struct btrfs_root *log,
3489 struct btrfs_path *path,
3490 int key_type, u64 dirid,
3491 u64 first_offset, u64 last_offset)
3492{
3493 int ret;
3494 struct btrfs_key key;
3495 struct btrfs_dir_log_item *item;
3496
3497 key.objectid = dirid;
3498 key.offset = first_offset;
3499 if (key_type == BTRFS_DIR_ITEM_KEY)
3500 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3501 else
3502 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3503 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003504 if (ret)
3505 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003506
3507 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3508 struct btrfs_dir_log_item);
3509 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3510 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003511 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003512 return 0;
3513}
3514
3515/*
3516 * log all the items included in the current transaction for a given
3517 * directory. This also creates the range items in the log tree required
3518 * to replay anything deleted before the fsync
3519 */
3520static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Nikolay Borisov684a5772017-01-18 00:31:41 +02003521 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003522 struct btrfs_path *path,
3523 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003524 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003525 u64 min_offset, u64 *last_offset_ret)
3526{
3527 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003528 struct btrfs_root *log = root->log_root;
3529 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003530 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003531 int ret;
3532 int i;
3533 int nritems;
3534 u64 first_offset = min_offset;
3535 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003536 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003537
3538 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003539
Li Zefan33345d012011-04-20 10:31:50 +08003540 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003541 min_key.type = key_type;
3542 min_key.offset = min_offset;
3543
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003544 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003545
3546 /*
3547 * we didn't find anything from this transaction, see if there
3548 * is anything at all
3549 */
Li Zefan33345d012011-04-20 10:31:50 +08003550 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3551 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003552 min_key.type = key_type;
3553 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003554 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003555 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3556 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003557 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003558 return ret;
3559 }
Li Zefan33345d012011-04-20 10:31:50 +08003560 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003561
3562 /* if ret == 0 there are items for this type,
3563 * create a range to tell us the last key of this type.
3564 * otherwise, there are no items in this directory after
3565 * *min_offset, and we create a range to indicate that.
3566 */
3567 if (ret == 0) {
3568 struct btrfs_key tmp;
3569 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3570 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003571 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003572 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003573 }
3574 goto done;
3575 }
3576
3577 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003578 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003579 if (ret == 0) {
3580 struct btrfs_key tmp;
3581 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3582 if (key_type == tmp.type) {
3583 first_offset = tmp.offset;
3584 ret = overwrite_item(trans, log, dst_path,
3585 path->nodes[0], path->slots[0],
3586 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003587 if (ret) {
3588 err = ret;
3589 goto done;
3590 }
Chris Masone02119d2008-09-05 16:13:11 -04003591 }
3592 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003593 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003594
Josef Bacik2cc83342019-03-06 17:13:04 -05003595 /*
3596 * Find the first key from this transaction again. See the note for
3597 * log_new_dir_dentries, if we're logging a directory recursively we
3598 * won't be holding its i_mutex, which means we can modify the directory
3599 * while we're logging it. If we remove an entry between our first
3600 * search and this search we'll not find the key again and can just
3601 * bail.
3602 */
Filipe Mananabb56f022020-09-14 15:27:50 +01003603search:
Chris Masone02119d2008-09-05 16:13:11 -04003604 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Josef Bacik2cc83342019-03-06 17:13:04 -05003605 if (ret != 0)
Chris Masone02119d2008-09-05 16:13:11 -04003606 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003607
3608 /*
3609 * we have a block from this transaction, log every item in it
3610 * from our directory
3611 */
Chris Masond3977122009-01-05 21:25:51 -05003612 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003613 struct btrfs_key tmp;
3614 src = path->nodes[0];
3615 nritems = btrfs_header_nritems(src);
3616 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003617 struct btrfs_dir_item *di;
3618
Chris Masone02119d2008-09-05 16:13:11 -04003619 btrfs_item_key_to_cpu(src, &min_key, i);
3620
Li Zefan33345d012011-04-20 10:31:50 +08003621 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003622 goto done;
Filipe Mananabb56f022020-09-14 15:27:50 +01003623
3624 if (need_resched()) {
3625 btrfs_release_path(path);
3626 cond_resched();
3627 goto search;
3628 }
3629
Chris Masone02119d2008-09-05 16:13:11 -04003630 ret = overwrite_item(trans, log, dst_path, src, i,
3631 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003632 if (ret) {
3633 err = ret;
3634 goto done;
3635 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003636
3637 /*
3638 * We must make sure that when we log a directory entry,
3639 * the corresponding inode, after log replay, has a
3640 * matching link count. For example:
3641 *
3642 * touch foo
3643 * mkdir mydir
3644 * sync
3645 * ln foo mydir/bar
3646 * xfs_io -c "fsync" mydir
3647 * <crash>
3648 * <mount fs and log replay>
3649 *
3650 * Would result in a fsync log that when replayed, our
3651 * file inode would have a link count of 1, but we get
3652 * two directory entries pointing to the same inode.
3653 * After removing one of the names, it would not be
3654 * possible to remove the other name, which resulted
3655 * always in stale file handle errors, and would not
3656 * be possible to rmdir the parent directory, since
3657 * its i_size could never decrement to the value
3658 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3659 */
3660 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3661 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3662 if (ctx &&
3663 (btrfs_dir_transid(src, di) == trans->transid ||
3664 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3665 tmp.type != BTRFS_ROOT_ITEM_KEY)
3666 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003667 }
3668 path->slots[0] = nritems;
3669
3670 /*
3671 * look ahead to the next item and see if it is also
3672 * from this directory and from this transaction
3673 */
3674 ret = btrfs_next_leaf(root, path);
Liu Bo80c0b422018-04-03 01:59:47 +08003675 if (ret) {
3676 if (ret == 1)
3677 last_offset = (u64)-1;
3678 else
3679 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003680 goto done;
3681 }
3682 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003683 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003684 last_offset = (u64)-1;
3685 goto done;
3686 }
3687 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3688 ret = overwrite_item(trans, log, dst_path,
3689 path->nodes[0], path->slots[0],
3690 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003691 if (ret)
3692 err = ret;
3693 else
3694 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003695 goto done;
3696 }
3697 }
3698done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003699 btrfs_release_path(path);
3700 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003701
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003702 if (err == 0) {
3703 *last_offset_ret = last_offset;
3704 /*
3705 * insert the log range keys to indicate where the log
3706 * is valid
3707 */
3708 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003709 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003710 if (ret)
3711 err = ret;
3712 }
3713 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003714}
3715
3716/*
3717 * logging directories is very similar to logging inodes, We find all the items
3718 * from the current transaction and write them to the log.
3719 *
3720 * The recovery code scans the directory in the subvolume, and if it finds a
3721 * key in the range logged that is not present in the log tree, then it means
3722 * that dir entry was unlinked during the transaction.
3723 *
3724 * In order for that scan to work, we must include one key smaller than
3725 * the smallest logged by this transaction and one key larger than the largest
3726 * key logged by this transaction.
3727 */
3728static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003729 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003730 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003731 struct btrfs_path *dst_path,
3732 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003733{
3734 u64 min_key;
3735 u64 max_key;
3736 int ret;
3737 int key_type = BTRFS_DIR_ITEM_KEY;
3738
3739again:
3740 min_key = 0;
3741 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003742 while (1) {
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003743 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3744 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003745 if (ret)
3746 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003747 if (max_key == (u64)-1)
3748 break;
3749 min_key = max_key + 1;
3750 }
3751
3752 if (key_type == BTRFS_DIR_ITEM_KEY) {
3753 key_type = BTRFS_DIR_INDEX_KEY;
3754 goto again;
3755 }
3756 return 0;
3757}
3758
3759/*
3760 * a helper function to drop items from the log before we relog an
3761 * inode. max_key_type indicates the highest item type to remove.
3762 * This cannot be run for file data extents because it does not
3763 * free the extents they point to.
3764 */
3765static int drop_objectid_items(struct btrfs_trans_handle *trans,
3766 struct btrfs_root *log,
3767 struct btrfs_path *path,
3768 u64 objectid, int max_key_type)
3769{
3770 int ret;
3771 struct btrfs_key key;
3772 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003773 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003774
3775 key.objectid = objectid;
3776 key.type = max_key_type;
3777 key.offset = (u64)-1;
3778
Chris Masond3977122009-01-05 21:25:51 -05003779 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003780 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003781 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003782 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003783 break;
3784
3785 if (path->slots[0] == 0)
3786 break;
3787
3788 path->slots[0]--;
3789 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3790 path->slots[0]);
3791
3792 if (found_key.objectid != objectid)
3793 break;
3794
Josef Bacik18ec90d2012-09-28 11:56:28 -04003795 found_key.offset = 0;
3796 found_key.type = 0;
Qu Wenruoe3b83362020-04-17 15:08:21 +08003797 ret = btrfs_bin_search(path->nodes[0], &found_key, &start_slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00003798 if (ret < 0)
3799 break;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003800
3801 ret = btrfs_del_items(trans, log, path, start_slot,
3802 path->slots[0] - start_slot + 1);
3803 /*
3804 * If start slot isn't 0 then we don't need to re-search, we've
3805 * found the last guy with the objectid in this tree.
3806 */
3807 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003808 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003809 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003810 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003811 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003812 if (ret > 0)
3813 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003814 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003815}
3816
Josef Bacik94edf4a2012-09-25 14:56:25 -04003817static void fill_inode_item(struct btrfs_trans_handle *trans,
3818 struct extent_buffer *leaf,
3819 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003820 struct inode *inode, int log_inode_only,
3821 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003822{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003823 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003824
David Sterbac82f8232019-08-09 17:48:21 +02003825 btrfs_init_map_token(&token, leaf);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003826
3827 if (log_inode_only) {
3828 /* set the generation to zero so the recover code
3829 * can tell the difference between an logging
3830 * just to say 'this inode exists' and a logging
3831 * to say 'update this inode with these values'
3832 */
David Sterbacc4c13d2020-04-29 02:15:56 +02003833 btrfs_set_token_inode_generation(&token, item, 0);
3834 btrfs_set_token_inode_size(&token, item, logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003835 } else {
David Sterbacc4c13d2020-04-29 02:15:56 +02003836 btrfs_set_token_inode_generation(&token, item,
3837 BTRFS_I(inode)->generation);
3838 btrfs_set_token_inode_size(&token, item, inode->i_size);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003839 }
3840
David Sterbacc4c13d2020-04-29 02:15:56 +02003841 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3842 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3843 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3844 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003845
David Sterbacc4c13d2020-04-29 02:15:56 +02003846 btrfs_set_token_timespec_sec(&token, &item->atime,
3847 inode->i_atime.tv_sec);
3848 btrfs_set_token_timespec_nsec(&token, &item->atime,
3849 inode->i_atime.tv_nsec);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003850
David Sterbacc4c13d2020-04-29 02:15:56 +02003851 btrfs_set_token_timespec_sec(&token, &item->mtime,
3852 inode->i_mtime.tv_sec);
3853 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3854 inode->i_mtime.tv_nsec);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003855
David Sterbacc4c13d2020-04-29 02:15:56 +02003856 btrfs_set_token_timespec_sec(&token, &item->ctime,
3857 inode->i_ctime.tv_sec);
3858 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3859 inode->i_ctime.tv_nsec);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003860
David Sterbacc4c13d2020-04-29 02:15:56 +02003861 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003862
David Sterbacc4c13d2020-04-29 02:15:56 +02003863 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3864 btrfs_set_token_inode_transid(&token, item, trans->transid);
3865 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3866 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3867 btrfs_set_token_inode_block_group(&token, item, 0);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003868}
3869
Josef Bacika95249b2012-10-11 16:17:34 -04003870static int log_inode_item(struct btrfs_trans_handle *trans,
3871 struct btrfs_root *log, struct btrfs_path *path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003872 struct btrfs_inode *inode)
Josef Bacika95249b2012-10-11 16:17:34 -04003873{
3874 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003875 int ret;
3876
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003877 ret = btrfs_insert_empty_item(trans, log, path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003878 &inode->location, sizeof(*inode_item));
Josef Bacika95249b2012-10-11 16:17:34 -04003879 if (ret && ret != -EEXIST)
3880 return ret;
3881 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3882 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003883 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3884 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003885 btrfs_release_path(path);
3886 return 0;
3887}
3888
Filipe Manana40e046a2019-12-05 16:58:30 +00003889static int log_csums(struct btrfs_trans_handle *trans,
Filipe Manana3ebac172020-07-15 12:30:43 +01003890 struct btrfs_inode *inode,
Filipe Manana40e046a2019-12-05 16:58:30 +00003891 struct btrfs_root *log_root,
3892 struct btrfs_ordered_sum *sums)
3893{
Filipe Mananae289f032020-05-18 12:14:50 +01003894 const u64 lock_end = sums->bytenr + sums->len - 1;
3895 struct extent_state *cached_state = NULL;
Filipe Manana40e046a2019-12-05 16:58:30 +00003896 int ret;
3897
3898 /*
Filipe Manana3ebac172020-07-15 12:30:43 +01003899 * If this inode was not used for reflink operations in the current
3900 * transaction with new extents, then do the fast path, no need to
3901 * worry about logging checksum items with overlapping ranges.
3902 */
3903 if (inode->last_reflink_trans < trans->transid)
3904 return btrfs_csum_file_blocks(trans, log_root, sums);
3905
3906 /*
Filipe Mananae289f032020-05-18 12:14:50 +01003907 * Serialize logging for checksums. This is to avoid racing with the
3908 * same checksum being logged by another task that is logging another
3909 * file which happens to refer to the same extent as well. Such races
3910 * can leave checksum items in the log with overlapping ranges.
3911 */
3912 ret = lock_extent_bits(&log_root->log_csum_range, sums->bytenr,
3913 lock_end, &cached_state);
3914 if (ret)
3915 return ret;
3916 /*
Filipe Manana40e046a2019-12-05 16:58:30 +00003917 * Due to extent cloning, we might have logged a csum item that covers a
3918 * subrange of a cloned extent, and later we can end up logging a csum
3919 * item for a larger subrange of the same extent or the entire range.
3920 * This would leave csum items in the log tree that cover the same range
3921 * and break the searches for checksums in the log tree, resulting in
3922 * some checksums missing in the fs/subvolume tree. So just delete (or
3923 * trim and adjust) any existing csum items in the log for this range.
3924 */
3925 ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len);
Filipe Mananae289f032020-05-18 12:14:50 +01003926 if (!ret)
3927 ret = btrfs_csum_file_blocks(trans, log_root, sums);
Filipe Manana40e046a2019-12-05 16:58:30 +00003928
Filipe Mananae289f032020-05-18 12:14:50 +01003929 unlock_extent_cached(&log_root->log_csum_range, sums->bytenr, lock_end,
3930 &cached_state);
3931
3932 return ret;
Filipe Manana40e046a2019-12-05 16:58:30 +00003933}
3934
Chris Mason31ff1cd2008-09-11 16:17:57 -04003935static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003936 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003937 struct btrfs_path *dst_path,
Filipe Manana0e563152019-11-19 12:07:33 +00003938 struct btrfs_path *src_path,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003939 int start_slot, int nr, int inode_only,
3940 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003941{
David Sterba3ffbd682018-06-29 10:56:42 +02003942 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003943 unsigned long src_offset;
3944 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003945 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003946 struct btrfs_file_extent_item *extent;
3947 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003948 struct extent_buffer *src = src_path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003949 int ret;
3950 struct btrfs_key *ins_keys;
3951 u32 *ins_sizes;
3952 char *ins_data;
3953 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003954 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003955 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Chris Masond20f7042008-12-08 16:58:54 -05003956
3957 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003958
3959 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3960 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003961 if (!ins_data)
3962 return -ENOMEM;
3963
Chris Mason31ff1cd2008-09-11 16:17:57 -04003964 ins_sizes = (u32 *)ins_data;
3965 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3966
3967 for (i = 0; i < nr; i++) {
3968 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3969 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3970 }
3971 ret = btrfs_insert_empty_items(trans, log, dst_path,
3972 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003973 if (ret) {
3974 kfree(ins_data);
3975 return ret;
3976 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003977
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003978 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003979 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3980 dst_path->slots[0]);
3981
3982 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3983
Josef Bacik94edf4a2012-09-25 14:56:25 -04003984 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003985 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3986 dst_path->slots[0],
3987 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003988 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01003989 &inode->vfs_inode,
3990 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003991 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003992 } else {
3993 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3994 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003995 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003996
Chris Mason31ff1cd2008-09-11 16:17:57 -04003997 /* take a reference on file data extents so that truncates
3998 * or deletes of this inode don't have to relog the inode
3999 * again
4000 */
David Sterba962a2982014-06-04 18:41:45 +02004001 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06004002 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004003 int found_type;
4004 extent = btrfs_item_ptr(src, start_slot + i,
4005 struct btrfs_file_extent_item);
4006
liubo8e531cd2011-05-06 10:36:09 +08004007 if (btrfs_file_extent_generation(src, extent) < trans->transid)
4008 continue;
4009
Chris Mason31ff1cd2008-09-11 16:17:57 -04004010 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04004011 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004012 u64 ds, dl, cs, cl;
4013 ds = btrfs_file_extent_disk_bytenr(src,
4014 extent);
4015 /* ds == 0 is a hole */
4016 if (ds == 0)
4017 continue;
4018
4019 dl = btrfs_file_extent_disk_num_bytes(src,
4020 extent);
4021 cs = btrfs_file_extent_offset(src, extent);
4022 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07004023 extent);
Chris Mason580afd72008-12-08 19:15:39 -05004024 if (btrfs_file_extent_compression(src,
4025 extent)) {
4026 cs = 0;
4027 cl = dl;
4028 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004029
4030 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004031 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004032 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01004033 &ordered_sums, 0);
Filipe Manana4f264332020-07-29 10:17:50 +01004034 if (ret)
4035 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004036 }
4037 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004038 }
4039
4040 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004041 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04004042 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05004043
4044 /*
4045 * we have to do this after the loop above to avoid changing the
4046 * log tree while trying to change the log tree.
4047 */
Chris Masond3977122009-01-05 21:25:51 -05004048 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05004049 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4050 struct btrfs_ordered_sum,
4051 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004052 if (!ret)
Filipe Manana3ebac172020-07-15 12:30:43 +01004053 ret = log_csums(trans, inode, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05004054 list_del(&sums->list);
4055 kfree(sums);
4056 }
Josef Bacik16e75492013-10-22 12:18:51 -04004057
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004058 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004059}
4060
Josef Bacik5dc562c2012-08-17 13:14:17 -04004061static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4062{
4063 struct extent_map *em1, *em2;
4064
4065 em1 = list_entry(a, struct extent_map, list);
4066 em2 = list_entry(b, struct extent_map, list);
4067
4068 if (em1->start < em2->start)
4069 return -1;
4070 else if (em1->start > em2->start)
4071 return 1;
4072 return 0;
4073}
4074
Josef Bacike7175a62018-05-23 11:58:34 -04004075static int log_extent_csums(struct btrfs_trans_handle *trans,
4076 struct btrfs_inode *inode,
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004077 struct btrfs_root *log_root,
Filipe Manana48778172020-08-11 12:43:58 +01004078 const struct extent_map *em,
4079 struct btrfs_log_ctx *ctx)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004080{
Filipe Manana48778172020-08-11 12:43:58 +01004081 struct btrfs_ordered_extent *ordered;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004082 u64 csum_offset;
4083 u64 csum_len;
Filipe Manana48778172020-08-11 12:43:58 +01004084 u64 mod_start = em->mod_start;
4085 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004086 LIST_HEAD(ordered_sums);
4087 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004088
Josef Bacike7175a62018-05-23 11:58:34 -04004089 if (inode->flags & BTRFS_INODE_NODATASUM ||
4090 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
Filipe Manana8407f552014-09-05 15:14:39 +01004091 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04004092 return 0;
4093
Filipe Manana48778172020-08-11 12:43:58 +01004094 list_for_each_entry(ordered, &ctx->ordered_extents, log_list) {
4095 const u64 ordered_end = ordered->file_offset + ordered->num_bytes;
4096 const u64 mod_end = mod_start + mod_len;
4097 struct btrfs_ordered_sum *sums;
4098
4099 if (mod_len == 0)
4100 break;
4101
4102 if (ordered_end <= mod_start)
4103 continue;
4104 if (mod_end <= ordered->file_offset)
4105 break;
4106
4107 /*
4108 * We are going to copy all the csums on this ordered extent, so
4109 * go ahead and adjust mod_start and mod_len in case this ordered
4110 * extent has already been logged.
4111 */
4112 if (ordered->file_offset > mod_start) {
4113 if (ordered_end >= mod_end)
4114 mod_len = ordered->file_offset - mod_start;
4115 /*
4116 * If we have this case
4117 *
4118 * |--------- logged extent ---------|
4119 * |----- ordered extent ----|
4120 *
4121 * Just don't mess with mod_start and mod_len, we'll
4122 * just end up logging more csums than we need and it
4123 * will be ok.
4124 */
4125 } else {
4126 if (ordered_end < mod_end) {
4127 mod_len = mod_end - ordered_end;
4128 mod_start = ordered_end;
4129 } else {
4130 mod_len = 0;
4131 }
4132 }
4133
4134 /*
4135 * To keep us from looping for the above case of an ordered
4136 * extent that falls inside of the logged extent.
4137 */
4138 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags))
4139 continue;
4140
4141 list_for_each_entry(sums, &ordered->list, list) {
4142 ret = log_csums(trans, inode, log_root, sums);
4143 if (ret)
4144 return ret;
4145 }
4146 }
4147
4148 /* We're done, found all csums in the ordered extents. */
4149 if (mod_len == 0)
4150 return 0;
4151
Josef Bacike7175a62018-05-23 11:58:34 -04004152 /* If we're compressed we have to save the entire range of csums. */
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004153 if (em->compress_type) {
4154 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004155 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004156 } else {
Filipe Manana48778172020-08-11 12:43:58 +01004157 csum_offset = mod_start - em->start;
4158 csum_len = mod_len;
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004159 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004160
Josef Bacik70c8a912012-10-11 16:54:30 -04004161 /* block start is already adjusted for the file extent offset. */
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004162 ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004163 em->block_start + csum_offset,
4164 em->block_start + csum_offset +
4165 csum_len - 1, &ordered_sums, 0);
4166 if (ret)
4167 return ret;
4168
4169 while (!list_empty(&ordered_sums)) {
4170 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4171 struct btrfs_ordered_sum,
4172 list);
4173 if (!ret)
Filipe Manana3ebac172020-07-15 12:30:43 +01004174 ret = log_csums(trans, inode, log_root, sums);
Josef Bacik70c8a912012-10-11 16:54:30 -04004175 list_del(&sums->list);
4176 kfree(sums);
4177 }
4178
4179 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004180}
4181
Filipe Manana8407f552014-09-05 15:14:39 +01004182static int log_one_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004183 struct btrfs_inode *inode, struct btrfs_root *root,
Filipe Manana8407f552014-09-05 15:14:39 +01004184 const struct extent_map *em,
4185 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004186 struct btrfs_log_ctx *ctx)
4187{
4188 struct btrfs_root *log = root->log_root;
4189 struct btrfs_file_extent_item *fi;
4190 struct extent_buffer *leaf;
4191 struct btrfs_map_token token;
4192 struct btrfs_key key;
4193 u64 extent_offset = em->start - em->orig_start;
4194 u64 block_len;
4195 int ret;
4196 int extent_inserted = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004197
Filipe Manana48778172020-08-11 12:43:58 +01004198 ret = log_extent_csums(trans, inode, log, em, ctx);
Filipe Manana8407f552014-09-05 15:14:39 +01004199 if (ret)
4200 return ret;
4201
Nikolay Borisov906c4482020-06-03 08:55:08 +03004202 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
Filipe Manana8407f552014-09-05 15:14:39 +01004203 em->start + em->len, NULL, 0, 1,
4204 sizeof(*fi), &extent_inserted);
4205 if (ret)
4206 return ret;
4207
4208 if (!extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004209 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004210 key.type = BTRFS_EXTENT_DATA_KEY;
4211 key.offset = em->start;
4212
4213 ret = btrfs_insert_empty_item(trans, log, path, &key,
4214 sizeof(*fi));
4215 if (ret)
4216 return ret;
4217 }
4218 leaf = path->nodes[0];
David Sterbac82f8232019-08-09 17:48:21 +02004219 btrfs_init_map_token(&token, leaf);
Filipe Manana8407f552014-09-05 15:14:39 +01004220 fi = btrfs_item_ptr(leaf, path->slots[0],
4221 struct btrfs_file_extent_item);
4222
David Sterbacc4c13d2020-04-29 02:15:56 +02004223 btrfs_set_token_file_extent_generation(&token, fi, trans->transid);
Filipe Manana8407f552014-09-05 15:14:39 +01004224 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
David Sterbacc4c13d2020-04-29 02:15:56 +02004225 btrfs_set_token_file_extent_type(&token, fi,
4226 BTRFS_FILE_EXTENT_PREALLOC);
Filipe Manana8407f552014-09-05 15:14:39 +01004227 else
David Sterbacc4c13d2020-04-29 02:15:56 +02004228 btrfs_set_token_file_extent_type(&token, fi,
4229 BTRFS_FILE_EXTENT_REG);
Filipe Manana8407f552014-09-05 15:14:39 +01004230
4231 block_len = max(em->block_len, em->orig_block_len);
4232 if (em->compress_type != BTRFS_COMPRESS_NONE) {
David Sterbacc4c13d2020-04-29 02:15:56 +02004233 btrfs_set_token_file_extent_disk_bytenr(&token, fi,
4234 em->block_start);
4235 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
Filipe Manana8407f552014-09-05 15:14:39 +01004236 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
David Sterbacc4c13d2020-04-29 02:15:56 +02004237 btrfs_set_token_file_extent_disk_bytenr(&token, fi,
Filipe Manana8407f552014-09-05 15:14:39 +01004238 em->block_start -
David Sterbacc4c13d2020-04-29 02:15:56 +02004239 extent_offset);
4240 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
Filipe Manana8407f552014-09-05 15:14:39 +01004241 } else {
David Sterbacc4c13d2020-04-29 02:15:56 +02004242 btrfs_set_token_file_extent_disk_bytenr(&token, fi, 0);
4243 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, 0);
Filipe Manana8407f552014-09-05 15:14:39 +01004244 }
4245
David Sterbacc4c13d2020-04-29 02:15:56 +02004246 btrfs_set_token_file_extent_offset(&token, fi, extent_offset);
4247 btrfs_set_token_file_extent_num_bytes(&token, fi, em->len);
4248 btrfs_set_token_file_extent_ram_bytes(&token, fi, em->ram_bytes);
4249 btrfs_set_token_file_extent_compression(&token, fi, em->compress_type);
4250 btrfs_set_token_file_extent_encryption(&token, fi, 0);
4251 btrfs_set_token_file_extent_other_encoding(&token, fi, 0);
Filipe Manana8407f552014-09-05 15:14:39 +01004252 btrfs_mark_buffer_dirty(leaf);
4253
4254 btrfs_release_path(path);
4255
4256 return ret;
4257}
4258
Filipe Manana31d11b82018-05-09 16:01:46 +01004259/*
4260 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4261 * lose them after doing a fast fsync and replaying the log. We scan the
4262 * subvolume's root instead of iterating the inode's extent map tree because
4263 * otherwise we can log incorrect extent items based on extent map conversion.
4264 * That can happen due to the fact that extent maps are merged when they
4265 * are not in the extent map tree's list of modified extents.
4266 */
4267static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4268 struct btrfs_inode *inode,
4269 struct btrfs_path *path)
4270{
4271 struct btrfs_root *root = inode->root;
4272 struct btrfs_key key;
4273 const u64 i_size = i_size_read(&inode->vfs_inode);
4274 const u64 ino = btrfs_ino(inode);
4275 struct btrfs_path *dst_path = NULL;
Filipe Manana0e563152019-11-19 12:07:33 +00004276 bool dropped_extents = false;
Filipe Mananaf135cea2020-04-23 16:30:53 +01004277 u64 truncate_offset = i_size;
4278 struct extent_buffer *leaf;
4279 int slot;
Filipe Manana31d11b82018-05-09 16:01:46 +01004280 int ins_nr = 0;
4281 int start_slot;
4282 int ret;
4283
4284 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4285 return 0;
4286
4287 key.objectid = ino;
4288 key.type = BTRFS_EXTENT_DATA_KEY;
4289 key.offset = i_size;
4290 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4291 if (ret < 0)
4292 goto out;
4293
Filipe Mananaf135cea2020-04-23 16:30:53 +01004294 /*
4295 * We must check if there is a prealloc extent that starts before the
4296 * i_size and crosses the i_size boundary. This is to ensure later we
4297 * truncate down to the end of that extent and not to the i_size, as
4298 * otherwise we end up losing part of the prealloc extent after a log
4299 * replay and with an implicit hole if there is another prealloc extent
4300 * that starts at an offset beyond i_size.
4301 */
4302 ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
4303 if (ret < 0)
4304 goto out;
4305
4306 if (ret == 0) {
4307 struct btrfs_file_extent_item *ei;
4308
4309 leaf = path->nodes[0];
4310 slot = path->slots[0];
4311 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4312
4313 if (btrfs_file_extent_type(leaf, ei) ==
4314 BTRFS_FILE_EXTENT_PREALLOC) {
4315 u64 extent_end;
4316
4317 btrfs_item_key_to_cpu(leaf, &key, slot);
4318 extent_end = key.offset +
4319 btrfs_file_extent_num_bytes(leaf, ei);
4320
4321 if (extent_end > i_size)
4322 truncate_offset = extent_end;
4323 }
4324 } else {
4325 ret = 0;
4326 }
4327
Filipe Manana31d11b82018-05-09 16:01:46 +01004328 while (true) {
Filipe Mananaf135cea2020-04-23 16:30:53 +01004329 leaf = path->nodes[0];
4330 slot = path->slots[0];
Filipe Manana31d11b82018-05-09 16:01:46 +01004331
4332 if (slot >= btrfs_header_nritems(leaf)) {
4333 if (ins_nr > 0) {
4334 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana0e563152019-11-19 12:07:33 +00004335 start_slot, ins_nr, 1, 0);
Filipe Manana31d11b82018-05-09 16:01:46 +01004336 if (ret < 0)
4337 goto out;
4338 ins_nr = 0;
4339 }
4340 ret = btrfs_next_leaf(root, path);
4341 if (ret < 0)
4342 goto out;
4343 if (ret > 0) {
4344 ret = 0;
4345 break;
4346 }
4347 continue;
4348 }
4349
4350 btrfs_item_key_to_cpu(leaf, &key, slot);
4351 if (key.objectid > ino)
4352 break;
4353 if (WARN_ON_ONCE(key.objectid < ino) ||
4354 key.type < BTRFS_EXTENT_DATA_KEY ||
4355 key.offset < i_size) {
4356 path->slots[0]++;
4357 continue;
4358 }
Filipe Manana0e563152019-11-19 12:07:33 +00004359 if (!dropped_extents) {
Filipe Manana31d11b82018-05-09 16:01:46 +01004360 /*
4361 * Avoid logging extent items logged in past fsync calls
4362 * and leading to duplicate keys in the log tree.
4363 */
4364 do {
4365 ret = btrfs_truncate_inode_items(trans,
4366 root->log_root,
4367 &inode->vfs_inode,
Filipe Mananaf135cea2020-04-23 16:30:53 +01004368 truncate_offset,
Filipe Manana31d11b82018-05-09 16:01:46 +01004369 BTRFS_EXTENT_DATA_KEY);
4370 } while (ret == -EAGAIN);
4371 if (ret)
4372 goto out;
Filipe Manana0e563152019-11-19 12:07:33 +00004373 dropped_extents = true;
Filipe Manana31d11b82018-05-09 16:01:46 +01004374 }
4375 if (ins_nr == 0)
4376 start_slot = slot;
4377 ins_nr++;
4378 path->slots[0]++;
4379 if (!dst_path) {
4380 dst_path = btrfs_alloc_path();
4381 if (!dst_path) {
4382 ret = -ENOMEM;
4383 goto out;
4384 }
4385 }
4386 }
Filipe Manana0bc2d3c2020-04-21 11:25:31 +01004387 if (ins_nr > 0)
Filipe Manana0e563152019-11-19 12:07:33 +00004388 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana31d11b82018-05-09 16:01:46 +01004389 start_slot, ins_nr, 1, 0);
Filipe Manana31d11b82018-05-09 16:01:46 +01004390out:
4391 btrfs_release_path(path);
4392 btrfs_free_path(dst_path);
4393 return ret;
4394}
4395
Josef Bacik5dc562c2012-08-17 13:14:17 -04004396static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4397 struct btrfs_root *root,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004398 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004399 struct btrfs_path *path,
Filipe Manana48778172020-08-11 12:43:58 +01004400 struct btrfs_log_ctx *ctx)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004401{
Filipe Manana48778172020-08-11 12:43:58 +01004402 struct btrfs_ordered_extent *ordered;
4403 struct btrfs_ordered_extent *tmp;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004404 struct extent_map *em, *n;
4405 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004406 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004407 u64 test_gen;
4408 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004409 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004410
4411 INIT_LIST_HEAD(&extents);
4412
Josef Bacik5dc562c2012-08-17 13:14:17 -04004413 write_lock(&tree->lock);
4414 test_gen = root->fs_info->last_trans_committed;
4415
4416 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4417 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004418 /*
4419 * Just an arbitrary number, this can be really CPU intensive
4420 * once we start getting a lot of extents, and really once we
4421 * have a bunch of extents we just want to commit since it will
4422 * be faster.
4423 */
4424 if (++num > 32768) {
4425 list_del_init(&tree->modified_extents);
4426 ret = -EFBIG;
4427 goto process;
4428 }
4429
Josef Bacik5dc562c2012-08-17 13:14:17 -04004430 if (em->generation <= test_gen)
4431 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004432
Filipe Manana31d11b82018-05-09 16:01:46 +01004433 /* We log prealloc extents beyond eof later. */
4434 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4435 em->start >= i_size_read(&inode->vfs_inode))
4436 continue;
4437
Josef Bacikff44c6e2012-09-14 12:59:20 -04004438 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004439 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004440 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004441 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004442 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004443 }
4444
4445 list_sort(NULL, &extents, extent_cmp);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004446process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004447 while (!list_empty(&extents)) {
4448 em = list_entry(extents.next, struct extent_map, list);
4449
4450 list_del_init(&em->list);
4451
4452 /*
4453 * If we had an error we just need to delete everybody from our
4454 * private list.
4455 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004456 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004457 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004458 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004459 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004460 }
4461
4462 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004463
Josef Bacika2120a42018-05-23 11:58:35 -04004464 ret = log_one_extent(trans, inode, root, em, path, ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004465 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004466 clear_em_logging(tree, em);
4467 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004468 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004469 WARN_ON(!list_empty(&extents));
4470 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004471
Josef Bacik5dc562c2012-08-17 13:14:17 -04004472 btrfs_release_path(path);
Filipe Manana31d11b82018-05-09 16:01:46 +01004473 if (!ret)
4474 ret = btrfs_log_prealloc_extents(trans, inode, path);
Filipe Manana48778172020-08-11 12:43:58 +01004475 if (ret)
4476 return ret;
Filipe Manana31d11b82018-05-09 16:01:46 +01004477
Filipe Manana48778172020-08-11 12:43:58 +01004478 /*
4479 * We have logged all extents successfully, now make sure the commit of
4480 * the current transaction waits for the ordered extents to complete
4481 * before it commits and wipes out the log trees, otherwise we would
4482 * lose data if an ordered extents completes after the transaction
4483 * commits and a power failure happens after the transaction commit.
4484 */
4485 list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
4486 list_del_init(&ordered->log_list);
4487 set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
4488
4489 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4490 spin_lock_irq(&inode->ordered_tree.lock);
4491 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4492 set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
4493 atomic_inc(&trans->transaction->pending_ordered);
4494 }
4495 spin_unlock_irq(&inode->ordered_tree.lock);
4496 }
4497 btrfs_put_ordered_extent(ordered);
4498 }
4499
4500 return 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004501}
4502
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004503static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004504 struct btrfs_path *path, u64 *size_ret)
4505{
4506 struct btrfs_key key;
4507 int ret;
4508
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004509 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004510 key.type = BTRFS_INODE_ITEM_KEY;
4511 key.offset = 0;
4512
4513 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4514 if (ret < 0) {
4515 return ret;
4516 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004517 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004518 } else {
4519 struct btrfs_inode_item *item;
4520
4521 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4522 struct btrfs_inode_item);
4523 *size_ret = btrfs_inode_size(path->nodes[0], item);
Filipe Mananabf504112019-03-04 14:06:12 +00004524 /*
4525 * If the in-memory inode's i_size is smaller then the inode
4526 * size stored in the btree, return the inode's i_size, so
4527 * that we get a correct inode size after replaying the log
4528 * when before a power failure we had a shrinking truncate
4529 * followed by addition of a new name (rename / new hard link).
4530 * Otherwise return the inode size from the btree, to avoid
4531 * data loss when replaying a log due to previously doing a
4532 * write that expands the inode's size and logging a new name
4533 * immediately after.
4534 */
4535 if (*size_ret > inode->vfs_inode.i_size)
4536 *size_ret = inode->vfs_inode.i_size;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004537 }
4538
4539 btrfs_release_path(path);
4540 return 0;
4541}
4542
Filipe Manana36283bf2015-06-20 00:44:51 +01004543/*
4544 * At the moment we always log all xattrs. This is to figure out at log replay
4545 * time which xattrs must have their deletion replayed. If a xattr is missing
4546 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4547 * because if a xattr is deleted, the inode is fsynced and a power failure
4548 * happens, causing the log to be replayed the next time the fs is mounted,
4549 * we want the xattr to not exist anymore (same behaviour as other filesystems
4550 * with a journal, ext3/4, xfs, f2fs, etc).
4551 */
4552static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4553 struct btrfs_root *root,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004554 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004555 struct btrfs_path *path,
4556 struct btrfs_path *dst_path)
4557{
4558 int ret;
4559 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004560 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004561 int ins_nr = 0;
4562 int start_slot = 0;
4563
4564 key.objectid = ino;
4565 key.type = BTRFS_XATTR_ITEM_KEY;
4566 key.offset = 0;
4567
4568 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4569 if (ret < 0)
4570 return ret;
4571
4572 while (true) {
4573 int slot = path->slots[0];
4574 struct extent_buffer *leaf = path->nodes[0];
4575 int nritems = btrfs_header_nritems(leaf);
4576
4577 if (slot >= nritems) {
4578 if (ins_nr > 0) {
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004579 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana0e563152019-11-19 12:07:33 +00004580 start_slot, ins_nr, 1, 0);
Filipe Manana36283bf2015-06-20 00:44:51 +01004581 if (ret < 0)
4582 return ret;
4583 ins_nr = 0;
4584 }
4585 ret = btrfs_next_leaf(root, path);
4586 if (ret < 0)
4587 return ret;
4588 else if (ret > 0)
4589 break;
4590 continue;
4591 }
4592
4593 btrfs_item_key_to_cpu(leaf, &key, slot);
4594 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4595 break;
4596
4597 if (ins_nr == 0)
4598 start_slot = slot;
4599 ins_nr++;
4600 path->slots[0]++;
4601 cond_resched();
4602 }
4603 if (ins_nr > 0) {
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004604 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana0e563152019-11-19 12:07:33 +00004605 start_slot, ins_nr, 1, 0);
Filipe Manana36283bf2015-06-20 00:44:51 +01004606 if (ret < 0)
4607 return ret;
4608 }
4609
4610 return 0;
4611}
4612
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004613/*
Filipe Manana0e563152019-11-19 12:07:33 +00004614 * When using the NO_HOLES feature if we punched a hole that causes the
4615 * deletion of entire leafs or all the extent items of the first leaf (the one
4616 * that contains the inode item and references) we may end up not processing
4617 * any extents, because there are no leafs with a generation matching the
4618 * current transaction that have extent items for our inode. So we need to find
4619 * if any holes exist and then log them. We also need to log holes after any
4620 * truncate operation that changes the inode's size.
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004621 */
Filipe Manana0e563152019-11-19 12:07:33 +00004622static int btrfs_log_holes(struct btrfs_trans_handle *trans,
4623 struct btrfs_root *root,
4624 struct btrfs_inode *inode,
Filipe Manana7af59742020-04-07 11:37:44 +01004625 struct btrfs_path *path)
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004626{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004627 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004628 struct btrfs_key key;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004629 const u64 ino = btrfs_ino(inode);
4630 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Manana7af59742020-04-07 11:37:44 +01004631 u64 prev_extent_end = 0;
Filipe Manana0e563152019-11-19 12:07:33 +00004632 int ret;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004633
Filipe Manana0e563152019-11-19 12:07:33 +00004634 if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004635 return 0;
4636
4637 key.objectid = ino;
4638 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana7af59742020-04-07 11:37:44 +01004639 key.offset = 0;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004640
4641 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004642 if (ret < 0)
4643 return ret;
4644
Filipe Manana0e563152019-11-19 12:07:33 +00004645 while (true) {
Filipe Manana0e563152019-11-19 12:07:33 +00004646 struct extent_buffer *leaf = path->nodes[0];
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004647
Filipe Manana0e563152019-11-19 12:07:33 +00004648 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4649 ret = btrfs_next_leaf(root, path);
4650 if (ret < 0)
4651 return ret;
4652 if (ret > 0) {
4653 ret = 0;
4654 break;
4655 }
4656 leaf = path->nodes[0];
4657 }
4658
4659 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4660 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
4661 break;
4662
4663 /* We have a hole, log it. */
4664 if (prev_extent_end < key.offset) {
Filipe Manana7af59742020-04-07 11:37:44 +01004665 const u64 hole_len = key.offset - prev_extent_end;
Filipe Manana0e563152019-11-19 12:07:33 +00004666
4667 /*
4668 * Release the path to avoid deadlocks with other code
4669 * paths that search the root while holding locks on
4670 * leafs from the log root.
4671 */
4672 btrfs_release_path(path);
4673 ret = btrfs_insert_file_extent(trans, root->log_root,
4674 ino, prev_extent_end, 0,
4675 0, hole_len, 0, hole_len,
4676 0, 0, 0);
4677 if (ret < 0)
4678 return ret;
4679
4680 /*
4681 * Search for the same key again in the root. Since it's
4682 * an extent item and we are holding the inode lock, the
4683 * key must still exist. If it doesn't just emit warning
4684 * and return an error to fall back to a transaction
4685 * commit.
4686 */
4687 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4688 if (ret < 0)
4689 return ret;
4690 if (WARN_ON(ret > 0))
4691 return -ENOENT;
4692 leaf = path->nodes[0];
4693 }
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004694
Filipe Manana7af59742020-04-07 11:37:44 +01004695 prev_extent_end = btrfs_file_extent_end(path);
Filipe Manana0e563152019-11-19 12:07:33 +00004696 path->slots[0]++;
4697 cond_resched();
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004698 }
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004699
Filipe Manana7af59742020-04-07 11:37:44 +01004700 if (prev_extent_end < i_size) {
Filipe Manana0e563152019-11-19 12:07:33 +00004701 u64 hole_len;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004702
Filipe Manana0e563152019-11-19 12:07:33 +00004703 btrfs_release_path(path);
Filipe Manana7af59742020-04-07 11:37:44 +01004704 hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
Filipe Manana0e563152019-11-19 12:07:33 +00004705 ret = btrfs_insert_file_extent(trans, root->log_root,
4706 ino, prev_extent_end, 0, 0,
4707 hole_len, 0, hole_len,
4708 0, 0, 0);
4709 if (ret < 0)
4710 return ret;
4711 }
4712
4713 return 0;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004714}
4715
Filipe Manana56f23fd2016-03-30 23:37:21 +01004716/*
4717 * When we are logging a new inode X, check if it doesn't have a reference that
4718 * matches the reference from some other inode Y created in a past transaction
4719 * and that was renamed in the current transaction. If we don't do this, then at
4720 * log replay time we can lose inode Y (and all its files if it's a directory):
4721 *
4722 * mkdir /mnt/x
4723 * echo "hello world" > /mnt/x/foobar
4724 * sync
4725 * mv /mnt/x /mnt/y
4726 * mkdir /mnt/x # or touch /mnt/x
4727 * xfs_io -c fsync /mnt/x
4728 * <power fail>
4729 * mount fs, trigger log replay
4730 *
4731 * After the log replay procedure, we would lose the first directory and all its
4732 * files (file foobar).
4733 * For the case where inode Y is not a directory we simply end up losing it:
4734 *
4735 * echo "123" > /mnt/foo
4736 * sync
4737 * mv /mnt/foo /mnt/bar
4738 * echo "abc" > /mnt/foo
4739 * xfs_io -c fsync /mnt/foo
4740 * <power fail>
4741 *
4742 * We also need this for cases where a snapshot entry is replaced by some other
4743 * entry (file or directory) otherwise we end up with an unreplayable log due to
4744 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4745 * if it were a regular entry:
4746 *
4747 * mkdir /mnt/x
4748 * btrfs subvolume snapshot /mnt /mnt/x/snap
4749 * btrfs subvolume delete /mnt/x/snap
4750 * rmdir /mnt/x
4751 * mkdir /mnt/x
4752 * fsync /mnt/x or fsync some new file inside it
4753 * <power fail>
4754 *
4755 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4756 * the same transaction.
4757 */
4758static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4759 const int slot,
4760 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004761 struct btrfs_inode *inode,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004762 u64 *other_ino, u64 *other_parent)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004763{
4764 int ret;
4765 struct btrfs_path *search_path;
4766 char *name = NULL;
4767 u32 name_len = 0;
4768 u32 item_size = btrfs_item_size_nr(eb, slot);
4769 u32 cur_offset = 0;
4770 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4771
4772 search_path = btrfs_alloc_path();
4773 if (!search_path)
4774 return -ENOMEM;
4775 search_path->search_commit_root = 1;
4776 search_path->skip_locking = 1;
4777
4778 while (cur_offset < item_size) {
4779 u64 parent;
4780 u32 this_name_len;
4781 u32 this_len;
4782 unsigned long name_ptr;
4783 struct btrfs_dir_item *di;
4784
4785 if (key->type == BTRFS_INODE_REF_KEY) {
4786 struct btrfs_inode_ref *iref;
4787
4788 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4789 parent = key->offset;
4790 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4791 name_ptr = (unsigned long)(iref + 1);
4792 this_len = sizeof(*iref) + this_name_len;
4793 } else {
4794 struct btrfs_inode_extref *extref;
4795
4796 extref = (struct btrfs_inode_extref *)(ptr +
4797 cur_offset);
4798 parent = btrfs_inode_extref_parent(eb, extref);
4799 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4800 name_ptr = (unsigned long)&extref->name;
4801 this_len = sizeof(*extref) + this_name_len;
4802 }
4803
4804 if (this_name_len > name_len) {
4805 char *new_name;
4806
4807 new_name = krealloc(name, this_name_len, GFP_NOFS);
4808 if (!new_name) {
4809 ret = -ENOMEM;
4810 goto out;
4811 }
4812 name_len = this_name_len;
4813 name = new_name;
4814 }
4815
4816 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004817 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4818 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004819 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004820 struct btrfs_key di_key;
4821
4822 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4823 di, &di_key);
4824 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
Filipe Manana6b5fc432019-02-13 12:14:03 +00004825 if (di_key.objectid != key->objectid) {
4826 ret = 1;
4827 *other_ino = di_key.objectid;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004828 *other_parent = parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004829 } else {
4830 ret = 0;
4831 }
Filipe Manana44f714d2016-06-06 16:11:13 +01004832 } else {
4833 ret = -EAGAIN;
4834 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004835 goto out;
4836 } else if (IS_ERR(di)) {
4837 ret = PTR_ERR(di);
4838 goto out;
4839 }
4840 btrfs_release_path(search_path);
4841
4842 cur_offset += this_len;
4843 }
4844 ret = 0;
4845out:
4846 btrfs_free_path(search_path);
4847 kfree(name);
4848 return ret;
4849}
4850
Filipe Manana6b5fc432019-02-13 12:14:03 +00004851struct btrfs_ino_list {
4852 u64 ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004853 u64 parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004854 struct list_head list;
4855};
4856
4857static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
4858 struct btrfs_root *root,
4859 struct btrfs_path *path,
4860 struct btrfs_log_ctx *ctx,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004861 u64 ino, u64 parent)
Filipe Manana6b5fc432019-02-13 12:14:03 +00004862{
4863 struct btrfs_ino_list *ino_elem;
4864 LIST_HEAD(inode_list);
4865 int ret = 0;
4866
4867 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
4868 if (!ino_elem)
4869 return -ENOMEM;
4870 ino_elem->ino = ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004871 ino_elem->parent = parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004872 list_add_tail(&ino_elem->list, &inode_list);
4873
4874 while (!list_empty(&inode_list)) {
4875 struct btrfs_fs_info *fs_info = root->fs_info;
4876 struct btrfs_key key;
4877 struct inode *inode;
4878
4879 ino_elem = list_first_entry(&inode_list, struct btrfs_ino_list,
4880 list);
4881 ino = ino_elem->ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004882 parent = ino_elem->parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004883 list_del(&ino_elem->list);
4884 kfree(ino_elem);
4885 if (ret)
4886 continue;
4887
4888 btrfs_release_path(path);
4889
David Sterba0202e832020-05-15 19:35:59 +02004890 inode = btrfs_iget(fs_info->sb, ino, root);
Filipe Manana6b5fc432019-02-13 12:14:03 +00004891 /*
4892 * If the other inode that had a conflicting dir entry was
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004893 * deleted in the current transaction, we need to log its parent
4894 * directory.
Filipe Manana6b5fc432019-02-13 12:14:03 +00004895 */
4896 if (IS_ERR(inode)) {
4897 ret = PTR_ERR(inode);
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004898 if (ret == -ENOENT) {
David Sterba0202e832020-05-15 19:35:59 +02004899 inode = btrfs_iget(fs_info->sb, parent, root);
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004900 if (IS_ERR(inode)) {
4901 ret = PTR_ERR(inode);
4902 } else {
4903 ret = btrfs_log_inode(trans, root,
4904 BTRFS_I(inode),
4905 LOG_OTHER_INODE_ALL,
Filipe Manana48778172020-08-11 12:43:58 +01004906 ctx);
Filipe Manana410f9542019-09-10 15:26:49 +01004907 btrfs_add_delayed_iput(inode);
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004908 }
4909 }
Filipe Manana6b5fc432019-02-13 12:14:03 +00004910 continue;
4911 }
4912 /*
Filipe Mananab5e4ff92020-01-15 13:21:35 +00004913 * If the inode was already logged skip it - otherwise we can
4914 * hit an infinite loop. Example:
4915 *
4916 * From the commit root (previous transaction) we have the
4917 * following inodes:
4918 *
4919 * inode 257 a directory
4920 * inode 258 with references "zz" and "zz_link" on inode 257
4921 * inode 259 with reference "a" on inode 257
4922 *
4923 * And in the current (uncommitted) transaction we have:
4924 *
4925 * inode 257 a directory, unchanged
4926 * inode 258 with references "a" and "a2" on inode 257
4927 * inode 259 with reference "zz_link" on inode 257
4928 * inode 261 with reference "zz" on inode 257
4929 *
4930 * When logging inode 261 the following infinite loop could
4931 * happen if we don't skip already logged inodes:
4932 *
4933 * - we detect inode 258 as a conflicting inode, with inode 261
4934 * on reference "zz", and log it;
4935 *
4936 * - we detect inode 259 as a conflicting inode, with inode 258
4937 * on reference "a", and log it;
4938 *
4939 * - we detect inode 258 as a conflicting inode, with inode 259
4940 * on reference "zz_link", and log it - again! After this we
4941 * repeat the above steps forever.
4942 */
4943 spin_lock(&BTRFS_I(inode)->lock);
4944 /*
4945 * Check the inode's logged_trans only instead of
4946 * btrfs_inode_in_log(). This is because the last_log_commit of
4947 * the inode is not updated when we only log that it exists and
Randy Dunlap260db432020-08-04 19:48:34 -07004948 * it has the full sync bit set (see btrfs_log_inode()).
Filipe Mananab5e4ff92020-01-15 13:21:35 +00004949 */
4950 if (BTRFS_I(inode)->logged_trans == trans->transid) {
4951 spin_unlock(&BTRFS_I(inode)->lock);
4952 btrfs_add_delayed_iput(inode);
4953 continue;
4954 }
4955 spin_unlock(&BTRFS_I(inode)->lock);
4956 /*
Filipe Manana6b5fc432019-02-13 12:14:03 +00004957 * We are safe logging the other inode without acquiring its
4958 * lock as long as we log with the LOG_INODE_EXISTS mode. We
4959 * are safe against concurrent renames of the other inode as
4960 * well because during a rename we pin the log and update the
4961 * log with the new name before we unpin it.
4962 */
4963 ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
Filipe Manana48778172020-08-11 12:43:58 +01004964 LOG_OTHER_INODE, ctx);
Filipe Manana6b5fc432019-02-13 12:14:03 +00004965 if (ret) {
Filipe Manana410f9542019-09-10 15:26:49 +01004966 btrfs_add_delayed_iput(inode);
Filipe Manana6b5fc432019-02-13 12:14:03 +00004967 continue;
4968 }
4969
4970 key.objectid = ino;
4971 key.type = BTRFS_INODE_REF_KEY;
4972 key.offset = 0;
4973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4974 if (ret < 0) {
Filipe Manana410f9542019-09-10 15:26:49 +01004975 btrfs_add_delayed_iput(inode);
Filipe Manana6b5fc432019-02-13 12:14:03 +00004976 continue;
4977 }
4978
4979 while (true) {
4980 struct extent_buffer *leaf = path->nodes[0];
4981 int slot = path->slots[0];
4982 u64 other_ino = 0;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004983 u64 other_parent = 0;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004984
4985 if (slot >= btrfs_header_nritems(leaf)) {
4986 ret = btrfs_next_leaf(root, path);
4987 if (ret < 0) {
4988 break;
4989 } else if (ret > 0) {
4990 ret = 0;
4991 break;
4992 }
4993 continue;
4994 }
4995
4996 btrfs_item_key_to_cpu(leaf, &key, slot);
4997 if (key.objectid != ino ||
4998 (key.type != BTRFS_INODE_REF_KEY &&
4999 key.type != BTRFS_INODE_EXTREF_KEY)) {
5000 ret = 0;
5001 break;
5002 }
5003
5004 ret = btrfs_check_ref_name_override(leaf, slot, &key,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005005 BTRFS_I(inode), &other_ino,
5006 &other_parent);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005007 if (ret < 0)
5008 break;
5009 if (ret > 0) {
5010 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
5011 if (!ino_elem) {
5012 ret = -ENOMEM;
5013 break;
5014 }
5015 ino_elem->ino = other_ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005016 ino_elem->parent = other_parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005017 list_add_tail(&ino_elem->list, &inode_list);
5018 ret = 0;
5019 }
5020 path->slots[0]++;
5021 }
Filipe Manana410f9542019-09-10 15:26:49 +01005022 btrfs_add_delayed_iput(inode);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005023 }
5024
5025 return ret;
5026}
5027
Filipe Mananada447002020-03-09 12:41:07 +00005028static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,
5029 struct btrfs_inode *inode,
5030 struct btrfs_key *min_key,
5031 const struct btrfs_key *max_key,
5032 struct btrfs_path *path,
5033 struct btrfs_path *dst_path,
5034 const u64 logged_isize,
5035 const bool recursive_logging,
5036 const int inode_only,
5037 struct btrfs_log_ctx *ctx,
5038 bool *need_log_inode_item)
5039{
5040 struct btrfs_root *root = inode->root;
5041 int ins_start_slot = 0;
5042 int ins_nr = 0;
5043 int ret;
5044
5045 while (1) {
5046 ret = btrfs_search_forward(root, min_key, path, trans->transid);
5047 if (ret < 0)
5048 return ret;
5049 if (ret > 0) {
5050 ret = 0;
5051 break;
5052 }
5053again:
5054 /* Note, ins_nr might be > 0 here, cleanup outside the loop */
5055 if (min_key->objectid != max_key->objectid)
5056 break;
5057 if (min_key->type > max_key->type)
5058 break;
5059
5060 if (min_key->type == BTRFS_INODE_ITEM_KEY)
5061 *need_log_inode_item = false;
5062
5063 if ((min_key->type == BTRFS_INODE_REF_KEY ||
5064 min_key->type == BTRFS_INODE_EXTREF_KEY) &&
5065 inode->generation == trans->transid &&
5066 !recursive_logging) {
5067 u64 other_ino = 0;
5068 u64 other_parent = 0;
5069
5070 ret = btrfs_check_ref_name_override(path->nodes[0],
5071 path->slots[0], min_key, inode,
5072 &other_ino, &other_parent);
5073 if (ret < 0) {
5074 return ret;
5075 } else if (ret > 0 && ctx &&
5076 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
5077 if (ins_nr > 0) {
5078 ins_nr++;
5079 } else {
5080 ins_nr = 1;
5081 ins_start_slot = path->slots[0];
5082 }
5083 ret = copy_items(trans, inode, dst_path, path,
5084 ins_start_slot, ins_nr,
5085 inode_only, logged_isize);
5086 if (ret < 0)
5087 return ret;
5088 ins_nr = 0;
5089
5090 ret = log_conflicting_inodes(trans, root, path,
5091 ctx, other_ino, other_parent);
5092 if (ret)
5093 return ret;
5094 btrfs_release_path(path);
5095 goto next_key;
5096 }
5097 }
5098
5099 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5100 if (min_key->type == BTRFS_XATTR_ITEM_KEY) {
5101 if (ins_nr == 0)
5102 goto next_slot;
5103 ret = copy_items(trans, inode, dst_path, path,
5104 ins_start_slot,
5105 ins_nr, inode_only, logged_isize);
5106 if (ret < 0)
5107 return ret;
5108 ins_nr = 0;
5109 goto next_slot;
5110 }
5111
5112 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5113 ins_nr++;
5114 goto next_slot;
5115 } else if (!ins_nr) {
5116 ins_start_slot = path->slots[0];
5117 ins_nr = 1;
5118 goto next_slot;
5119 }
5120
5121 ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
5122 ins_nr, inode_only, logged_isize);
5123 if (ret < 0)
5124 return ret;
5125 ins_nr = 1;
5126 ins_start_slot = path->slots[0];
5127next_slot:
5128 path->slots[0]++;
5129 if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
5130 btrfs_item_key_to_cpu(path->nodes[0], min_key,
5131 path->slots[0]);
5132 goto again;
5133 }
5134 if (ins_nr) {
5135 ret = copy_items(trans, inode, dst_path, path,
5136 ins_start_slot, ins_nr, inode_only,
5137 logged_isize);
5138 if (ret < 0)
5139 return ret;
5140 ins_nr = 0;
5141 }
5142 btrfs_release_path(path);
5143next_key:
5144 if (min_key->offset < (u64)-1) {
5145 min_key->offset++;
5146 } else if (min_key->type < max_key->type) {
5147 min_key->type++;
5148 min_key->offset = 0;
5149 } else {
5150 break;
5151 }
5152 }
5153 if (ins_nr)
5154 ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
5155 ins_nr, inode_only, logged_isize);
5156
5157 return ret;
5158}
5159
Chris Masone02119d2008-09-05 16:13:11 -04005160/* log a single inode in the tree log.
5161 * At least one parent directory for this inode must exist in the tree
5162 * or be logged already.
5163 *
5164 * Any items from this inode changed by the current transaction are copied
5165 * to the log tree. An extra reference is taken on any extents in this
5166 * file, allowing us to avoid a whole pile of corner cases around logging
5167 * blocks that have been removed from the tree.
5168 *
5169 * See LOG_INODE_ALL and related defines for a description of what inode_only
5170 * does.
5171 *
5172 * This handles both files and directories.
5173 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005174static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005175 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005176 int inode_only,
Filipe Manana8407f552014-09-05 15:14:39 +01005177 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005178{
5179 struct btrfs_path *path;
5180 struct btrfs_path *dst_path;
5181 struct btrfs_key min_key;
5182 struct btrfs_key max_key;
5183 struct btrfs_root *log = root->log_root;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005184 int err = 0;
Filipe Manana8c8648d2020-07-02 12:31:59 +01005185 int ret = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005186 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005187 u64 ino = btrfs_ino(inode);
5188 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005189 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01005190 bool need_log_inode_item = true;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005191 bool xattrs_logged = false;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005192 bool recursive_logging = false;
Chris Masone02119d2008-09-05 16:13:11 -04005193
Chris Masone02119d2008-09-05 16:13:11 -04005194 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00005195 if (!path)
5196 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04005197 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00005198 if (!dst_path) {
5199 btrfs_free_path(path);
5200 return -ENOMEM;
5201 }
Chris Masone02119d2008-09-05 16:13:11 -04005202
Li Zefan33345d012011-04-20 10:31:50 +08005203 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04005204 min_key.type = BTRFS_INODE_ITEM_KEY;
5205 min_key.offset = 0;
5206
Li Zefan33345d012011-04-20 10:31:50 +08005207 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04005208
Chris Mason12fcfd22009-03-24 10:24:20 -04005209
Josef Bacik5dc562c2012-08-17 13:14:17 -04005210 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005211 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00005212 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005213 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08005214 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04005215 max_key.type = BTRFS_XATTR_ITEM_KEY;
5216 else
5217 max_key.type = (u8)-1;
5218 max_key.offset = (u64)-1;
5219
Filipe Manana2c2c4522015-01-13 16:40:04 +00005220 /*
Filipe Manana5aa7d1a2020-07-02 12:32:20 +01005221 * Only run delayed items if we are a directory. We want to make sure
5222 * all directory indexes hit the fs/subvolume tree so we can find them
5223 * and figure out which index ranges have to be logged.
5224 *
Filipe Manana8c8648d2020-07-02 12:31:59 +01005225 * Otherwise commit the delayed inode only if the full sync flag is set,
5226 * as we want to make sure an up to date version is in the subvolume
5227 * tree so copy_inode_items_to_log() / copy_items() can find it and copy
5228 * it to the log tree. For a non full sync, we always log the inode item
5229 * based on the in-memory struct btrfs_inode which is always up to date.
Filipe Manana2c2c4522015-01-13 16:40:04 +00005230 */
Filipe Manana5aa7d1a2020-07-02 12:32:20 +01005231 if (S_ISDIR(inode->vfs_inode.i_mode))
Nikolay Borisova59108a2017-01-18 00:31:48 +02005232 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana8c8648d2020-07-02 12:31:59 +01005233 else if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
Nikolay Borisova59108a2017-01-18 00:31:48 +02005234 ret = btrfs_commit_inode_delayed_inode(inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00005235
5236 if (ret) {
5237 btrfs_free_path(path);
5238 btrfs_free_path(dst_path);
5239 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08005240 }
5241
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005242 if (inode_only == LOG_OTHER_INODE || inode_only == LOG_OTHER_INODE_ALL) {
5243 recursive_logging = true;
5244 if (inode_only == LOG_OTHER_INODE)
5245 inode_only = LOG_INODE_EXISTS;
5246 else
5247 inode_only = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005248 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08005249 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005250 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08005251 }
Chris Masone02119d2008-09-05 16:13:11 -04005252
Filipe Manana5e33a2b2016-02-25 23:19:38 +00005253 /*
Chris Masone02119d2008-09-05 16:13:11 -04005254 * a brute force approach to making sure we get the most uptodate
5255 * copies of everything.
5256 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005257 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04005258 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
5259
Filipe Manana4f764e52015-02-23 19:53:35 +00005260 if (inode_only == LOG_INODE_EXISTS)
5261 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08005262 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04005263 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005264 if (inode_only == LOG_INODE_EXISTS) {
5265 /*
5266 * Make sure the new inode item we write to the log has
5267 * the same isize as the current one (if it exists).
5268 * This is necessary to prevent data loss after log
5269 * replay, and also to prevent doing a wrong expanding
5270 * truncate - for e.g. create file, write 4K into offset
5271 * 0, fsync, write 4K into offset 4096, add hard link,
5272 * fsync some other file (to sync log), power fail - if
5273 * we use the inode's current i_size, after log replay
5274 * we get a 8Kb file, with the last 4Kb extent as a hole
5275 * (zeroes), as if an expanding truncate happened,
5276 * instead of getting a file of 4Kb only.
5277 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005278 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005279 if (err)
5280 goto out_unlock;
5281 }
Filipe Mananaa7429942015-02-13 16:56:14 +00005282 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005283 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00005284 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005285 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00005286 ret = drop_objectid_items(trans, log, path, ino,
5287 max_key.type);
5288 } else {
5289 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005290 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00005291 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005292 &inode->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08005293 while(1) {
5294 ret = btrfs_truncate_inode_items(trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005295 log, &inode->vfs_inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005296 if (ret != -EAGAIN)
5297 break;
5298 }
Filipe Mananaa7429942015-02-13 16:56:14 +00005299 }
Filipe Manana4f764e52015-02-23 19:53:35 +00005300 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005301 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05005302 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005303 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04005304 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00005305 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04005306 ret = drop_objectid_items(trans, log, path, ino,
5307 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005308 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00005309 if (inode_only == LOG_INODE_ALL)
5310 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04005311 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005312 }
Josef Bacika95249b2012-10-11 16:17:34 -04005313
Chris Masone02119d2008-09-05 16:13:11 -04005314 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005315 if (ret) {
5316 err = ret;
5317 goto out_unlock;
5318 }
Chris Masone02119d2008-09-05 16:13:11 -04005319
Filipe Mananada447002020-03-09 12:41:07 +00005320 err = copy_inode_items_to_log(trans, inode, &min_key, &max_key,
5321 path, dst_path, logged_isize,
Filipe Manana7af59742020-04-07 11:37:44 +01005322 recursive_logging, inode_only, ctx,
5323 &need_log_inode_item);
Filipe Mananada447002020-03-09 12:41:07 +00005324 if (err)
5325 goto out_unlock;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005326
Filipe Manana36283bf2015-06-20 00:44:51 +01005327 btrfs_release_path(path);
5328 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005329 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01005330 if (err)
5331 goto out_unlock;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005332 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005333 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5334 btrfs_release_path(path);
5335 btrfs_release_path(dst_path);
Filipe Manana7af59742020-04-07 11:37:44 +01005336 err = btrfs_log_holes(trans, root, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005337 if (err)
5338 goto out_unlock;
5339 }
Josef Bacika95249b2012-10-11 16:17:34 -04005340log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04005341 btrfs_release_path(path);
5342 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01005343 if (need_log_inode_item) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005344 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana9a8fca62018-05-11 16:42:42 +01005345 if (!err && !xattrs_logged) {
5346 err = btrfs_log_all_xattrs(trans, root, inode, path,
5347 dst_path);
5348 btrfs_release_path(path);
5349 }
Filipe Mananae4545de2015-06-17 12:49:23 +01005350 if (err)
5351 goto out_unlock;
5352 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005353 if (fast_search) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005354 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Manana48778172020-08-11 12:43:58 +01005355 ctx);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005356 if (ret) {
5357 err = ret;
5358 goto out_unlock;
5359 }
Josef Bacikd006a042013-11-12 20:54:09 -05005360 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06005361 struct extent_map *em, *n;
5362
Filipe Manana49dae1b2014-09-06 22:34:39 +01005363 write_lock(&em_tree->lock);
Filipe Manana48778172020-08-11 12:43:58 +01005364 list_for_each_entry_safe(em, n, &em_tree->modified_extents, list)
5365 list_del_init(&em->list);
Filipe Manana49dae1b2014-09-06 22:34:39 +01005366 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005367 }
5368
Nikolay Borisova59108a2017-01-18 00:31:48 +02005369 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5370 ret = log_directory_changes(trans, root, inode, path, dst_path,
5371 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005372 if (ret) {
5373 err = ret;
5374 goto out_unlock;
5375 }
Chris Masone02119d2008-09-05 16:13:11 -04005376 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005377
Filipe Mananad1d832a2019-06-07 11:25:24 +01005378 /*
Filipe Manana75b463d2020-08-11 12:43:48 +01005379 * If we are logging that an ancestor inode exists as part of logging a
5380 * new name from a link or rename operation, don't mark the inode as
5381 * logged - otherwise if an explicit fsync is made against an ancestor,
5382 * the fsync considers the inode in the log and doesn't sync the log,
5383 * resulting in the ancestor missing after a power failure unless the
5384 * log was synced as part of an fsync against any other unrelated inode.
5385 * So keep it simple for this case and just don't flag the ancestors as
5386 * logged.
Filipe Mananad1d832a2019-06-07 11:25:24 +01005387 */
Filipe Manana75b463d2020-08-11 12:43:48 +01005388 if (!ctx ||
5389 !(S_ISDIR(inode->vfs_inode.i_mode) && ctx->logging_new_name &&
5390 &inode->vfs_inode != ctx->inode)) {
5391 spin_lock(&inode->lock);
5392 inode->logged_trans = trans->transid;
5393 /*
5394 * Don't update last_log_commit if we logged that an inode exists
5395 * after it was loaded to memory (full_sync bit set).
5396 * This is to prevent data loss when we do a write to the inode,
5397 * then the inode gets evicted after all delalloc was flushed,
5398 * then we log it exists (due to a rename for example) and then
5399 * fsync it. This last fsync would do nothing (not logging the
5400 * extents previously written).
5401 */
5402 if (inode_only != LOG_INODE_EXISTS ||
5403 !test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
5404 inode->last_log_commit = inode->last_sub_trans;
5405 spin_unlock(&inode->lock);
5406 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005407out_unlock:
Nikolay Borisova59108a2017-01-18 00:31:48 +02005408 mutex_unlock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04005409
5410 btrfs_free_path(path);
5411 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005412 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005413}
5414
Chris Mason12fcfd22009-03-24 10:24:20 -04005415/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005416 * Check if we must fallback to a transaction commit when logging an inode.
5417 * This must be called after logging the inode and is used only in the context
5418 * when fsyncing an inode requires the need to log some other inode - in which
5419 * case we can't lock the i_mutex of each other inode we need to log as that
5420 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5421 * log inodes up or down in the hierarchy) or rename operations for example. So
5422 * we take the log_mutex of the inode after we have logged it and then check for
5423 * its last_unlink_trans value - this is safe because any task setting
5424 * last_unlink_trans must take the log_mutex and it must do this before it does
5425 * the actual unlink operation, so if we do this check before a concurrent task
5426 * sets last_unlink_trans it means we've logged a consistent version/state of
5427 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005428 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005429 * we logged the inode or it might have also done the unlink).
5430 */
5431static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005432 struct btrfs_inode *inode)
Filipe Manana2be63d52016-02-12 11:34:23 +00005433{
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005434 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana2be63d52016-02-12 11:34:23 +00005435 bool ret = false;
5436
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005437 mutex_lock(&inode->log_mutex);
5438 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
Filipe Manana2be63d52016-02-12 11:34:23 +00005439 /*
5440 * Make sure any commits to the log are forced to be full
5441 * commits.
5442 */
David Sterba90787762019-03-20 13:28:05 +01005443 btrfs_set_log_full_commit(trans);
Filipe Manana2be63d52016-02-12 11:34:23 +00005444 ret = true;
5445 }
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005446 mutex_unlock(&inode->log_mutex);
Filipe Manana2be63d52016-02-12 11:34:23 +00005447
5448 return ret;
5449}
5450
5451/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005452 * follow the dentry parent pointers up the chain and see if any
5453 * of the directories in it require a full commit before they can
5454 * be logged. Returns zero if nothing special needs to be done or 1 if
5455 * a full commit is required.
5456 */
5457static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005458 struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005459 struct dentry *parent,
5460 struct super_block *sb,
5461 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005462{
Chris Mason12fcfd22009-03-24 10:24:20 -04005463 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005464 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005465
Chris Masonaf4176b2009-03-24 10:24:31 -04005466 /*
5467 * for regular files, if its inode is already on disk, we don't
5468 * have to worry about the parents at all. This is because
5469 * we can use the last_unlink_trans field to record renames
5470 * and other fun in this file.
5471 */
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005472 if (S_ISREG(inode->vfs_inode.i_mode) &&
5473 inode->generation <= last_committed &&
5474 inode->last_unlink_trans <= last_committed)
5475 goto out;
Chris Masonaf4176b2009-03-24 10:24:31 -04005476
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005477 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005478 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005479 goto out;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005480 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005481 }
5482
5483 while (1) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005484 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005485 ret = 1;
5486 break;
5487 }
5488
Al Virofc640052016-04-10 01:33:30 -04005489 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005490 break;
5491
Filipe Manana44f714d2016-06-06 16:11:13 +01005492 if (IS_ROOT(parent)) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005493 inode = BTRFS_I(d_inode(parent));
5494 if (btrfs_must_commit_transaction(trans, inode))
Filipe Manana44f714d2016-06-06 16:11:13 +01005495 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005496 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005497 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005498
Josef Bacik6a912212010-11-20 09:48:00 +00005499 parent = dget_parent(parent);
5500 dput(old_parent);
5501 old_parent = parent;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005502 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005503
5504 }
Josef Bacik6a912212010-11-20 09:48:00 +00005505 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005506out:
Chris Masone02119d2008-09-05 16:13:11 -04005507 return ret;
5508}
5509
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005510struct btrfs_dir_list {
5511 u64 ino;
5512 struct list_head list;
5513};
5514
5515/*
5516 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5517 * details about the why it is needed.
5518 * This is a recursive operation - if an existing dentry corresponds to a
5519 * directory, that directory's new entries are logged too (same behaviour as
5520 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5521 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5522 * complains about the following circular lock dependency / possible deadlock:
5523 *
5524 * CPU0 CPU1
5525 * ---- ----
5526 * lock(&type->i_mutex_dir_key#3/2);
5527 * lock(sb_internal#2);
5528 * lock(&type->i_mutex_dir_key#3/2);
5529 * lock(&sb->s_type->i_mutex_key#14);
5530 *
5531 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5532 * sb_start_intwrite() in btrfs_start_transaction().
5533 * Not locking i_mutex of the inodes is still safe because:
5534 *
5535 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5536 * that while logging the inode new references (names) are added or removed
5537 * from the inode, leaving the logged inode item with a link count that does
5538 * not match the number of logged inode reference items. This is fine because
5539 * at log replay time we compute the real number of links and correct the
5540 * link count in the inode item (see replay_one_buffer() and
5541 * link_to_fixup_dir());
5542 *
5543 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5544 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5545 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5546 * has a size that doesn't match the sum of the lengths of all the logged
5547 * names. This does not result in a problem because if a dir_item key is
5548 * logged but its matching dir_index key is not logged, at log replay time we
5549 * don't use it to replay the respective name (see replay_one_name()). On the
5550 * other hand if only the dir_index key ends up being logged, the respective
5551 * name is added to the fs/subvol tree with both the dir_item and dir_index
5552 * keys created (see replay_one_name()).
5553 * The directory's inode item with a wrong i_size is not a problem as well,
5554 * since we don't use it at log replay time to set the i_size in the inode
5555 * item of the fs/subvol tree (see overwrite_item()).
5556 */
5557static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5558 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005559 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005560 struct btrfs_log_ctx *ctx)
5561{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005562 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005563 struct btrfs_root *log = root->log_root;
5564 struct btrfs_path *path;
5565 LIST_HEAD(dir_list);
5566 struct btrfs_dir_list *dir_elem;
5567 int ret = 0;
5568
5569 path = btrfs_alloc_path();
5570 if (!path)
5571 return -ENOMEM;
5572
5573 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5574 if (!dir_elem) {
5575 btrfs_free_path(path);
5576 return -ENOMEM;
5577 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005578 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005579 list_add_tail(&dir_elem->list, &dir_list);
5580
5581 while (!list_empty(&dir_list)) {
5582 struct extent_buffer *leaf;
5583 struct btrfs_key min_key;
5584 int nritems;
5585 int i;
5586
5587 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5588 list);
5589 if (ret)
5590 goto next_dir_inode;
5591
5592 min_key.objectid = dir_elem->ino;
5593 min_key.type = BTRFS_DIR_ITEM_KEY;
5594 min_key.offset = 0;
5595again:
5596 btrfs_release_path(path);
5597 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5598 if (ret < 0) {
5599 goto next_dir_inode;
5600 } else if (ret > 0) {
5601 ret = 0;
5602 goto next_dir_inode;
5603 }
5604
5605process_leaf:
5606 leaf = path->nodes[0];
5607 nritems = btrfs_header_nritems(leaf);
5608 for (i = path->slots[0]; i < nritems; i++) {
5609 struct btrfs_dir_item *di;
5610 struct btrfs_key di_key;
5611 struct inode *di_inode;
5612 struct btrfs_dir_list *new_dir_elem;
5613 int log_mode = LOG_INODE_EXISTS;
5614 int type;
5615
5616 btrfs_item_key_to_cpu(leaf, &min_key, i);
5617 if (min_key.objectid != dir_elem->ino ||
5618 min_key.type != BTRFS_DIR_ITEM_KEY)
5619 goto next_dir_inode;
5620
5621 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5622 type = btrfs_dir_type(leaf, di);
5623 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5624 type != BTRFS_FT_DIR)
5625 continue;
5626 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5627 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5628 continue;
5629
Robbie Koec125cf2016-10-28 10:48:26 +08005630 btrfs_release_path(path);
David Sterba0202e832020-05-15 19:35:59 +02005631 di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005632 if (IS_ERR(di_inode)) {
5633 ret = PTR_ERR(di_inode);
5634 goto next_dir_inode;
5635 }
5636
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005637 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
Filipe Manana410f9542019-09-10 15:26:49 +01005638 btrfs_add_delayed_iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005639 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005640 }
5641
5642 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005643 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005644 log_mode = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005645 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Filipe Manana48778172020-08-11 12:43:58 +01005646 log_mode, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005647 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005648 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005649 ret = 1;
Filipe Manana410f9542019-09-10 15:26:49 +01005650 btrfs_add_delayed_iput(di_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005651 if (ret)
5652 goto next_dir_inode;
5653 if (ctx->log_new_dentries) {
5654 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5655 GFP_NOFS);
5656 if (!new_dir_elem) {
5657 ret = -ENOMEM;
5658 goto next_dir_inode;
5659 }
5660 new_dir_elem->ino = di_key.objectid;
5661 list_add_tail(&new_dir_elem->list, &dir_list);
5662 }
5663 break;
5664 }
5665 if (i == nritems) {
5666 ret = btrfs_next_leaf(log, path);
5667 if (ret < 0) {
5668 goto next_dir_inode;
5669 } else if (ret > 0) {
5670 ret = 0;
5671 goto next_dir_inode;
5672 }
5673 goto process_leaf;
5674 }
5675 if (min_key.offset < (u64)-1) {
5676 min_key.offset++;
5677 goto again;
5678 }
5679next_dir_inode:
5680 list_del(&dir_elem->list);
5681 kfree(dir_elem);
5682 }
5683
5684 btrfs_free_path(path);
5685 return ret;
5686}
5687
Filipe Manana18aa0922015-08-05 16:49:08 +01005688static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005689 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01005690 struct btrfs_log_ctx *ctx)
5691{
David Sterba3ffbd682018-06-29 10:56:42 +02005692 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana18aa0922015-08-05 16:49:08 +01005693 int ret;
5694 struct btrfs_path *path;
5695 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005696 struct btrfs_root *root = inode->root;
5697 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005698
5699 path = btrfs_alloc_path();
5700 if (!path)
5701 return -ENOMEM;
5702 path->skip_locking = 1;
5703 path->search_commit_root = 1;
5704
5705 key.objectid = ino;
5706 key.type = BTRFS_INODE_REF_KEY;
5707 key.offset = 0;
5708 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5709 if (ret < 0)
5710 goto out;
5711
5712 while (true) {
5713 struct extent_buffer *leaf = path->nodes[0];
5714 int slot = path->slots[0];
5715 u32 cur_offset = 0;
5716 u32 item_size;
5717 unsigned long ptr;
5718
5719 if (slot >= btrfs_header_nritems(leaf)) {
5720 ret = btrfs_next_leaf(root, path);
5721 if (ret < 0)
5722 goto out;
5723 else if (ret > 0)
5724 break;
5725 continue;
5726 }
5727
5728 btrfs_item_key_to_cpu(leaf, &key, slot);
5729 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5730 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5731 break;
5732
5733 item_size = btrfs_item_size_nr(leaf, slot);
5734 ptr = btrfs_item_ptr_offset(leaf, slot);
5735 while (cur_offset < item_size) {
5736 struct btrfs_key inode_key;
5737 struct inode *dir_inode;
5738
5739 inode_key.type = BTRFS_INODE_ITEM_KEY;
5740 inode_key.offset = 0;
5741
5742 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5743 struct btrfs_inode_extref *extref;
5744
5745 extref = (struct btrfs_inode_extref *)
5746 (ptr + cur_offset);
5747 inode_key.objectid = btrfs_inode_extref_parent(
5748 leaf, extref);
5749 cur_offset += sizeof(*extref);
5750 cur_offset += btrfs_inode_extref_name_len(leaf,
5751 extref);
5752 } else {
5753 inode_key.objectid = key.offset;
5754 cur_offset = item_size;
5755 }
5756
David Sterba0202e832020-05-15 19:35:59 +02005757 dir_inode = btrfs_iget(fs_info->sb, inode_key.objectid,
5758 root);
Filipe Manana0f375ee2018-10-09 15:05:29 +01005759 /*
5760 * If the parent inode was deleted, return an error to
5761 * fallback to a transaction commit. This is to prevent
5762 * getting an inode that was moved from one parent A to
5763 * a parent B, got its former parent A deleted and then
5764 * it got fsync'ed, from existing at both parents after
5765 * a log replay (and the old parent still existing).
5766 * Example:
5767 *
5768 * mkdir /mnt/A
5769 * mkdir /mnt/B
5770 * touch /mnt/B/bar
5771 * sync
5772 * mv /mnt/B/bar /mnt/A/bar
5773 * mv -T /mnt/A /mnt/B
5774 * fsync /mnt/B/bar
5775 * <power fail>
5776 *
5777 * If we ignore the old parent B which got deleted,
5778 * after a log replay we would have file bar linked
5779 * at both parents and the old parent B would still
5780 * exist.
5781 */
5782 if (IS_ERR(dir_inode)) {
5783 ret = PTR_ERR(dir_inode);
5784 goto out;
5785 }
Filipe Manana18aa0922015-08-05 16:49:08 +01005786
Filipe Manana657ed1a2016-04-06 17:11:56 +01005787 if (ctx)
5788 ctx->log_new_dentries = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005789 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Filipe Manana48778172020-08-11 12:43:58 +01005790 LOG_INODE_ALL, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005791 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005792 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005793 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005794 if (!ret && ctx && ctx->log_new_dentries)
5795 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01005796 BTRFS_I(dir_inode), ctx);
Filipe Manana410f9542019-09-10 15:26:49 +01005797 btrfs_add_delayed_iput(dir_inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005798 if (ret)
5799 goto out;
5800 }
5801 path->slots[0]++;
5802 }
5803 ret = 0;
5804out:
5805 btrfs_free_path(path);
5806 return ret;
5807}
5808
Filipe Mananab8aa3302019-04-17 11:31:06 +01005809static int log_new_ancestors(struct btrfs_trans_handle *trans,
5810 struct btrfs_root *root,
5811 struct btrfs_path *path,
5812 struct btrfs_log_ctx *ctx)
5813{
5814 struct btrfs_key found_key;
5815
5816 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
5817
5818 while (true) {
5819 struct btrfs_fs_info *fs_info = root->fs_info;
5820 const u64 last_committed = fs_info->last_trans_committed;
5821 struct extent_buffer *leaf = path->nodes[0];
5822 int slot = path->slots[0];
5823 struct btrfs_key search_key;
5824 struct inode *inode;
David Sterba0202e832020-05-15 19:35:59 +02005825 u64 ino;
Filipe Mananab8aa3302019-04-17 11:31:06 +01005826 int ret = 0;
5827
5828 btrfs_release_path(path);
5829
David Sterba0202e832020-05-15 19:35:59 +02005830 ino = found_key.offset;
5831
Filipe Mananab8aa3302019-04-17 11:31:06 +01005832 search_key.objectid = found_key.offset;
5833 search_key.type = BTRFS_INODE_ITEM_KEY;
5834 search_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02005835 inode = btrfs_iget(fs_info->sb, ino, root);
Filipe Mananab8aa3302019-04-17 11:31:06 +01005836 if (IS_ERR(inode))
5837 return PTR_ERR(inode);
5838
5839 if (BTRFS_I(inode)->generation > last_committed)
5840 ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
Filipe Manana48778172020-08-11 12:43:58 +01005841 LOG_INODE_EXISTS, ctx);
Filipe Manana410f9542019-09-10 15:26:49 +01005842 btrfs_add_delayed_iput(inode);
Filipe Mananab8aa3302019-04-17 11:31:06 +01005843 if (ret)
5844 return ret;
5845
5846 if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
5847 break;
5848
5849 search_key.type = BTRFS_INODE_REF_KEY;
5850 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
5851 if (ret < 0)
5852 return ret;
5853
5854 leaf = path->nodes[0];
5855 slot = path->slots[0];
5856 if (slot >= btrfs_header_nritems(leaf)) {
5857 ret = btrfs_next_leaf(root, path);
5858 if (ret < 0)
5859 return ret;
5860 else if (ret > 0)
5861 return -ENOENT;
5862 leaf = path->nodes[0];
5863 slot = path->slots[0];
5864 }
5865
5866 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5867 if (found_key.objectid != search_key.objectid ||
5868 found_key.type != BTRFS_INODE_REF_KEY)
5869 return -ENOENT;
5870 }
5871 return 0;
5872}
5873
5874static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
5875 struct btrfs_inode *inode,
5876 struct dentry *parent,
5877 struct btrfs_log_ctx *ctx)
5878{
5879 struct btrfs_root *root = inode->root;
5880 struct btrfs_fs_info *fs_info = root->fs_info;
5881 struct dentry *old_parent = NULL;
5882 struct super_block *sb = inode->vfs_inode.i_sb;
5883 int ret = 0;
5884
5885 while (true) {
5886 if (!parent || d_really_is_negative(parent) ||
5887 sb != parent->d_sb)
5888 break;
5889
5890 inode = BTRFS_I(d_inode(parent));
5891 if (root != inode->root)
5892 break;
5893
5894 if (inode->generation > fs_info->last_trans_committed) {
5895 ret = btrfs_log_inode(trans, root, inode,
Filipe Manana48778172020-08-11 12:43:58 +01005896 LOG_INODE_EXISTS, ctx);
Filipe Mananab8aa3302019-04-17 11:31:06 +01005897 if (ret)
5898 break;
5899 }
5900 if (IS_ROOT(parent))
5901 break;
5902
5903 parent = dget_parent(parent);
5904 dput(old_parent);
5905 old_parent = parent;
5906 }
5907 dput(old_parent);
5908
5909 return ret;
5910}
5911
5912static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
5913 struct btrfs_inode *inode,
5914 struct dentry *parent,
5915 struct btrfs_log_ctx *ctx)
5916{
5917 struct btrfs_root *root = inode->root;
5918 const u64 ino = btrfs_ino(inode);
5919 struct btrfs_path *path;
5920 struct btrfs_key search_key;
5921 int ret;
5922
5923 /*
5924 * For a single hard link case, go through a fast path that does not
5925 * need to iterate the fs/subvolume tree.
5926 */
5927 if (inode->vfs_inode.i_nlink < 2)
5928 return log_new_ancestors_fast(trans, inode, parent, ctx);
5929
5930 path = btrfs_alloc_path();
5931 if (!path)
5932 return -ENOMEM;
5933
5934 search_key.objectid = ino;
5935 search_key.type = BTRFS_INODE_REF_KEY;
5936 search_key.offset = 0;
5937again:
5938 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
5939 if (ret < 0)
5940 goto out;
5941 if (ret == 0)
5942 path->slots[0]++;
5943
5944 while (true) {
5945 struct extent_buffer *leaf = path->nodes[0];
5946 int slot = path->slots[0];
5947 struct btrfs_key found_key;
5948
5949 if (slot >= btrfs_header_nritems(leaf)) {
5950 ret = btrfs_next_leaf(root, path);
5951 if (ret < 0)
5952 goto out;
5953 else if (ret > 0)
5954 break;
5955 continue;
5956 }
5957
5958 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5959 if (found_key.objectid != ino ||
5960 found_key.type > BTRFS_INODE_EXTREF_KEY)
5961 break;
5962
5963 /*
5964 * Don't deal with extended references because they are rare
5965 * cases and too complex to deal with (we would need to keep
5966 * track of which subitem we are processing for each item in
5967 * this loop, etc). So just return some error to fallback to
5968 * a transaction commit.
5969 */
5970 if (found_key.type == BTRFS_INODE_EXTREF_KEY) {
5971 ret = -EMLINK;
5972 goto out;
5973 }
5974
5975 /*
5976 * Logging ancestors needs to do more searches on the fs/subvol
5977 * tree, so it releases the path as needed to avoid deadlocks.
5978 * Keep track of the last inode ref key and resume from that key
5979 * after logging all new ancestors for the current hard link.
5980 */
5981 memcpy(&search_key, &found_key, sizeof(search_key));
5982
5983 ret = log_new_ancestors(trans, root, path, ctx);
5984 if (ret)
5985 goto out;
5986 btrfs_release_path(path);
5987 goto again;
5988 }
5989 ret = 0;
5990out:
5991 btrfs_free_path(path);
5992 return ret;
5993}
5994
Chris Masone02119d2008-09-05 16:13:11 -04005995/*
5996 * helper function around btrfs_log_inode to make sure newly created
5997 * parent directories also end up in the log. A minimal inode and backref
5998 * only logging is done of any parent directories that are older than
5999 * the last committed transaction
6000 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006001static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006002 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01006003 struct dentry *parent,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07006004 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08006005 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04006006{
Nikolay Borisovf8822742018-02-27 17:37:17 +02006007 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006008 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04006009 struct super_block *sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04006010 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006011 u64 last_committed = fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006012 bool log_dentries = false;
Chris Mason12fcfd22009-03-24 10:24:20 -04006013
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006014 sb = inode->vfs_inode.i_sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04006015
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006016 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04006017 ret = 1;
6018 goto end_no_trans;
6019 }
6020
Miao Xie995946d2014-04-02 19:51:06 +08006021 /*
6022 * The prev transaction commit doesn't complete, we need do
6023 * full commit by ourselves.
6024 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006025 if (fs_info->last_trans_log_full_commit >
6026 fs_info->last_trans_committed) {
Chris Mason12fcfd22009-03-24 10:24:20 -04006027 ret = 1;
6028 goto end_no_trans;
6029 }
6030
Nikolay Borisovf8822742018-02-27 17:37:17 +02006031 if (btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04006032 ret = 1;
6033 goto end_no_trans;
6034 }
6035
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006036 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
6037 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04006038 if (ret)
6039 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04006040
Filipe Mananaf2d72f42018-10-08 11:12:55 +01006041 /*
6042 * Skip already logged inodes or inodes corresponding to tmpfiles
6043 * (since logging them is pointless, a link count of 0 means they
6044 * will never be accessible).
6045 */
6046 if (btrfs_inode_in_log(inode, trans->transid) ||
6047 inode->vfs_inode.i_nlink == 0) {
Chris Mason257c62e2009-10-13 13:21:08 -04006048 ret = BTRFS_NO_LOG_SYNC;
6049 goto end_no_trans;
6050 }
6051
Miao Xie8b050d32014-02-20 18:08:58 +08006052 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006053 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08006054 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04006055
Filipe Manana48778172020-08-11 12:43:58 +01006056 ret = btrfs_log_inode(trans, root, inode, inode_only, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006057 if (ret)
6058 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04006059
Chris Masonaf4176b2009-03-24 10:24:31 -04006060 /*
6061 * for regular files, if its inode is already on disk, we don't
6062 * have to worry about the parents at all. This is because
6063 * we can use the last_unlink_trans field to record renames
6064 * and other fun in this file.
6065 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006066 if (S_ISREG(inode->vfs_inode.i_mode) &&
6067 inode->generation <= last_committed &&
6068 inode->last_unlink_trans <= last_committed) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006069 ret = 0;
6070 goto end_trans;
6071 }
Chris Masonaf4176b2009-03-24 10:24:31 -04006072
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006073 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006074 log_dentries = true;
6075
Filipe Manana18aa0922015-08-05 16:49:08 +01006076 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006077 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01006078 * inodes are fully logged. This is to prevent leaving dangling
6079 * directory index entries in directories that were our parents but are
6080 * not anymore. Not doing this results in old parent directory being
6081 * impossible to delete after log replay (rmdir will always fail with
6082 * error -ENOTEMPTY).
6083 *
6084 * Example 1:
6085 *
6086 * mkdir testdir
6087 * touch testdir/foo
6088 * ln testdir/foo testdir/bar
6089 * sync
6090 * unlink testdir/bar
6091 * xfs_io -c fsync testdir/foo
6092 * <power failure>
6093 * mount fs, triggers log replay
6094 *
6095 * If we don't log the parent directory (testdir), after log replay the
6096 * directory still has an entry pointing to the file inode using the bar
6097 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
6098 * the file inode has a link count of 1.
6099 *
6100 * Example 2:
6101 *
6102 * mkdir testdir
6103 * touch foo
6104 * ln foo testdir/foo2
6105 * ln foo testdir/foo3
6106 * sync
6107 * unlink testdir/foo3
6108 * xfs_io -c fsync foo
6109 * <power failure>
6110 * mount fs, triggers log replay
6111 *
6112 * Similar as the first example, after log replay the parent directory
6113 * testdir still has an entry pointing to the inode file with name foo3
6114 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
6115 * and has a link count of 2.
6116 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006117 if (inode->last_unlink_trans > last_committed) {
Filipe Mananab8aa3302019-04-17 11:31:06 +01006118 ret = btrfs_log_all_parents(trans, inode, ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01006119 if (ret)
6120 goto end_trans;
6121 }
6122
Filipe Mananab8aa3302019-04-17 11:31:06 +01006123 ret = log_all_new_ancestors(trans, inode, parent, ctx);
6124 if (ret)
Filipe Manana41bd6062018-11-28 14:54:28 +00006125 goto end_trans;
Filipe Manana41bd6062018-11-28 14:54:28 +00006126
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006127 if (log_dentries)
Filipe Mananab8aa3302019-04-17 11:31:06 +01006128 ret = log_new_dir_dentries(trans, root, inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006129 else
6130 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006131end_trans:
6132 if (ret < 0) {
David Sterba90787762019-03-20 13:28:05 +01006133 btrfs_set_log_full_commit(trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006134 ret = 1;
6135 }
Miao Xie8b050d32014-02-20 18:08:58 +08006136
6137 if (ret)
6138 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04006139 btrfs_end_log_trans(root);
6140end_no_trans:
6141 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006142}
6143
6144/*
6145 * it is not safe to log dentry if the chunk root has added new
6146 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
6147 * If this returns 1, you must commit the transaction to safely get your
6148 * data on disk.
6149 */
6150int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Nikolay Borisove5b84f7a2018-02-27 17:37:18 +02006151 struct dentry *dentry,
Miao Xie8b050d32014-02-20 18:08:58 +08006152 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04006153{
Josef Bacik6a912212010-11-20 09:48:00 +00006154 struct dentry *parent = dget_parent(dentry);
6155 int ret;
6156
Nikolay Borisovf8822742018-02-27 17:37:17 +02006157 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
Filipe Manana48778172020-08-11 12:43:58 +01006158 LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00006159 dput(parent);
6160
6161 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006162}
6163
6164/*
6165 * should be called during mount to recover any replay any log trees
6166 * from the FS
6167 */
6168int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
6169{
6170 int ret;
6171 struct btrfs_path *path;
6172 struct btrfs_trans_handle *trans;
6173 struct btrfs_key key;
6174 struct btrfs_key found_key;
Chris Masone02119d2008-09-05 16:13:11 -04006175 struct btrfs_root *log;
6176 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
6177 struct walk_control wc = {
6178 .process_func = process_one_buffer,
David Sterba430a6622019-08-01 14:50:35 +02006179 .stage = LOG_WALK_PIN_ONLY,
Chris Masone02119d2008-09-05 16:13:11 -04006180 };
6181
Chris Masone02119d2008-09-05 16:13:11 -04006182 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006183 if (!path)
6184 return -ENOMEM;
6185
Josef Bacikafcdd122016-09-02 15:40:02 -04006186 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04006187
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006188 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006189 if (IS_ERR(trans)) {
6190 ret = PTR_ERR(trans);
6191 goto error;
6192 }
Chris Masone02119d2008-09-05 16:13:11 -04006193
6194 wc.trans = trans;
6195 wc.pin = 1;
6196
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006197 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006198 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006199 btrfs_handle_fs_error(fs_info, ret,
6200 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006201 goto error;
6202 }
Chris Masone02119d2008-09-05 16:13:11 -04006203
6204again:
6205 key.objectid = BTRFS_TREE_LOG_OBJECTID;
6206 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02006207 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04006208
Chris Masond3977122009-01-05 21:25:51 -05006209 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04006210 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006211
6212 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08006213 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006214 "Couldn't find tree log root.");
6215 goto error;
6216 }
Chris Masone02119d2008-09-05 16:13:11 -04006217 if (ret > 0) {
6218 if (path->slots[0] == 0)
6219 break;
6220 path->slots[0]--;
6221 }
6222 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
6223 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02006224 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006225 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
6226 break;
6227
Josef Bacik62a2c732020-01-24 09:32:21 -05006228 log = btrfs_read_tree_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006229 if (IS_ERR(log)) {
6230 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08006231 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006232 "Couldn't read tree log root.");
6233 goto error;
6234 }
Chris Masone02119d2008-09-05 16:13:11 -04006235
David Sterba56e93572020-05-15 19:35:55 +02006236 wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset,
6237 true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006238 if (IS_ERR(wc.replay_dest)) {
6239 ret = PTR_ERR(wc.replay_dest);
Josef Bacik9bc574d2019-12-06 09:37:17 -05006240
6241 /*
6242 * We didn't find the subvol, likely because it was
6243 * deleted. This is ok, simply skip this log and go to
6244 * the next one.
6245 *
6246 * We need to exclude the root because we can't have
6247 * other log replays overwriting this log as we'll read
6248 * it back in a few more times. This will keep our
6249 * block from being modified, and we'll just bail for
6250 * each subsequent pass.
6251 */
6252 if (ret == -ENOENT)
Nikolay Borisov9fce5702020-01-20 16:09:13 +02006253 ret = btrfs_pin_extent_for_log_replay(trans,
Josef Bacik9bc574d2019-12-06 09:37:17 -05006254 log->node->start,
6255 log->node->len);
Josef Bacik00246522020-01-24 09:33:01 -05006256 btrfs_put_root(log);
Josef Bacik9bc574d2019-12-06 09:37:17 -05006257
6258 if (!ret)
6259 goto next;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006260 btrfs_handle_fs_error(fs_info, ret,
6261 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006262 goto error;
6263 }
Chris Masone02119d2008-09-05 16:13:11 -04006264
Yan Zheng07d400a2009-01-06 11:42:00 -05006265 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006266 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04006267 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04006268
Josef Bacikb50c6e22013-04-25 15:55:30 -04006269 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04006270 ret = fixup_inode_link_counts(trans, wc.replay_dest,
6271 path);
Chris Masone02119d2008-09-05 16:13:11 -04006272 }
Chris Masone02119d2008-09-05 16:13:11 -04006273
Liu Bo900c9982018-01-25 11:02:56 -07006274 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
6275 struct btrfs_root *root = wc.replay_dest;
6276
6277 btrfs_release_path(path);
6278
6279 /*
6280 * We have just replayed everything, and the highest
6281 * objectid of fs roots probably has changed in case
6282 * some inode_item's got replayed.
6283 *
6284 * root->objectid_mutex is not acquired as log replay
6285 * could only happen during mount.
6286 */
6287 ret = btrfs_find_highest_objectid(root,
6288 &root->highest_objectid);
6289 }
6290
Yan Zheng07d400a2009-01-06 11:42:00 -05006291 wc.replay_dest->log_root = NULL;
Josef Bacik00246522020-01-24 09:33:01 -05006292 btrfs_put_root(wc.replay_dest);
Josef Bacik00246522020-01-24 09:33:01 -05006293 btrfs_put_root(log);
Chris Masone02119d2008-09-05 16:13:11 -04006294
Josef Bacikb50c6e22013-04-25 15:55:30 -04006295 if (ret)
6296 goto error;
Josef Bacik9bc574d2019-12-06 09:37:17 -05006297next:
Chris Masone02119d2008-09-05 16:13:11 -04006298 if (found_key.offset == 0)
6299 break;
Josef Bacik9bc574d2019-12-06 09:37:17 -05006300 key.offset = found_key.offset - 1;
Chris Masone02119d2008-09-05 16:13:11 -04006301 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006302 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006303
6304 /* step one is to pin it all, step two is to replay just inodes */
6305 if (wc.pin) {
6306 wc.pin = 0;
6307 wc.process_func = replay_one_buffer;
6308 wc.stage = LOG_WALK_REPLAY_INODES;
6309 goto again;
6310 }
6311 /* step three is to replay everything */
6312 if (wc.stage < LOG_WALK_REPLAY_ALL) {
6313 wc.stage++;
6314 goto again;
6315 }
6316
6317 btrfs_free_path(path);
6318
Josef Bacikabefa552013-04-24 16:40:05 -04006319 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006320 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04006321 if (ret)
6322 return ret;
6323
Chris Masone02119d2008-09-05 16:13:11 -04006324 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04006325 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Josef Bacik00246522020-01-24 09:33:01 -05006326 btrfs_put_root(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006327
Josef Bacikabefa552013-04-24 16:40:05 -04006328 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006329error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04006330 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006331 btrfs_end_transaction(wc.trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006332 btrfs_free_path(path);
6333 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006334}
Chris Mason12fcfd22009-03-24 10:24:20 -04006335
6336/*
6337 * there are some corner cases where we want to force a full
6338 * commit instead of allowing a directory to be logged.
6339 *
6340 * They revolve around files there were unlinked from the directory, and
6341 * this function updates the parent directory so that a full commit is
6342 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00006343 *
6344 * Must be called before the unlink operations (updates to the subvolume tree,
6345 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04006346 */
6347void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006348 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04006349 int for_rename)
6350{
6351 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006352 * when we're logging a file, if it hasn't been renamed
6353 * or unlinked, and its inode is fully committed on disk,
6354 * we don't have to worry about walking up the directory chain
6355 * to log its parents.
6356 *
6357 * So, we use the last_unlink_trans field to put this transid
6358 * into the file. When the file is logged we check it and
6359 * don't log the parents if the file is fully on disk.
6360 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006361 mutex_lock(&inode->log_mutex);
6362 inode->last_unlink_trans = trans->transid;
6363 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04006364
6365 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006366 * if this directory was already logged any new
6367 * names for this file/dir will get recorded
6368 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006369 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006370 return;
6371
6372 /*
6373 * if the inode we're about to unlink was logged,
6374 * the log will be properly updated for any new names
6375 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006376 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006377 return;
6378
6379 /*
6380 * when renaming files across directories, if the directory
6381 * there we're unlinking from gets fsync'd later on, there's
6382 * no way to find the destination directory later and fsync it
6383 * properly. So, we have to be conservative and force commits
6384 * so the new name gets discovered.
6385 */
6386 if (for_rename)
6387 goto record;
6388
6389 /* we can safely do the unlink without any special recording */
6390 return;
6391
6392record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006393 mutex_lock(&dir->log_mutex);
6394 dir->last_unlink_trans = trans->transid;
6395 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04006396}
6397
6398/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006399 * Make sure that if someone attempts to fsync the parent directory of a deleted
6400 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6401 * that after replaying the log tree of the parent directory's root we will not
6402 * see the snapshot anymore and at log replay time we will not see any log tree
6403 * corresponding to the deleted snapshot's root, which could lead to replaying
6404 * it after replaying the log tree of the parent directory (which would replay
6405 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00006406 *
6407 * Must be called before the actual snapshot destroy operation (updates to the
6408 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006409 */
6410void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02006411 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006412{
Nikolay Borisov43663552017-01-18 00:31:29 +02006413 mutex_lock(&dir->log_mutex);
6414 dir->last_unlink_trans = trans->transid;
6415 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006416}
6417
6418/*
Chris Mason12fcfd22009-03-24 10:24:20 -04006419 * Call this after adding a new name for a file and it will properly
6420 * update the log to reflect the new name.
Chris Mason12fcfd22009-03-24 10:24:20 -04006421 */
Filipe Manana75b463d2020-08-11 12:43:48 +01006422void btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006423 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Filipe Manana75b463d2020-08-11 12:43:48 +01006424 struct dentry *parent)
Chris Mason12fcfd22009-03-24 10:24:20 -04006425{
David Sterba3ffbd682018-06-29 10:56:42 +02006426 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana75b463d2020-08-11 12:43:48 +01006427 struct btrfs_log_ctx ctx;
Chris Mason12fcfd22009-03-24 10:24:20 -04006428
6429 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006430 * this will force the logging code to walk the dentry chain
6431 * up for the file
6432 */
Filipe Manana9a6509c2018-02-28 15:55:40 +00006433 if (!S_ISDIR(inode->vfs_inode.i_mode))
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006434 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04006435
6436 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006437 * if this inode hasn't been logged and directory we're renaming it
6438 * from hasn't been logged, we don't need to log it
6439 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006440 if (inode->logged_trans <= fs_info->last_trans_committed &&
6441 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
Filipe Manana75b463d2020-08-11 12:43:48 +01006442 return;
Chris Mason12fcfd22009-03-24 10:24:20 -04006443
Filipe Manana75b463d2020-08-11 12:43:48 +01006444 btrfs_init_log_ctx(&ctx, &inode->vfs_inode);
6445 ctx.logging_new_name = true;
6446 /*
6447 * We don't care about the return value. If we fail to log the new name
6448 * then we know the next attempt to sync the log will fallback to a full
6449 * transaction commit (due to a call to btrfs_set_log_full_commit()), so
6450 * we don't need to worry about getting a log committed that has an
6451 * inconsistent state after a rename operation.
6452 */
Filipe Manana48778172020-08-11 12:43:58 +01006453 btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04006454}
6455