blob: 34d087008b72eebb7af75e70acae7edaf86fdee3 [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"
Chris Masone02119d2008-09-05 16:13:11 -040021
22/* magic values for the inode_only field in btrfs_log_inode:
23 *
24 * LOG_INODE_ALL means to log everything
25 * LOG_INODE_EXISTS means to log just enough to recreate the inode
26 * during log replay
27 */
David Sterbae13976c2019-08-01 14:50:30 +020028enum {
29 LOG_INODE_ALL,
30 LOG_INODE_EXISTS,
31 LOG_OTHER_INODE,
32 LOG_OTHER_INODE_ALL,
33};
Chris Masone02119d2008-09-05 16:13:11 -040034
35/*
Chris Mason12fcfd22009-03-24 10:24:20 -040036 * directory trouble cases
37 *
38 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
39 * log, we must force a full commit before doing an fsync of the directory
40 * where the unlink was done.
41 * ---> record transid of last unlink/rename per directory
42 *
43 * mkdir foo/some_dir
44 * normal commit
45 * rename foo/some_dir foo2/some_dir
46 * mkdir foo/some_dir
47 * fsync foo/some_dir/some_file
48 *
49 * The fsync above will unlink the original some_dir without recording
50 * it in its new location (foo2). After a crash, some_dir will be gone
51 * unless the fsync of some_file forces a full commit
52 *
53 * 2) we must log any new names for any file or dir that is in the fsync
54 * log. ---> check inode while renaming/linking.
55 *
56 * 2a) we must log any new names for any file or dir during rename
57 * when the directory they are being removed from was logged.
58 * ---> check inode and old parent dir during rename
59 *
60 * 2a is actually the more important variant. With the extra logging
61 * a crash might unlink the old name without recreating the new one
62 *
63 * 3) after a crash, we must go through any directories with a link count
64 * of zero and redo the rm -rf
65 *
66 * mkdir f1/foo
67 * normal commit
68 * rm -rf f1/foo
69 * fsync(f1)
70 *
71 * The directory f1 was fully removed from the FS, but fsync was never
72 * called on f1, only its parent dir. After a crash the rm -rf must
73 * be replayed. This must be able to recurse down the entire
74 * directory tree. The inode link count fixup code takes care of the
75 * ugly details.
76 */
77
78/*
Chris Masone02119d2008-09-05 16:13:11 -040079 * stages for the tree walking. The first
80 * stage (0) is to only pin down the blocks we find
81 * the second stage (1) is to make sure that all the inodes
82 * we find in the log are created in the subvolume.
83 *
84 * The last stage is to deal with directories and links and extents
85 * and all the other fun semantics
86 */
David Sterbae13976c2019-08-01 14:50:30 +020087enum {
88 LOG_WALK_PIN_ONLY,
89 LOG_WALK_REPLAY_INODES,
90 LOG_WALK_REPLAY_DIR_INDEX,
91 LOG_WALK_REPLAY_ALL,
92};
Chris Masone02119d2008-09-05 16:13:11 -040093
Chris Mason12fcfd22009-03-24 10:24:20 -040094static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +020095 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +010096 int inode_only,
97 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +010098 const loff_t end,
99 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
170 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
171 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400172 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800173
Miao Xie2ecb7922012-09-06 04:04:27 -0600174 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500175 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800176 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800177 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800178 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800179 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800180 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800181
Miao Xiee87ac132014-02-20 18:08:53 +0800182out:
Yan Zheng7237f182009-01-21 12:54:03 -0500183 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800184 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400185}
186
187/*
188 * returns 0 if there was a log transaction running and we were able
189 * to join, or returns -ENOENT if there were not transactions
190 * in progress
191 */
192static int join_running_log_trans(struct btrfs_root *root)
193{
194 int ret = -ENOENT;
195
Yan Zheng7237f182009-01-21 12:54:03 -0500196 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400197 if (root->log_root) {
198 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500199 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400200 }
Yan Zheng7237f182009-01-21 12:54:03 -0500201 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400202 return ret;
203}
204
205/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400206 * This either makes the current running log transaction wait
207 * until you call btrfs_end_log_trans() or it makes any future
208 * log transactions wait until you call btrfs_end_log_trans()
209 */
zhong jiang45128b02018-08-17 00:37:15 +0800210void btrfs_pin_log_trans(struct btrfs_root *root)
Chris Mason12fcfd22009-03-24 10:24:20 -0400211{
Chris Mason12fcfd22009-03-24 10:24:20 -0400212 mutex_lock(&root->log_mutex);
213 atomic_inc(&root->log_writers);
214 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -0400215}
216
217/*
Chris Masone02119d2008-09-05 16:13:11 -0400218 * indicate we're done making changes to the log tree
219 * and wake up anyone waiting to do a sync
220 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100221void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400222{
Yan Zheng7237f182009-01-21 12:54:03 -0500223 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +0100224 /* atomic_dec_and_test implies a barrier */
225 cond_wake_up_nomb(&root->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -0500226 }
Chris Masone02119d2008-09-05 16:13:11 -0400227}
228
David Sterba247462a2019-03-21 20:21:05 +0100229static int btrfs_write_tree_block(struct extent_buffer *buf)
230{
231 return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start,
232 buf->start + buf->len - 1);
233}
234
235static void btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
236{
237 filemap_fdatawait_range(buf->pages[0]->mapping,
238 buf->start, buf->start + buf->len - 1);
239}
Chris Masone02119d2008-09-05 16:13:11 -0400240
241/*
242 * the walk control struct is used to pass state down the chain when
243 * processing the log tree. The stage field tells us which part
244 * of the log tree processing we are currently doing. The others
245 * are state fields used for that specific part
246 */
247struct walk_control {
248 /* should we free the extent on disk when done? This is used
249 * at transaction commit time while freeing a log tree
250 */
251 int free;
252
253 /* should we write out the extent buffer? This is used
254 * while flushing the log tree to disk during a sync
255 */
256 int write;
257
258 /* should we wait for the extent buffer io to finish? Also used
259 * while flushing the log tree to disk for a sync
260 */
261 int wait;
262
263 /* pin only walk, we record which extents on disk belong to the
264 * log trees
265 */
266 int pin;
267
268 /* what stage of the replay code we're currently in */
269 int stage;
270
Filipe Mananaf2d72f42018-10-08 11:12:55 +0100271 /*
272 * Ignore any items from the inode currently being processed. Needs
273 * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
274 * the LOG_WALK_REPLAY_INODES stage.
275 */
276 bool ignore_cur_inode;
277
Chris Masone02119d2008-09-05 16:13:11 -0400278 /* the root we are currently replaying */
279 struct btrfs_root *replay_dest;
280
281 /* the trans handle for the current replay */
282 struct btrfs_trans_handle *trans;
283
284 /* the function that gets used to process blocks we find in the
285 * tree. Note the extent_buffer might not be up to date when it is
286 * passed in, and it must be checked or read if you need the data
287 * inside it
288 */
289 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800290 struct walk_control *wc, u64 gen, int level);
Chris Masone02119d2008-09-05 16:13:11 -0400291};
292
293/*
294 * process_func used to pin down extents, write them or wait on them
295 */
296static int process_one_buffer(struct btrfs_root *log,
297 struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800298 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -0400299{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400300 struct btrfs_fs_info *fs_info = log->fs_info;
Josef Bacikb50c6e22013-04-25 15:55:30 -0400301 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400302
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400303 /*
304 * If this fs is mixed then we need to be able to process the leaves to
305 * pin down any logged extents, so we have to read the block.
306 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400307 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Qu Wenruo581c1762018-03-29 09:08:11 +0800308 ret = btrfs_read_buffer(eb, gen, level, NULL);
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400309 if (ret)
310 return ret;
311 }
312
Josef Bacikb50c6e22013-04-25 15:55:30 -0400313 if (wc->pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400314 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
315 eb->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400316
317 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400318 if (wc->pin && btrfs_header_level(eb) == 0)
David Sterbabcdc4282019-03-20 12:14:33 +0100319 ret = btrfs_exclude_logged_extents(eb);
Chris Masone02119d2008-09-05 16:13:11 -0400320 if (wc->write)
321 btrfs_write_tree_block(eb);
322 if (wc->wait)
323 btrfs_wait_tree_block_writeback(eb);
324 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400325 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400326}
327
328/*
329 * Item overwrite used by replay and tree logging. eb, slot and key all refer
330 * to the src data we are copying out.
331 *
332 * root is the tree we are copying into, and path is a scratch
333 * path for use in this function (it should be released on entry and
334 * will be released on exit).
335 *
336 * If the key is already in the destination tree the existing item is
337 * overwritten. If the existing item isn't big enough, it is extended.
338 * If it is too large, it is truncated.
339 *
340 * If the key isn't in the destination yet, a new item is inserted.
341 */
342static noinline int overwrite_item(struct btrfs_trans_handle *trans,
343 struct btrfs_root *root,
344 struct btrfs_path *path,
345 struct extent_buffer *eb, int slot,
346 struct btrfs_key *key)
347{
348 int ret;
349 u32 item_size;
350 u64 saved_i_size = 0;
351 int save_old_i_size = 0;
352 unsigned long src_ptr;
353 unsigned long dst_ptr;
354 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000355 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400356
357 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
358 overwrite_root = 1;
359
360 item_size = btrfs_item_size_nr(eb, slot);
361 src_ptr = btrfs_item_ptr_offset(eb, slot);
362
363 /* look for the key in the destination tree */
364 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000365 if (ret < 0)
366 return ret;
367
Chris Masone02119d2008-09-05 16:13:11 -0400368 if (ret == 0) {
369 char *src_copy;
370 char *dst_copy;
371 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
372 path->slots[0]);
373 if (dst_size != item_size)
374 goto insert;
375
376 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200377 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400378 return 0;
379 }
380 dst_copy = kmalloc(item_size, GFP_NOFS);
381 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000382 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200383 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000384 kfree(dst_copy);
385 kfree(src_copy);
386 return -ENOMEM;
387 }
Chris Masone02119d2008-09-05 16:13:11 -0400388
389 read_extent_buffer(eb, src_copy, src_ptr, item_size);
390
391 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
392 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
393 item_size);
394 ret = memcmp(dst_copy, src_copy, item_size);
395
396 kfree(dst_copy);
397 kfree(src_copy);
398 /*
399 * they have the same contents, just return, this saves
400 * us from cowing blocks in the destination tree and doing
401 * extra writes that may not have been done by a previous
402 * sync
403 */
404 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200405 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400406 return 0;
407 }
408
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000409 /*
410 * We need to load the old nbytes into the inode so when we
411 * replay the extents we've logged we get the right nbytes.
412 */
413 if (inode_item) {
414 struct btrfs_inode_item *item;
415 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400416 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000417
418 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
419 struct btrfs_inode_item);
420 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
421 item = btrfs_item_ptr(eb, slot,
422 struct btrfs_inode_item);
423 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400424
425 /*
426 * If this is a directory we need to reset the i_size to
427 * 0 so that we can set it up properly when replaying
428 * the rest of the items in this log.
429 */
430 mode = btrfs_inode_mode(eb, item);
431 if (S_ISDIR(mode))
432 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000433 }
434 } else if (inode_item) {
435 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400436 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000437
438 /*
439 * New inode, set nbytes to 0 so that the nbytes comes out
440 * properly when we replay the extents.
441 */
442 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
443 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400444
445 /*
446 * If this is a directory we need to reset the i_size to 0 so
447 * that we can set it up properly when replaying the rest of
448 * the items in this log.
449 */
450 mode = btrfs_inode_mode(eb, item);
451 if (S_ISDIR(mode))
452 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400453 }
454insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200455 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400456 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000457 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400458 ret = btrfs_insert_empty_item(trans, root, path,
459 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000460 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400461
462 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000463 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400464 u32 found_size;
465 found_size = btrfs_item_size_nr(path->nodes[0],
466 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100467 if (found_size > item_size)
David Sterba78ac4f92019-03-20 14:49:12 +0100468 btrfs_truncate_item(path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100469 else if (found_size < item_size)
David Sterbac71dd882019-03-20 14:51:10 +0100470 btrfs_extend_item(path, item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400471 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400472 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400473 }
474 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
475 path->slots[0]);
476
477 /* don't overwrite an existing inode if the generation number
478 * was logged as zero. This is done when the tree logging code
479 * is just logging an inode to make sure it exists after recovery.
480 *
481 * Also, don't overwrite i_size on directories during replay.
482 * log replay inserts and removes directory items based on the
483 * state of the tree found in the subvolume, and i_size is modified
484 * as it goes
485 */
486 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
487 struct btrfs_inode_item *src_item;
488 struct btrfs_inode_item *dst_item;
489
490 src_item = (struct btrfs_inode_item *)src_ptr;
491 dst_item = (struct btrfs_inode_item *)dst_ptr;
492
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000493 if (btrfs_inode_generation(eb, src_item) == 0) {
494 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000495 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000496
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000497 /*
498 * For regular files an ino_size == 0 is used only when
499 * logging that an inode exists, as part of a directory
500 * fsync, and the inode wasn't fsynced before. In this
501 * case don't set the size of the inode in the fs/subvol
502 * tree, otherwise we would be throwing valid data away.
503 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000504 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000505 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
506 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000507 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000508
509 btrfs_init_map_token(&token);
510 btrfs_set_token_inode_size(dst_eb, dst_item,
511 ino_size, &token);
512 }
Chris Masone02119d2008-09-05 16:13:11 -0400513 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000514 }
Chris Masone02119d2008-09-05 16:13:11 -0400515
516 if (overwrite_root &&
517 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
518 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
519 save_old_i_size = 1;
520 saved_i_size = btrfs_inode_size(path->nodes[0],
521 dst_item);
522 }
523 }
524
525 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
526 src_ptr, item_size);
527
528 if (save_old_i_size) {
529 struct btrfs_inode_item *dst_item;
530 dst_item = (struct btrfs_inode_item *)dst_ptr;
531 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
532 }
533
534 /* make sure the generation is filled in */
535 if (key->type == BTRFS_INODE_ITEM_KEY) {
536 struct btrfs_inode_item *dst_item;
537 dst_item = (struct btrfs_inode_item *)dst_ptr;
538 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
539 btrfs_set_inode_generation(path->nodes[0], dst_item,
540 trans->transid);
541 }
542 }
543no_copy:
544 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200545 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400546 return 0;
547}
548
549/*
550 * simple helper to read an inode off the disk from a given root
551 * This can only be called for subvolume roots and not for the log
552 */
553static noinline struct inode *read_one_inode(struct btrfs_root *root,
554 u64 objectid)
555{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400556 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400557 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400558
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400559 key.objectid = objectid;
560 key.type = BTRFS_INODE_ITEM_KEY;
561 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000562 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Al Viro2e19f1f2018-07-29 23:04:45 +0100563 if (IS_ERR(inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400564 inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -0400565 return inode;
566}
567
568/* replays a single extent in 'eb' at 'slot' with 'key' into the
569 * subvolume 'root'. path is released on entry and should be released
570 * on exit.
571 *
572 * extents in the log tree have not been allocated out of the extent
573 * tree yet. So, this completes the allocation, taking a reference
574 * as required if the extent already exists or creating a new extent
575 * if it isn't in the extent allocation tree yet.
576 *
577 * The extent is inserted into the file, dropping any existing extents
578 * from the file that overlap the new one.
579 */
580static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
581 struct btrfs_root *root,
582 struct btrfs_path *path,
583 struct extent_buffer *eb, int slot,
584 struct btrfs_key *key)
585{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400586 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400587 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400588 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400589 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000590 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400591 struct btrfs_file_extent_item *item;
592 struct inode *inode = NULL;
593 unsigned long size;
594 int ret = 0;
595
596 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
597 found_type = btrfs_file_extent_type(eb, item);
598
Yan Zhengd899e052008-10-30 14:25:28 -0400599 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000600 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
601 nbytes = btrfs_file_extent_num_bytes(eb, item);
602 extent_end = start + nbytes;
603
604 /*
605 * We don't add to the inodes nbytes if we are prealloc or a
606 * hole.
607 */
608 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
609 nbytes = 0;
610 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +0800611 size = btrfs_file_extent_ram_bytes(eb, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000612 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400613 extent_end = ALIGN(start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400614 fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400615 } else {
616 ret = 0;
617 goto out;
618 }
619
620 inode = read_one_inode(root, key->objectid);
621 if (!inode) {
622 ret = -EIO;
623 goto out;
624 }
625
626 /*
627 * first check to see if we already have this extent in the
628 * file. This must be done before the btrfs_drop_extents run
629 * so we don't try to drop this extent.
630 */
David Sterbaf85b7372017-01-20 14:54:07 +0100631 ret = btrfs_lookup_file_extent(trans, root, path,
632 btrfs_ino(BTRFS_I(inode)), start, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400633
Yan Zhengd899e052008-10-30 14:25:28 -0400634 if (ret == 0 &&
635 (found_type == BTRFS_FILE_EXTENT_REG ||
636 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400637 struct btrfs_file_extent_item cmp1;
638 struct btrfs_file_extent_item cmp2;
639 struct btrfs_file_extent_item *existing;
640 struct extent_buffer *leaf;
641
642 leaf = path->nodes[0];
643 existing = btrfs_item_ptr(leaf, path->slots[0],
644 struct btrfs_file_extent_item);
645
646 read_extent_buffer(eb, &cmp1, (unsigned long)item,
647 sizeof(cmp1));
648 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
649 sizeof(cmp2));
650
651 /*
652 * we already have a pointer to this exact extent,
653 * we don't have to do anything
654 */
655 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200656 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400657 goto out;
658 }
659 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200660 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400661
662 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400663 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400664 if (ret)
665 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400666
Yan Zheng07d400a2009-01-06 11:42:00 -0500667 if (found_type == BTRFS_FILE_EXTENT_REG ||
668 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400669 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500670 unsigned long dest_offset;
671 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400672
Filipe Manana3168021c2017-02-01 14:58:02 +0000673 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
674 btrfs_fs_incompat(fs_info, NO_HOLES))
675 goto update_inode;
676
Yan Zheng07d400a2009-01-06 11:42:00 -0500677 ret = btrfs_insert_empty_item(trans, root, path, key,
678 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400679 if (ret)
680 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500681 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
682 path->slots[0]);
683 copy_extent_buffer(path->nodes[0], eb, dest_offset,
684 (unsigned long)item, sizeof(*item));
685
686 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
687 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
688 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400689 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500690
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800691 /*
692 * Manually record dirty extent, as here we did a shallow
693 * file extent item copy and skip normal backref update,
694 * but modifying extent tree all by ourselves.
695 * So need to manually record dirty extent for qgroup,
696 * as the owner of the file extent changed from log tree
697 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
698 */
Lu Fengqia95f3aa2018-07-18 16:28:03 +0800699 ret = btrfs_qgroup_trace_extent(trans,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800700 btrfs_file_extent_disk_bytenr(eb, item),
701 btrfs_file_extent_disk_num_bytes(eb, item),
702 GFP_NOFS);
703 if (ret < 0)
704 goto out;
705
Yan Zheng07d400a2009-01-06 11:42:00 -0500706 if (ins.objectid > 0) {
Qu Wenruo82fa1132019-04-04 14:45:35 +0800707 struct btrfs_ref ref = { 0 };
Yan Zheng07d400a2009-01-06 11:42:00 -0500708 u64 csum_start;
709 u64 csum_end;
710 LIST_HEAD(ordered_sums);
Qu Wenruo82fa1132019-04-04 14:45:35 +0800711
Yan Zheng07d400a2009-01-06 11:42:00 -0500712 /*
713 * is this extent already allocated in the extent
714 * allocation tree? If so, just add a reference
715 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400716 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500717 ins.offset);
718 if (ret == 0) {
Qu Wenruo82fa1132019-04-04 14:45:35 +0800719 btrfs_init_generic_ref(&ref,
720 BTRFS_ADD_DELAYED_REF,
721 ins.objectid, ins.offset, 0);
722 btrfs_init_data_ref(&ref,
723 root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100724 key->objectid, offset);
Qu Wenruo82fa1132019-04-04 14:45:35 +0800725 ret = btrfs_inc_extent_ref(trans, &ref);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400726 if (ret)
727 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500728 } else {
729 /*
730 * insert the extent pointer in the extent
731 * allocation tree
732 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400733 ret = btrfs_alloc_logged_file_extent(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400734 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400735 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400736 if (ret)
737 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500738 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200739 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500740
741 if (btrfs_file_extent_compression(eb, item)) {
742 csum_start = ins.objectid;
743 csum_end = csum_start + ins.offset;
744 } else {
745 csum_start = ins.objectid +
746 btrfs_file_extent_offset(eb, item);
747 csum_end = csum_start +
748 btrfs_file_extent_num_bytes(eb, item);
749 }
750
751 ret = btrfs_lookup_csums_range(root->log_root,
752 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100753 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400754 if (ret)
755 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100756 /*
757 * Now delete all existing cums in the csum root that
758 * cover our range. We do this because we can have an
759 * extent that is completely referenced by one file
760 * extent item and partially referenced by another
761 * file extent item (like after using the clone or
762 * extent_same ioctls). In this case if we end up doing
763 * the replay of the one that partially references the
764 * extent first, and we do not do the csum deletion
765 * below, we can get 2 csum items in the csum tree that
766 * overlap each other. For example, imagine our log has
767 * the two following file extent items:
768 *
769 * key (257 EXTENT_DATA 409600)
770 * extent data disk byte 12845056 nr 102400
771 * extent data offset 20480 nr 20480 ram 102400
772 *
773 * key (257 EXTENT_DATA 819200)
774 * extent data disk byte 12845056 nr 102400
775 * extent data offset 0 nr 102400 ram 102400
776 *
777 * Where the second one fully references the 100K extent
778 * that starts at disk byte 12845056, and the log tree
779 * has a single csum item that covers the entire range
780 * of the extent:
781 *
782 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
783 *
784 * After the first file extent item is replayed, the
785 * csum tree gets the following csum item:
786 *
787 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
788 *
789 * Which covers the 20K sub-range starting at offset 20K
790 * of our extent. Now when we replay the second file
791 * extent item, if we do not delete existing csum items
792 * that cover any of its blocks, we end up getting two
793 * csum items in our csum tree that overlap each other:
794 *
795 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
796 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
797 *
798 * Which is a problem, because after this anyone trying
799 * to lookup up for the checksum of any block of our
800 * extent starting at an offset of 40K or higher, will
801 * end up looking at the second csum item only, which
802 * does not contain the checksum for any block starting
803 * at offset 40K or higher of our extent.
804 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500805 while (!list_empty(&ordered_sums)) {
806 struct btrfs_ordered_sum *sums;
807 sums = list_entry(ordered_sums.next,
808 struct btrfs_ordered_sum,
809 list);
Josef Bacik36508602013-04-25 16:23:32 -0400810 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400811 ret = btrfs_del_csums(trans, fs_info,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400812 sums->bytenr,
813 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100814 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400815 ret = btrfs_csum_file_blocks(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400816 fs_info->csum_root, sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500817 list_del(&sums->list);
818 kfree(sums);
819 }
Josef Bacik36508602013-04-25 16:23:32 -0400820 if (ret)
821 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500822 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200823 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500824 }
825 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
826 /* inline extents are easy, we just overwrite them */
827 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400828 if (ret)
829 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500830 }
831
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000832 inode_add_bytes(inode, nbytes);
Filipe Manana3168021c2017-02-01 14:58:02 +0000833update_inode:
Tsutomu Itohb9959292012-06-25 21:25:22 -0600834 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400835out:
836 if (inode)
837 iput(inode);
838 return ret;
839}
840
841/*
842 * when cleaning up conflicts between the directory names in the
843 * subvolume, directory names in the log and directory names in the
844 * inode back references, we may have to unlink inodes from directories.
845 *
846 * This is a helper function to do the unlink of a specific directory
847 * item
848 */
849static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
850 struct btrfs_root *root,
851 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200852 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400853 struct btrfs_dir_item *di)
854{
855 struct inode *inode;
856 char *name;
857 int name_len;
858 struct extent_buffer *leaf;
859 struct btrfs_key location;
860 int ret;
861
862 leaf = path->nodes[0];
863
864 btrfs_dir_item_key_to_cpu(leaf, di, &location);
865 name_len = btrfs_dir_name_len(leaf, di);
866 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000867 if (!name)
868 return -ENOMEM;
869
Chris Masone02119d2008-09-05 16:13:11 -0400870 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200871 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400872
873 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000874 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400875 ret = -EIO;
876 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000877 }
Chris Masone02119d2008-09-05 16:13:11 -0400878
Yan Zhengec051c02009-01-05 15:43:42 -0500879 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400880 if (ret)
881 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400882
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200883 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
884 name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400885 if (ret)
886 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100887 else
Nikolay Borisove5c304e62018-02-07 17:55:43 +0200888 ret = btrfs_run_delayed_items(trans);
Josef Bacik36508602013-04-25 16:23:32 -0400889out:
890 kfree(name);
891 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400892 return ret;
893}
894
895/*
896 * helper function to see if a given name and sequence number found
897 * in an inode back reference are already in a directory and correctly
898 * point to this inode
899 */
900static noinline int inode_in_dir(struct btrfs_root *root,
901 struct btrfs_path *path,
902 u64 dirid, u64 objectid, u64 index,
903 const char *name, int name_len)
904{
905 struct btrfs_dir_item *di;
906 struct btrfs_key location;
907 int match = 0;
908
909 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
910 index, name, name_len, 0);
911 if (di && !IS_ERR(di)) {
912 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
913 if (location.objectid != objectid)
914 goto out;
915 } else
916 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200917 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400918
919 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
920 if (di && !IS_ERR(di)) {
921 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
922 if (location.objectid != objectid)
923 goto out;
924 } else
925 goto out;
926 match = 1;
927out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200928 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400929 return match;
930}
931
932/*
933 * helper function to check a log tree for a named back reference in
934 * an inode. This is used to decide if a back reference that is
935 * found in the subvolume conflicts with what we find in the log.
936 *
937 * inode backreferences may have multiple refs in a single item,
938 * during replay we process one reference at a time, and we don't
939 * want to delete valid links to a file from the subvolume if that
940 * link is also in the log.
941 */
942static noinline int backref_in_log(struct btrfs_root *log,
943 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700944 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000945 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400946{
947 struct btrfs_path *path;
948 struct btrfs_inode_ref *ref;
949 unsigned long ptr;
950 unsigned long ptr_end;
951 unsigned long name_ptr;
952 int found_name_len;
953 int item_size;
954 int ret;
955 int match = 0;
956
957 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000958 if (!path)
959 return -ENOMEM;
960
Chris Masone02119d2008-09-05 16:13:11 -0400961 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
962 if (ret != 0)
963 goto out;
964
Chris Masone02119d2008-09-05 16:13:11 -0400965 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700966
967 if (key->type == BTRFS_INODE_EXTREF_KEY) {
Filipe Manana1f250e92018-02-28 15:56:10 +0000968 if (btrfs_find_name_in_ext_backref(path->nodes[0],
969 path->slots[0],
970 ref_objectid,
Mark Fashehf1863732012-08-08 11:32:27 -0700971 name, namelen, NULL))
972 match = 1;
973
974 goto out;
975 }
976
977 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400978 ptr_end = ptr + item_size;
979 while (ptr < ptr_end) {
980 ref = (struct btrfs_inode_ref *)ptr;
981 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
982 if (found_name_len == namelen) {
983 name_ptr = (unsigned long)(ref + 1);
984 ret = memcmp_extent_buffer(path->nodes[0], name,
985 name_ptr, namelen);
986 if (ret == 0) {
987 match = 1;
988 goto out;
989 }
990 }
991 ptr = (unsigned long)(ref + 1) + found_name_len;
992 }
993out:
994 btrfs_free_path(path);
995 return match;
996}
997
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700998static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
999 struct btrfs_root *root,
1000 struct btrfs_path *path,
1001 struct btrfs_root *log_root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001002 struct btrfs_inode *dir,
1003 struct btrfs_inode *inode,
Mark Fashehf1863732012-08-08 11:32:27 -07001004 u64 inode_objectid, u64 parent_objectid,
1005 u64 ref_index, char *name, int namelen,
1006 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001007{
1008 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001009 char *victim_name;
1010 int victim_name_len;
1011 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001012 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -07001013 struct btrfs_key search_key;
1014 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001015
Mark Fashehf1863732012-08-08 11:32:27 -07001016again:
1017 /* Search old style refs */
1018 search_key.objectid = inode_objectid;
1019 search_key.type = BTRFS_INODE_REF_KEY;
1020 search_key.offset = parent_objectid;
1021 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001022 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001023 struct btrfs_inode_ref *victim_ref;
1024 unsigned long ptr;
1025 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001026
1027 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001028
1029 /* are we trying to overwrite a back ref for the root directory
1030 * if so, just jump out, we're done
1031 */
Mark Fashehf1863732012-08-08 11:32:27 -07001032 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001033 return 1;
1034
1035 /* check all the names in this back reference to see
1036 * if they are in the log. if so, we allow them to stay
1037 * otherwise they must be unlinked as a conflict
1038 */
1039 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1040 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1041 while (ptr < ptr_end) {
1042 victim_ref = (struct btrfs_inode_ref *)ptr;
1043 victim_name_len = btrfs_inode_ref_name_len(leaf,
1044 victim_ref);
1045 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001046 if (!victim_name)
1047 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001048
1049 read_extent_buffer(leaf, victim_name,
1050 (unsigned long)(victim_ref + 1),
1051 victim_name_len);
1052
Mark Fashehf1863732012-08-08 11:32:27 -07001053 if (!backref_in_log(log_root, &search_key,
1054 parent_objectid,
1055 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001056 victim_name_len)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001057 inc_nlink(&inode->vfs_inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001058 btrfs_release_path(path);
1059
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001060 ret = btrfs_unlink_inode(trans, root, dir, inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001061 victim_name, victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001062 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001063 if (ret)
1064 return ret;
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001065 ret = btrfs_run_delayed_items(trans);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001066 if (ret)
1067 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001068 *search_done = 1;
1069 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001070 }
1071 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001072
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001073 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1074 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001075
1076 /*
1077 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001078 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001079 */
1080 *search_done = 1;
1081 }
1082 btrfs_release_path(path);
1083
Mark Fashehf1863732012-08-08 11:32:27 -07001084 /* Same search but for extended refs */
1085 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1086 inode_objectid, parent_objectid, 0,
1087 0);
1088 if (!IS_ERR_OR_NULL(extref)) {
1089 u32 item_size;
1090 u32 cur_offset = 0;
1091 unsigned long base;
1092 struct inode *victim_parent;
1093
1094 leaf = path->nodes[0];
1095
1096 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1097 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1098
1099 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001100 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001101
1102 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1103
1104 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1105 goto next;
1106
1107 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001108 if (!victim_name)
1109 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001110 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1111 victim_name_len);
1112
1113 search_key.objectid = inode_objectid;
1114 search_key.type = BTRFS_INODE_EXTREF_KEY;
1115 search_key.offset = btrfs_extref_hash(parent_objectid,
1116 victim_name,
1117 victim_name_len);
1118 ret = 0;
1119 if (!backref_in_log(log_root, &search_key,
1120 parent_objectid, victim_name,
1121 victim_name_len)) {
1122 ret = -ENOENT;
1123 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001124 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001125 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001126 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001127 btrfs_release_path(path);
1128
1129 ret = btrfs_unlink_inode(trans, root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001130 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001131 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001132 victim_name,
1133 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001134 if (!ret)
1135 ret = btrfs_run_delayed_items(
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001136 trans);
Mark Fashehf1863732012-08-08 11:32:27 -07001137 }
Mark Fashehf1863732012-08-08 11:32:27 -07001138 iput(victim_parent);
1139 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001140 if (ret)
1141 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001142 *search_done = 1;
1143 goto again;
1144 }
1145 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001146next:
1147 cur_offset += victim_name_len + sizeof(*extref);
1148 }
1149 *search_done = 1;
1150 }
1151 btrfs_release_path(path);
1152
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001153 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001154 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001155 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001156 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001157 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001158 if (ret)
1159 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001160 }
1161 btrfs_release_path(path);
1162
Andrea Gelmini52042d82018-11-28 12:05:13 +01001163 /* look for a conflicting name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001164 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001165 name, namelen, 0);
1166 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001167 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001168 if (ret)
1169 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001170 }
1171 btrfs_release_path(path);
1172
1173 return 0;
1174}
Chris Masone02119d2008-09-05 16:13:11 -04001175
Qu Wenruobae15d92017-11-08 08:54:26 +08001176static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1177 u32 *namelen, char **name, u64 *index,
1178 u64 *parent_objectid)
Mark Fashehf1863732012-08-08 11:32:27 -07001179{
1180 struct btrfs_inode_extref *extref;
1181
1182 extref = (struct btrfs_inode_extref *)ref_ptr;
1183
1184 *namelen = btrfs_inode_extref_name_len(eb, extref);
1185 *name = kmalloc(*namelen, GFP_NOFS);
1186 if (*name == NULL)
1187 return -ENOMEM;
1188
1189 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1190 *namelen);
1191
Filipe Manana1f250e92018-02-28 15:56:10 +00001192 if (index)
1193 *index = btrfs_inode_extref_index(eb, extref);
Mark Fashehf1863732012-08-08 11:32:27 -07001194 if (parent_objectid)
1195 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1196
1197 return 0;
1198}
1199
Qu Wenruobae15d92017-11-08 08:54:26 +08001200static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1201 u32 *namelen, char **name, u64 *index)
Mark Fashehf1863732012-08-08 11:32:27 -07001202{
1203 struct btrfs_inode_ref *ref;
1204
1205 ref = (struct btrfs_inode_ref *)ref_ptr;
1206
1207 *namelen = btrfs_inode_ref_name_len(eb, ref);
1208 *name = kmalloc(*namelen, GFP_NOFS);
1209 if (*name == NULL)
1210 return -ENOMEM;
1211
1212 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1213
Filipe Manana1f250e92018-02-28 15:56:10 +00001214 if (index)
1215 *index = btrfs_inode_ref_index(eb, ref);
Mark Fashehf1863732012-08-08 11:32:27 -07001216
1217 return 0;
1218}
1219
Chris Masone02119d2008-09-05 16:13:11 -04001220/*
Filipe Manana1f250e92018-02-28 15:56:10 +00001221 * Take an inode reference item from the log tree and iterate all names from the
1222 * inode reference item in the subvolume tree with the same key (if it exists).
1223 * For any name that is not in the inode reference item from the log tree, do a
1224 * proper unlink of that name (that is, remove its entry from the inode
1225 * reference item and both dir index keys).
1226 */
1227static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1228 struct btrfs_root *root,
1229 struct btrfs_path *path,
1230 struct btrfs_inode *inode,
1231 struct extent_buffer *log_eb,
1232 int log_slot,
1233 struct btrfs_key *key)
1234{
1235 int ret;
1236 unsigned long ref_ptr;
1237 unsigned long ref_end;
1238 struct extent_buffer *eb;
1239
1240again:
1241 btrfs_release_path(path);
1242 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1243 if (ret > 0) {
1244 ret = 0;
1245 goto out;
1246 }
1247 if (ret < 0)
1248 goto out;
1249
1250 eb = path->nodes[0];
1251 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1252 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1253 while (ref_ptr < ref_end) {
1254 char *name = NULL;
1255 int namelen;
1256 u64 parent_id;
1257
1258 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1259 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1260 NULL, &parent_id);
1261 } else {
1262 parent_id = key->offset;
1263 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1264 NULL);
1265 }
1266 if (ret)
1267 goto out;
1268
1269 if (key->type == BTRFS_INODE_EXTREF_KEY)
1270 ret = btrfs_find_name_in_ext_backref(log_eb, log_slot,
1271 parent_id, name,
1272 namelen, NULL);
1273 else
1274 ret = btrfs_find_name_in_backref(log_eb, log_slot, name,
1275 namelen, NULL);
1276
1277 if (!ret) {
1278 struct inode *dir;
1279
1280 btrfs_release_path(path);
1281 dir = read_one_inode(root, parent_id);
1282 if (!dir) {
1283 ret = -ENOENT;
1284 kfree(name);
1285 goto out;
1286 }
1287 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1288 inode, name, namelen);
1289 kfree(name);
1290 iput(dir);
1291 if (ret)
1292 goto out;
1293 goto again;
1294 }
1295
1296 kfree(name);
1297 ref_ptr += namelen;
1298 if (key->type == BTRFS_INODE_EXTREF_KEY)
1299 ref_ptr += sizeof(struct btrfs_inode_extref);
1300 else
1301 ref_ptr += sizeof(struct btrfs_inode_ref);
1302 }
1303 ret = 0;
1304 out:
1305 btrfs_release_path(path);
1306 return ret;
1307}
1308
Filipe Manana0d836392018-07-20 10:59:06 +01001309static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
1310 const u8 ref_type, const char *name,
1311 const int namelen)
1312{
1313 struct btrfs_key key;
1314 struct btrfs_path *path;
1315 const u64 parent_id = btrfs_ino(BTRFS_I(dir));
1316 int ret;
1317
1318 path = btrfs_alloc_path();
1319 if (!path)
1320 return -ENOMEM;
1321
1322 key.objectid = btrfs_ino(BTRFS_I(inode));
1323 key.type = ref_type;
1324 if (key.type == BTRFS_INODE_REF_KEY)
1325 key.offset = parent_id;
1326 else
1327 key.offset = btrfs_extref_hash(parent_id, name, namelen);
1328
1329 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
1330 if (ret < 0)
1331 goto out;
1332 if (ret > 0) {
1333 ret = 0;
1334 goto out;
1335 }
1336 if (key.type == BTRFS_INODE_EXTREF_KEY)
1337 ret = btrfs_find_name_in_ext_backref(path->nodes[0],
1338 path->slots[0], parent_id,
1339 name, namelen, NULL);
1340 else
1341 ret = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
1342 name, namelen, NULL);
1343
1344out:
1345 btrfs_free_path(path);
1346 return ret;
1347}
1348
Filipe Manana6b5fc432019-02-13 12:14:03 +00001349static int add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1350 struct inode *dir, struct inode *inode, const char *name,
1351 int namelen, u64 ref_index)
1352{
1353 struct btrfs_dir_item *dir_item;
1354 struct btrfs_key key;
1355 struct btrfs_path *path;
1356 struct inode *other_inode = NULL;
1357 int ret;
1358
1359 path = btrfs_alloc_path();
1360 if (!path)
1361 return -ENOMEM;
1362
1363 dir_item = btrfs_lookup_dir_item(NULL, root, path,
1364 btrfs_ino(BTRFS_I(dir)),
1365 name, namelen, 0);
1366 if (!dir_item) {
1367 btrfs_release_path(path);
1368 goto add_link;
1369 } else if (IS_ERR(dir_item)) {
1370 ret = PTR_ERR(dir_item);
1371 goto out;
1372 }
1373
1374 /*
1375 * Our inode's dentry collides with the dentry of another inode which is
1376 * in the log but not yet processed since it has a higher inode number.
1377 * So delete that other dentry.
1378 */
1379 btrfs_dir_item_key_to_cpu(path->nodes[0], dir_item, &key);
1380 btrfs_release_path(path);
1381 other_inode = read_one_inode(root, key.objectid);
1382 if (!other_inode) {
1383 ret = -ENOENT;
1384 goto out;
1385 }
1386 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), BTRFS_I(other_inode),
1387 name, namelen);
1388 if (ret)
1389 goto out;
1390 /*
1391 * If we dropped the link count to 0, bump it so that later the iput()
1392 * on the inode will not free it. We will fixup the link count later.
1393 */
1394 if (other_inode->i_nlink == 0)
1395 inc_nlink(other_inode);
1396
1397 ret = btrfs_run_delayed_items(trans);
1398 if (ret)
1399 goto out;
1400add_link:
1401 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
1402 name, namelen, 0, ref_index);
1403out:
1404 iput(other_inode);
1405 btrfs_free_path(path);
1406
1407 return ret;
1408}
1409
Filipe Manana1f250e92018-02-28 15:56:10 +00001410/*
Chris Masone02119d2008-09-05 16:13:11 -04001411 * replay one inode back reference item found in the log tree.
1412 * eb, slot and key refer to the buffer and key found in the log tree.
1413 * root is the destination we are replaying into, and path is for temp
1414 * use by this function. (it should be released on return).
1415 */
1416static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1417 struct btrfs_root *root,
1418 struct btrfs_root *log,
1419 struct btrfs_path *path,
1420 struct extent_buffer *eb, int slot,
1421 struct btrfs_key *key)
1422{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001423 struct inode *dir = NULL;
1424 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001425 unsigned long ref_ptr;
1426 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001427 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001428 int namelen;
1429 int ret;
liuboc622ae62011-03-26 08:01:12 -04001430 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001431 int log_ref_ver = 0;
1432 u64 parent_objectid;
1433 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001434 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001435 int ref_struct_size;
1436
1437 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1438 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1439
1440 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1441 struct btrfs_inode_extref *r;
1442
1443 ref_struct_size = sizeof(struct btrfs_inode_extref);
1444 log_ref_ver = 1;
1445 r = (struct btrfs_inode_extref *)ref_ptr;
1446 parent_objectid = btrfs_inode_extref_parent(eb, r);
1447 } else {
1448 ref_struct_size = sizeof(struct btrfs_inode_ref);
1449 parent_objectid = key->offset;
1450 }
1451 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001452
Chris Masone02119d2008-09-05 16:13:11 -04001453 /*
1454 * it is possible that we didn't log all the parent directories
1455 * for a given inode. If we don't find the dir, just don't
1456 * copy the back ref in. The link count fixup code will take
1457 * care of the rest
1458 */
Mark Fashehf1863732012-08-08 11:32:27 -07001459 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001460 if (!dir) {
1461 ret = -ENOENT;
1462 goto out;
1463 }
Chris Masone02119d2008-09-05 16:13:11 -04001464
Mark Fashehf1863732012-08-08 11:32:27 -07001465 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001466 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001467 ret = -EIO;
1468 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001469 }
Chris Masone02119d2008-09-05 16:13:11 -04001470
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001471 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001472 if (log_ref_ver) {
Qu Wenruobae15d92017-11-08 08:54:26 +08001473 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1474 &ref_index, &parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001475 /*
1476 * parent object can change from one array
1477 * item to another.
1478 */
1479 if (!dir)
1480 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001481 if (!dir) {
1482 ret = -ENOENT;
1483 goto out;
1484 }
Mark Fashehf1863732012-08-08 11:32:27 -07001485 } else {
Qu Wenruobae15d92017-11-08 08:54:26 +08001486 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1487 &ref_index);
Mark Fashehf1863732012-08-08 11:32:27 -07001488 }
1489 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001490 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001491
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001492 /* if we already have a perfect match, we're done */
David Sterbaf85b7372017-01-20 14:54:07 +01001493 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1494 btrfs_ino(BTRFS_I(inode)), ref_index,
1495 name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001496 /*
1497 * look for a conflicting back reference in the
1498 * metadata. if we find one we have to unlink that name
1499 * of the file before we add our new link. Later on, we
1500 * overwrite any existing back reference, and we don't
1501 * want to create dangling pointers in the directory.
1502 */
Chris Masone02119d2008-09-05 16:13:11 -04001503
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001504 if (!search_done) {
1505 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001506 BTRFS_I(dir),
David Sterbad75eefd2017-02-10 20:20:19 +01001507 BTRFS_I(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001508 inode_objectid,
1509 parent_objectid,
1510 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001511 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001512 if (ret) {
1513 if (ret == 1)
1514 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001515 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001516 }
Chris Masone02119d2008-09-05 16:13:11 -04001517 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001518
Filipe Manana0d836392018-07-20 10:59:06 +01001519 /*
1520 * If a reference item already exists for this inode
1521 * with the same parent and name, but different index,
1522 * drop it and the corresponding directory index entries
1523 * from the parent before adding the new reference item
1524 * and dir index entries, otherwise we would fail with
1525 * -EEXIST returned from btrfs_add_link() below.
1526 */
1527 ret = btrfs_inode_ref_exists(inode, dir, key->type,
1528 name, namelen);
1529 if (ret > 0) {
1530 ret = btrfs_unlink_inode(trans, root,
1531 BTRFS_I(dir),
1532 BTRFS_I(inode),
1533 name, namelen);
1534 /*
1535 * If we dropped the link count to 0, bump it so
1536 * that later the iput() on the inode will not
1537 * free it. We will fixup the link count later.
1538 */
1539 if (!ret && inode->i_nlink == 0)
1540 inc_nlink(inode);
1541 }
1542 if (ret < 0)
1543 goto out;
1544
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001545 /* insert our name */
Filipe Manana6b5fc432019-02-13 12:14:03 +00001546 ret = add_link(trans, root, dir, inode, name, namelen,
1547 ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001548 if (ret)
1549 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001550
1551 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001552 }
liuboc622ae62011-03-26 08:01:12 -04001553
Mark Fashehf1863732012-08-08 11:32:27 -07001554 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001555 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001556 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001557 if (log_ref_ver) {
1558 iput(dir);
1559 dir = NULL;
1560 }
Chris Masone02119d2008-09-05 16:13:11 -04001561 }
Chris Masone02119d2008-09-05 16:13:11 -04001562
Filipe Manana1f250e92018-02-28 15:56:10 +00001563 /*
1564 * Before we overwrite the inode reference item in the subvolume tree
1565 * with the item from the log tree, we must unlink all names from the
1566 * parent directory that are in the subvolume's tree inode reference
1567 * item, otherwise we end up with an inconsistent subvolume tree where
1568 * dir index entries exist for a name but there is no inode reference
1569 * item with the same name.
1570 */
1571 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1572 key);
1573 if (ret)
1574 goto out;
1575
Chris Masone02119d2008-09-05 16:13:11 -04001576 /* finally write the back reference in the inode */
1577 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001578out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001579 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001580 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001581 iput(dir);
1582 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001583 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001584}
1585
Yan, Zhengc71bf092009-11-12 09:34:40 +00001586static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001587 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001588{
1589 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001590
David Sterba9c4f61f2015-01-02 19:12:57 +01001591 ret = btrfs_insert_orphan_item(trans, root, ino);
1592 if (ret == -EEXIST)
1593 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001594
Yan, Zhengc71bf092009-11-12 09:34:40 +00001595 return ret;
1596}
1597
Mark Fashehf1863732012-08-08 11:32:27 -07001598static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001599 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001600{
Mark Fashehf1863732012-08-08 11:32:27 -07001601 int ret = 0;
1602 int name_len;
1603 unsigned int nlink = 0;
1604 u32 item_size;
1605 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001606 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001607 u64 offset = 0;
1608 unsigned long ptr;
1609 struct btrfs_inode_extref *extref;
1610 struct extent_buffer *leaf;
1611
1612 while (1) {
1613 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1614 &extref, &offset);
1615 if (ret)
1616 break;
1617
1618 leaf = path->nodes[0];
1619 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1620 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001621 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001622
1623 while (cur_offset < item_size) {
1624 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1625 name_len = btrfs_inode_extref_name_len(leaf, extref);
1626
1627 nlink++;
1628
1629 cur_offset += name_len + sizeof(*extref);
1630 }
1631
1632 offset++;
1633 btrfs_release_path(path);
1634 }
1635 btrfs_release_path(path);
1636
Filipe Manana2c2c4522015-01-13 16:40:04 +00001637 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001638 return ret;
1639 return nlink;
1640}
1641
1642static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001643 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001644{
Chris Masone02119d2008-09-05 16:13:11 -04001645 int ret;
1646 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001647 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001648 unsigned long ptr;
1649 unsigned long ptr_end;
1650 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001651 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001652
Li Zefan33345d012011-04-20 10:31:50 +08001653 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001654 key.type = BTRFS_INODE_REF_KEY;
1655 key.offset = (u64)-1;
1656
Chris Masond3977122009-01-05 21:25:51 -05001657 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001658 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1659 if (ret < 0)
1660 break;
1661 if (ret > 0) {
1662 if (path->slots[0] == 0)
1663 break;
1664 path->slots[0]--;
1665 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001666process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001667 btrfs_item_key_to_cpu(path->nodes[0], &key,
1668 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001669 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001670 key.type != BTRFS_INODE_REF_KEY)
1671 break;
1672 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1673 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1674 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001675 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001676 struct btrfs_inode_ref *ref;
1677
1678 ref = (struct btrfs_inode_ref *)ptr;
1679 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1680 ref);
1681 ptr = (unsigned long)(ref + 1) + name_len;
1682 nlink++;
1683 }
1684
1685 if (key.offset == 0)
1686 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001687 if (path->slots[0] > 0) {
1688 path->slots[0]--;
1689 goto process_slot;
1690 }
Chris Masone02119d2008-09-05 16:13:11 -04001691 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001692 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001693 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001694 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001695
1696 return nlink;
1697}
1698
1699/*
1700 * There are a few corners where the link count of the file can't
1701 * be properly maintained during replay. So, instead of adding
1702 * lots of complexity to the log code, we just scan the backrefs
1703 * for any file that has been through replay.
1704 *
1705 * The scan will update the link count on the inode to reflect the
1706 * number of back refs found. If it goes down to zero, the iput
1707 * will free the inode.
1708 */
1709static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1710 struct btrfs_root *root,
1711 struct inode *inode)
1712{
1713 struct btrfs_path *path;
1714 int ret;
1715 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001716 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001717
1718 path = btrfs_alloc_path();
1719 if (!path)
1720 return -ENOMEM;
1721
Nikolay Borisovf329e312017-01-18 00:31:50 +02001722 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001723 if (ret < 0)
1724 goto out;
1725
1726 nlink = ret;
1727
Nikolay Borisov36283652017-01-18 00:31:49 +02001728 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001729 if (ret < 0)
1730 goto out;
1731
1732 nlink += ret;
1733
1734 ret = 0;
1735
Chris Masone02119d2008-09-05 16:13:11 -04001736 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001737 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001738 btrfs_update_inode(trans, root, inode);
1739 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001740 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001741
Yan, Zhengc71bf092009-11-12 09:34:40 +00001742 if (inode->i_nlink == 0) {
1743 if (S_ISDIR(inode->i_mode)) {
1744 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001745 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001746 if (ret)
1747 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001748 }
Li Zefan33345d012011-04-20 10:31:50 +08001749 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001750 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001751
Mark Fashehf1863732012-08-08 11:32:27 -07001752out:
1753 btrfs_free_path(path);
1754 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001755}
1756
1757static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1758 struct btrfs_root *root,
1759 struct btrfs_path *path)
1760{
1761 int ret;
1762 struct btrfs_key key;
1763 struct inode *inode;
1764
1765 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1766 key.type = BTRFS_ORPHAN_ITEM_KEY;
1767 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001768 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001769 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1770 if (ret < 0)
1771 break;
1772
1773 if (ret == 1) {
1774 if (path->slots[0] == 0)
1775 break;
1776 path->slots[0]--;
1777 }
1778
1779 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1780 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1781 key.type != BTRFS_ORPHAN_ITEM_KEY)
1782 break;
1783
1784 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001785 if (ret)
1786 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001787
David Sterbab3b4aa72011-04-21 01:20:15 +02001788 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001789 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001790 if (!inode)
1791 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001792
1793 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001794 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001795 if (ret)
1796 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001797
Chris Mason12fcfd22009-03-24 10:24:20 -04001798 /*
1799 * fixup on a directory may create new entries,
1800 * make sure we always look for the highset possible
1801 * offset
1802 */
1803 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001804 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001805 ret = 0;
1806out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001807 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001808 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001809}
1810
1811
1812/*
1813 * record a given inode in the fixup dir so we can check its link
1814 * count when replay is done. The link count is incremented here
1815 * so the inode won't go away until we check it
1816 */
1817static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1818 struct btrfs_root *root,
1819 struct btrfs_path *path,
1820 u64 objectid)
1821{
1822 struct btrfs_key key;
1823 int ret = 0;
1824 struct inode *inode;
1825
1826 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001827 if (!inode)
1828 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001829
1830 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001831 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001832 key.offset = objectid;
1833
1834 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1835
David Sterbab3b4aa72011-04-21 01:20:15 +02001836 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001837 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001838 if (!inode->i_nlink)
1839 set_nlink(inode, 1);
1840 else
Zach Brown8b558c52013-10-16 12:10:34 -07001841 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001842 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001843 } else if (ret == -EEXIST) {
1844 ret = 0;
1845 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001846 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001847 }
1848 iput(inode);
1849
1850 return ret;
1851}
1852
1853/*
1854 * when replaying the log for a directory, we only insert names
1855 * for inodes that actually exist. This means an fsync on a directory
1856 * does not implicitly fsync all the new files in it
1857 */
1858static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1859 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001860 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001861 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001862 struct btrfs_key *location)
1863{
1864 struct inode *inode;
1865 struct inode *dir;
1866 int ret;
1867
1868 inode = read_one_inode(root, location->objectid);
1869 if (!inode)
1870 return -ENOENT;
1871
1872 dir = read_one_inode(root, dirid);
1873 if (!dir) {
1874 iput(inode);
1875 return -EIO;
1876 }
Josef Bacikd5554382013-09-11 14:17:00 -04001877
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001878 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1879 name_len, 1, index);
Chris Masone02119d2008-09-05 16:13:11 -04001880
1881 /* FIXME, put inode into FIXUP list */
1882
1883 iput(inode);
1884 iput(dir);
1885 return ret;
1886}
1887
1888/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001889 * Return true if an inode reference exists in the log for the given name,
1890 * inode and parent inode.
1891 */
1892static bool name_in_log_ref(struct btrfs_root *log_root,
1893 const char *name, const int name_len,
1894 const u64 dirid, const u64 ino)
1895{
1896 struct btrfs_key search_key;
1897
1898 search_key.objectid = ino;
1899 search_key.type = BTRFS_INODE_REF_KEY;
1900 search_key.offset = dirid;
1901 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1902 return true;
1903
1904 search_key.type = BTRFS_INODE_EXTREF_KEY;
1905 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1906 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1907 return true;
1908
1909 return false;
1910}
1911
1912/*
Chris Masone02119d2008-09-05 16:13:11 -04001913 * take a single entry in a log directory item and replay it into
1914 * the subvolume.
1915 *
1916 * if a conflicting item exists in the subdirectory already,
1917 * the inode it points to is unlinked and put into the link count
1918 * fix up tree.
1919 *
1920 * If a name from the log points to a file or directory that does
1921 * not exist in the FS, it is skipped. fsyncs on directories
1922 * do not force down inodes inside that directory, just changes to the
1923 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001924 *
1925 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1926 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001927 */
1928static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1929 struct btrfs_root *root,
1930 struct btrfs_path *path,
1931 struct extent_buffer *eb,
1932 struct btrfs_dir_item *di,
1933 struct btrfs_key *key)
1934{
1935 char *name;
1936 int name_len;
1937 struct btrfs_dir_item *dst_di;
1938 struct btrfs_key found_key;
1939 struct btrfs_key log_key;
1940 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001941 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001942 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001943 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001944 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001945 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001946
1947 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001948 if (!dir)
1949 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001950
1951 name_len = btrfs_dir_name_len(eb, di);
1952 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001953 if (!name) {
1954 ret = -ENOMEM;
1955 goto out;
1956 }
liubo2a29edc2011-01-26 06:22:08 +00001957
Chris Masone02119d2008-09-05 16:13:11 -04001958 log_type = btrfs_dir_type(eb, di);
1959 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1960 name_len);
1961
1962 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001963 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1964 if (exists == 0)
1965 exists = 1;
1966 else
1967 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001968 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001969
Chris Masone02119d2008-09-05 16:13:11 -04001970 if (key->type == BTRFS_DIR_ITEM_KEY) {
1971 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1972 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001973 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001974 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1975 key->objectid,
1976 key->offset, name,
1977 name_len, 1);
1978 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001979 /* Corruption */
1980 ret = -EINVAL;
1981 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001982 }
David Sterbac7040052011-04-19 18:00:01 +02001983 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001984 /* we need a sequence number to insert, so we only
1985 * do inserts for the BTRFS_DIR_INDEX_KEY types
1986 */
1987 if (key->type != BTRFS_DIR_INDEX_KEY)
1988 goto out;
1989 goto insert;
1990 }
1991
1992 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1993 /* the existing item matches the logged item */
1994 if (found_key.objectid == log_key.objectid &&
1995 found_key.type == log_key.type &&
1996 found_key.offset == log_key.offset &&
1997 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001998 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001999 goto out;
2000 }
2001
2002 /*
2003 * don't drop the conflicting directory entry if the inode
2004 * for the new entry doesn't exist
2005 */
Chris Mason4bef0842008-09-08 11:18:08 -04002006 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04002007 goto out;
2008
Nikolay Borisov207e7d92017-01-18 00:31:45 +02002009 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04002010 if (ret)
2011 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002012
2013 if (key->type == BTRFS_DIR_INDEX_KEY)
2014 goto insert;
2015out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002016 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04002017 if (!ret && update_size) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02002018 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
Josef Bacikd5554382013-09-11 14:17:00 -04002019 ret = btrfs_update_inode(trans, root, dir);
2020 }
Chris Masone02119d2008-09-05 16:13:11 -04002021 kfree(name);
2022 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01002023 if (!ret && name_added)
2024 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04002025 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002026
2027insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00002028 if (name_in_log_ref(root->log_root, name, name_len,
2029 key->objectid, log_key.objectid)) {
2030 /* The dentry will be added later. */
2031 ret = 0;
2032 update_size = false;
2033 goto out;
2034 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002035 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08002036 ret = insert_one_name(trans, root, key->objectid, key->offset,
2037 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00002038 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04002039 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002040 if (!ret)
2041 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04002042 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04002043 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002044 goto out;
2045}
2046
2047/*
2048 * find all the names in a directory item and reconcile them into
2049 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
2050 * one name in a directory item, but the same code gets used for
2051 * both directory index types
2052 */
2053static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
2054 struct btrfs_root *root,
2055 struct btrfs_path *path,
2056 struct extent_buffer *eb, int slot,
2057 struct btrfs_key *key)
2058{
Filipe Mananabb53eda2015-07-15 23:26:43 +01002059 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002060 u32 item_size = btrfs_item_size_nr(eb, slot);
2061 struct btrfs_dir_item *di;
2062 int name_len;
2063 unsigned long ptr;
2064 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002065 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04002066
2067 ptr = btrfs_item_ptr_offset(eb, slot);
2068 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002069 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002070 di = (struct btrfs_dir_item *)ptr;
2071 name_len = btrfs_dir_name_len(eb, di);
2072 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01002073 if (ret < 0)
2074 break;
Chris Masone02119d2008-09-05 16:13:11 -04002075 ptr = (unsigned long)(di + 1);
2076 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002077
2078 /*
2079 * If this entry refers to a non-directory (directories can not
2080 * have a link count > 1) and it was added in the transaction
2081 * that was not committed, make sure we fixup the link count of
2082 * the inode it the entry points to. Otherwise something like
2083 * the following would result in a directory pointing to an
2084 * inode with a wrong link that does not account for this dir
2085 * entry:
2086 *
2087 * mkdir testdir
2088 * touch testdir/foo
2089 * touch testdir/bar
2090 * sync
2091 *
2092 * ln testdir/bar testdir/bar_link
2093 * ln testdir/foo testdir/foo_link
2094 * xfs_io -c "fsync" testdir/bar
2095 *
2096 * <power failure>
2097 *
2098 * mount fs, log replay happens
2099 *
2100 * File foo would remain with a link count of 1 when it has two
2101 * entries pointing to it in the directory testdir. This would
2102 * make it impossible to ever delete the parent directory has
2103 * it would result in stale dentries that can never be deleted.
2104 */
2105 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
2106 struct btrfs_key di_key;
2107
2108 if (!fixup_path) {
2109 fixup_path = btrfs_alloc_path();
2110 if (!fixup_path) {
2111 ret = -ENOMEM;
2112 break;
2113 }
2114 }
2115
2116 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2117 ret = link_to_fixup_dir(trans, root, fixup_path,
2118 di_key.objectid);
2119 if (ret)
2120 break;
2121 }
2122 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002123 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01002124 btrfs_free_path(fixup_path);
2125 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002126}
2127
2128/*
2129 * directory replay has two parts. There are the standard directory
2130 * items in the log copied from the subvolume, and range items
2131 * created in the log while the subvolume was logged.
2132 *
2133 * The range items tell us which parts of the key space the log
2134 * is authoritative for. During replay, if a key in the subvolume
2135 * directory is in a logged range item, but not actually in the log
2136 * that means it was deleted from the directory before the fsync
2137 * and should be removed.
2138 */
2139static noinline int find_dir_range(struct btrfs_root *root,
2140 struct btrfs_path *path,
2141 u64 dirid, int key_type,
2142 u64 *start_ret, u64 *end_ret)
2143{
2144 struct btrfs_key key;
2145 u64 found_end;
2146 struct btrfs_dir_log_item *item;
2147 int ret;
2148 int nritems;
2149
2150 if (*start_ret == (u64)-1)
2151 return 1;
2152
2153 key.objectid = dirid;
2154 key.type = key_type;
2155 key.offset = *start_ret;
2156
2157 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2158 if (ret < 0)
2159 goto out;
2160 if (ret > 0) {
2161 if (path->slots[0] == 0)
2162 goto out;
2163 path->slots[0]--;
2164 }
2165 if (ret != 0)
2166 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2167
2168 if (key.type != key_type || key.objectid != dirid) {
2169 ret = 1;
2170 goto next;
2171 }
2172 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2173 struct btrfs_dir_log_item);
2174 found_end = btrfs_dir_log_end(path->nodes[0], item);
2175
2176 if (*start_ret >= key.offset && *start_ret <= found_end) {
2177 ret = 0;
2178 *start_ret = key.offset;
2179 *end_ret = found_end;
2180 goto out;
2181 }
2182 ret = 1;
2183next:
2184 /* check the next slot in the tree to see if it is a valid item */
2185 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08002186 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04002187 if (path->slots[0] >= nritems) {
2188 ret = btrfs_next_leaf(root, path);
2189 if (ret)
2190 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002191 }
2192
2193 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2194
2195 if (key.type != key_type || key.objectid != dirid) {
2196 ret = 1;
2197 goto out;
2198 }
2199 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2200 struct btrfs_dir_log_item);
2201 found_end = btrfs_dir_log_end(path->nodes[0], item);
2202 *start_ret = key.offset;
2203 *end_ret = found_end;
2204 ret = 0;
2205out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002206 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002207 return ret;
2208}
2209
2210/*
2211 * this looks for a given directory item in the log. If the directory
2212 * item is not in the log, the item is removed and the inode it points
2213 * to is unlinked
2214 */
2215static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2216 struct btrfs_root *root,
2217 struct btrfs_root *log,
2218 struct btrfs_path *path,
2219 struct btrfs_path *log_path,
2220 struct inode *dir,
2221 struct btrfs_key *dir_key)
2222{
2223 int ret;
2224 struct extent_buffer *eb;
2225 int slot;
2226 u32 item_size;
2227 struct btrfs_dir_item *di;
2228 struct btrfs_dir_item *log_di;
2229 int name_len;
2230 unsigned long ptr;
2231 unsigned long ptr_end;
2232 char *name;
2233 struct inode *inode;
2234 struct btrfs_key location;
2235
2236again:
2237 eb = path->nodes[0];
2238 slot = path->slots[0];
2239 item_size = btrfs_item_size_nr(eb, slot);
2240 ptr = btrfs_item_ptr_offset(eb, slot);
2241 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002242 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002243 di = (struct btrfs_dir_item *)ptr;
2244 name_len = btrfs_dir_name_len(eb, di);
2245 name = kmalloc(name_len, GFP_NOFS);
2246 if (!name) {
2247 ret = -ENOMEM;
2248 goto out;
2249 }
2250 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2251 name_len);
2252 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002253 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002254 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2255 dir_key->objectid,
2256 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002257 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002258 log_di = btrfs_lookup_dir_index_item(trans, log,
2259 log_path,
2260 dir_key->objectid,
2261 dir_key->offset,
2262 name, name_len, 0);
2263 }
Al Viro8d9e2202018-07-29 23:04:46 +01002264 if (!log_di || log_di == ERR_PTR(-ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002265 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002266 btrfs_release_path(path);
2267 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002268 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002269 if (!inode) {
2270 kfree(name);
2271 return -EIO;
2272 }
Chris Masone02119d2008-09-05 16:13:11 -04002273
2274 ret = link_to_fixup_dir(trans, root,
2275 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002276 if (ret) {
2277 kfree(name);
2278 iput(inode);
2279 goto out;
2280 }
2281
Zach Brown8b558c52013-10-16 12:10:34 -07002282 inc_nlink(inode);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002283 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2284 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002285 if (!ret)
Nikolay Borisove5c304e62018-02-07 17:55:43 +02002286 ret = btrfs_run_delayed_items(trans);
Chris Masone02119d2008-09-05 16:13:11 -04002287 kfree(name);
2288 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002289 if (ret)
2290 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002291
2292 /* there might still be more names under this key
2293 * check and repeat if required
2294 */
2295 ret = btrfs_search_slot(NULL, root, dir_key, path,
2296 0, 0);
2297 if (ret == 0)
2298 goto again;
2299 ret = 0;
2300 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002301 } else if (IS_ERR(log_di)) {
2302 kfree(name);
2303 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002304 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002305 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002306 kfree(name);
2307
2308 ptr = (unsigned long)(di + 1);
2309 ptr += name_len;
2310 }
2311 ret = 0;
2312out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002313 btrfs_release_path(path);
2314 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002315 return ret;
2316}
2317
Filipe Manana4f764e52015-02-23 19:53:35 +00002318static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2319 struct btrfs_root *root,
2320 struct btrfs_root *log,
2321 struct btrfs_path *path,
2322 const u64 ino)
2323{
2324 struct btrfs_key search_key;
2325 struct btrfs_path *log_path;
2326 int i;
2327 int nritems;
2328 int ret;
2329
2330 log_path = btrfs_alloc_path();
2331 if (!log_path)
2332 return -ENOMEM;
2333
2334 search_key.objectid = ino;
2335 search_key.type = BTRFS_XATTR_ITEM_KEY;
2336 search_key.offset = 0;
2337again:
2338 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2339 if (ret < 0)
2340 goto out;
2341process_leaf:
2342 nritems = btrfs_header_nritems(path->nodes[0]);
2343 for (i = path->slots[0]; i < nritems; i++) {
2344 struct btrfs_key key;
2345 struct btrfs_dir_item *di;
2346 struct btrfs_dir_item *log_di;
2347 u32 total_size;
2348 u32 cur;
2349
2350 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2351 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2352 ret = 0;
2353 goto out;
2354 }
2355
2356 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2357 total_size = btrfs_item_size_nr(path->nodes[0], i);
2358 cur = 0;
2359 while (cur < total_size) {
2360 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2361 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2362 u32 this_len = sizeof(*di) + name_len + data_len;
2363 char *name;
2364
2365 name = kmalloc(name_len, GFP_NOFS);
2366 if (!name) {
2367 ret = -ENOMEM;
2368 goto out;
2369 }
2370 read_extent_buffer(path->nodes[0], name,
2371 (unsigned long)(di + 1), name_len);
2372
2373 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2374 name, name_len, 0);
2375 btrfs_release_path(log_path);
2376 if (!log_di) {
2377 /* Doesn't exist in log tree, so delete it. */
2378 btrfs_release_path(path);
2379 di = btrfs_lookup_xattr(trans, root, path, ino,
2380 name, name_len, -1);
2381 kfree(name);
2382 if (IS_ERR(di)) {
2383 ret = PTR_ERR(di);
2384 goto out;
2385 }
2386 ASSERT(di);
2387 ret = btrfs_delete_one_dir_name(trans, root,
2388 path, di);
2389 if (ret)
2390 goto out;
2391 btrfs_release_path(path);
2392 search_key = key;
2393 goto again;
2394 }
2395 kfree(name);
2396 if (IS_ERR(log_di)) {
2397 ret = PTR_ERR(log_di);
2398 goto out;
2399 }
2400 cur += this_len;
2401 di = (struct btrfs_dir_item *)((char *)di + this_len);
2402 }
2403 }
2404 ret = btrfs_next_leaf(root, path);
2405 if (ret > 0)
2406 ret = 0;
2407 else if (ret == 0)
2408 goto process_leaf;
2409out:
2410 btrfs_free_path(log_path);
2411 btrfs_release_path(path);
2412 return ret;
2413}
2414
2415
Chris Masone02119d2008-09-05 16:13:11 -04002416/*
2417 * deletion replay happens before we copy any new directory items
2418 * out of the log or out of backreferences from inodes. It
2419 * scans the log to find ranges of keys that log is authoritative for,
2420 * and then scans the directory to find items in those ranges that are
2421 * not present in the log.
2422 *
2423 * Anything we don't find in the log is unlinked and removed from the
2424 * directory.
2425 */
2426static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2427 struct btrfs_root *root,
2428 struct btrfs_root *log,
2429 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002430 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002431{
2432 u64 range_start;
2433 u64 range_end;
2434 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2435 int ret = 0;
2436 struct btrfs_key dir_key;
2437 struct btrfs_key found_key;
2438 struct btrfs_path *log_path;
2439 struct inode *dir;
2440
2441 dir_key.objectid = dirid;
2442 dir_key.type = BTRFS_DIR_ITEM_KEY;
2443 log_path = btrfs_alloc_path();
2444 if (!log_path)
2445 return -ENOMEM;
2446
2447 dir = read_one_inode(root, dirid);
2448 /* it isn't an error if the inode isn't there, that can happen
2449 * because we replay the deletes before we copy in the inode item
2450 * from the log
2451 */
2452 if (!dir) {
2453 btrfs_free_path(log_path);
2454 return 0;
2455 }
2456again:
2457 range_start = 0;
2458 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002459 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002460 if (del_all)
2461 range_end = (u64)-1;
2462 else {
2463 ret = find_dir_range(log, path, dirid, key_type,
2464 &range_start, &range_end);
2465 if (ret != 0)
2466 break;
2467 }
Chris Masone02119d2008-09-05 16:13:11 -04002468
2469 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002470 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002471 int nritems;
2472 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2473 0, 0);
2474 if (ret < 0)
2475 goto out;
2476
2477 nritems = btrfs_header_nritems(path->nodes[0]);
2478 if (path->slots[0] >= nritems) {
2479 ret = btrfs_next_leaf(root, path);
Liu Bob98def72018-04-03 01:59:48 +08002480 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002481 break;
Liu Bob98def72018-04-03 01:59:48 +08002482 else if (ret < 0)
2483 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002484 }
2485 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2486 path->slots[0]);
2487 if (found_key.objectid != dirid ||
2488 found_key.type != dir_key.type)
2489 goto next_type;
2490
2491 if (found_key.offset > range_end)
2492 break;
2493
2494 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002495 log_path, dir,
2496 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002497 if (ret)
2498 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002499 if (found_key.offset == (u64)-1)
2500 break;
2501 dir_key.offset = found_key.offset + 1;
2502 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002503 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002504 if (range_end == (u64)-1)
2505 break;
2506 range_start = range_end + 1;
2507 }
2508
2509next_type:
2510 ret = 0;
2511 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2512 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2513 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002514 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002515 goto again;
2516 }
2517out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002518 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002519 btrfs_free_path(log_path);
2520 iput(dir);
2521 return ret;
2522}
2523
2524/*
2525 * the process_func used to replay items from the log tree. This
2526 * gets called in two different stages. The first stage just looks
2527 * for inodes and makes sure they are all copied into the subvolume.
2528 *
2529 * The second stage copies all the other item types from the log into
2530 * the subvolume. The two stage approach is slower, but gets rid of
2531 * lots of complexity around inodes referencing other inodes that exist
2532 * only in the log (references come from either directory items or inode
2533 * back refs).
2534 */
2535static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +08002536 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -04002537{
2538 int nritems;
2539 struct btrfs_path *path;
2540 struct btrfs_root *root = wc->replay_dest;
2541 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002542 int i;
2543 int ret;
2544
Qu Wenruo581c1762018-03-29 09:08:11 +08002545 ret = btrfs_read_buffer(eb, gen, level, NULL);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002546 if (ret)
2547 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002548
2549 level = btrfs_header_level(eb);
2550
2551 if (level != 0)
2552 return 0;
2553
2554 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002555 if (!path)
2556 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002557
2558 nritems = btrfs_header_nritems(eb);
2559 for (i = 0; i < nritems; i++) {
2560 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002561
2562 /* inode keys are done during the first stage */
2563 if (key.type == BTRFS_INODE_ITEM_KEY &&
2564 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002565 struct btrfs_inode_item *inode_item;
2566 u32 mode;
2567
2568 inode_item = btrfs_item_ptr(eb, i,
2569 struct btrfs_inode_item);
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002570 /*
2571 * If we have a tmpfile (O_TMPFILE) that got fsync'ed
2572 * and never got linked before the fsync, skip it, as
2573 * replaying it is pointless since it would be deleted
2574 * later. We skip logging tmpfiles, but it's always
2575 * possible we are replaying a log created with a kernel
2576 * that used to log tmpfiles.
2577 */
2578 if (btrfs_inode_nlink(eb, inode_item) == 0) {
2579 wc->ignore_cur_inode = true;
2580 continue;
2581 } else {
2582 wc->ignore_cur_inode = false;
2583 }
Filipe Manana4f764e52015-02-23 19:53:35 +00002584 ret = replay_xattr_deletes(wc->trans, root, log,
2585 path, key.objectid);
2586 if (ret)
2587 break;
Chris Masone02119d2008-09-05 16:13:11 -04002588 mode = btrfs_inode_mode(eb, inode_item);
2589 if (S_ISDIR(mode)) {
2590 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002591 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002592 if (ret)
2593 break;
Chris Masone02119d2008-09-05 16:13:11 -04002594 }
2595 ret = overwrite_item(wc->trans, root, path,
2596 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002597 if (ret)
2598 break;
Chris Masone02119d2008-09-05 16:13:11 -04002599
Filipe Manana471d5572018-04-05 22:55:12 +01002600 /*
2601 * Before replaying extents, truncate the inode to its
2602 * size. We need to do it now and not after log replay
2603 * because before an fsync we can have prealloc extents
2604 * added beyond the inode's i_size. If we did it after,
2605 * through orphan cleanup for example, we would drop
2606 * those prealloc extents just after replaying them.
Chris Masone02119d2008-09-05 16:13:11 -04002607 */
2608 if (S_ISREG(mode)) {
Filipe Manana471d5572018-04-05 22:55:12 +01002609 struct inode *inode;
2610 u64 from;
2611
2612 inode = read_one_inode(root, key.objectid);
2613 if (!inode) {
2614 ret = -EIO;
2615 break;
2616 }
2617 from = ALIGN(i_size_read(inode),
2618 root->fs_info->sectorsize);
2619 ret = btrfs_drop_extents(wc->trans, root, inode,
2620 from, (u64)-1, 1);
Filipe Manana471d5572018-04-05 22:55:12 +01002621 if (!ret) {
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002622 /* Update the inode's nbytes. */
Filipe Manana471d5572018-04-05 22:55:12 +01002623 ret = btrfs_update_inode(wc->trans,
2624 root, inode);
2625 }
2626 iput(inode);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002627 if (ret)
2628 break;
Chris Masone02119d2008-09-05 16:13:11 -04002629 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002630
Chris Masone02119d2008-09-05 16:13:11 -04002631 ret = link_to_fixup_dir(wc->trans, root,
2632 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002633 if (ret)
2634 break;
Chris Masone02119d2008-09-05 16:13:11 -04002635 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002636
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002637 if (wc->ignore_cur_inode)
2638 continue;
2639
Josef Bacikdd8e7212013-09-11 11:57:23 -04002640 if (key.type == BTRFS_DIR_INDEX_KEY &&
2641 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2642 ret = replay_one_dir_item(wc->trans, root, path,
2643 eb, i, &key);
2644 if (ret)
2645 break;
2646 }
2647
Chris Masone02119d2008-09-05 16:13:11 -04002648 if (wc->stage < LOG_WALK_REPLAY_ALL)
2649 continue;
2650
2651 /* these keys are simply copied */
2652 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2653 ret = overwrite_item(wc->trans, root, path,
2654 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002655 if (ret)
2656 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002657 } else if (key.type == BTRFS_INODE_REF_KEY ||
2658 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002659 ret = add_inode_ref(wc->trans, root, log, path,
2660 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002661 if (ret && ret != -ENOENT)
2662 break;
2663 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002664 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2665 ret = replay_one_extent(wc->trans, root, path,
2666 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002667 if (ret)
2668 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002669 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002670 ret = replay_one_dir_item(wc->trans, root, path,
2671 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002672 if (ret)
2673 break;
Chris Masone02119d2008-09-05 16:13:11 -04002674 }
2675 }
2676 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002677 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002678}
2679
Chris Masond3977122009-01-05 21:25:51 -05002680static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002681 struct btrfs_root *root,
2682 struct btrfs_path *path, int *level,
2683 struct walk_control *wc)
2684{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002685 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002686 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002687 u64 bytenr;
2688 u64 ptr_gen;
2689 struct extent_buffer *next;
2690 struct extent_buffer *cur;
2691 struct extent_buffer *parent;
2692 u32 blocksize;
2693 int ret = 0;
2694
2695 WARN_ON(*level < 0);
2696 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2697
Chris Masond3977122009-01-05 21:25:51 -05002698 while (*level > 0) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002699 struct btrfs_key first_key;
2700
Chris Masone02119d2008-09-05 16:13:11 -04002701 WARN_ON(*level < 0);
2702 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2703 cur = path->nodes[*level];
2704
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302705 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002706
2707 if (path->slots[*level] >=
2708 btrfs_header_nritems(cur))
2709 break;
2710
2711 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2712 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08002713 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002714 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002715
2716 parent = path->nodes[*level];
2717 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002718
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002719 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002720 if (IS_ERR(next))
2721 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002722
Chris Masone02119d2008-09-05 16:13:11 -04002723 if (*level == 1) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002724 ret = wc->process_func(root, next, wc, ptr_gen,
2725 *level - 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002726 if (ret) {
2727 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002728 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002729 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002730
Chris Masone02119d2008-09-05 16:13:11 -04002731 path->slots[*level]++;
2732 if (wc->free) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002733 ret = btrfs_read_buffer(next, ptr_gen,
2734 *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002735 if (ret) {
2736 free_extent_buffer(next);
2737 return ret;
2738 }
Chris Masone02119d2008-09-05 16:13:11 -04002739
Josef Bacik681ae502013-10-07 15:11:00 -04002740 if (trans) {
2741 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02002742 btrfs_set_lock_blocking_write(next);
David Sterba6a884d7d2019-03-20 14:30:02 +01002743 btrfs_clean_tree_block(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002744 btrfs_wait_tree_block_writeback(next);
2745 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002746 } else {
2747 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2748 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002749 }
Chris Masone02119d2008-09-05 16:13:11 -04002750
Chris Masone02119d2008-09-05 16:13:11 -04002751 WARN_ON(root_owner !=
2752 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002753 ret = btrfs_free_and_pin_reserved_extent(
2754 fs_info, bytenr,
2755 blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002756 if (ret) {
2757 free_extent_buffer(next);
2758 return ret;
2759 }
Chris Masone02119d2008-09-05 16:13:11 -04002760 }
2761 free_extent_buffer(next);
2762 continue;
2763 }
Qu Wenruo581c1762018-03-29 09:08:11 +08002764 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002765 if (ret) {
2766 free_extent_buffer(next);
2767 return ret;
2768 }
Chris Masone02119d2008-09-05 16:13:11 -04002769
2770 WARN_ON(*level <= 0);
2771 if (path->nodes[*level-1])
2772 free_extent_buffer(path->nodes[*level-1]);
2773 path->nodes[*level-1] = next;
2774 *level = btrfs_header_level(next);
2775 path->slots[*level] = 0;
2776 cond_resched();
2777 }
2778 WARN_ON(*level < 0);
2779 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2780
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002781 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002782
2783 cond_resched();
2784 return 0;
2785}
2786
Chris Masond3977122009-01-05 21:25:51 -05002787static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002788 struct btrfs_root *root,
2789 struct btrfs_path *path, int *level,
2790 struct walk_control *wc)
2791{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002792 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002793 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002794 int i;
2795 int slot;
2796 int ret;
2797
Chris Masond3977122009-01-05 21:25:51 -05002798 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002799 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002800 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002801 path->slots[i]++;
2802 *level = i;
2803 WARN_ON(*level == 0);
2804 return 0;
2805 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002806 struct extent_buffer *parent;
2807 if (path->nodes[*level] == root->node)
2808 parent = path->nodes[*level];
2809 else
2810 parent = path->nodes[*level + 1];
2811
2812 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002813 ret = wc->process_func(root, path->nodes[*level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002814 btrfs_header_generation(path->nodes[*level]),
2815 *level);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002816 if (ret)
2817 return ret;
2818
Chris Masone02119d2008-09-05 16:13:11 -04002819 if (wc->free) {
2820 struct extent_buffer *next;
2821
2822 next = path->nodes[*level];
2823
Josef Bacik681ae502013-10-07 15:11:00 -04002824 if (trans) {
2825 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02002826 btrfs_set_lock_blocking_write(next);
David Sterba6a884d7d2019-03-20 14:30:02 +01002827 btrfs_clean_tree_block(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002828 btrfs_wait_tree_block_writeback(next);
2829 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002830 } else {
2831 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2832 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002833 }
Chris Masone02119d2008-09-05 16:13:11 -04002834
Chris Masone02119d2008-09-05 16:13:11 -04002835 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002836 ret = btrfs_free_and_pin_reserved_extent(
2837 fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04002838 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002839 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002840 if (ret)
2841 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002842 }
2843 free_extent_buffer(path->nodes[*level]);
2844 path->nodes[*level] = NULL;
2845 *level = i + 1;
2846 }
2847 }
2848 return 1;
2849}
2850
2851/*
2852 * drop the reference count on the tree rooted at 'snap'. This traverses
2853 * the tree freeing any blocks that have a ref count of zero after being
2854 * decremented.
2855 */
2856static int walk_log_tree(struct btrfs_trans_handle *trans,
2857 struct btrfs_root *log, struct walk_control *wc)
2858{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002859 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002860 int ret = 0;
2861 int wret;
2862 int level;
2863 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002864 int orig_level;
2865
2866 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002867 if (!path)
2868 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002869
2870 level = btrfs_header_level(log->node);
2871 orig_level = level;
2872 path->nodes[level] = log->node;
2873 extent_buffer_get(log->node);
2874 path->slots[level] = 0;
2875
Chris Masond3977122009-01-05 21:25:51 -05002876 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002877 wret = walk_down_log_tree(trans, log, path, &level, wc);
2878 if (wret > 0)
2879 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002880 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002881 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002882 goto out;
2883 }
Chris Masone02119d2008-09-05 16:13:11 -04002884
2885 wret = walk_up_log_tree(trans, log, path, &level, wc);
2886 if (wret > 0)
2887 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002888 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002889 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002890 goto out;
2891 }
Chris Masone02119d2008-09-05 16:13:11 -04002892 }
2893
2894 /* was the root node processed? if not, catch it here */
2895 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002896 ret = wc->process_func(log, path->nodes[orig_level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002897 btrfs_header_generation(path->nodes[orig_level]),
2898 orig_level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002899 if (ret)
2900 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002901 if (wc->free) {
2902 struct extent_buffer *next;
2903
2904 next = path->nodes[orig_level];
2905
Josef Bacik681ae502013-10-07 15:11:00 -04002906 if (trans) {
2907 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02002908 btrfs_set_lock_blocking_write(next);
David Sterba6a884d7d2019-03-20 14:30:02 +01002909 btrfs_clean_tree_block(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002910 btrfs_wait_tree_block_writeback(next);
2911 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002912 } else {
2913 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2914 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002915 }
Chris Masone02119d2008-09-05 16:13:11 -04002916
Chris Masone02119d2008-09-05 16:13:11 -04002917 WARN_ON(log->root_key.objectid !=
2918 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002919 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2920 next->start, next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002921 if (ret)
2922 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002923 }
2924 }
2925
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002926out:
Chris Masone02119d2008-09-05 16:13:11 -04002927 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002928 return ret;
2929}
2930
Yan Zheng7237f182009-01-21 12:54:03 -05002931/*
2932 * helper function to update the item for a given subvolumes log root
2933 * in the tree of log roots
2934 */
2935static int update_log_root(struct btrfs_trans_handle *trans,
2936 struct btrfs_root *log)
2937{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002938 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05002939 int ret;
2940
2941 if (log->log_transid == 1) {
2942 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002943 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002944 &log->root_key, &log->root_item);
2945 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002946 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002947 &log->root_key, &log->root_item);
2948 }
2949 return ret;
2950}
2951
Zhaolei60d53eb2015-08-17 18:44:46 +08002952static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002953{
2954 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002955 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002956
Yan Zheng7237f182009-01-21 12:54:03 -05002957 /*
2958 * we only allow two pending log transactions at a time,
2959 * so we know that if ours is more than 2 older than the
2960 * current transaction, we're done
2961 */
Liu Bo49e83f52017-09-01 16:14:30 -06002962 for (;;) {
Yan Zheng7237f182009-01-21 12:54:03 -05002963 prepare_to_wait(&root->log_commit_wait[index],
2964 &wait, TASK_UNINTERRUPTIBLE);
Liu Bo49e83f52017-09-01 16:14:30 -06002965
2966 if (!(root->log_transid_committed < transid &&
2967 atomic_read(&root->log_commit[index])))
2968 break;
2969
Yan Zheng7237f182009-01-21 12:54:03 -05002970 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002971 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002972 mutex_lock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002973 }
2974 finish_wait(&root->log_commit_wait[index], &wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002975}
2976
Zhaolei60d53eb2015-08-17 18:44:46 +08002977static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002978{
2979 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002980
Liu Bo49e83f52017-09-01 16:14:30 -06002981 for (;;) {
2982 prepare_to_wait(&root->log_writer_wait, &wait,
2983 TASK_UNINTERRUPTIBLE);
2984 if (!atomic_read(&root->log_writers))
2985 break;
2986
Yan Zheng7237f182009-01-21 12:54:03 -05002987 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002988 schedule();
Filipe Manana575849e2015-02-11 11:12:39 +00002989 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002990 }
Liu Bo49e83f52017-09-01 16:14:30 -06002991 finish_wait(&root->log_writer_wait, &wait);
Chris Masone02119d2008-09-05 16:13:11 -04002992}
2993
Miao Xie8b050d32014-02-20 18:08:58 +08002994static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2995 struct btrfs_log_ctx *ctx)
2996{
2997 if (!ctx)
2998 return;
2999
3000 mutex_lock(&root->log_mutex);
3001 list_del_init(&ctx->list);
3002 mutex_unlock(&root->log_mutex);
3003}
3004
3005/*
3006 * Invoked in log mutex context, or be sure there is no other task which
3007 * can access the list.
3008 */
3009static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
3010 int index, int error)
3011{
3012 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07003013 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08003014
Chris Mason570dd452016-10-27 10:42:20 -07003015 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
3016 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08003017 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07003018 }
Miao Xie8b050d32014-02-20 18:08:58 +08003019
3020 INIT_LIST_HEAD(&root->log_ctxs[index]);
3021}
3022
Chris Masone02119d2008-09-05 16:13:11 -04003023/*
3024 * btrfs_sync_log does sends a given tree log down to the disk and
3025 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04003026 * you know that any inodes previously logged are safely on disk only
3027 * if it returns 0.
3028 *
3029 * Any other return value means you need to call btrfs_commit_transaction.
3030 * Some of the edge cases for fsyncing directories that have had unlinks
3031 * or renames done in the past mean that sometimes the only safe
3032 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
3033 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04003034 */
3035int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08003036 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003037{
Yan Zheng7237f182009-01-21 12:54:03 -05003038 int index1;
3039 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003040 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04003041 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003042 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003043 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003044 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08003045 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08003046 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00003047 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04003048
Yan Zheng7237f182009-01-21 12:54:03 -05003049 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08003050 log_transid = ctx->log_transid;
3051 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05003052 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003053 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04003054 }
Miao Xied1433de2014-02-20 18:08:59 +08003055
3056 index1 = log_transid % 2;
3057 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003058 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08003059 mutex_unlock(&root->log_mutex);
3060 return ctx->log_ret;
3061 }
3062 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003063 atomic_set(&root->log_commit[index1], 1);
3064
3065 /* wait for previous tree log sync to complete */
3066 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08003067 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08003068
Yan, Zheng86df7eb2009-10-14 09:24:59 -04003069 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06003070 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04003071 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003072 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08003073 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04003074 mutex_unlock(&root->log_mutex);
3075 schedule_timeout_uninterruptible(1);
3076 mutex_lock(&root->log_mutex);
3077 }
Zhaolei60d53eb2015-08-17 18:44:46 +08003078 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06003079 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04003080 break;
3081 }
Chris Masond0c803c2008-09-11 16:17:57 -04003082
Chris Mason12fcfd22009-03-24 10:24:20 -04003083 /* bail out if we need to do a full commit */
David Sterba4884b8e2019-03-20 13:25:34 +01003084 if (btrfs_need_log_full_commit(trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003085 ret = -EAGAIN;
3086 mutex_unlock(&root->log_mutex);
3087 goto out;
3088 }
3089
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003090 if (log_transid % 2 == 0)
3091 mark = EXTENT_DIRTY;
3092 else
3093 mark = EXTENT_NEW;
3094
Chris Mason690587d2009-10-13 13:29:19 -04003095 /* we start IO on all the marked extents here, but we don't actually
3096 * wait for them until later.
3097 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00003098 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003099 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003100 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003101 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04003102 btrfs_abort_transaction(trans, ret);
David Sterba90787762019-03-20 13:28:05 +01003103 btrfs_set_log_full_commit(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003104 mutex_unlock(&root->log_mutex);
3105 goto out;
3106 }
Yan Zheng7237f182009-01-21 12:54:03 -05003107
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003108 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05003109
Yan Zheng7237f182009-01-21 12:54:03 -05003110 root->log_transid++;
3111 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04003112 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05003113 /*
Filipe Manana06989c72019-05-15 16:03:17 +01003114 * Update or create log root item under the root's log_mutex to prevent
3115 * races with concurrent log syncs that can lead to failure to update
3116 * log root item because it was not created yet.
3117 */
3118 ret = update_log_root(trans, log);
3119 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003120 * IO has been started, blocks of the log tree have WRITTEN flag set
3121 * in their headers. new modifications of the log will be written to
3122 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05003123 */
3124 mutex_unlock(&root->log_mutex);
3125
Filipe Manana28a23592016-08-23 21:13:51 +01003126 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08003127
Yan Zheng7237f182009-01-21 12:54:03 -05003128 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06003129 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05003130 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08003131
3132 index2 = log_root_tree->log_transid % 2;
3133 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3134 root_log_ctx.log_transid = log_root_tree->log_transid;
3135
Yan Zheng7237f182009-01-21 12:54:03 -05003136 mutex_unlock(&log_root_tree->log_mutex);
3137
Yan Zheng7237f182009-01-21 12:54:03 -05003138 mutex_lock(&log_root_tree->log_mutex);
3139 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +01003140 /* atomic_dec_and_test implies a barrier */
3141 cond_wake_up_nomb(&log_root_tree->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -05003142 }
3143
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003144 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08003145 if (!list_empty(&root_log_ctx.list))
3146 list_del_init(&root_log_ctx.list);
3147
Miao Xiec6adc9c2013-05-28 10:05:39 +00003148 blk_finish_plug(&plug);
David Sterba90787762019-03-20 13:28:05 +01003149 btrfs_set_log_full_commit(trans);
Miao Xie995946d2014-04-02 19:51:06 +08003150
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003151 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003152 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003153 mutex_unlock(&log_root_tree->log_mutex);
3154 goto out;
3155 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003156 btrfs_wait_tree_log_extents(log, mark);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003157 mutex_unlock(&log_root_tree->log_mutex);
3158 ret = -EAGAIN;
3159 goto out;
3160 }
3161
Miao Xied1433de2014-02-20 18:08:59 +08003162 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08003163 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07003164 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08003165 mutex_unlock(&log_root_tree->log_mutex);
3166 ret = root_log_ctx.log_ret;
3167 goto out;
3168 }
Miao Xie8b050d32014-02-20 18:08:58 +08003169
Miao Xied1433de2014-02-20 18:08:59 +08003170 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05003171 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003172 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003173 ret = btrfs_wait_tree_log_extents(log, mark);
Zhaolei60d53eb2015-08-17 18:44:46 +08003174 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003175 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003176 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003177 if (!ret)
3178 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05003179 goto out;
3180 }
Miao Xied1433de2014-02-20 18:08:59 +08003181 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003182 atomic_set(&log_root_tree->log_commit[index2], 1);
3183
Chris Mason12fcfd22009-03-24 10:24:20 -04003184 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003185 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003186 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04003187 }
Yan Zheng7237f182009-01-21 12:54:03 -05003188
Zhaolei60d53eb2015-08-17 18:44:46 +08003189 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04003190
3191 /*
3192 * now that we've moved on to the tree of log tree roots,
3193 * check the full commit flag again
3194 */
David Sterba4884b8e2019-03-20 13:25:34 +01003195 if (btrfs_need_log_full_commit(trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003196 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003197 btrfs_wait_tree_log_extents(log, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04003198 mutex_unlock(&log_root_tree->log_mutex);
3199 ret = -EAGAIN;
3200 goto out_wake_log_root;
3201 }
Yan Zheng7237f182009-01-21 12:54:03 -05003202
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003203 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00003204 &log_root_tree->dirty_log_pages,
3205 EXTENT_DIRTY | EXTENT_NEW);
3206 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003207 if (ret) {
David Sterba90787762019-03-20 13:28:05 +01003208 btrfs_set_log_full_commit(trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003209 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003210 mutex_unlock(&log_root_tree->log_mutex);
3211 goto out_wake_log_root;
3212 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003213 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003214 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003215 ret = btrfs_wait_tree_log_extents(log_root_tree,
3216 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003217 if (ret) {
David Sterba90787762019-03-20 13:28:05 +01003218 btrfs_set_log_full_commit(trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003219 mutex_unlock(&log_root_tree->log_mutex);
3220 goto out_wake_log_root;
3221 }
Chris Masone02119d2008-09-05 16:13:11 -04003222
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003223 btrfs_set_super_log_root(fs_info->super_for_commit,
3224 log_root_tree->node->start);
3225 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3226 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04003227
Yan Zheng7237f182009-01-21 12:54:03 -05003228 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05003229 mutex_unlock(&log_root_tree->log_mutex);
3230
3231 /*
Andrea Gelmini52042d82018-11-28 12:05:13 +01003232 * Nobody else is going to jump in and write the ctree
Yan Zheng7237f182009-01-21 12:54:03 -05003233 * super here because the log_commit atomic below is protecting
3234 * us. We must be called with a transaction handle pinning
3235 * the running transaction open, so a full commit can't hop
3236 * in and cause problems either.
3237 */
David Sterbaeece6a92017-02-10 19:04:32 +01003238 ret = write_all_supers(fs_info, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003239 if (ret) {
David Sterba90787762019-03-20 13:28:05 +01003240 btrfs_set_log_full_commit(trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003241 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003242 goto out_wake_log_root;
3243 }
Yan Zheng7237f182009-01-21 12:54:03 -05003244
Chris Mason257c62e2009-10-13 13:21:08 -04003245 mutex_lock(&root->log_mutex);
3246 if (root->last_log_commit < log_transid)
3247 root->last_log_commit = log_transid;
3248 mutex_unlock(&root->log_mutex);
3249
Chris Mason12fcfd22009-03-24 10:24:20 -04003250out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07003251 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003252 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3253
Miao Xied1433de2014-02-20 18:08:59 +08003254 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003255 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003256 mutex_unlock(&log_root_tree->log_mutex);
3257
David Sterba33a9eca2015-10-10 18:35:10 +02003258 /*
David Sterba093258e2018-02-26 16:15:17 +01003259 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3260 * all the updates above are seen by the woken threads. It might not be
3261 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003262 */
David Sterba093258e2018-02-26 16:15:17 +01003263 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04003264out:
Miao Xied1433de2014-02-20 18:08:59 +08003265 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07003266 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08003267 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003268 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003269 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003270
David Sterba33a9eca2015-10-10 18:35:10 +02003271 /*
David Sterba093258e2018-02-26 16:15:17 +01003272 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3273 * all the updates above are seen by the woken threads. It might not be
3274 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003275 */
David Sterba093258e2018-02-26 16:15:17 +01003276 cond_wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003277 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003278}
3279
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003280static void free_log_tree(struct btrfs_trans_handle *trans,
3281 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003282{
3283 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04003284 struct walk_control wc = {
3285 .free = 1,
3286 .process_func = process_one_buffer
3287 };
3288
Josef Bacik681ae502013-10-07 15:11:00 -04003289 ret = walk_log_tree(trans, log, &wc);
Jeff Mahoney374b0e22018-09-06 16:59:33 -04003290 if (ret) {
3291 if (trans)
3292 btrfs_abort_transaction(trans, ret);
3293 else
3294 btrfs_handle_fs_error(log->fs_info, ret, NULL);
3295 }
Chris Masone02119d2008-09-05 16:13:11 -04003296
Filipe Manana59b07132018-11-09 10:43:08 +00003297 clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
3298 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Yan Zheng7237f182009-01-21 12:54:03 -05003299 free_extent_buffer(log->node);
3300 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003301}
3302
3303/*
3304 * free all the extents used by the tree log. This should be called
3305 * at commit time of the full transaction
3306 */
3307int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3308{
3309 if (root->log_root) {
3310 free_log_tree(trans, root->log_root);
3311 root->log_root = NULL;
3312 }
3313 return 0;
3314}
3315
3316int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3317 struct btrfs_fs_info *fs_info)
3318{
3319 if (fs_info->log_root_tree) {
3320 free_log_tree(trans, fs_info->log_root_tree);
3321 fs_info->log_root_tree = NULL;
3322 }
Chris Masone02119d2008-09-05 16:13:11 -04003323 return 0;
3324}
3325
3326/*
Filipe Manana803f0f62019-06-19 13:05:39 +01003327 * Check if an inode was logged in the current transaction. We can't always rely
3328 * on an inode's logged_trans value, because it's an in-memory only field and
3329 * therefore not persisted. This means that its value is lost if the inode gets
3330 * evicted and loaded again from disk (in which case it has a value of 0, and
3331 * certainly it is smaller then any possible transaction ID), when that happens
3332 * the full_sync flag is set in the inode's runtime flags, so on that case we
3333 * assume eviction happened and ignore the logged_trans value, assuming the
3334 * worst case, that the inode was logged before in the current transaction.
3335 */
3336static bool inode_logged(struct btrfs_trans_handle *trans,
3337 struct btrfs_inode *inode)
3338{
3339 if (inode->logged_trans == trans->transid)
3340 return true;
3341
3342 if (inode->last_trans == trans->transid &&
3343 test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) &&
3344 !test_bit(BTRFS_FS_LOG_RECOVERING, &trans->fs_info->flags))
3345 return true;
3346
3347 return false;
3348}
3349
3350/*
Chris Masone02119d2008-09-05 16:13:11 -04003351 * If both a file and directory are logged, and unlinks or renames are
3352 * mixed in, we have a few interesting corners:
3353 *
3354 * create file X in dir Y
3355 * link file X to X.link in dir Y
3356 * fsync file X
3357 * unlink file X but leave X.link
3358 * fsync dir Y
3359 *
3360 * After a crash we would expect only X.link to exist. But file X
3361 * didn't get fsync'd again so the log has back refs for X and X.link.
3362 *
3363 * We solve this by removing directory entries and inode backrefs from the
3364 * log when a file that was logged in the current transaction is
3365 * unlinked. Any later fsync will include the updated log entries, and
3366 * we'll be able to reconstruct the proper directory items from backrefs.
3367 *
3368 * This optimizations allows us to avoid relogging the entire inode
3369 * or the entire directory.
3370 */
3371int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3372 struct btrfs_root *root,
3373 const char *name, int name_len,
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003374 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003375{
3376 struct btrfs_root *log;
3377 struct btrfs_dir_item *di;
3378 struct btrfs_path *path;
3379 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003380 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003381 int bytes_del = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003382 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003383
Filipe Manana803f0f62019-06-19 13:05:39 +01003384 if (!inode_logged(trans, dir))
Chris Mason3a5f1d42008-09-11 15:53:37 -04003385 return 0;
3386
Chris Masone02119d2008-09-05 16:13:11 -04003387 ret = join_running_log_trans(root);
3388 if (ret)
3389 return 0;
3390
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003391 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003392
3393 log = root->log_root;
3394 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003395 if (!path) {
3396 err = -ENOMEM;
3397 goto out_unlock;
3398 }
liubo2a29edc2011-01-26 06:22:08 +00003399
Li Zefan33345d012011-04-20 10:31:50 +08003400 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003401 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003402 if (IS_ERR(di)) {
3403 err = PTR_ERR(di);
3404 goto fail;
3405 }
3406 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003407 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3408 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003409 if (ret) {
3410 err = ret;
3411 goto fail;
3412 }
Chris Masone02119d2008-09-05 16:13:11 -04003413 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003414 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003415 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003416 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003417 if (IS_ERR(di)) {
3418 err = PTR_ERR(di);
3419 goto fail;
3420 }
3421 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003422 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3423 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003424 if (ret) {
3425 err = ret;
3426 goto fail;
3427 }
Chris Masone02119d2008-09-05 16:13:11 -04003428 }
3429
3430 /* update the directory size in the log to reflect the names
3431 * we have removed
3432 */
3433 if (bytes_del) {
3434 struct btrfs_key key;
3435
Li Zefan33345d012011-04-20 10:31:50 +08003436 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003437 key.offset = 0;
3438 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003439 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003440
3441 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003442 if (ret < 0) {
3443 err = ret;
3444 goto fail;
3445 }
Chris Masone02119d2008-09-05 16:13:11 -04003446 if (ret == 0) {
3447 struct btrfs_inode_item *item;
3448 u64 i_size;
3449
3450 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3451 struct btrfs_inode_item);
3452 i_size = btrfs_inode_size(path->nodes[0], item);
3453 if (i_size > bytes_del)
3454 i_size -= bytes_del;
3455 else
3456 i_size = 0;
3457 btrfs_set_inode_size(path->nodes[0], item, i_size);
3458 btrfs_mark_buffer_dirty(path->nodes[0]);
3459 } else
3460 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003461 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003462 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003463fail:
Chris Masone02119d2008-09-05 16:13:11 -04003464 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003465out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003466 mutex_unlock(&dir->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003467 if (ret == -ENOSPC) {
David Sterba90787762019-03-20 13:28:05 +01003468 btrfs_set_log_full_commit(trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003469 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003470 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003471 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003472
Chris Mason12fcfd22009-03-24 10:24:20 -04003473 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003474
Andi Kleen411fc6b2010-10-29 15:14:31 -04003475 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003476}
3477
3478/* see comments for btrfs_del_dir_entries_in_log */
3479int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3480 struct btrfs_root *root,
3481 const char *name, int name_len,
Nikolay Borisova491abb2017-01-18 00:31:33 +02003482 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003483{
3484 struct btrfs_root *log;
3485 u64 index;
3486 int ret;
3487
Filipe Manana803f0f62019-06-19 13:05:39 +01003488 if (!inode_logged(trans, inode))
Chris Mason3a5f1d42008-09-11 15:53:37 -04003489 return 0;
3490
Chris Masone02119d2008-09-05 16:13:11 -04003491 ret = join_running_log_trans(root);
3492 if (ret)
3493 return 0;
3494 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003495 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003496
Nikolay Borisova491abb2017-01-18 00:31:33 +02003497 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003498 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003499 mutex_unlock(&inode->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003500 if (ret == -ENOSPC) {
David Sterba90787762019-03-20 13:28:05 +01003501 btrfs_set_log_full_commit(trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003502 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003503 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003504 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003505 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003506
Chris Masone02119d2008-09-05 16:13:11 -04003507 return ret;
3508}
3509
3510/*
3511 * creates a range item in the log for 'dirid'. first_offset and
3512 * last_offset tell us which parts of the key space the log should
3513 * be considered authoritative for.
3514 */
3515static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3516 struct btrfs_root *log,
3517 struct btrfs_path *path,
3518 int key_type, u64 dirid,
3519 u64 first_offset, u64 last_offset)
3520{
3521 int ret;
3522 struct btrfs_key key;
3523 struct btrfs_dir_log_item *item;
3524
3525 key.objectid = dirid;
3526 key.offset = first_offset;
3527 if (key_type == BTRFS_DIR_ITEM_KEY)
3528 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3529 else
3530 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3531 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003532 if (ret)
3533 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003534
3535 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3536 struct btrfs_dir_log_item);
3537 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3538 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003539 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003540 return 0;
3541}
3542
3543/*
3544 * log all the items included in the current transaction for a given
3545 * directory. This also creates the range items in the log tree required
3546 * to replay anything deleted before the fsync
3547 */
3548static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Nikolay Borisov684a5772017-01-18 00:31:41 +02003549 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003550 struct btrfs_path *path,
3551 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003552 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003553 u64 min_offset, u64 *last_offset_ret)
3554{
3555 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003556 struct btrfs_root *log = root->log_root;
3557 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003558 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003559 int ret;
3560 int i;
3561 int nritems;
3562 u64 first_offset = min_offset;
3563 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003564 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003565
3566 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003567
Li Zefan33345d012011-04-20 10:31:50 +08003568 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003569 min_key.type = key_type;
3570 min_key.offset = min_offset;
3571
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003572 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003573
3574 /*
3575 * we didn't find anything from this transaction, see if there
3576 * is anything at all
3577 */
Li Zefan33345d012011-04-20 10:31:50 +08003578 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3579 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003580 min_key.type = key_type;
3581 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003582 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003583 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3584 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003585 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003586 return ret;
3587 }
Li Zefan33345d012011-04-20 10:31:50 +08003588 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003589
3590 /* if ret == 0 there are items for this type,
3591 * create a range to tell us the last key of this type.
3592 * otherwise, there are no items in this directory after
3593 * *min_offset, and we create a range to indicate that.
3594 */
3595 if (ret == 0) {
3596 struct btrfs_key tmp;
3597 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3598 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003599 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003600 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003601 }
3602 goto done;
3603 }
3604
3605 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003606 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003607 if (ret == 0) {
3608 struct btrfs_key tmp;
3609 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3610 if (key_type == tmp.type) {
3611 first_offset = tmp.offset;
3612 ret = overwrite_item(trans, log, dst_path,
3613 path->nodes[0], path->slots[0],
3614 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003615 if (ret) {
3616 err = ret;
3617 goto done;
3618 }
Chris Masone02119d2008-09-05 16:13:11 -04003619 }
3620 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003621 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003622
Josef Bacik2cc83342019-03-06 17:13:04 -05003623 /*
3624 * Find the first key from this transaction again. See the note for
3625 * log_new_dir_dentries, if we're logging a directory recursively we
3626 * won't be holding its i_mutex, which means we can modify the directory
3627 * while we're logging it. If we remove an entry between our first
3628 * search and this search we'll not find the key again and can just
3629 * bail.
3630 */
Chris Masone02119d2008-09-05 16:13:11 -04003631 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Josef Bacik2cc83342019-03-06 17:13:04 -05003632 if (ret != 0)
Chris Masone02119d2008-09-05 16:13:11 -04003633 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003634
3635 /*
3636 * we have a block from this transaction, log every item in it
3637 * from our directory
3638 */
Chris Masond3977122009-01-05 21:25:51 -05003639 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003640 struct btrfs_key tmp;
3641 src = path->nodes[0];
3642 nritems = btrfs_header_nritems(src);
3643 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003644 struct btrfs_dir_item *di;
3645
Chris Masone02119d2008-09-05 16:13:11 -04003646 btrfs_item_key_to_cpu(src, &min_key, i);
3647
Li Zefan33345d012011-04-20 10:31:50 +08003648 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003649 goto done;
3650 ret = overwrite_item(trans, log, dst_path, src, i,
3651 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003652 if (ret) {
3653 err = ret;
3654 goto done;
3655 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003656
3657 /*
3658 * We must make sure that when we log a directory entry,
3659 * the corresponding inode, after log replay, has a
3660 * matching link count. For example:
3661 *
3662 * touch foo
3663 * mkdir mydir
3664 * sync
3665 * ln foo mydir/bar
3666 * xfs_io -c "fsync" mydir
3667 * <crash>
3668 * <mount fs and log replay>
3669 *
3670 * Would result in a fsync log that when replayed, our
3671 * file inode would have a link count of 1, but we get
3672 * two directory entries pointing to the same inode.
3673 * After removing one of the names, it would not be
3674 * possible to remove the other name, which resulted
3675 * always in stale file handle errors, and would not
3676 * be possible to rmdir the parent directory, since
3677 * its i_size could never decrement to the value
3678 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3679 */
3680 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3681 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3682 if (ctx &&
3683 (btrfs_dir_transid(src, di) == trans->transid ||
3684 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3685 tmp.type != BTRFS_ROOT_ITEM_KEY)
3686 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003687 }
3688 path->slots[0] = nritems;
3689
3690 /*
3691 * look ahead to the next item and see if it is also
3692 * from this directory and from this transaction
3693 */
3694 ret = btrfs_next_leaf(root, path);
Liu Bo80c0b422018-04-03 01:59:47 +08003695 if (ret) {
3696 if (ret == 1)
3697 last_offset = (u64)-1;
3698 else
3699 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003700 goto done;
3701 }
3702 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003703 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003704 last_offset = (u64)-1;
3705 goto done;
3706 }
3707 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3708 ret = overwrite_item(trans, log, dst_path,
3709 path->nodes[0], path->slots[0],
3710 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003711 if (ret)
3712 err = ret;
3713 else
3714 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003715 goto done;
3716 }
3717 }
3718done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003719 btrfs_release_path(path);
3720 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003721
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003722 if (err == 0) {
3723 *last_offset_ret = last_offset;
3724 /*
3725 * insert the log range keys to indicate where the log
3726 * is valid
3727 */
3728 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003729 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003730 if (ret)
3731 err = ret;
3732 }
3733 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003734}
3735
3736/*
3737 * logging directories is very similar to logging inodes, We find all the items
3738 * from the current transaction and write them to the log.
3739 *
3740 * The recovery code scans the directory in the subvolume, and if it finds a
3741 * key in the range logged that is not present in the log tree, then it means
3742 * that dir entry was unlinked during the transaction.
3743 *
3744 * In order for that scan to work, we must include one key smaller than
3745 * the smallest logged by this transaction and one key larger than the largest
3746 * key logged by this transaction.
3747 */
3748static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003749 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003750 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003751 struct btrfs_path *dst_path,
3752 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003753{
3754 u64 min_key;
3755 u64 max_key;
3756 int ret;
3757 int key_type = BTRFS_DIR_ITEM_KEY;
3758
3759again:
3760 min_key = 0;
3761 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003762 while (1) {
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003763 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3764 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003765 if (ret)
3766 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003767 if (max_key == (u64)-1)
3768 break;
3769 min_key = max_key + 1;
3770 }
3771
3772 if (key_type == BTRFS_DIR_ITEM_KEY) {
3773 key_type = BTRFS_DIR_INDEX_KEY;
3774 goto again;
3775 }
3776 return 0;
3777}
3778
3779/*
3780 * a helper function to drop items from the log before we relog an
3781 * inode. max_key_type indicates the highest item type to remove.
3782 * This cannot be run for file data extents because it does not
3783 * free the extents they point to.
3784 */
3785static int drop_objectid_items(struct btrfs_trans_handle *trans,
3786 struct btrfs_root *log,
3787 struct btrfs_path *path,
3788 u64 objectid, int max_key_type)
3789{
3790 int ret;
3791 struct btrfs_key key;
3792 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003793 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003794
3795 key.objectid = objectid;
3796 key.type = max_key_type;
3797 key.offset = (u64)-1;
3798
Chris Masond3977122009-01-05 21:25:51 -05003799 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003800 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003801 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003802 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003803 break;
3804
3805 if (path->slots[0] == 0)
3806 break;
3807
3808 path->slots[0]--;
3809 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3810 path->slots[0]);
3811
3812 if (found_key.objectid != objectid)
3813 break;
3814
Josef Bacik18ec90d2012-09-28 11:56:28 -04003815 found_key.offset = 0;
3816 found_key.type = 0;
3817 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3818 &start_slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00003819 if (ret < 0)
3820 break;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003821
3822 ret = btrfs_del_items(trans, log, path, start_slot,
3823 path->slots[0] - start_slot + 1);
3824 /*
3825 * If start slot isn't 0 then we don't need to re-search, we've
3826 * found the last guy with the objectid in this tree.
3827 */
3828 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003829 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003830 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003831 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003832 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003833 if (ret > 0)
3834 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003835 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003836}
3837
Josef Bacik94edf4a2012-09-25 14:56:25 -04003838static void fill_inode_item(struct btrfs_trans_handle *trans,
3839 struct extent_buffer *leaf,
3840 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003841 struct inode *inode, int log_inode_only,
3842 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003843{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003844 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003845
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003846 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003847
3848 if (log_inode_only) {
3849 /* set the generation to zero so the recover code
3850 * can tell the difference between an logging
3851 * just to say 'this inode exists' and a logging
3852 * to say 'update this inode with these values'
3853 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003854 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003855 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003856 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003857 btrfs_set_token_inode_generation(leaf, item,
3858 BTRFS_I(inode)->generation,
3859 &token);
3860 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003861 }
3862
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003863 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3864 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3865 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3866 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3867
David Sterbaa937b972014-12-12 17:39:12 +01003868 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003869 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003870 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003871 inode->i_atime.tv_nsec, &token);
3872
David Sterbaa937b972014-12-12 17:39:12 +01003873 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003874 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003875 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003876 inode->i_mtime.tv_nsec, &token);
3877
David Sterbaa937b972014-12-12 17:39:12 +01003878 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003879 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003880 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003881 inode->i_ctime.tv_nsec, &token);
3882
3883 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3884 &token);
3885
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003886 btrfs_set_token_inode_sequence(leaf, item,
3887 inode_peek_iversion(inode), &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003888 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3889 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3890 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3891 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003892}
3893
Josef Bacika95249b2012-10-11 16:17:34 -04003894static int log_inode_item(struct btrfs_trans_handle *trans,
3895 struct btrfs_root *log, struct btrfs_path *path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003896 struct btrfs_inode *inode)
Josef Bacika95249b2012-10-11 16:17:34 -04003897{
3898 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003899 int ret;
3900
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003901 ret = btrfs_insert_empty_item(trans, log, path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003902 &inode->location, sizeof(*inode_item));
Josef Bacika95249b2012-10-11 16:17:34 -04003903 if (ret && ret != -EEXIST)
3904 return ret;
3905 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3906 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003907 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3908 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003909 btrfs_release_path(path);
3910 return 0;
3911}
3912
Chris Mason31ff1cd2008-09-11 16:17:57 -04003913static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003914 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003915 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003916 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003917 int start_slot, int nr, int inode_only,
3918 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003919{
David Sterba3ffbd682018-06-29 10:56:42 +02003920 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003921 unsigned long src_offset;
3922 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003923 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003924 struct btrfs_file_extent_item *extent;
3925 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003926 struct extent_buffer *src = src_path->nodes[0];
3927 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003928 int ret;
3929 struct btrfs_key *ins_keys;
3930 u32 *ins_sizes;
3931 char *ins_data;
3932 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003933 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003934 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003935 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003936 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003937 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003938
3939 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003940
3941 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3942 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003943 if (!ins_data)
3944 return -ENOMEM;
3945
Josef Bacik16e75492013-10-22 12:18:51 -04003946 first_key.objectid = (u64)-1;
3947
Chris Mason31ff1cd2008-09-11 16:17:57 -04003948 ins_sizes = (u32 *)ins_data;
3949 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3950
3951 for (i = 0; i < nr; i++) {
3952 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3953 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3954 }
3955 ret = btrfs_insert_empty_items(trans, log, dst_path,
3956 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003957 if (ret) {
3958 kfree(ins_data);
3959 return ret;
3960 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003961
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003962 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003963 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3964 dst_path->slots[0]);
3965
3966 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3967
Matthias Kaehlcke0dde10b2017-07-27 14:30:23 -07003968 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003969 last_key = ins_keys[i];
3970
Josef Bacik94edf4a2012-09-25 14:56:25 -04003971 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003972 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3973 dst_path->slots[0],
3974 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003975 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01003976 &inode->vfs_inode,
3977 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003978 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003979 } else {
3980 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3981 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003982 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003983
Josef Bacik16e75492013-10-22 12:18:51 -04003984 /*
3985 * We set need_find_last_extent here in case we know we were
3986 * processing other items and then walk into the first extent in
3987 * the inode. If we don't hit an extent then nothing changes,
3988 * we'll do the last search the next time around.
3989 */
3990 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3991 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003992 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003993 first_key = ins_keys[i];
3994 } else {
3995 need_find_last_extent = false;
3996 }
3997
Chris Mason31ff1cd2008-09-11 16:17:57 -04003998 /* take a reference on file data extents so that truncates
3999 * or deletes of this inode don't have to relog the inode
4000 * again
4001 */
David Sterba962a2982014-06-04 18:41:45 +02004002 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06004003 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004004 int found_type;
4005 extent = btrfs_item_ptr(src, start_slot + i,
4006 struct btrfs_file_extent_item);
4007
liubo8e531cd2011-05-06 10:36:09 +08004008 if (btrfs_file_extent_generation(src, extent) < trans->transid)
4009 continue;
4010
Chris Mason31ff1cd2008-09-11 16:17:57 -04004011 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04004012 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004013 u64 ds, dl, cs, cl;
4014 ds = btrfs_file_extent_disk_bytenr(src,
4015 extent);
4016 /* ds == 0 is a hole */
4017 if (ds == 0)
4018 continue;
4019
4020 dl = btrfs_file_extent_disk_num_bytes(src,
4021 extent);
4022 cs = btrfs_file_extent_offset(src, extent);
4023 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07004024 extent);
Chris Mason580afd72008-12-08 19:15:39 -05004025 if (btrfs_file_extent_compression(src,
4026 extent)) {
4027 cs = 0;
4028 cl = dl;
4029 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004030
4031 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004032 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004033 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01004034 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04004035 if (ret) {
4036 btrfs_release_path(dst_path);
4037 kfree(ins_data);
4038 return ret;
4039 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004040 }
4041 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004042 }
4043
4044 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004045 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04004046 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05004047
4048 /*
4049 * we have to do this after the loop above to avoid changing the
4050 * log tree while trying to change the log tree.
4051 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004052 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004053 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05004054 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4055 struct btrfs_ordered_sum,
4056 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004057 if (!ret)
4058 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05004059 list_del(&sums->list);
4060 kfree(sums);
4061 }
Josef Bacik16e75492013-10-22 12:18:51 -04004062
4063 if (!has_extents)
4064 return ret;
4065
Filipe Manana74121f7c2014-08-07 12:00:44 +01004066 if (need_find_last_extent && *last_extent == first_key.offset) {
4067 /*
4068 * We don't have any leafs between our current one and the one
4069 * we processed before that can have file extent items for our
4070 * inode (and have a generation number smaller than our current
4071 * transaction id).
4072 */
4073 need_find_last_extent = false;
4074 }
4075
Josef Bacik16e75492013-10-22 12:18:51 -04004076 /*
4077 * Because we use btrfs_search_forward we could skip leaves that were
4078 * not modified and then assume *last_extent is valid when it really
4079 * isn't. So back up to the previous leaf and read the end of the last
4080 * extent before we go and fill in holes.
4081 */
4082 if (need_find_last_extent) {
4083 u64 len;
4084
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004085 ret = btrfs_prev_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04004086 if (ret < 0)
4087 return ret;
4088 if (ret)
4089 goto fill_holes;
4090 if (src_path->slots[0])
4091 src_path->slots[0]--;
4092 src = src_path->nodes[0];
4093 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004094 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04004095 key.type != BTRFS_EXTENT_DATA_KEY)
4096 goto fill_holes;
4097 extent = btrfs_item_ptr(src, src_path->slots[0],
4098 struct btrfs_file_extent_item);
4099 if (btrfs_file_extent_type(src, extent) ==
4100 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004101 len = btrfs_file_extent_ram_bytes(src, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04004102 *last_extent = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004103 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04004104 } else {
4105 len = btrfs_file_extent_num_bytes(src, extent);
4106 *last_extent = key.offset + len;
4107 }
4108 }
4109fill_holes:
4110 /* So we did prev_leaf, now we need to move to the next leaf, but a few
4111 * things could have happened
4112 *
4113 * 1) A merge could have happened, so we could currently be on a leaf
4114 * that holds what we were copying in the first place.
4115 * 2) A split could have happened, and now not all of the items we want
4116 * are on the same leaf.
4117 *
4118 * So we need to adjust how we search for holes, we need to drop the
4119 * path and re-search for the first extent key we found, and then walk
4120 * forward until we hit the last one we copied.
4121 */
4122 if (need_find_last_extent) {
4123 /* btrfs_prev_leaf could return 1 without releasing the path */
4124 btrfs_release_path(src_path);
David Sterbaf85b7372017-01-20 14:54:07 +01004125 ret = btrfs_search_slot(NULL, inode->root, &first_key,
4126 src_path, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004127 if (ret < 0)
4128 return ret;
4129 ASSERT(ret == 0);
4130 src = src_path->nodes[0];
4131 i = src_path->slots[0];
4132 } else {
4133 i = start_slot;
4134 }
4135
4136 /*
4137 * Ok so here we need to go through and fill in any holes we may have
4138 * to make sure that holes are punched for those areas in case they had
4139 * extents previously.
4140 */
4141 while (!done) {
4142 u64 offset, len;
4143 u64 extent_end;
4144
4145 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004146 ret = btrfs_next_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04004147 if (ret < 0)
4148 return ret;
4149 ASSERT(ret == 0);
4150 src = src_path->nodes[0];
4151 i = 0;
Filipe Manana8434ec42018-03-26 23:59:12 +01004152 need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04004153 }
4154
4155 btrfs_item_key_to_cpu(src, &key, i);
4156 if (!btrfs_comp_cpu_keys(&key, &last_key))
4157 done = true;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004158 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04004159 key.type != BTRFS_EXTENT_DATA_KEY) {
4160 i++;
4161 continue;
4162 }
4163 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
4164 if (btrfs_file_extent_type(src, extent) ==
4165 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004166 len = btrfs_file_extent_ram_bytes(src, extent);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004167 extent_end = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004168 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04004169 } else {
4170 len = btrfs_file_extent_num_bytes(src, extent);
4171 extent_end = key.offset + len;
4172 }
4173 i++;
4174
4175 if (*last_extent == key.offset) {
4176 *last_extent = extent_end;
4177 continue;
4178 }
4179 offset = *last_extent;
4180 len = key.offset - *last_extent;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004181 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01004182 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004183 if (ret)
4184 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01004185 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04004186 }
Filipe Manana4ee3fad2018-03-26 23:59:00 +01004187
4188 /*
4189 * Check if there is a hole between the last extent found in our leaf
4190 * and the first extent in the next leaf. If there is one, we need to
4191 * log an explicit hole so that at replay time we can punch the hole.
4192 */
4193 if (ret == 0 &&
4194 key.objectid == btrfs_ino(inode) &&
4195 key.type == BTRFS_EXTENT_DATA_KEY &&
4196 i == btrfs_header_nritems(src_path->nodes[0])) {
4197 ret = btrfs_next_leaf(inode->root, src_path);
4198 need_find_last_extent = true;
4199 if (ret > 0) {
4200 ret = 0;
4201 } else if (ret == 0) {
4202 btrfs_item_key_to_cpu(src_path->nodes[0], &key,
4203 src_path->slots[0]);
4204 if (key.objectid == btrfs_ino(inode) &&
4205 key.type == BTRFS_EXTENT_DATA_KEY &&
4206 *last_extent < key.offset) {
4207 const u64 len = key.offset - *last_extent;
4208
4209 ret = btrfs_insert_file_extent(trans, log,
4210 btrfs_ino(inode),
4211 *last_extent, 0,
4212 0, len, 0, len,
4213 0, 0, 0);
Filipe Mananaebb92902019-05-06 16:43:51 +01004214 *last_extent += len;
Filipe Manana4ee3fad2018-03-26 23:59:00 +01004215 }
4216 }
4217 }
Josef Bacik16e75492013-10-22 12:18:51 -04004218 /*
4219 * Need to let the callers know we dropped the path so they should
4220 * re-search.
4221 */
4222 if (!ret && need_find_last_extent)
4223 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004224 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004225}
4226
Josef Bacik5dc562c2012-08-17 13:14:17 -04004227static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4228{
4229 struct extent_map *em1, *em2;
4230
4231 em1 = list_entry(a, struct extent_map, list);
4232 em2 = list_entry(b, struct extent_map, list);
4233
4234 if (em1->start < em2->start)
4235 return -1;
4236 else if (em1->start > em2->start)
4237 return 1;
4238 return 0;
4239}
4240
Josef Bacike7175a62018-05-23 11:58:34 -04004241static int log_extent_csums(struct btrfs_trans_handle *trans,
4242 struct btrfs_inode *inode,
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004243 struct btrfs_root *log_root,
Josef Bacike7175a62018-05-23 11:58:34 -04004244 const struct extent_map *em)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004245{
Josef Bacik2ab28f32012-10-12 15:27:49 -04004246 u64 csum_offset;
4247 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004248 LIST_HEAD(ordered_sums);
4249 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004250
Josef Bacike7175a62018-05-23 11:58:34 -04004251 if (inode->flags & BTRFS_INODE_NODATASUM ||
4252 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
Filipe Manana8407f552014-09-05 15:14:39 +01004253 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04004254 return 0;
4255
Josef Bacike7175a62018-05-23 11:58:34 -04004256 /* If we're compressed we have to save the entire range of csums. */
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004257 if (em->compress_type) {
4258 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004259 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004260 } else {
Josef Bacike7175a62018-05-23 11:58:34 -04004261 csum_offset = em->mod_start - em->start;
4262 csum_len = em->mod_len;
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004263 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004264
Josef Bacik70c8a912012-10-11 16:54:30 -04004265 /* block start is already adjusted for the file extent offset. */
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004266 ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004267 em->block_start + csum_offset,
4268 em->block_start + csum_offset +
4269 csum_len - 1, &ordered_sums, 0);
4270 if (ret)
4271 return ret;
4272
4273 while (!list_empty(&ordered_sums)) {
4274 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4275 struct btrfs_ordered_sum,
4276 list);
4277 if (!ret)
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004278 ret = btrfs_csum_file_blocks(trans, log_root, sums);
Josef Bacik70c8a912012-10-11 16:54:30 -04004279 list_del(&sums->list);
4280 kfree(sums);
4281 }
4282
4283 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004284}
4285
Filipe Manana8407f552014-09-05 15:14:39 +01004286static int log_one_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004287 struct btrfs_inode *inode, struct btrfs_root *root,
Filipe Manana8407f552014-09-05 15:14:39 +01004288 const struct extent_map *em,
4289 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004290 struct btrfs_log_ctx *ctx)
4291{
4292 struct btrfs_root *log = root->log_root;
4293 struct btrfs_file_extent_item *fi;
4294 struct extent_buffer *leaf;
4295 struct btrfs_map_token token;
4296 struct btrfs_key key;
4297 u64 extent_offset = em->start - em->orig_start;
4298 u64 block_len;
4299 int ret;
4300 int extent_inserted = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004301
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004302 ret = log_extent_csums(trans, inode, log, em);
Filipe Manana8407f552014-09-05 15:14:39 +01004303 if (ret)
4304 return ret;
4305
Filipe Manana8407f552014-09-05 15:14:39 +01004306 btrfs_init_map_token(&token);
4307
Nikolay Borisov9d122622017-01-18 00:31:40 +02004308 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
Filipe Manana8407f552014-09-05 15:14:39 +01004309 em->start + em->len, NULL, 0, 1,
4310 sizeof(*fi), &extent_inserted);
4311 if (ret)
4312 return ret;
4313
4314 if (!extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004315 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004316 key.type = BTRFS_EXTENT_DATA_KEY;
4317 key.offset = em->start;
4318
4319 ret = btrfs_insert_empty_item(trans, log, path, &key,
4320 sizeof(*fi));
4321 if (ret)
4322 return ret;
4323 }
4324 leaf = path->nodes[0];
4325 fi = btrfs_item_ptr(leaf, path->slots[0],
4326 struct btrfs_file_extent_item);
4327
Josef Bacik50d9aa92014-11-21 14:52:38 -05004328 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004329 &token);
4330 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4331 btrfs_set_token_file_extent_type(leaf, fi,
4332 BTRFS_FILE_EXTENT_PREALLOC,
4333 &token);
4334 else
4335 btrfs_set_token_file_extent_type(leaf, fi,
4336 BTRFS_FILE_EXTENT_REG,
4337 &token);
4338
4339 block_len = max(em->block_len, em->orig_block_len);
4340 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4341 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4342 em->block_start,
4343 &token);
4344 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4345 &token);
4346 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4347 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4348 em->block_start -
4349 extent_offset, &token);
4350 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4351 &token);
4352 } else {
4353 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4354 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4355 &token);
4356 }
4357
4358 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4359 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4360 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4361 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4362 &token);
4363 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4364 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4365 btrfs_mark_buffer_dirty(leaf);
4366
4367 btrfs_release_path(path);
4368
4369 return ret;
4370}
4371
Filipe Manana31d11b82018-05-09 16:01:46 +01004372/*
4373 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4374 * lose them after doing a fast fsync and replaying the log. We scan the
4375 * subvolume's root instead of iterating the inode's extent map tree because
4376 * otherwise we can log incorrect extent items based on extent map conversion.
4377 * That can happen due to the fact that extent maps are merged when they
4378 * are not in the extent map tree's list of modified extents.
4379 */
4380static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4381 struct btrfs_inode *inode,
4382 struct btrfs_path *path)
4383{
4384 struct btrfs_root *root = inode->root;
4385 struct btrfs_key key;
4386 const u64 i_size = i_size_read(&inode->vfs_inode);
4387 const u64 ino = btrfs_ino(inode);
4388 struct btrfs_path *dst_path = NULL;
4389 u64 last_extent = (u64)-1;
4390 int ins_nr = 0;
4391 int start_slot;
4392 int ret;
4393
4394 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4395 return 0;
4396
4397 key.objectid = ino;
4398 key.type = BTRFS_EXTENT_DATA_KEY;
4399 key.offset = i_size;
4400 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4401 if (ret < 0)
4402 goto out;
4403
4404 while (true) {
4405 struct extent_buffer *leaf = path->nodes[0];
4406 int slot = path->slots[0];
4407
4408 if (slot >= btrfs_header_nritems(leaf)) {
4409 if (ins_nr > 0) {
4410 ret = copy_items(trans, inode, dst_path, path,
4411 &last_extent, start_slot,
4412 ins_nr, 1, 0);
4413 if (ret < 0)
4414 goto out;
4415 ins_nr = 0;
4416 }
4417 ret = btrfs_next_leaf(root, path);
4418 if (ret < 0)
4419 goto out;
4420 if (ret > 0) {
4421 ret = 0;
4422 break;
4423 }
4424 continue;
4425 }
4426
4427 btrfs_item_key_to_cpu(leaf, &key, slot);
4428 if (key.objectid > ino)
4429 break;
4430 if (WARN_ON_ONCE(key.objectid < ino) ||
4431 key.type < BTRFS_EXTENT_DATA_KEY ||
4432 key.offset < i_size) {
4433 path->slots[0]++;
4434 continue;
4435 }
4436 if (last_extent == (u64)-1) {
4437 last_extent = key.offset;
4438 /*
4439 * Avoid logging extent items logged in past fsync calls
4440 * and leading to duplicate keys in the log tree.
4441 */
4442 do {
4443 ret = btrfs_truncate_inode_items(trans,
4444 root->log_root,
4445 &inode->vfs_inode,
4446 i_size,
4447 BTRFS_EXTENT_DATA_KEY);
4448 } while (ret == -EAGAIN);
4449 if (ret)
4450 goto out;
4451 }
4452 if (ins_nr == 0)
4453 start_slot = slot;
4454 ins_nr++;
4455 path->slots[0]++;
4456 if (!dst_path) {
4457 dst_path = btrfs_alloc_path();
4458 if (!dst_path) {
4459 ret = -ENOMEM;
4460 goto out;
4461 }
4462 }
4463 }
4464 if (ins_nr > 0) {
4465 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4466 start_slot, ins_nr, 1, 0);
4467 if (ret > 0)
4468 ret = 0;
4469 }
4470out:
4471 btrfs_release_path(path);
4472 btrfs_free_path(dst_path);
4473 return ret;
4474}
4475
Josef Bacik5dc562c2012-08-17 13:14:17 -04004476static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4477 struct btrfs_root *root,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004478 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004479 struct btrfs_path *path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004480 struct btrfs_log_ctx *ctx,
4481 const u64 start,
4482 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004483{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004484 struct extent_map *em, *n;
4485 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004486 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004487 u64 test_gen;
4488 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004489 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004490
4491 INIT_LIST_HEAD(&extents);
4492
Josef Bacik5dc562c2012-08-17 13:14:17 -04004493 write_lock(&tree->lock);
4494 test_gen = root->fs_info->last_trans_committed;
4495
4496 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
Filipe Manana008c6752018-10-29 09:42:06 +00004497 /*
4498 * Skip extents outside our logging range. It's important to do
4499 * it for correctness because if we don't ignore them, we may
4500 * log them before their ordered extent completes, and therefore
4501 * we could log them without logging their respective checksums
4502 * (the checksum items are added to the csum tree at the very
4503 * end of btrfs_finish_ordered_io()). Also leave such extents
4504 * outside of our range in the list, since we may have another
4505 * ranged fsync in the near future that needs them. If an extent
4506 * outside our range corresponds to a hole, log it to avoid
4507 * leaving gaps between extents (fsck will complain when we are
4508 * not using the NO_HOLES feature).
4509 */
4510 if ((em->start > end || em->start + em->len <= start) &&
4511 em->block_start != EXTENT_MAP_HOLE)
4512 continue;
4513
Josef Bacik5dc562c2012-08-17 13:14:17 -04004514 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004515 /*
4516 * Just an arbitrary number, this can be really CPU intensive
4517 * once we start getting a lot of extents, and really once we
4518 * have a bunch of extents we just want to commit since it will
4519 * be faster.
4520 */
4521 if (++num > 32768) {
4522 list_del_init(&tree->modified_extents);
4523 ret = -EFBIG;
4524 goto process;
4525 }
4526
Josef Bacik5dc562c2012-08-17 13:14:17 -04004527 if (em->generation <= test_gen)
4528 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004529
Filipe Manana31d11b82018-05-09 16:01:46 +01004530 /* We log prealloc extents beyond eof later. */
4531 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4532 em->start >= i_size_read(&inode->vfs_inode))
4533 continue;
4534
Josef Bacikff44c6e2012-09-14 12:59:20 -04004535 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004536 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004537 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004538 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004539 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004540 }
4541
4542 list_sort(NULL, &extents, extent_cmp);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004543process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004544 while (!list_empty(&extents)) {
4545 em = list_entry(extents.next, struct extent_map, list);
4546
4547 list_del_init(&em->list);
4548
4549 /*
4550 * If we had an error we just need to delete everybody from our
4551 * private list.
4552 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004553 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004554 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004555 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004556 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004557 }
4558
4559 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004560
Josef Bacika2120a42018-05-23 11:58:35 -04004561 ret = log_one_extent(trans, inode, root, em, path, ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004562 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004563 clear_em_logging(tree, em);
4564 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004565 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004566 WARN_ON(!list_empty(&extents));
4567 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004568
Josef Bacik5dc562c2012-08-17 13:14:17 -04004569 btrfs_release_path(path);
Filipe Manana31d11b82018-05-09 16:01:46 +01004570 if (!ret)
4571 ret = btrfs_log_prealloc_extents(trans, inode, path);
4572
Josef Bacik5dc562c2012-08-17 13:14:17 -04004573 return ret;
4574}
4575
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004576static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004577 struct btrfs_path *path, u64 *size_ret)
4578{
4579 struct btrfs_key key;
4580 int ret;
4581
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004582 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004583 key.type = BTRFS_INODE_ITEM_KEY;
4584 key.offset = 0;
4585
4586 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4587 if (ret < 0) {
4588 return ret;
4589 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004590 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004591 } else {
4592 struct btrfs_inode_item *item;
4593
4594 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4595 struct btrfs_inode_item);
4596 *size_ret = btrfs_inode_size(path->nodes[0], item);
Filipe Mananabf504112019-03-04 14:06:12 +00004597 /*
4598 * If the in-memory inode's i_size is smaller then the inode
4599 * size stored in the btree, return the inode's i_size, so
4600 * that we get a correct inode size after replaying the log
4601 * when before a power failure we had a shrinking truncate
4602 * followed by addition of a new name (rename / new hard link).
4603 * Otherwise return the inode size from the btree, to avoid
4604 * data loss when replaying a log due to previously doing a
4605 * write that expands the inode's size and logging a new name
4606 * immediately after.
4607 */
4608 if (*size_ret > inode->vfs_inode.i_size)
4609 *size_ret = inode->vfs_inode.i_size;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004610 }
4611
4612 btrfs_release_path(path);
4613 return 0;
4614}
4615
Filipe Manana36283bf2015-06-20 00:44:51 +01004616/*
4617 * At the moment we always log all xattrs. This is to figure out at log replay
4618 * time which xattrs must have their deletion replayed. If a xattr is missing
4619 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4620 * because if a xattr is deleted, the inode is fsynced and a power failure
4621 * happens, causing the log to be replayed the next time the fs is mounted,
4622 * we want the xattr to not exist anymore (same behaviour as other filesystems
4623 * with a journal, ext3/4, xfs, f2fs, etc).
4624 */
4625static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4626 struct btrfs_root *root,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004627 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004628 struct btrfs_path *path,
4629 struct btrfs_path *dst_path)
4630{
4631 int ret;
4632 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004633 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004634 int ins_nr = 0;
4635 int start_slot = 0;
4636
4637 key.objectid = ino;
4638 key.type = BTRFS_XATTR_ITEM_KEY;
4639 key.offset = 0;
4640
4641 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4642 if (ret < 0)
4643 return ret;
4644
4645 while (true) {
4646 int slot = path->slots[0];
4647 struct extent_buffer *leaf = path->nodes[0];
4648 int nritems = btrfs_header_nritems(leaf);
4649
4650 if (slot >= nritems) {
4651 if (ins_nr > 0) {
4652 u64 last_extent = 0;
4653
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004654 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004655 &last_extent, start_slot,
4656 ins_nr, 1, 0);
4657 /* can't be 1, extent items aren't processed */
4658 ASSERT(ret <= 0);
4659 if (ret < 0)
4660 return ret;
4661 ins_nr = 0;
4662 }
4663 ret = btrfs_next_leaf(root, path);
4664 if (ret < 0)
4665 return ret;
4666 else if (ret > 0)
4667 break;
4668 continue;
4669 }
4670
4671 btrfs_item_key_to_cpu(leaf, &key, slot);
4672 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4673 break;
4674
4675 if (ins_nr == 0)
4676 start_slot = slot;
4677 ins_nr++;
4678 path->slots[0]++;
4679 cond_resched();
4680 }
4681 if (ins_nr > 0) {
4682 u64 last_extent = 0;
4683
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004684 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004685 &last_extent, start_slot,
4686 ins_nr, 1, 0);
4687 /* can't be 1, extent items aren't processed */
4688 ASSERT(ret <= 0);
4689 if (ret < 0)
4690 return ret;
4691 }
4692
4693 return 0;
4694}
4695
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004696/*
4697 * If the no holes feature is enabled we need to make sure any hole between the
4698 * last extent and the i_size of our inode is explicitly marked in the log. This
4699 * is to make sure that doing something like:
4700 *
4701 * 1) create file with 128Kb of data
4702 * 2) truncate file to 64Kb
4703 * 3) truncate file to 256Kb
4704 * 4) fsync file
4705 * 5) <crash/power failure>
4706 * 6) mount fs and trigger log replay
4707 *
4708 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4709 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4710 * file correspond to a hole. The presence of explicit holes in a log tree is
4711 * what guarantees that log replay will remove/adjust file extent items in the
4712 * fs/subvol tree.
4713 *
4714 * Here we do not need to care about holes between extents, that is already done
4715 * by copy_items(). We also only need to do this in the full sync path, where we
4716 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4717 * lookup the list of modified extent maps and if any represents a hole, we
4718 * insert a corresponding extent representing a hole in the log tree.
4719 */
4720static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4721 struct btrfs_root *root,
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004722 struct btrfs_inode *inode,
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004723 struct btrfs_path *path)
4724{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004725 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004726 int ret;
4727 struct btrfs_key key;
4728 u64 hole_start;
4729 u64 hole_size;
4730 struct extent_buffer *leaf;
4731 struct btrfs_root *log = root->log_root;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004732 const u64 ino = btrfs_ino(inode);
4733 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004734
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004735 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004736 return 0;
4737
4738 key.objectid = ino;
4739 key.type = BTRFS_EXTENT_DATA_KEY;
4740 key.offset = (u64)-1;
4741
4742 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4743 ASSERT(ret != 0);
4744 if (ret < 0)
4745 return ret;
4746
4747 ASSERT(path->slots[0] > 0);
4748 path->slots[0]--;
4749 leaf = path->nodes[0];
4750 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4751
4752 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4753 /* inode does not have any extents */
4754 hole_start = 0;
4755 hole_size = i_size;
4756 } else {
4757 struct btrfs_file_extent_item *extent;
4758 u64 len;
4759
4760 /*
4761 * If there's an extent beyond i_size, an explicit hole was
4762 * already inserted by copy_items().
4763 */
4764 if (key.offset >= i_size)
4765 return 0;
4766
4767 extent = btrfs_item_ptr(leaf, path->slots[0],
4768 struct btrfs_file_extent_item);
4769
4770 if (btrfs_file_extent_type(leaf, extent) ==
Filipe Manana0ccc3872019-03-19 17:18:13 +00004771 BTRFS_FILE_EXTENT_INLINE)
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004772 return 0;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004773
4774 len = btrfs_file_extent_num_bytes(leaf, extent);
4775 /* Last extent goes beyond i_size, no need to log a hole. */
4776 if (key.offset + len > i_size)
4777 return 0;
4778 hole_start = key.offset + len;
4779 hole_size = i_size - hole_start;
4780 }
4781 btrfs_release_path(path);
4782
4783 /* Last extent ends at i_size. */
4784 if (hole_size == 0)
4785 return 0;
4786
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004787 hole_size = ALIGN(hole_size, fs_info->sectorsize);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004788 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4789 hole_size, 0, hole_size, 0, 0, 0);
4790 return ret;
4791}
4792
Filipe Manana56f23fd2016-03-30 23:37:21 +01004793/*
4794 * When we are logging a new inode X, check if it doesn't have a reference that
4795 * matches the reference from some other inode Y created in a past transaction
4796 * and that was renamed in the current transaction. If we don't do this, then at
4797 * log replay time we can lose inode Y (and all its files if it's a directory):
4798 *
4799 * mkdir /mnt/x
4800 * echo "hello world" > /mnt/x/foobar
4801 * sync
4802 * mv /mnt/x /mnt/y
4803 * mkdir /mnt/x # or touch /mnt/x
4804 * xfs_io -c fsync /mnt/x
4805 * <power fail>
4806 * mount fs, trigger log replay
4807 *
4808 * After the log replay procedure, we would lose the first directory and all its
4809 * files (file foobar).
4810 * For the case where inode Y is not a directory we simply end up losing it:
4811 *
4812 * echo "123" > /mnt/foo
4813 * sync
4814 * mv /mnt/foo /mnt/bar
4815 * echo "abc" > /mnt/foo
4816 * xfs_io -c fsync /mnt/foo
4817 * <power fail>
4818 *
4819 * We also need this for cases where a snapshot entry is replaced by some other
4820 * entry (file or directory) otherwise we end up with an unreplayable log due to
4821 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4822 * if it were a regular entry:
4823 *
4824 * mkdir /mnt/x
4825 * btrfs subvolume snapshot /mnt /mnt/x/snap
4826 * btrfs subvolume delete /mnt/x/snap
4827 * rmdir /mnt/x
4828 * mkdir /mnt/x
4829 * fsync /mnt/x or fsync some new file inside it
4830 * <power fail>
4831 *
4832 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4833 * the same transaction.
4834 */
4835static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4836 const int slot,
4837 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004838 struct btrfs_inode *inode,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004839 u64 *other_ino, u64 *other_parent)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004840{
4841 int ret;
4842 struct btrfs_path *search_path;
4843 char *name = NULL;
4844 u32 name_len = 0;
4845 u32 item_size = btrfs_item_size_nr(eb, slot);
4846 u32 cur_offset = 0;
4847 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4848
4849 search_path = btrfs_alloc_path();
4850 if (!search_path)
4851 return -ENOMEM;
4852 search_path->search_commit_root = 1;
4853 search_path->skip_locking = 1;
4854
4855 while (cur_offset < item_size) {
4856 u64 parent;
4857 u32 this_name_len;
4858 u32 this_len;
4859 unsigned long name_ptr;
4860 struct btrfs_dir_item *di;
4861
4862 if (key->type == BTRFS_INODE_REF_KEY) {
4863 struct btrfs_inode_ref *iref;
4864
4865 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4866 parent = key->offset;
4867 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4868 name_ptr = (unsigned long)(iref + 1);
4869 this_len = sizeof(*iref) + this_name_len;
4870 } else {
4871 struct btrfs_inode_extref *extref;
4872
4873 extref = (struct btrfs_inode_extref *)(ptr +
4874 cur_offset);
4875 parent = btrfs_inode_extref_parent(eb, extref);
4876 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4877 name_ptr = (unsigned long)&extref->name;
4878 this_len = sizeof(*extref) + this_name_len;
4879 }
4880
4881 if (this_name_len > name_len) {
4882 char *new_name;
4883
4884 new_name = krealloc(name, this_name_len, GFP_NOFS);
4885 if (!new_name) {
4886 ret = -ENOMEM;
4887 goto out;
4888 }
4889 name_len = this_name_len;
4890 name = new_name;
4891 }
4892
4893 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004894 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4895 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004896 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004897 struct btrfs_key di_key;
4898
4899 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4900 di, &di_key);
4901 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
Filipe Manana6b5fc432019-02-13 12:14:03 +00004902 if (di_key.objectid != key->objectid) {
4903 ret = 1;
4904 *other_ino = di_key.objectid;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004905 *other_parent = parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004906 } else {
4907 ret = 0;
4908 }
Filipe Manana44f714d2016-06-06 16:11:13 +01004909 } else {
4910 ret = -EAGAIN;
4911 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004912 goto out;
4913 } else if (IS_ERR(di)) {
4914 ret = PTR_ERR(di);
4915 goto out;
4916 }
4917 btrfs_release_path(search_path);
4918
4919 cur_offset += this_len;
4920 }
4921 ret = 0;
4922out:
4923 btrfs_free_path(search_path);
4924 kfree(name);
4925 return ret;
4926}
4927
Filipe Manana6b5fc432019-02-13 12:14:03 +00004928struct btrfs_ino_list {
4929 u64 ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004930 u64 parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004931 struct list_head list;
4932};
4933
4934static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
4935 struct btrfs_root *root,
4936 struct btrfs_path *path,
4937 struct btrfs_log_ctx *ctx,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004938 u64 ino, u64 parent)
Filipe Manana6b5fc432019-02-13 12:14:03 +00004939{
4940 struct btrfs_ino_list *ino_elem;
4941 LIST_HEAD(inode_list);
4942 int ret = 0;
4943
4944 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
4945 if (!ino_elem)
4946 return -ENOMEM;
4947 ino_elem->ino = ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004948 ino_elem->parent = parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004949 list_add_tail(&ino_elem->list, &inode_list);
4950
4951 while (!list_empty(&inode_list)) {
4952 struct btrfs_fs_info *fs_info = root->fs_info;
4953 struct btrfs_key key;
4954 struct inode *inode;
4955
4956 ino_elem = list_first_entry(&inode_list, struct btrfs_ino_list,
4957 list);
4958 ino = ino_elem->ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004959 parent = ino_elem->parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004960 list_del(&ino_elem->list);
4961 kfree(ino_elem);
4962 if (ret)
4963 continue;
4964
4965 btrfs_release_path(path);
4966
4967 key.objectid = ino;
4968 key.type = BTRFS_INODE_ITEM_KEY;
4969 key.offset = 0;
4970 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4971 /*
4972 * If the other inode that had a conflicting dir entry was
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004973 * deleted in the current transaction, we need to log its parent
4974 * directory.
Filipe Manana6b5fc432019-02-13 12:14:03 +00004975 */
4976 if (IS_ERR(inode)) {
4977 ret = PTR_ERR(inode);
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004978 if (ret == -ENOENT) {
4979 key.objectid = parent;
4980 inode = btrfs_iget(fs_info->sb, &key, root,
4981 NULL);
4982 if (IS_ERR(inode)) {
4983 ret = PTR_ERR(inode);
4984 } else {
4985 ret = btrfs_log_inode(trans, root,
4986 BTRFS_I(inode),
4987 LOG_OTHER_INODE_ALL,
4988 0, LLONG_MAX, ctx);
4989 iput(inode);
4990 }
4991 }
Filipe Manana6b5fc432019-02-13 12:14:03 +00004992 continue;
4993 }
4994 /*
4995 * We are safe logging the other inode without acquiring its
4996 * lock as long as we log with the LOG_INODE_EXISTS mode. We
4997 * are safe against concurrent renames of the other inode as
4998 * well because during a rename we pin the log and update the
4999 * log with the new name before we unpin it.
5000 */
5001 ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
5002 LOG_OTHER_INODE, 0, LLONG_MAX, ctx);
5003 if (ret) {
5004 iput(inode);
5005 continue;
5006 }
5007
5008 key.objectid = ino;
5009 key.type = BTRFS_INODE_REF_KEY;
5010 key.offset = 0;
5011 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5012 if (ret < 0) {
5013 iput(inode);
5014 continue;
5015 }
5016
5017 while (true) {
5018 struct extent_buffer *leaf = path->nodes[0];
5019 int slot = path->slots[0];
5020 u64 other_ino = 0;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005021 u64 other_parent = 0;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005022
5023 if (slot >= btrfs_header_nritems(leaf)) {
5024 ret = btrfs_next_leaf(root, path);
5025 if (ret < 0) {
5026 break;
5027 } else if (ret > 0) {
5028 ret = 0;
5029 break;
5030 }
5031 continue;
5032 }
5033
5034 btrfs_item_key_to_cpu(leaf, &key, slot);
5035 if (key.objectid != ino ||
5036 (key.type != BTRFS_INODE_REF_KEY &&
5037 key.type != BTRFS_INODE_EXTREF_KEY)) {
5038 ret = 0;
5039 break;
5040 }
5041
5042 ret = btrfs_check_ref_name_override(leaf, slot, &key,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005043 BTRFS_I(inode), &other_ino,
5044 &other_parent);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005045 if (ret < 0)
5046 break;
5047 if (ret > 0) {
5048 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
5049 if (!ino_elem) {
5050 ret = -ENOMEM;
5051 break;
5052 }
5053 ino_elem->ino = other_ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005054 ino_elem->parent = other_parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005055 list_add_tail(&ino_elem->list, &inode_list);
5056 ret = 0;
5057 }
5058 path->slots[0]++;
5059 }
5060 iput(inode);
5061 }
5062
5063 return ret;
5064}
5065
Chris Masone02119d2008-09-05 16:13:11 -04005066/* log a single inode in the tree log.
5067 * At least one parent directory for this inode must exist in the tree
5068 * or be logged already.
5069 *
5070 * Any items from this inode changed by the current transaction are copied
5071 * to the log tree. An extra reference is taken on any extents in this
5072 * file, allowing us to avoid a whole pile of corner cases around logging
5073 * blocks that have been removed from the tree.
5074 *
5075 * See LOG_INODE_ALL and related defines for a description of what inode_only
5076 * does.
5077 *
5078 * This handles both files and directories.
5079 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005080static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005081 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005082 int inode_only,
5083 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01005084 const loff_t end,
5085 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005086{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005087 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04005088 struct btrfs_path *path;
5089 struct btrfs_path *dst_path;
5090 struct btrfs_key min_key;
5091 struct btrfs_key max_key;
5092 struct btrfs_root *log = root->log_root;
Josef Bacik16e75492013-10-22 12:18:51 -04005093 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005094 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005095 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04005096 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005097 int ins_start_slot = 0;
5098 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005099 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005100 u64 ino = btrfs_ino(inode);
5101 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005102 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01005103 bool need_log_inode_item = true;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005104 bool xattrs_logged = false;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005105 bool recursive_logging = false;
Chris Masone02119d2008-09-05 16:13:11 -04005106
Chris Masone02119d2008-09-05 16:13:11 -04005107 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00005108 if (!path)
5109 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04005110 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00005111 if (!dst_path) {
5112 btrfs_free_path(path);
5113 return -ENOMEM;
5114 }
Chris Masone02119d2008-09-05 16:13:11 -04005115
Li Zefan33345d012011-04-20 10:31:50 +08005116 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04005117 min_key.type = BTRFS_INODE_ITEM_KEY;
5118 min_key.offset = 0;
5119
Li Zefan33345d012011-04-20 10:31:50 +08005120 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04005121
Chris Mason12fcfd22009-03-24 10:24:20 -04005122
Josef Bacik5dc562c2012-08-17 13:14:17 -04005123 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005124 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00005125 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005126 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08005127 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04005128 max_key.type = BTRFS_XATTR_ITEM_KEY;
5129 else
5130 max_key.type = (u8)-1;
5131 max_key.offset = (u64)-1;
5132
Filipe Manana2c2c4522015-01-13 16:40:04 +00005133 /*
5134 * Only run delayed items if we are a dir or a new file.
5135 * Otherwise commit the delayed inode only, which is needed in
5136 * order for the log replay code to mark inodes for link count
5137 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
5138 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005139 if (S_ISDIR(inode->vfs_inode.i_mode) ||
5140 inode->generation > fs_info->last_trans_committed)
5141 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00005142 else
Nikolay Borisova59108a2017-01-18 00:31:48 +02005143 ret = btrfs_commit_inode_delayed_inode(inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00005144
5145 if (ret) {
5146 btrfs_free_path(path);
5147 btrfs_free_path(dst_path);
5148 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08005149 }
5150
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005151 if (inode_only == LOG_OTHER_INODE || inode_only == LOG_OTHER_INODE_ALL) {
5152 recursive_logging = true;
5153 if (inode_only == LOG_OTHER_INODE)
5154 inode_only = LOG_INODE_EXISTS;
5155 else
5156 inode_only = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005157 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08005158 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005159 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08005160 }
Chris Masone02119d2008-09-05 16:13:11 -04005161
Filipe Manana5e33a2b2016-02-25 23:19:38 +00005162 /*
Chris Masone02119d2008-09-05 16:13:11 -04005163 * a brute force approach to making sure we get the most uptodate
5164 * copies of everything.
5165 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005166 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04005167 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
5168
Filipe Manana4f764e52015-02-23 19:53:35 +00005169 if (inode_only == LOG_INODE_EXISTS)
5170 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08005171 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04005172 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005173 if (inode_only == LOG_INODE_EXISTS) {
5174 /*
5175 * Make sure the new inode item we write to the log has
5176 * the same isize as the current one (if it exists).
5177 * This is necessary to prevent data loss after log
5178 * replay, and also to prevent doing a wrong expanding
5179 * truncate - for e.g. create file, write 4K into offset
5180 * 0, fsync, write 4K into offset 4096, add hard link,
5181 * fsync some other file (to sync log), power fail - if
5182 * we use the inode's current i_size, after log replay
5183 * we get a 8Kb file, with the last 4Kb extent as a hole
5184 * (zeroes), as if an expanding truncate happened,
5185 * instead of getting a file of 4Kb only.
5186 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005187 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005188 if (err)
5189 goto out_unlock;
5190 }
Filipe Mananaa7429942015-02-13 16:56:14 +00005191 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005192 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00005193 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005194 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00005195 ret = drop_objectid_items(trans, log, path, ino,
5196 max_key.type);
5197 } else {
5198 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005199 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00005200 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005201 &inode->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08005202 while(1) {
5203 ret = btrfs_truncate_inode_items(trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005204 log, &inode->vfs_inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005205 if (ret != -EAGAIN)
5206 break;
5207 }
Filipe Mananaa7429942015-02-13 16:56:14 +00005208 }
Filipe Manana4f764e52015-02-23 19:53:35 +00005209 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005210 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05005211 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005212 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04005213 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00005214 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04005215 ret = drop_objectid_items(trans, log, path, ino,
5216 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005217 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00005218 if (inode_only == LOG_INODE_ALL)
5219 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04005220 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005221 }
Josef Bacika95249b2012-10-11 16:17:34 -04005222
Chris Masone02119d2008-09-05 16:13:11 -04005223 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005224 if (ret) {
5225 err = ret;
5226 goto out_unlock;
5227 }
Chris Masone02119d2008-09-05 16:13:11 -04005228
Chris Masond3977122009-01-05 21:25:51 -05005229 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04005230 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01005231 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005232 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07005233 if (ret < 0) {
5234 err = ret;
5235 goto out_unlock;
5236 }
Chris Masone02119d2008-09-05 16:13:11 -04005237 if (ret != 0)
5238 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04005239again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04005240 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08005241 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04005242 break;
5243 if (min_key.type > max_key.type)
5244 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005245
Filipe Mananae4545de2015-06-17 12:49:23 +01005246 if (min_key.type == BTRFS_INODE_ITEM_KEY)
5247 need_log_inode_item = false;
5248
Filipe Manana56f23fd2016-03-30 23:37:21 +01005249 if ((min_key.type == BTRFS_INODE_REF_KEY ||
5250 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
Filipe Manana6b5fc432019-02-13 12:14:03 +00005251 inode->generation == trans->transid &&
5252 !recursive_logging) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005253 u64 other_ino = 0;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005254 u64 other_parent = 0;
Filipe Manana44f714d2016-06-06 16:11:13 +01005255
Filipe Manana56f23fd2016-03-30 23:37:21 +01005256 ret = btrfs_check_ref_name_override(path->nodes[0],
Nikolay Borisova59108a2017-01-18 00:31:48 +02005257 path->slots[0], &min_key, inode,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005258 &other_ino, &other_parent);
Filipe Manana56f23fd2016-03-30 23:37:21 +01005259 if (ret < 0) {
5260 err = ret;
5261 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01005262 } else if (ret > 0 && ctx &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005263 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005264 if (ins_nr > 0) {
5265 ins_nr++;
5266 } else {
5267 ins_nr = 1;
5268 ins_start_slot = path->slots[0];
5269 }
Nikolay Borisova59108a2017-01-18 00:31:48 +02005270 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana44f714d2016-06-06 16:11:13 +01005271 &last_extent, ins_start_slot,
5272 ins_nr, inode_only,
5273 logged_isize);
5274 if (ret < 0) {
5275 err = ret;
5276 goto out_unlock;
5277 }
5278 ins_nr = 0;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005279
5280 err = log_conflicting_inodes(trans, root, path,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005281 ctx, other_ino, other_parent);
Filipe Manana44f714d2016-06-06 16:11:13 +01005282 if (err)
5283 goto out_unlock;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005284 btrfs_release_path(path);
5285 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01005286 }
5287 }
5288
Filipe Manana36283bf2015-06-20 00:44:51 +01005289 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5290 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
5291 if (ins_nr == 0)
5292 goto next_slot;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005293 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01005294 &last_extent, ins_start_slot,
5295 ins_nr, inode_only, logged_isize);
5296 if (ret < 0) {
5297 err = ret;
5298 goto out_unlock;
5299 }
5300 ins_nr = 0;
5301 if (ret) {
5302 btrfs_release_path(path);
5303 continue;
5304 }
5305 goto next_slot;
5306 }
5307
Chris Mason31ff1cd2008-09-11 16:17:57 -04005308 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5309 ins_nr++;
5310 goto next_slot;
5311 } else if (!ins_nr) {
5312 ins_start_slot = path->slots[0];
5313 ins_nr = 1;
5314 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04005315 }
5316
Nikolay Borisova59108a2017-01-18 00:31:48 +02005317 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005318 ins_start_slot, ins_nr, inode_only,
5319 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005320 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005321 err = ret;
5322 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02005323 }
5324 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04005325 ins_nr = 0;
5326 btrfs_release_path(path);
5327 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005328 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005329 ins_nr = 1;
5330 ins_start_slot = path->slots[0];
5331next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04005332
Chris Mason3a5f1d42008-09-11 15:53:37 -04005333 nritems = btrfs_header_nritems(path->nodes[0]);
5334 path->slots[0]++;
5335 if (path->slots[0] < nritems) {
5336 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
5337 path->slots[0]);
5338 goto again;
5339 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005340 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005341 ret = copy_items(trans, inode, dst_path, path,
Josef Bacik16e75492013-10-22 12:18:51 -04005342 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005343 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005344 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005345 err = ret;
5346 goto out_unlock;
5347 }
Josef Bacik16e75492013-10-22 12:18:51 -04005348 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005349 ins_nr = 0;
5350 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005351 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01005352next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005353 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04005354 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005355 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04005356 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005357 min_key.offset = 0;
5358 } else {
Chris Masone02119d2008-09-05 16:13:11 -04005359 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005360 }
Chris Masone02119d2008-09-05 16:13:11 -04005361 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005362 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005363 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005364 ins_start_slot, ins_nr, inode_only,
5365 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005366 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005367 err = ret;
5368 goto out_unlock;
5369 }
Josef Bacik16e75492013-10-22 12:18:51 -04005370 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005371 ins_nr = 0;
5372 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005373
Filipe Manana36283bf2015-06-20 00:44:51 +01005374 btrfs_release_path(path);
5375 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005376 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01005377 if (err)
5378 goto out_unlock;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005379 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005380 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5381 btrfs_release_path(path);
5382 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005383 err = btrfs_log_trailing_hole(trans, root, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005384 if (err)
5385 goto out_unlock;
5386 }
Josef Bacika95249b2012-10-11 16:17:34 -04005387log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04005388 btrfs_release_path(path);
5389 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01005390 if (need_log_inode_item) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005391 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana9a8fca62018-05-11 16:42:42 +01005392 if (!err && !xattrs_logged) {
5393 err = btrfs_log_all_xattrs(trans, root, inode, path,
5394 dst_path);
5395 btrfs_release_path(path);
5396 }
Filipe Mananae4545de2015-06-17 12:49:23 +01005397 if (err)
5398 goto out_unlock;
5399 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005400 if (fast_search) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005401 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Josef Bacika2120a42018-05-23 11:58:35 -04005402 ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005403 if (ret) {
5404 err = ret;
5405 goto out_unlock;
5406 }
Josef Bacikd006a042013-11-12 20:54:09 -05005407 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06005408 struct extent_map *em, *n;
5409
Filipe Manana49dae1b2014-09-06 22:34:39 +01005410 write_lock(&em_tree->lock);
5411 /*
5412 * We can't just remove every em if we're called for a ranged
5413 * fsync - that is, one that doesn't cover the whole possible
5414 * file range (0 to LLONG_MAX). This is because we can have
5415 * em's that fall outside the range we're logging and therefore
5416 * their ordered operations haven't completed yet
5417 * (btrfs_finish_ordered_io() not invoked yet). This means we
5418 * didn't get their respective file extent item in the fs/subvol
5419 * tree yet, and need to let the next fast fsync (one which
5420 * consults the list of modified extent maps) find the em so
5421 * that it logs a matching file extent item and waits for the
5422 * respective ordered operation to complete (if it's still
5423 * running).
5424 *
5425 * Removing every em outside the range we're logging would make
5426 * the next fast fsync not log their matching file extent items,
5427 * therefore making us lose data after a log replay.
5428 */
5429 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5430 list) {
5431 const u64 mod_end = em->mod_start + em->mod_len - 1;
5432
5433 if (em->mod_start >= start && mod_end <= end)
5434 list_del_init(&em->list);
5435 }
5436 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005437 }
5438
Nikolay Borisova59108a2017-01-18 00:31:48 +02005439 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5440 ret = log_directory_changes(trans, root, inode, path, dst_path,
5441 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005442 if (ret) {
5443 err = ret;
5444 goto out_unlock;
5445 }
Chris Masone02119d2008-09-05 16:13:11 -04005446 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005447
Filipe Mananad1d832a2019-06-07 11:25:24 +01005448 /*
5449 * Don't update last_log_commit if we logged that an inode exists after
5450 * it was loaded to memory (full_sync bit set).
5451 * This is to prevent data loss when we do a write to the inode, then
5452 * the inode gets evicted after all delalloc was flushed, then we log
5453 * it exists (due to a rename for example) and then fsync it. This last
5454 * fsync would do nothing (not logging the extents previously written).
5455 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005456 spin_lock(&inode->lock);
5457 inode->logged_trans = trans->transid;
Filipe Mananad1d832a2019-06-07 11:25:24 +01005458 if (inode_only != LOG_INODE_EXISTS ||
5459 !test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
5460 inode->last_log_commit = inode->last_sub_trans;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005461 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005462out_unlock:
Nikolay Borisova59108a2017-01-18 00:31:48 +02005463 mutex_unlock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04005464
5465 btrfs_free_path(path);
5466 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005467 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005468}
5469
Chris Mason12fcfd22009-03-24 10:24:20 -04005470/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005471 * Check if we must fallback to a transaction commit when logging an inode.
5472 * This must be called after logging the inode and is used only in the context
5473 * when fsyncing an inode requires the need to log some other inode - in which
5474 * case we can't lock the i_mutex of each other inode we need to log as that
5475 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5476 * log inodes up or down in the hierarchy) or rename operations for example. So
5477 * we take the log_mutex of the inode after we have logged it and then check for
5478 * its last_unlink_trans value - this is safe because any task setting
5479 * last_unlink_trans must take the log_mutex and it must do this before it does
5480 * the actual unlink operation, so if we do this check before a concurrent task
5481 * sets last_unlink_trans it means we've logged a consistent version/state of
5482 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005483 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005484 * we logged the inode or it might have also done the unlink).
5485 */
5486static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005487 struct btrfs_inode *inode)
Filipe Manana2be63d52016-02-12 11:34:23 +00005488{
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005489 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana2be63d52016-02-12 11:34:23 +00005490 bool ret = false;
5491
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005492 mutex_lock(&inode->log_mutex);
5493 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
Filipe Manana2be63d52016-02-12 11:34:23 +00005494 /*
5495 * Make sure any commits to the log are forced to be full
5496 * commits.
5497 */
David Sterba90787762019-03-20 13:28:05 +01005498 btrfs_set_log_full_commit(trans);
Filipe Manana2be63d52016-02-12 11:34:23 +00005499 ret = true;
5500 }
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005501 mutex_unlock(&inode->log_mutex);
Filipe Manana2be63d52016-02-12 11:34:23 +00005502
5503 return ret;
5504}
5505
5506/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005507 * follow the dentry parent pointers up the chain and see if any
5508 * of the directories in it require a full commit before they can
5509 * be logged. Returns zero if nothing special needs to be done or 1 if
5510 * a full commit is required.
5511 */
5512static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005513 struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005514 struct dentry *parent,
5515 struct super_block *sb,
5516 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005517{
Chris Mason12fcfd22009-03-24 10:24:20 -04005518 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005519 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005520
Chris Masonaf4176b2009-03-24 10:24:31 -04005521 /*
5522 * for regular files, if its inode is already on disk, we don't
5523 * have to worry about the parents at all. This is because
5524 * we can use the last_unlink_trans field to record renames
5525 * and other fun in this file.
5526 */
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005527 if (S_ISREG(inode->vfs_inode.i_mode) &&
5528 inode->generation <= last_committed &&
5529 inode->last_unlink_trans <= last_committed)
5530 goto out;
Chris Masonaf4176b2009-03-24 10:24:31 -04005531
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005532 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005533 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005534 goto out;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005535 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005536 }
5537
5538 while (1) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005539 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005540 ret = 1;
5541 break;
5542 }
5543
Al Virofc640052016-04-10 01:33:30 -04005544 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005545 break;
5546
Filipe Manana44f714d2016-06-06 16:11:13 +01005547 if (IS_ROOT(parent)) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005548 inode = BTRFS_I(d_inode(parent));
5549 if (btrfs_must_commit_transaction(trans, inode))
Filipe Manana44f714d2016-06-06 16:11:13 +01005550 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005551 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005552 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005553
Josef Bacik6a912212010-11-20 09:48:00 +00005554 parent = dget_parent(parent);
5555 dput(old_parent);
5556 old_parent = parent;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005557 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005558
5559 }
Josef Bacik6a912212010-11-20 09:48:00 +00005560 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005561out:
Chris Masone02119d2008-09-05 16:13:11 -04005562 return ret;
5563}
5564
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005565struct btrfs_dir_list {
5566 u64 ino;
5567 struct list_head list;
5568};
5569
5570/*
5571 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5572 * details about the why it is needed.
5573 * This is a recursive operation - if an existing dentry corresponds to a
5574 * directory, that directory's new entries are logged too (same behaviour as
5575 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5576 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5577 * complains about the following circular lock dependency / possible deadlock:
5578 *
5579 * CPU0 CPU1
5580 * ---- ----
5581 * lock(&type->i_mutex_dir_key#3/2);
5582 * lock(sb_internal#2);
5583 * lock(&type->i_mutex_dir_key#3/2);
5584 * lock(&sb->s_type->i_mutex_key#14);
5585 *
5586 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5587 * sb_start_intwrite() in btrfs_start_transaction().
5588 * Not locking i_mutex of the inodes is still safe because:
5589 *
5590 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5591 * that while logging the inode new references (names) are added or removed
5592 * from the inode, leaving the logged inode item with a link count that does
5593 * not match the number of logged inode reference items. This is fine because
5594 * at log replay time we compute the real number of links and correct the
5595 * link count in the inode item (see replay_one_buffer() and
5596 * link_to_fixup_dir());
5597 *
5598 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5599 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5600 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5601 * has a size that doesn't match the sum of the lengths of all the logged
5602 * names. This does not result in a problem because if a dir_item key is
5603 * logged but its matching dir_index key is not logged, at log replay time we
5604 * don't use it to replay the respective name (see replay_one_name()). On the
5605 * other hand if only the dir_index key ends up being logged, the respective
5606 * name is added to the fs/subvol tree with both the dir_item and dir_index
5607 * keys created (see replay_one_name()).
5608 * The directory's inode item with a wrong i_size is not a problem as well,
5609 * since we don't use it at log replay time to set the i_size in the inode
5610 * item of the fs/subvol tree (see overwrite_item()).
5611 */
5612static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5613 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005614 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005615 struct btrfs_log_ctx *ctx)
5616{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005617 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005618 struct btrfs_root *log = root->log_root;
5619 struct btrfs_path *path;
5620 LIST_HEAD(dir_list);
5621 struct btrfs_dir_list *dir_elem;
5622 int ret = 0;
5623
5624 path = btrfs_alloc_path();
5625 if (!path)
5626 return -ENOMEM;
5627
5628 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5629 if (!dir_elem) {
5630 btrfs_free_path(path);
5631 return -ENOMEM;
5632 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005633 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005634 list_add_tail(&dir_elem->list, &dir_list);
5635
5636 while (!list_empty(&dir_list)) {
5637 struct extent_buffer *leaf;
5638 struct btrfs_key min_key;
5639 int nritems;
5640 int i;
5641
5642 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5643 list);
5644 if (ret)
5645 goto next_dir_inode;
5646
5647 min_key.objectid = dir_elem->ino;
5648 min_key.type = BTRFS_DIR_ITEM_KEY;
5649 min_key.offset = 0;
5650again:
5651 btrfs_release_path(path);
5652 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5653 if (ret < 0) {
5654 goto next_dir_inode;
5655 } else if (ret > 0) {
5656 ret = 0;
5657 goto next_dir_inode;
5658 }
5659
5660process_leaf:
5661 leaf = path->nodes[0];
5662 nritems = btrfs_header_nritems(leaf);
5663 for (i = path->slots[0]; i < nritems; i++) {
5664 struct btrfs_dir_item *di;
5665 struct btrfs_key di_key;
5666 struct inode *di_inode;
5667 struct btrfs_dir_list *new_dir_elem;
5668 int log_mode = LOG_INODE_EXISTS;
5669 int type;
5670
5671 btrfs_item_key_to_cpu(leaf, &min_key, i);
5672 if (min_key.objectid != dir_elem->ino ||
5673 min_key.type != BTRFS_DIR_ITEM_KEY)
5674 goto next_dir_inode;
5675
5676 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5677 type = btrfs_dir_type(leaf, di);
5678 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5679 type != BTRFS_FT_DIR)
5680 continue;
5681 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5682 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5683 continue;
5684
Robbie Koec125cf2016-10-28 10:48:26 +08005685 btrfs_release_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005686 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005687 if (IS_ERR(di_inode)) {
5688 ret = PTR_ERR(di_inode);
5689 goto next_dir_inode;
5690 }
5691
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005692 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005693 iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005694 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005695 }
5696
5697 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005698 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005699 log_mode = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005700 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005701 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005702 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005703 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005704 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005705 iput(di_inode);
5706 if (ret)
5707 goto next_dir_inode;
5708 if (ctx->log_new_dentries) {
5709 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5710 GFP_NOFS);
5711 if (!new_dir_elem) {
5712 ret = -ENOMEM;
5713 goto next_dir_inode;
5714 }
5715 new_dir_elem->ino = di_key.objectid;
5716 list_add_tail(&new_dir_elem->list, &dir_list);
5717 }
5718 break;
5719 }
5720 if (i == nritems) {
5721 ret = btrfs_next_leaf(log, path);
5722 if (ret < 0) {
5723 goto next_dir_inode;
5724 } else if (ret > 0) {
5725 ret = 0;
5726 goto next_dir_inode;
5727 }
5728 goto process_leaf;
5729 }
5730 if (min_key.offset < (u64)-1) {
5731 min_key.offset++;
5732 goto again;
5733 }
5734next_dir_inode:
5735 list_del(&dir_elem->list);
5736 kfree(dir_elem);
5737 }
5738
5739 btrfs_free_path(path);
5740 return ret;
5741}
5742
Filipe Manana18aa0922015-08-05 16:49:08 +01005743static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005744 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01005745 struct btrfs_log_ctx *ctx)
5746{
David Sterba3ffbd682018-06-29 10:56:42 +02005747 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana18aa0922015-08-05 16:49:08 +01005748 int ret;
5749 struct btrfs_path *path;
5750 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005751 struct btrfs_root *root = inode->root;
5752 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005753
5754 path = btrfs_alloc_path();
5755 if (!path)
5756 return -ENOMEM;
5757 path->skip_locking = 1;
5758 path->search_commit_root = 1;
5759
5760 key.objectid = ino;
5761 key.type = BTRFS_INODE_REF_KEY;
5762 key.offset = 0;
5763 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5764 if (ret < 0)
5765 goto out;
5766
5767 while (true) {
5768 struct extent_buffer *leaf = path->nodes[0];
5769 int slot = path->slots[0];
5770 u32 cur_offset = 0;
5771 u32 item_size;
5772 unsigned long ptr;
5773
5774 if (slot >= btrfs_header_nritems(leaf)) {
5775 ret = btrfs_next_leaf(root, path);
5776 if (ret < 0)
5777 goto out;
5778 else if (ret > 0)
5779 break;
5780 continue;
5781 }
5782
5783 btrfs_item_key_to_cpu(leaf, &key, slot);
5784 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5785 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5786 break;
5787
5788 item_size = btrfs_item_size_nr(leaf, slot);
5789 ptr = btrfs_item_ptr_offset(leaf, slot);
5790 while (cur_offset < item_size) {
5791 struct btrfs_key inode_key;
5792 struct inode *dir_inode;
5793
5794 inode_key.type = BTRFS_INODE_ITEM_KEY;
5795 inode_key.offset = 0;
5796
5797 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5798 struct btrfs_inode_extref *extref;
5799
5800 extref = (struct btrfs_inode_extref *)
5801 (ptr + cur_offset);
5802 inode_key.objectid = btrfs_inode_extref_parent(
5803 leaf, extref);
5804 cur_offset += sizeof(*extref);
5805 cur_offset += btrfs_inode_extref_name_len(leaf,
5806 extref);
5807 } else {
5808 inode_key.objectid = key.offset;
5809 cur_offset = item_size;
5810 }
5811
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005812 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
Filipe Manana18aa0922015-08-05 16:49:08 +01005813 root, NULL);
Filipe Manana0f375ee2018-10-09 15:05:29 +01005814 /*
5815 * If the parent inode was deleted, return an error to
5816 * fallback to a transaction commit. This is to prevent
5817 * getting an inode that was moved from one parent A to
5818 * a parent B, got its former parent A deleted and then
5819 * it got fsync'ed, from existing at both parents after
5820 * a log replay (and the old parent still existing).
5821 * Example:
5822 *
5823 * mkdir /mnt/A
5824 * mkdir /mnt/B
5825 * touch /mnt/B/bar
5826 * sync
5827 * mv /mnt/B/bar /mnt/A/bar
5828 * mv -T /mnt/A /mnt/B
5829 * fsync /mnt/B/bar
5830 * <power fail>
5831 *
5832 * If we ignore the old parent B which got deleted,
5833 * after a log replay we would have file bar linked
5834 * at both parents and the old parent B would still
5835 * exist.
5836 */
5837 if (IS_ERR(dir_inode)) {
5838 ret = PTR_ERR(dir_inode);
5839 goto out;
5840 }
Filipe Manana18aa0922015-08-05 16:49:08 +01005841
Filipe Manana657ed1a2016-04-06 17:11:56 +01005842 if (ctx)
5843 ctx->log_new_dentries = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005844 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Filipe Manana18aa0922015-08-05 16:49:08 +01005845 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005846 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005847 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005848 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005849 if (!ret && ctx && ctx->log_new_dentries)
5850 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01005851 BTRFS_I(dir_inode), ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005852 iput(dir_inode);
5853 if (ret)
5854 goto out;
5855 }
5856 path->slots[0]++;
5857 }
5858 ret = 0;
5859out:
5860 btrfs_free_path(path);
5861 return ret;
5862}
5863
Filipe Mananab8aa3302019-04-17 11:31:06 +01005864static int log_new_ancestors(struct btrfs_trans_handle *trans,
5865 struct btrfs_root *root,
5866 struct btrfs_path *path,
5867 struct btrfs_log_ctx *ctx)
5868{
5869 struct btrfs_key found_key;
5870
5871 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
5872
5873 while (true) {
5874 struct btrfs_fs_info *fs_info = root->fs_info;
5875 const u64 last_committed = fs_info->last_trans_committed;
5876 struct extent_buffer *leaf = path->nodes[0];
5877 int slot = path->slots[0];
5878 struct btrfs_key search_key;
5879 struct inode *inode;
5880 int ret = 0;
5881
5882 btrfs_release_path(path);
5883
5884 search_key.objectid = found_key.offset;
5885 search_key.type = BTRFS_INODE_ITEM_KEY;
5886 search_key.offset = 0;
5887 inode = btrfs_iget(fs_info->sb, &search_key, root, NULL);
5888 if (IS_ERR(inode))
5889 return PTR_ERR(inode);
5890
5891 if (BTRFS_I(inode)->generation > last_committed)
5892 ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
5893 LOG_INODE_EXISTS,
5894 0, LLONG_MAX, ctx);
5895 iput(inode);
5896 if (ret)
5897 return ret;
5898
5899 if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
5900 break;
5901
5902 search_key.type = BTRFS_INODE_REF_KEY;
5903 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
5904 if (ret < 0)
5905 return ret;
5906
5907 leaf = path->nodes[0];
5908 slot = path->slots[0];
5909 if (slot >= btrfs_header_nritems(leaf)) {
5910 ret = btrfs_next_leaf(root, path);
5911 if (ret < 0)
5912 return ret;
5913 else if (ret > 0)
5914 return -ENOENT;
5915 leaf = path->nodes[0];
5916 slot = path->slots[0];
5917 }
5918
5919 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5920 if (found_key.objectid != search_key.objectid ||
5921 found_key.type != BTRFS_INODE_REF_KEY)
5922 return -ENOENT;
5923 }
5924 return 0;
5925}
5926
5927static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
5928 struct btrfs_inode *inode,
5929 struct dentry *parent,
5930 struct btrfs_log_ctx *ctx)
5931{
5932 struct btrfs_root *root = inode->root;
5933 struct btrfs_fs_info *fs_info = root->fs_info;
5934 struct dentry *old_parent = NULL;
5935 struct super_block *sb = inode->vfs_inode.i_sb;
5936 int ret = 0;
5937
5938 while (true) {
5939 if (!parent || d_really_is_negative(parent) ||
5940 sb != parent->d_sb)
5941 break;
5942
5943 inode = BTRFS_I(d_inode(parent));
5944 if (root != inode->root)
5945 break;
5946
5947 if (inode->generation > fs_info->last_trans_committed) {
5948 ret = btrfs_log_inode(trans, root, inode,
5949 LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
5950 if (ret)
5951 break;
5952 }
5953 if (IS_ROOT(parent))
5954 break;
5955
5956 parent = dget_parent(parent);
5957 dput(old_parent);
5958 old_parent = parent;
5959 }
5960 dput(old_parent);
5961
5962 return ret;
5963}
5964
5965static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
5966 struct btrfs_inode *inode,
5967 struct dentry *parent,
5968 struct btrfs_log_ctx *ctx)
5969{
5970 struct btrfs_root *root = inode->root;
5971 const u64 ino = btrfs_ino(inode);
5972 struct btrfs_path *path;
5973 struct btrfs_key search_key;
5974 int ret;
5975
5976 /*
5977 * For a single hard link case, go through a fast path that does not
5978 * need to iterate the fs/subvolume tree.
5979 */
5980 if (inode->vfs_inode.i_nlink < 2)
5981 return log_new_ancestors_fast(trans, inode, parent, ctx);
5982
5983 path = btrfs_alloc_path();
5984 if (!path)
5985 return -ENOMEM;
5986
5987 search_key.objectid = ino;
5988 search_key.type = BTRFS_INODE_REF_KEY;
5989 search_key.offset = 0;
5990again:
5991 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
5992 if (ret < 0)
5993 goto out;
5994 if (ret == 0)
5995 path->slots[0]++;
5996
5997 while (true) {
5998 struct extent_buffer *leaf = path->nodes[0];
5999 int slot = path->slots[0];
6000 struct btrfs_key found_key;
6001
6002 if (slot >= btrfs_header_nritems(leaf)) {
6003 ret = btrfs_next_leaf(root, path);
6004 if (ret < 0)
6005 goto out;
6006 else if (ret > 0)
6007 break;
6008 continue;
6009 }
6010
6011 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6012 if (found_key.objectid != ino ||
6013 found_key.type > BTRFS_INODE_EXTREF_KEY)
6014 break;
6015
6016 /*
6017 * Don't deal with extended references because they are rare
6018 * cases and too complex to deal with (we would need to keep
6019 * track of which subitem we are processing for each item in
6020 * this loop, etc). So just return some error to fallback to
6021 * a transaction commit.
6022 */
6023 if (found_key.type == BTRFS_INODE_EXTREF_KEY) {
6024 ret = -EMLINK;
6025 goto out;
6026 }
6027
6028 /*
6029 * Logging ancestors needs to do more searches on the fs/subvol
6030 * tree, so it releases the path as needed to avoid deadlocks.
6031 * Keep track of the last inode ref key and resume from that key
6032 * after logging all new ancestors for the current hard link.
6033 */
6034 memcpy(&search_key, &found_key, sizeof(search_key));
6035
6036 ret = log_new_ancestors(trans, root, path, ctx);
6037 if (ret)
6038 goto out;
6039 btrfs_release_path(path);
6040 goto again;
6041 }
6042 ret = 0;
6043out:
6044 btrfs_free_path(path);
6045 return ret;
6046}
6047
Chris Masone02119d2008-09-05 16:13:11 -04006048/*
6049 * helper function around btrfs_log_inode to make sure newly created
6050 * parent directories also end up in the log. A minimal inode and backref
6051 * only logging is done of any parent directories that are older than
6052 * the last committed transaction
6053 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006054static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006055 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01006056 struct dentry *parent,
6057 const loff_t start,
6058 const loff_t end,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07006059 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08006060 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04006061{
Nikolay Borisovf8822742018-02-27 17:37:17 +02006062 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006063 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04006064 struct super_block *sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04006065 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006066 u64 last_committed = fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006067 bool log_dentries = false;
Chris Mason12fcfd22009-03-24 10:24:20 -04006068
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006069 sb = inode->vfs_inode.i_sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04006070
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006071 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04006072 ret = 1;
6073 goto end_no_trans;
6074 }
6075
Miao Xie995946d2014-04-02 19:51:06 +08006076 /*
6077 * The prev transaction commit doesn't complete, we need do
6078 * full commit by ourselves.
6079 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006080 if (fs_info->last_trans_log_full_commit >
6081 fs_info->last_trans_committed) {
Chris Mason12fcfd22009-03-24 10:24:20 -04006082 ret = 1;
6083 goto end_no_trans;
6084 }
6085
Nikolay Borisovf8822742018-02-27 17:37:17 +02006086 if (btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04006087 ret = 1;
6088 goto end_no_trans;
6089 }
6090
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006091 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
6092 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04006093 if (ret)
6094 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04006095
Filipe Mananaf2d72f42018-10-08 11:12:55 +01006096 /*
6097 * Skip already logged inodes or inodes corresponding to tmpfiles
6098 * (since logging them is pointless, a link count of 0 means they
6099 * will never be accessible).
6100 */
6101 if (btrfs_inode_in_log(inode, trans->transid) ||
6102 inode->vfs_inode.i_nlink == 0) {
Chris Mason257c62e2009-10-13 13:21:08 -04006103 ret = BTRFS_NO_LOG_SYNC;
6104 goto end_no_trans;
6105 }
6106
Miao Xie8b050d32014-02-20 18:08:58 +08006107 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006108 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08006109 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04006110
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006111 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006112 if (ret)
6113 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04006114
Chris Masonaf4176b2009-03-24 10:24:31 -04006115 /*
6116 * for regular files, if its inode is already on disk, we don't
6117 * have to worry about the parents at all. This is because
6118 * we can use the last_unlink_trans field to record renames
6119 * and other fun in this file.
6120 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006121 if (S_ISREG(inode->vfs_inode.i_mode) &&
6122 inode->generation <= last_committed &&
6123 inode->last_unlink_trans <= last_committed) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006124 ret = 0;
6125 goto end_trans;
6126 }
Chris Masonaf4176b2009-03-24 10:24:31 -04006127
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006128 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006129 log_dentries = true;
6130
Filipe Manana18aa0922015-08-05 16:49:08 +01006131 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006132 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01006133 * inodes are fully logged. This is to prevent leaving dangling
6134 * directory index entries in directories that were our parents but are
6135 * not anymore. Not doing this results in old parent directory being
6136 * impossible to delete after log replay (rmdir will always fail with
6137 * error -ENOTEMPTY).
6138 *
6139 * Example 1:
6140 *
6141 * mkdir testdir
6142 * touch testdir/foo
6143 * ln testdir/foo testdir/bar
6144 * sync
6145 * unlink testdir/bar
6146 * xfs_io -c fsync testdir/foo
6147 * <power failure>
6148 * mount fs, triggers log replay
6149 *
6150 * If we don't log the parent directory (testdir), after log replay the
6151 * directory still has an entry pointing to the file inode using the bar
6152 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
6153 * the file inode has a link count of 1.
6154 *
6155 * Example 2:
6156 *
6157 * mkdir testdir
6158 * touch foo
6159 * ln foo testdir/foo2
6160 * ln foo testdir/foo3
6161 * sync
6162 * unlink testdir/foo3
6163 * xfs_io -c fsync foo
6164 * <power failure>
6165 * mount fs, triggers log replay
6166 *
6167 * Similar as the first example, after log replay the parent directory
6168 * testdir still has an entry pointing to the inode file with name foo3
6169 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
6170 * and has a link count of 2.
6171 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02006172 if (inode->last_unlink_trans > last_committed) {
Filipe Mananab8aa3302019-04-17 11:31:06 +01006173 ret = btrfs_log_all_parents(trans, inode, ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01006174 if (ret)
6175 goto end_trans;
6176 }
6177
Filipe Mananab8aa3302019-04-17 11:31:06 +01006178 ret = log_all_new_ancestors(trans, inode, parent, ctx);
6179 if (ret)
Filipe Manana41bd6062018-11-28 14:54:28 +00006180 goto end_trans;
Filipe Manana41bd6062018-11-28 14:54:28 +00006181
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006182 if (log_dentries)
Filipe Mananab8aa3302019-04-17 11:31:06 +01006183 ret = log_new_dir_dentries(trans, root, inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00006184 else
6185 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006186end_trans:
6187 if (ret < 0) {
David Sterba90787762019-03-20 13:28:05 +01006188 btrfs_set_log_full_commit(trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006189 ret = 1;
6190 }
Miao Xie8b050d32014-02-20 18:08:58 +08006191
6192 if (ret)
6193 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04006194 btrfs_end_log_trans(root);
6195end_no_trans:
6196 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006197}
6198
6199/*
6200 * it is not safe to log dentry if the chunk root has added new
6201 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
6202 * If this returns 1, you must commit the transaction to safely get your
6203 * data on disk.
6204 */
6205int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Nikolay Borisove5b84f7a2018-02-27 17:37:18 +02006206 struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01006207 const loff_t start,
6208 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08006209 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04006210{
Josef Bacik6a912212010-11-20 09:48:00 +00006211 struct dentry *parent = dget_parent(dentry);
6212 int ret;
6213
Nikolay Borisovf8822742018-02-27 17:37:17 +02006214 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
6215 start, end, LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00006216 dput(parent);
6217
6218 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006219}
6220
6221/*
6222 * should be called during mount to recover any replay any log trees
6223 * from the FS
6224 */
6225int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
6226{
6227 int ret;
6228 struct btrfs_path *path;
6229 struct btrfs_trans_handle *trans;
6230 struct btrfs_key key;
6231 struct btrfs_key found_key;
6232 struct btrfs_key tmp_key;
6233 struct btrfs_root *log;
6234 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
6235 struct walk_control wc = {
6236 .process_func = process_one_buffer,
David Sterba430a6622019-08-01 14:50:35 +02006237 .stage = LOG_WALK_PIN_ONLY,
Chris Masone02119d2008-09-05 16:13:11 -04006238 };
6239
Chris Masone02119d2008-09-05 16:13:11 -04006240 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006241 if (!path)
6242 return -ENOMEM;
6243
Josef Bacikafcdd122016-09-02 15:40:02 -04006244 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04006245
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006246 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006247 if (IS_ERR(trans)) {
6248 ret = PTR_ERR(trans);
6249 goto error;
6250 }
Chris Masone02119d2008-09-05 16:13:11 -04006251
6252 wc.trans = trans;
6253 wc.pin = 1;
6254
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006255 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006256 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006257 btrfs_handle_fs_error(fs_info, ret,
6258 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006259 goto error;
6260 }
Chris Masone02119d2008-09-05 16:13:11 -04006261
6262again:
6263 key.objectid = BTRFS_TREE_LOG_OBJECTID;
6264 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02006265 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04006266
Chris Masond3977122009-01-05 21:25:51 -05006267 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04006268 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006269
6270 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08006271 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006272 "Couldn't find tree log root.");
6273 goto error;
6274 }
Chris Masone02119d2008-09-05 16:13:11 -04006275 if (ret > 0) {
6276 if (path->slots[0] == 0)
6277 break;
6278 path->slots[0]--;
6279 }
6280 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
6281 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02006282 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006283 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
6284 break;
6285
Miao Xiecb517ea2013-05-15 07:48:19 +00006286 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006287 if (IS_ERR(log)) {
6288 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08006289 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006290 "Couldn't read tree log root.");
6291 goto error;
6292 }
Chris Masone02119d2008-09-05 16:13:11 -04006293
6294 tmp_key.objectid = found_key.offset;
6295 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
6296 tmp_key.offset = (u64)-1;
6297
6298 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006299 if (IS_ERR(wc.replay_dest)) {
6300 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006301 free_extent_buffer(log->node);
6302 free_extent_buffer(log->commit_root);
6303 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006304 btrfs_handle_fs_error(fs_info, ret,
6305 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006306 goto error;
6307 }
Chris Masone02119d2008-09-05 16:13:11 -04006308
Yan Zheng07d400a2009-01-06 11:42:00 -05006309 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006310 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04006311 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04006312
Josef Bacikb50c6e22013-04-25 15:55:30 -04006313 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04006314 ret = fixup_inode_link_counts(trans, wc.replay_dest,
6315 path);
Chris Masone02119d2008-09-05 16:13:11 -04006316 }
Chris Masone02119d2008-09-05 16:13:11 -04006317
Liu Bo900c9982018-01-25 11:02:56 -07006318 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
6319 struct btrfs_root *root = wc.replay_dest;
6320
6321 btrfs_release_path(path);
6322
6323 /*
6324 * We have just replayed everything, and the highest
6325 * objectid of fs roots probably has changed in case
6326 * some inode_item's got replayed.
6327 *
6328 * root->objectid_mutex is not acquired as log replay
6329 * could only happen during mount.
6330 */
6331 ret = btrfs_find_highest_objectid(root,
6332 &root->highest_objectid);
6333 }
6334
Chris Masone02119d2008-09-05 16:13:11 -04006335 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05006336 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04006337 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04006338 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04006339 kfree(log);
6340
Josef Bacikb50c6e22013-04-25 15:55:30 -04006341 if (ret)
6342 goto error;
6343
Chris Masone02119d2008-09-05 16:13:11 -04006344 if (found_key.offset == 0)
6345 break;
6346 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006347 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006348
6349 /* step one is to pin it all, step two is to replay just inodes */
6350 if (wc.pin) {
6351 wc.pin = 0;
6352 wc.process_func = replay_one_buffer;
6353 wc.stage = LOG_WALK_REPLAY_INODES;
6354 goto again;
6355 }
6356 /* step three is to replay everything */
6357 if (wc.stage < LOG_WALK_REPLAY_ALL) {
6358 wc.stage++;
6359 goto again;
6360 }
6361
6362 btrfs_free_path(path);
6363
Josef Bacikabefa552013-04-24 16:40:05 -04006364 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006365 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04006366 if (ret)
6367 return ret;
6368
Chris Masone02119d2008-09-05 16:13:11 -04006369 free_extent_buffer(log_root_tree->node);
6370 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04006371 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04006372 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006373
Josef Bacikabefa552013-04-24 16:40:05 -04006374 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006375error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04006376 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006377 btrfs_end_transaction(wc.trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006378 btrfs_free_path(path);
6379 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006380}
Chris Mason12fcfd22009-03-24 10:24:20 -04006381
6382/*
6383 * there are some corner cases where we want to force a full
6384 * commit instead of allowing a directory to be logged.
6385 *
6386 * They revolve around files there were unlinked from the directory, and
6387 * this function updates the parent directory so that a full commit is
6388 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00006389 *
6390 * Must be called before the unlink operations (updates to the subvolume tree,
6391 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04006392 */
6393void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006394 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04006395 int for_rename)
6396{
6397 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006398 * when we're logging a file, if it hasn't been renamed
6399 * or unlinked, and its inode is fully committed on disk,
6400 * we don't have to worry about walking up the directory chain
6401 * to log its parents.
6402 *
6403 * So, we use the last_unlink_trans field to put this transid
6404 * into the file. When the file is logged we check it and
6405 * don't log the parents if the file is fully on disk.
6406 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006407 mutex_lock(&inode->log_mutex);
6408 inode->last_unlink_trans = trans->transid;
6409 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04006410
6411 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006412 * if this directory was already logged any new
6413 * names for this file/dir will get recorded
6414 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006415 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006416 return;
6417
6418 /*
6419 * if the inode we're about to unlink was logged,
6420 * the log will be properly updated for any new names
6421 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006422 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006423 return;
6424
6425 /*
6426 * when renaming files across directories, if the directory
6427 * there we're unlinking from gets fsync'd later on, there's
6428 * no way to find the destination directory later and fsync it
6429 * properly. So, we have to be conservative and force commits
6430 * so the new name gets discovered.
6431 */
6432 if (for_rename)
6433 goto record;
6434
6435 /* we can safely do the unlink without any special recording */
6436 return;
6437
6438record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006439 mutex_lock(&dir->log_mutex);
6440 dir->last_unlink_trans = trans->transid;
6441 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04006442}
6443
6444/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006445 * Make sure that if someone attempts to fsync the parent directory of a deleted
6446 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6447 * that after replaying the log tree of the parent directory's root we will not
6448 * see the snapshot anymore and at log replay time we will not see any log tree
6449 * corresponding to the deleted snapshot's root, which could lead to replaying
6450 * it after replaying the log tree of the parent directory (which would replay
6451 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00006452 *
6453 * Must be called before the actual snapshot destroy operation (updates to the
6454 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006455 */
6456void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02006457 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006458{
Nikolay Borisov43663552017-01-18 00:31:29 +02006459 mutex_lock(&dir->log_mutex);
6460 dir->last_unlink_trans = trans->transid;
6461 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006462}
6463
6464/*
Chris Mason12fcfd22009-03-24 10:24:20 -04006465 * Call this after adding a new name for a file and it will properly
6466 * update the log to reflect the new name.
6467 *
Filipe Mananad4682ba2018-06-11 19:24:28 +01006468 * @ctx can not be NULL when @sync_log is false, and should be NULL when it's
6469 * true (because it's not used).
6470 *
6471 * Return value depends on whether @sync_log is true or false.
6472 * When true: returns BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6473 * committed by the caller, and BTRFS_DONT_NEED_TRANS_COMMIT
6474 * otherwise.
6475 * When false: returns BTRFS_DONT_NEED_LOG_SYNC if the caller does not need to
6476 * to sync the log, BTRFS_NEED_LOG_SYNC if it needs to sync the log,
6477 * or BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6478 * committed (without attempting to sync the log).
Chris Mason12fcfd22009-03-24 10:24:20 -04006479 */
6480int btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006481 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Filipe Mananad4682ba2018-06-11 19:24:28 +01006482 struct dentry *parent,
6483 bool sync_log, struct btrfs_log_ctx *ctx)
Chris Mason12fcfd22009-03-24 10:24:20 -04006484{
David Sterba3ffbd682018-06-29 10:56:42 +02006485 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006486 int ret;
Chris Mason12fcfd22009-03-24 10:24:20 -04006487
6488 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006489 * this will force the logging code to walk the dentry chain
6490 * up for the file
6491 */
Filipe Manana9a6509c2018-02-28 15:55:40 +00006492 if (!S_ISDIR(inode->vfs_inode.i_mode))
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006493 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04006494
6495 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006496 * if this inode hasn't been logged and directory we're renaming it
6497 * from hasn't been logged, we don't need to log it
6498 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006499 if (inode->logged_trans <= fs_info->last_trans_committed &&
6500 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
Filipe Mananad4682ba2018-06-11 19:24:28 +01006501 return sync_log ? BTRFS_DONT_NEED_TRANS_COMMIT :
6502 BTRFS_DONT_NEED_LOG_SYNC;
Chris Mason12fcfd22009-03-24 10:24:20 -04006503
Filipe Mananad4682ba2018-06-11 19:24:28 +01006504 if (sync_log) {
6505 struct btrfs_log_ctx ctx2;
6506
6507 btrfs_init_log_ctx(&ctx2, &inode->vfs_inode);
6508 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6509 LOG_INODE_EXISTS, &ctx2);
6510 if (ret == BTRFS_NO_LOG_SYNC)
6511 return BTRFS_DONT_NEED_TRANS_COMMIT;
6512 else if (ret)
6513 return BTRFS_NEED_TRANS_COMMIT;
6514
6515 ret = btrfs_sync_log(trans, inode->root, &ctx2);
6516 if (ret)
6517 return BTRFS_NEED_TRANS_COMMIT;
6518 return BTRFS_DONT_NEED_TRANS_COMMIT;
6519 }
6520
6521 ASSERT(ctx);
6522 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6523 LOG_INODE_EXISTS, ctx);
6524 if (ret == BTRFS_NO_LOG_SYNC)
6525 return BTRFS_DONT_NEED_LOG_SYNC;
6526 else if (ret)
6527 return BTRFS_NEED_TRANS_COMMIT;
6528
6529 return BTRFS_NEED_LOG_SYNC;
Chris Mason12fcfd22009-03-24 10:24:20 -04006530}
6531