blob: 3e903e6a33870293d850660f565346b10b0c0e39 [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>
Nikolay Borisov9678c542018-01-08 11:45:05 +020011#include "ctree.h"
Miao Xie995946d2014-04-02 19:51:06 +080012#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040013#include "disk-io.h"
14#include "locking.h"
15#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070016#include "backref.h"
Anand Jainebb87652016-03-10 17:26:59 +080017#include "compression.h"
Qu Wenruodf2c95f2016-08-15 10:36:52 +080018#include "qgroup.h"
Liu Bo900c9982018-01-25 11:02:56 -070019#include "inode-map.h"
Chris Masone02119d2008-09-05 16:13:11 -040020
21/* magic values for the inode_only field in btrfs_log_inode:
22 *
23 * LOG_INODE_ALL means to log everything
24 * LOG_INODE_EXISTS means to log just enough to recreate the inode
25 * during log replay
26 */
27#define LOG_INODE_ALL 0
28#define LOG_INODE_EXISTS 1
Liu Bo781feef2016-11-30 16:20:25 -080029#define LOG_OTHER_INODE 2
Chris Masone02119d2008-09-05 16:13:11 -040030
31/*
Chris Mason12fcfd22009-03-24 10:24:20 -040032 * directory trouble cases
33 *
34 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
35 * log, we must force a full commit before doing an fsync of the directory
36 * where the unlink was done.
37 * ---> record transid of last unlink/rename per directory
38 *
39 * mkdir foo/some_dir
40 * normal commit
41 * rename foo/some_dir foo2/some_dir
42 * mkdir foo/some_dir
43 * fsync foo/some_dir/some_file
44 *
45 * The fsync above will unlink the original some_dir without recording
46 * it in its new location (foo2). After a crash, some_dir will be gone
47 * unless the fsync of some_file forces a full commit
48 *
49 * 2) we must log any new names for any file or dir that is in the fsync
50 * log. ---> check inode while renaming/linking.
51 *
52 * 2a) we must log any new names for any file or dir during rename
53 * when the directory they are being removed from was logged.
54 * ---> check inode and old parent dir during rename
55 *
56 * 2a is actually the more important variant. With the extra logging
57 * a crash might unlink the old name without recreating the new one
58 *
59 * 3) after a crash, we must go through any directories with a link count
60 * of zero and redo the rm -rf
61 *
62 * mkdir f1/foo
63 * normal commit
64 * rm -rf f1/foo
65 * fsync(f1)
66 *
67 * The directory f1 was fully removed from the FS, but fsync was never
68 * called on f1, only its parent dir. After a crash the rm -rf must
69 * be replayed. This must be able to recurse down the entire
70 * directory tree. The inode link count fixup code takes care of the
71 * ugly details.
72 */
73
74/*
Chris Masone02119d2008-09-05 16:13:11 -040075 * stages for the tree walking. The first
76 * stage (0) is to only pin down the blocks we find
77 * the second stage (1) is to make sure that all the inodes
78 * we find in the log are created in the subvolume.
79 *
80 * The last stage is to deal with directories and links and extents
81 * and all the other fun semantics
82 */
83#define LOG_WALK_PIN_ONLY 0
84#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040085#define LOG_WALK_REPLAY_DIR_INDEX 2
86#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040087
Chris Mason12fcfd22009-03-24 10:24:20 -040088static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +020089 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +010090 int inode_only,
91 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +010092 const loff_t end,
93 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -050094static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
95 struct btrfs_root *root,
96 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -040097static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root,
99 struct btrfs_root *log,
100 struct btrfs_path *path,
101 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400102
103/*
104 * tree logging is a special write ahead log used to make sure that
105 * fsyncs and O_SYNCs can happen without doing full tree commits.
106 *
107 * Full tree commits are expensive because they require commonly
108 * modified blocks to be recowed, creating many dirty pages in the
109 * extent tree an 4x-6x higher write load than ext3.
110 *
111 * Instead of doing a tree commit on every fsync, we use the
112 * key ranges and transaction ids to find items for a given file or directory
113 * that have changed in this transaction. Those items are copied into
114 * a special tree (one per subvolume root), that tree is written to disk
115 * and then the fsync is considered complete.
116 *
117 * After a crash, items are copied out of the log-tree back into the
118 * subvolume tree. Any file data extents found are recorded in the extent
119 * allocation tree, and the log-tree freed.
120 *
121 * The log tree is read three times, once to pin down all the extents it is
122 * using in ram and once, once to create all the inodes logged in the tree
123 * and once to do all the other items.
124 */
125
126/*
Chris Masone02119d2008-09-05 16:13:11 -0400127 * start a sub transaction and setup the log tree
128 * this increments the log tree writer count to make the people
129 * syncing the tree wait for us to finish
130 */
131static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800132 struct btrfs_root *root,
133 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400134{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400135 struct btrfs_fs_info *fs_info = root->fs_info;
Zhaolei34eb2a52015-08-17 18:44:45 +0800136 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500137
138 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800139
Yan Zheng7237f182009-01-21 12:54:03 -0500140 if (root->log_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400141 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800142 ret = -EAGAIN;
143 goto out;
144 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800145
Josef Bacikff782e02009-10-08 15:30:04 -0400146 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800147 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800148 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400149 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800150 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400151 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800152 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400153 mutex_lock(&fs_info->tree_log_mutex);
154 if (!fs_info->log_root_tree)
155 ret = btrfs_init_log_root_tree(trans, fs_info);
156 mutex_unlock(&fs_info->tree_log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800157 if (ret)
158 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400159
Chris Masone02119d2008-09-05 16:13:11 -0400160 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400161 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800162 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800163
164 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
165 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400166 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800167
Miao Xie2ecb7922012-09-06 04:04:27 -0600168 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500169 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800170 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800171 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800172 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800173 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800174 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800175
Miao Xiee87ac132014-02-20 18:08:53 +0800176out:
Yan Zheng7237f182009-01-21 12:54:03 -0500177 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800178 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400179}
180
181/*
182 * returns 0 if there was a log transaction running and we were able
183 * to join, or returns -ENOENT if there were not transactions
184 * in progress
185 */
186static int join_running_log_trans(struct btrfs_root *root)
187{
188 int ret = -ENOENT;
189
190 smp_mb();
191 if (!root->log_root)
192 return -ENOENT;
193
Yan Zheng7237f182009-01-21 12:54:03 -0500194 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400195 if (root->log_root) {
196 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500197 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400198 }
Yan Zheng7237f182009-01-21 12:54:03 -0500199 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400200 return ret;
201}
202
203/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400204 * This either makes the current running log transaction wait
205 * until you call btrfs_end_log_trans() or it makes any future
206 * log transactions wait until you call btrfs_end_log_trans()
207 */
208int btrfs_pin_log_trans(struct btrfs_root *root)
209{
210 int ret = -ENOENT;
211
212 mutex_lock(&root->log_mutex);
213 atomic_inc(&root->log_writers);
214 mutex_unlock(&root->log_mutex);
215 return ret;
216}
217
218/*
Chris Masone02119d2008-09-05 16:13:11 -0400219 * indicate we're done making changes to the log tree
220 * and wake up anyone waiting to do a sync
221 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100222void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400223{
Yan Zheng7237f182009-01-21 12:54:03 -0500224 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +0100225 /* atomic_dec_and_test implies a barrier */
226 cond_wake_up_nomb(&root->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -0500227 }
Chris Masone02119d2008-09-05 16:13:11 -0400228}
229
230
231/*
232 * the walk control struct is used to pass state down the chain when
233 * processing the log tree. The stage field tells us which part
234 * of the log tree processing we are currently doing. The others
235 * are state fields used for that specific part
236 */
237struct walk_control {
238 /* should we free the extent on disk when done? This is used
239 * at transaction commit time while freeing a log tree
240 */
241 int free;
242
243 /* should we write out the extent buffer? This is used
244 * while flushing the log tree to disk during a sync
245 */
246 int write;
247
248 /* should we wait for the extent buffer io to finish? Also used
249 * while flushing the log tree to disk for a sync
250 */
251 int wait;
252
253 /* pin only walk, we record which extents on disk belong to the
254 * log trees
255 */
256 int pin;
257
258 /* what stage of the replay code we're currently in */
259 int stage;
260
Filipe Manana55f21e12018-10-08 11:12:55 +0100261 /*
262 * Ignore any items from the inode currently being processed. Needs
263 * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
264 * the LOG_WALK_REPLAY_INODES stage.
265 */
266 bool ignore_cur_inode;
267
Chris Masone02119d2008-09-05 16:13:11 -0400268 /* the root we are currently replaying */
269 struct btrfs_root *replay_dest;
270
271 /* the trans handle for the current replay */
272 struct btrfs_trans_handle *trans;
273
274 /* the function that gets used to process blocks we find in the
275 * tree. Note the extent_buffer might not be up to date when it is
276 * passed in, and it must be checked or read if you need the data
277 * inside it
278 */
279 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800280 struct walk_control *wc, u64 gen, int level);
Chris Masone02119d2008-09-05 16:13:11 -0400281};
282
283/*
284 * process_func used to pin down extents, write them or wait on them
285 */
286static int process_one_buffer(struct btrfs_root *log,
287 struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800288 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -0400289{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 struct btrfs_fs_info *fs_info = log->fs_info;
Josef Bacikb50c6e22013-04-25 15:55:30 -0400291 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400292
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400293 /*
294 * If this fs is mixed then we need to be able to process the leaves to
295 * pin down any logged extents, so we have to read the block.
296 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400297 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Qu Wenruo581c1762018-03-29 09:08:11 +0800298 ret = btrfs_read_buffer(eb, gen, level, NULL);
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400299 if (ret)
300 return ret;
301 }
302
Josef Bacikb50c6e22013-04-25 15:55:30 -0400303 if (wc->pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400304 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
305 eb->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400306
307 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400308 if (wc->pin && btrfs_header_level(eb) == 0)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400309 ret = btrfs_exclude_logged_extents(fs_info, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400310 if (wc->write)
311 btrfs_write_tree_block(eb);
312 if (wc->wait)
313 btrfs_wait_tree_block_writeback(eb);
314 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400315 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400316}
317
318/*
319 * Item overwrite used by replay and tree logging. eb, slot and key all refer
320 * to the src data we are copying out.
321 *
322 * root is the tree we are copying into, and path is a scratch
323 * path for use in this function (it should be released on entry and
324 * will be released on exit).
325 *
326 * If the key is already in the destination tree the existing item is
327 * overwritten. If the existing item isn't big enough, it is extended.
328 * If it is too large, it is truncated.
329 *
330 * If the key isn't in the destination yet, a new item is inserted.
331 */
332static noinline int overwrite_item(struct btrfs_trans_handle *trans,
333 struct btrfs_root *root,
334 struct btrfs_path *path,
335 struct extent_buffer *eb, int slot,
336 struct btrfs_key *key)
337{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400338 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400339 int ret;
340 u32 item_size;
341 u64 saved_i_size = 0;
342 int save_old_i_size = 0;
343 unsigned long src_ptr;
344 unsigned long dst_ptr;
345 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000346 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400347
348 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
349 overwrite_root = 1;
350
351 item_size = btrfs_item_size_nr(eb, slot);
352 src_ptr = btrfs_item_ptr_offset(eb, slot);
353
354 /* look for the key in the destination tree */
355 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000356 if (ret < 0)
357 return ret;
358
Chris Masone02119d2008-09-05 16:13:11 -0400359 if (ret == 0) {
360 char *src_copy;
361 char *dst_copy;
362 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
363 path->slots[0]);
364 if (dst_size != item_size)
365 goto insert;
366
367 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200368 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400369 return 0;
370 }
371 dst_copy = kmalloc(item_size, GFP_NOFS);
372 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000373 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200374 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000375 kfree(dst_copy);
376 kfree(src_copy);
377 return -ENOMEM;
378 }
Chris Masone02119d2008-09-05 16:13:11 -0400379
380 read_extent_buffer(eb, src_copy, src_ptr, item_size);
381
382 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
383 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
384 item_size);
385 ret = memcmp(dst_copy, src_copy, item_size);
386
387 kfree(dst_copy);
388 kfree(src_copy);
389 /*
390 * they have the same contents, just return, this saves
391 * us from cowing blocks in the destination tree and doing
392 * extra writes that may not have been done by a previous
393 * sync
394 */
395 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200396 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400397 return 0;
398 }
399
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000400 /*
401 * We need to load the old nbytes into the inode so when we
402 * replay the extents we've logged we get the right nbytes.
403 */
404 if (inode_item) {
405 struct btrfs_inode_item *item;
406 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400407 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000408
409 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
410 struct btrfs_inode_item);
411 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
412 item = btrfs_item_ptr(eb, slot,
413 struct btrfs_inode_item);
414 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400415
416 /*
417 * If this is a directory we need to reset the i_size to
418 * 0 so that we can set it up properly when replaying
419 * the rest of the items in this log.
420 */
421 mode = btrfs_inode_mode(eb, item);
422 if (S_ISDIR(mode))
423 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000424 }
425 } else if (inode_item) {
426 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400427 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000428
429 /*
430 * New inode, set nbytes to 0 so that the nbytes comes out
431 * properly when we replay the extents.
432 */
433 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
434 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400435
436 /*
437 * If this is a directory we need to reset the i_size to 0 so
438 * that we can set it up properly when replaying the rest of
439 * the items in this log.
440 */
441 mode = btrfs_inode_mode(eb, item);
442 if (S_ISDIR(mode))
443 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400444 }
445insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200446 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400447 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000448 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400449 ret = btrfs_insert_empty_item(trans, root, path,
450 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000451 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400452
453 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000454 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400455 u32 found_size;
456 found_size = btrfs_item_size_nr(path->nodes[0],
457 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100458 if (found_size > item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400459 btrfs_truncate_item(fs_info, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100460 else if (found_size < item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400461 btrfs_extend_item(fs_info, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100462 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400463 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400464 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400465 }
466 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
467 path->slots[0]);
468
469 /* don't overwrite an existing inode if the generation number
470 * was logged as zero. This is done when the tree logging code
471 * is just logging an inode to make sure it exists after recovery.
472 *
473 * Also, don't overwrite i_size on directories during replay.
474 * log replay inserts and removes directory items based on the
475 * state of the tree found in the subvolume, and i_size is modified
476 * as it goes
477 */
478 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
479 struct btrfs_inode_item *src_item;
480 struct btrfs_inode_item *dst_item;
481
482 src_item = (struct btrfs_inode_item *)src_ptr;
483 dst_item = (struct btrfs_inode_item *)dst_ptr;
484
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000485 if (btrfs_inode_generation(eb, src_item) == 0) {
486 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000487 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000488
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000489 /*
490 * For regular files an ino_size == 0 is used only when
491 * logging that an inode exists, as part of a directory
492 * fsync, and the inode wasn't fsynced before. In this
493 * case don't set the size of the inode in the fs/subvol
494 * tree, otherwise we would be throwing valid data away.
495 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000496 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000497 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
498 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000499 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000500
501 btrfs_init_map_token(&token);
502 btrfs_set_token_inode_size(dst_eb, dst_item,
503 ino_size, &token);
504 }
Chris Masone02119d2008-09-05 16:13:11 -0400505 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000506 }
Chris Masone02119d2008-09-05 16:13:11 -0400507
508 if (overwrite_root &&
509 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
510 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
511 save_old_i_size = 1;
512 saved_i_size = btrfs_inode_size(path->nodes[0],
513 dst_item);
514 }
515 }
516
517 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
518 src_ptr, item_size);
519
520 if (save_old_i_size) {
521 struct btrfs_inode_item *dst_item;
522 dst_item = (struct btrfs_inode_item *)dst_ptr;
523 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
524 }
525
526 /* make sure the generation is filled in */
527 if (key->type == BTRFS_INODE_ITEM_KEY) {
528 struct btrfs_inode_item *dst_item;
529 dst_item = (struct btrfs_inode_item *)dst_ptr;
530 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
531 btrfs_set_inode_generation(path->nodes[0], dst_item,
532 trans->transid);
533 }
534 }
535no_copy:
536 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200537 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400538 return 0;
539}
540
541/*
542 * simple helper to read an inode off the disk from a given root
543 * This can only be called for subvolume roots and not for the log
544 */
545static noinline struct inode *read_one_inode(struct btrfs_root *root,
546 u64 objectid)
547{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400548 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400549 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400550
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400551 key.objectid = objectid;
552 key.type = BTRFS_INODE_ITEM_KEY;
553 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000554 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Al Viro2e19f1f2018-07-29 23:04:45 +0100555 if (IS_ERR(inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400556 inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -0400557 return inode;
558}
559
560/* replays a single extent in 'eb' at 'slot' with 'key' into the
561 * subvolume 'root'. path is released on entry and should be released
562 * on exit.
563 *
564 * extents in the log tree have not been allocated out of the extent
565 * tree yet. So, this completes the allocation, taking a reference
566 * as required if the extent already exists or creating a new extent
567 * if it isn't in the extent allocation tree yet.
568 *
569 * The extent is inserted into the file, dropping any existing extents
570 * from the file that overlap the new one.
571 */
572static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
573 struct btrfs_root *root,
574 struct btrfs_path *path,
575 struct extent_buffer *eb, int slot,
576 struct btrfs_key *key)
577{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400578 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400579 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400580 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400581 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000582 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400583 struct btrfs_file_extent_item *item;
584 struct inode *inode = NULL;
585 unsigned long size;
586 int ret = 0;
587
588 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
589 found_type = btrfs_file_extent_type(eb, item);
590
Yan Zhengd899e052008-10-30 14:25:28 -0400591 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000592 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
593 nbytes = btrfs_file_extent_num_bytes(eb, item);
594 extent_end = start + nbytes;
595
596 /*
597 * We don't add to the inodes nbytes if we are prealloc or a
598 * hole.
599 */
600 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
601 nbytes = 0;
602 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +0800603 size = btrfs_file_extent_ram_bytes(eb, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000604 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400605 extent_end = ALIGN(start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400606 fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400607 } else {
608 ret = 0;
609 goto out;
610 }
611
612 inode = read_one_inode(root, key->objectid);
613 if (!inode) {
614 ret = -EIO;
615 goto out;
616 }
617
618 /*
619 * first check to see if we already have this extent in the
620 * file. This must be done before the btrfs_drop_extents run
621 * so we don't try to drop this extent.
622 */
David Sterbaf85b7372017-01-20 14:54:07 +0100623 ret = btrfs_lookup_file_extent(trans, root, path,
624 btrfs_ino(BTRFS_I(inode)), start, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400625
Yan Zhengd899e052008-10-30 14:25:28 -0400626 if (ret == 0 &&
627 (found_type == BTRFS_FILE_EXTENT_REG ||
628 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400629 struct btrfs_file_extent_item cmp1;
630 struct btrfs_file_extent_item cmp2;
631 struct btrfs_file_extent_item *existing;
632 struct extent_buffer *leaf;
633
634 leaf = path->nodes[0];
635 existing = btrfs_item_ptr(leaf, path->slots[0],
636 struct btrfs_file_extent_item);
637
638 read_extent_buffer(eb, &cmp1, (unsigned long)item,
639 sizeof(cmp1));
640 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
641 sizeof(cmp2));
642
643 /*
644 * we already have a pointer to this exact extent,
645 * we don't have to do anything
646 */
647 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200648 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400649 goto out;
650 }
651 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200652 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400653
654 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400655 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400656 if (ret)
657 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400658
Yan Zheng07d400a2009-01-06 11:42:00 -0500659 if (found_type == BTRFS_FILE_EXTENT_REG ||
660 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400661 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500662 unsigned long dest_offset;
663 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400664
Filipe Manana3168021c2017-02-01 14:58:02 +0000665 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
666 btrfs_fs_incompat(fs_info, NO_HOLES))
667 goto update_inode;
668
Yan Zheng07d400a2009-01-06 11:42:00 -0500669 ret = btrfs_insert_empty_item(trans, root, path, key,
670 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400671 if (ret)
672 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500673 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
674 path->slots[0]);
675 copy_extent_buffer(path->nodes[0], eb, dest_offset,
676 (unsigned long)item, sizeof(*item));
677
678 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
679 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
680 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400681 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500682
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800683 /*
684 * Manually record dirty extent, as here we did a shallow
685 * file extent item copy and skip normal backref update,
686 * but modifying extent tree all by ourselves.
687 * So need to manually record dirty extent for qgroup,
688 * as the owner of the file extent changed from log tree
689 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
690 */
Lu Fengqia95f3aa2018-07-18 16:28:03 +0800691 ret = btrfs_qgroup_trace_extent(trans,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800692 btrfs_file_extent_disk_bytenr(eb, item),
693 btrfs_file_extent_disk_num_bytes(eb, item),
694 GFP_NOFS);
695 if (ret < 0)
696 goto out;
697
Yan Zheng07d400a2009-01-06 11:42:00 -0500698 if (ins.objectid > 0) {
699 u64 csum_start;
700 u64 csum_end;
701 LIST_HEAD(ordered_sums);
702 /*
703 * is this extent already allocated in the extent
704 * allocation tree? If so, just add a reference
705 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400706 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500707 ins.offset);
708 if (ret == 0) {
Josef Bacik84f7d8e2017-09-29 15:43:49 -0400709 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng07d400a2009-01-06 11:42:00 -0500710 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400711 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100712 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400713 if (ret)
714 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500715 } else {
716 /*
717 * insert the extent pointer in the extent
718 * allocation tree
719 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400720 ret = btrfs_alloc_logged_file_extent(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400721 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400722 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400723 if (ret)
724 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500725 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200726 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500727
728 if (btrfs_file_extent_compression(eb, item)) {
729 csum_start = ins.objectid;
730 csum_end = csum_start + ins.offset;
731 } else {
732 csum_start = ins.objectid +
733 btrfs_file_extent_offset(eb, item);
734 csum_end = csum_start +
735 btrfs_file_extent_num_bytes(eb, item);
736 }
737
738 ret = btrfs_lookup_csums_range(root->log_root,
739 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100740 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400741 if (ret)
742 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100743 /*
744 * Now delete all existing cums in the csum root that
745 * cover our range. We do this because we can have an
746 * extent that is completely referenced by one file
747 * extent item and partially referenced by another
748 * file extent item (like after using the clone or
749 * extent_same ioctls). In this case if we end up doing
750 * the replay of the one that partially references the
751 * extent first, and we do not do the csum deletion
752 * below, we can get 2 csum items in the csum tree that
753 * overlap each other. For example, imagine our log has
754 * the two following file extent items:
755 *
756 * key (257 EXTENT_DATA 409600)
757 * extent data disk byte 12845056 nr 102400
758 * extent data offset 20480 nr 20480 ram 102400
759 *
760 * key (257 EXTENT_DATA 819200)
761 * extent data disk byte 12845056 nr 102400
762 * extent data offset 0 nr 102400 ram 102400
763 *
764 * Where the second one fully references the 100K extent
765 * that starts at disk byte 12845056, and the log tree
766 * has a single csum item that covers the entire range
767 * of the extent:
768 *
769 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
770 *
771 * After the first file extent item is replayed, the
772 * csum tree gets the following csum item:
773 *
774 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
775 *
776 * Which covers the 20K sub-range starting at offset 20K
777 * of our extent. Now when we replay the second file
778 * extent item, if we do not delete existing csum items
779 * that cover any of its blocks, we end up getting two
780 * csum items in our csum tree that overlap each other:
781 *
782 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
783 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
784 *
785 * Which is a problem, because after this anyone trying
786 * to lookup up for the checksum of any block of our
787 * extent starting at an offset of 40K or higher, will
788 * end up looking at the second csum item only, which
789 * does not contain the checksum for any block starting
790 * at offset 40K or higher of our extent.
791 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500792 while (!list_empty(&ordered_sums)) {
793 struct btrfs_ordered_sum *sums;
794 sums = list_entry(ordered_sums.next,
795 struct btrfs_ordered_sum,
796 list);
Josef Bacik36508602013-04-25 16:23:32 -0400797 if (!ret)
Filipe Manana01510492019-12-05 16:58:30 +0000798 ret = btrfs_del_csums(trans,
799 fs_info->csum_root,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400800 sums->bytenr,
801 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100802 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400803 ret = btrfs_csum_file_blocks(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400804 fs_info->csum_root, sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500805 list_del(&sums->list);
806 kfree(sums);
807 }
Josef Bacik36508602013-04-25 16:23:32 -0400808 if (ret)
809 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500810 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200811 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500812 }
813 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
814 /* inline extents are easy, we just overwrite them */
815 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400816 if (ret)
817 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500818 }
819
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000820 inode_add_bytes(inode, nbytes);
Filipe Manana3168021c2017-02-01 14:58:02 +0000821update_inode:
Tsutomu Itohb9959292012-06-25 21:25:22 -0600822 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400823out:
824 if (inode)
825 iput(inode);
826 return ret;
827}
828
829/*
830 * when cleaning up conflicts between the directory names in the
831 * subvolume, directory names in the log and directory names in the
832 * inode back references, we may have to unlink inodes from directories.
833 *
834 * This is a helper function to do the unlink of a specific directory
835 * item
836 */
837static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
838 struct btrfs_root *root,
839 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200840 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400841 struct btrfs_dir_item *di)
842{
843 struct inode *inode;
844 char *name;
845 int name_len;
846 struct extent_buffer *leaf;
847 struct btrfs_key location;
848 int ret;
849
850 leaf = path->nodes[0];
851
852 btrfs_dir_item_key_to_cpu(leaf, di, &location);
853 name_len = btrfs_dir_name_len(leaf, di);
854 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000855 if (!name)
856 return -ENOMEM;
857
Chris Masone02119d2008-09-05 16:13:11 -0400858 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200859 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400860
861 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000862 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400863 ret = -EIO;
864 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000865 }
Chris Masone02119d2008-09-05 16:13:11 -0400866
Yan Zhengec051c02009-01-05 15:43:42 -0500867 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400868 if (ret)
869 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400870
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200871 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
872 name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400873 if (ret)
874 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100875 else
Nikolay Borisove5c304e62018-02-07 17:55:43 +0200876 ret = btrfs_run_delayed_items(trans);
Josef Bacik36508602013-04-25 16:23:32 -0400877out:
878 kfree(name);
879 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400880 return ret;
881}
882
883/*
884 * helper function to see if a given name and sequence number found
885 * in an inode back reference are already in a directory and correctly
886 * point to this inode
887 */
888static noinline int inode_in_dir(struct btrfs_root *root,
889 struct btrfs_path *path,
890 u64 dirid, u64 objectid, u64 index,
891 const char *name, int name_len)
892{
893 struct btrfs_dir_item *di;
894 struct btrfs_key location;
895 int match = 0;
896
897 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
898 index, name, name_len, 0);
899 if (di && !IS_ERR(di)) {
900 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
901 if (location.objectid != objectid)
902 goto out;
903 } else
904 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200905 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400906
907 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
908 if (di && !IS_ERR(di)) {
909 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
910 if (location.objectid != objectid)
911 goto out;
912 } else
913 goto out;
914 match = 1;
915out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200916 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400917 return match;
918}
919
920/*
921 * helper function to check a log tree for a named back reference in
922 * an inode. This is used to decide if a back reference that is
923 * found in the subvolume conflicts with what we find in the log.
924 *
925 * inode backreferences may have multiple refs in a single item,
926 * during replay we process one reference at a time, and we don't
927 * want to delete valid links to a file from the subvolume if that
928 * link is also in the log.
929 */
930static noinline int backref_in_log(struct btrfs_root *log,
931 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700932 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000933 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400934{
935 struct btrfs_path *path;
936 struct btrfs_inode_ref *ref;
937 unsigned long ptr;
938 unsigned long ptr_end;
939 unsigned long name_ptr;
940 int found_name_len;
941 int item_size;
942 int ret;
943 int match = 0;
944
945 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000946 if (!path)
947 return -ENOMEM;
948
Chris Masone02119d2008-09-05 16:13:11 -0400949 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
950 if (ret != 0)
951 goto out;
952
Chris Masone02119d2008-09-05 16:13:11 -0400953 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700954
955 if (key->type == BTRFS_INODE_EXTREF_KEY) {
Filipe Manana1f250e92018-02-28 15:56:10 +0000956 if (btrfs_find_name_in_ext_backref(path->nodes[0],
957 path->slots[0],
958 ref_objectid,
Mark Fashehf1863732012-08-08 11:32:27 -0700959 name, namelen, NULL))
960 match = 1;
961
962 goto out;
963 }
964
965 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400966 ptr_end = ptr + item_size;
967 while (ptr < ptr_end) {
968 ref = (struct btrfs_inode_ref *)ptr;
969 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
970 if (found_name_len == namelen) {
971 name_ptr = (unsigned long)(ref + 1);
972 ret = memcmp_extent_buffer(path->nodes[0], name,
973 name_ptr, namelen);
974 if (ret == 0) {
975 match = 1;
976 goto out;
977 }
978 }
979 ptr = (unsigned long)(ref + 1) + found_name_len;
980 }
981out:
982 btrfs_free_path(path);
983 return match;
984}
985
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700986static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
987 struct btrfs_root *root,
988 struct btrfs_path *path,
989 struct btrfs_root *log_root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +0200990 struct btrfs_inode *dir,
991 struct btrfs_inode *inode,
Mark Fashehf1863732012-08-08 11:32:27 -0700992 u64 inode_objectid, u64 parent_objectid,
993 u64 ref_index, char *name, int namelen,
994 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700995{
996 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700997 char *victim_name;
998 int victim_name_len;
999 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001000 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -07001001 struct btrfs_key search_key;
1002 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001003
Mark Fashehf1863732012-08-08 11:32:27 -07001004again:
1005 /* Search old style refs */
1006 search_key.objectid = inode_objectid;
1007 search_key.type = BTRFS_INODE_REF_KEY;
1008 search_key.offset = parent_objectid;
1009 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001010 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001011 struct btrfs_inode_ref *victim_ref;
1012 unsigned long ptr;
1013 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001014
1015 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001016
1017 /* are we trying to overwrite a back ref for the root directory
1018 * if so, just jump out, we're done
1019 */
Mark Fashehf1863732012-08-08 11:32:27 -07001020 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001021 return 1;
1022
1023 /* check all the names in this back reference to see
1024 * if they are in the log. if so, we allow them to stay
1025 * otherwise they must be unlinked as a conflict
1026 */
1027 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1028 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1029 while (ptr < ptr_end) {
1030 victim_ref = (struct btrfs_inode_ref *)ptr;
1031 victim_name_len = btrfs_inode_ref_name_len(leaf,
1032 victim_ref);
1033 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001034 if (!victim_name)
1035 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001036
1037 read_extent_buffer(leaf, victim_name,
1038 (unsigned long)(victim_ref + 1),
1039 victim_name_len);
1040
Mark Fashehf1863732012-08-08 11:32:27 -07001041 if (!backref_in_log(log_root, &search_key,
1042 parent_objectid,
1043 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001044 victim_name_len)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001045 inc_nlink(&inode->vfs_inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001046 btrfs_release_path(path);
1047
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001048 ret = btrfs_unlink_inode(trans, root, dir, inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001049 victim_name, victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001050 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001051 if (ret)
1052 return ret;
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001053 ret = btrfs_run_delayed_items(trans);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001054 if (ret)
1055 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001056 *search_done = 1;
1057 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001058 }
1059 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001060
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001061 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1062 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001063
1064 /*
1065 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001066 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001067 */
1068 *search_done = 1;
1069 }
1070 btrfs_release_path(path);
1071
Mark Fashehf1863732012-08-08 11:32:27 -07001072 /* Same search but for extended refs */
1073 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1074 inode_objectid, parent_objectid, 0,
1075 0);
1076 if (!IS_ERR_OR_NULL(extref)) {
1077 u32 item_size;
1078 u32 cur_offset = 0;
1079 unsigned long base;
1080 struct inode *victim_parent;
1081
1082 leaf = path->nodes[0];
1083
1084 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1085 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1086
1087 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001088 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001089
1090 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1091
1092 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1093 goto next;
1094
1095 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001096 if (!victim_name)
1097 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001098 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1099 victim_name_len);
1100
1101 search_key.objectid = inode_objectid;
1102 search_key.type = BTRFS_INODE_EXTREF_KEY;
1103 search_key.offset = btrfs_extref_hash(parent_objectid,
1104 victim_name,
1105 victim_name_len);
1106 ret = 0;
1107 if (!backref_in_log(log_root, &search_key,
1108 parent_objectid, victim_name,
1109 victim_name_len)) {
1110 ret = -ENOENT;
1111 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001112 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001113 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001114 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001115 btrfs_release_path(path);
1116
1117 ret = btrfs_unlink_inode(trans, root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001118 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001119 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001120 victim_name,
1121 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001122 if (!ret)
1123 ret = btrfs_run_delayed_items(
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001124 trans);
Mark Fashehf1863732012-08-08 11:32:27 -07001125 }
Mark Fashehf1863732012-08-08 11:32:27 -07001126 iput(victim_parent);
1127 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001128 if (ret)
1129 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001130 *search_done = 1;
1131 goto again;
1132 }
1133 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001134next:
1135 cur_offset += victim_name_len + sizeof(*extref);
1136 }
1137 *search_done = 1;
1138 }
1139 btrfs_release_path(path);
1140
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001141 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001142 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001143 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001144 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001145 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001146 if (ret)
1147 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001148 }
1149 btrfs_release_path(path);
1150
1151 /* look for a conflicing name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001152 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001153 name, namelen, 0);
1154 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001155 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001156 if (ret)
1157 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001158 }
1159 btrfs_release_path(path);
1160
1161 return 0;
1162}
Chris Masone02119d2008-09-05 16:13:11 -04001163
Qu Wenruobae15d92017-11-08 08:54:26 +08001164static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1165 u32 *namelen, char **name, u64 *index,
1166 u64 *parent_objectid)
Mark Fashehf1863732012-08-08 11:32:27 -07001167{
1168 struct btrfs_inode_extref *extref;
1169
1170 extref = (struct btrfs_inode_extref *)ref_ptr;
1171
1172 *namelen = btrfs_inode_extref_name_len(eb, extref);
1173 *name = kmalloc(*namelen, GFP_NOFS);
1174 if (*name == NULL)
1175 return -ENOMEM;
1176
1177 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1178 *namelen);
1179
Filipe Manana1f250e92018-02-28 15:56:10 +00001180 if (index)
1181 *index = btrfs_inode_extref_index(eb, extref);
Mark Fashehf1863732012-08-08 11:32:27 -07001182 if (parent_objectid)
1183 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1184
1185 return 0;
1186}
1187
Qu Wenruobae15d92017-11-08 08:54:26 +08001188static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1189 u32 *namelen, char **name, u64 *index)
Mark Fashehf1863732012-08-08 11:32:27 -07001190{
1191 struct btrfs_inode_ref *ref;
1192
1193 ref = (struct btrfs_inode_ref *)ref_ptr;
1194
1195 *namelen = btrfs_inode_ref_name_len(eb, ref);
1196 *name = kmalloc(*namelen, GFP_NOFS);
1197 if (*name == NULL)
1198 return -ENOMEM;
1199
1200 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1201
Filipe Manana1f250e92018-02-28 15:56:10 +00001202 if (index)
1203 *index = btrfs_inode_ref_index(eb, ref);
Mark Fashehf1863732012-08-08 11:32:27 -07001204
1205 return 0;
1206}
1207
Chris Masone02119d2008-09-05 16:13:11 -04001208/*
Filipe Manana1f250e92018-02-28 15:56:10 +00001209 * Take an inode reference item from the log tree and iterate all names from the
1210 * inode reference item in the subvolume tree with the same key (if it exists).
1211 * For any name that is not in the inode reference item from the log tree, do a
1212 * proper unlink of that name (that is, remove its entry from the inode
1213 * reference item and both dir index keys).
1214 */
1215static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1216 struct btrfs_root *root,
1217 struct btrfs_path *path,
1218 struct btrfs_inode *inode,
1219 struct extent_buffer *log_eb,
1220 int log_slot,
1221 struct btrfs_key *key)
1222{
1223 int ret;
1224 unsigned long ref_ptr;
1225 unsigned long ref_end;
1226 struct extent_buffer *eb;
1227
1228again:
1229 btrfs_release_path(path);
1230 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1231 if (ret > 0) {
1232 ret = 0;
1233 goto out;
1234 }
1235 if (ret < 0)
1236 goto out;
1237
1238 eb = path->nodes[0];
1239 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1240 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1241 while (ref_ptr < ref_end) {
1242 char *name = NULL;
1243 int namelen;
1244 u64 parent_id;
1245
1246 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1247 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1248 NULL, &parent_id);
1249 } else {
1250 parent_id = key->offset;
1251 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1252 NULL);
1253 }
1254 if (ret)
1255 goto out;
1256
1257 if (key->type == BTRFS_INODE_EXTREF_KEY)
1258 ret = btrfs_find_name_in_ext_backref(log_eb, log_slot,
1259 parent_id, name,
1260 namelen, NULL);
1261 else
1262 ret = btrfs_find_name_in_backref(log_eb, log_slot, name,
1263 namelen, NULL);
1264
1265 if (!ret) {
1266 struct inode *dir;
1267
1268 btrfs_release_path(path);
1269 dir = read_one_inode(root, parent_id);
1270 if (!dir) {
1271 ret = -ENOENT;
1272 kfree(name);
1273 goto out;
1274 }
1275 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1276 inode, name, namelen);
1277 kfree(name);
1278 iput(dir);
1279 if (ret)
1280 goto out;
1281 goto again;
1282 }
1283
1284 kfree(name);
1285 ref_ptr += namelen;
1286 if (key->type == BTRFS_INODE_EXTREF_KEY)
1287 ref_ptr += sizeof(struct btrfs_inode_extref);
1288 else
1289 ref_ptr += sizeof(struct btrfs_inode_ref);
1290 }
1291 ret = 0;
1292 out:
1293 btrfs_release_path(path);
1294 return ret;
1295}
1296
Filipe Manana0d836392018-07-20 10:59:06 +01001297static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
1298 const u8 ref_type, const char *name,
1299 const int namelen)
1300{
1301 struct btrfs_key key;
1302 struct btrfs_path *path;
1303 const u64 parent_id = btrfs_ino(BTRFS_I(dir));
1304 int ret;
1305
1306 path = btrfs_alloc_path();
1307 if (!path)
1308 return -ENOMEM;
1309
1310 key.objectid = btrfs_ino(BTRFS_I(inode));
1311 key.type = ref_type;
1312 if (key.type == BTRFS_INODE_REF_KEY)
1313 key.offset = parent_id;
1314 else
1315 key.offset = btrfs_extref_hash(parent_id, name, namelen);
1316
1317 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
1318 if (ret < 0)
1319 goto out;
1320 if (ret > 0) {
1321 ret = 0;
1322 goto out;
1323 }
1324 if (key.type == BTRFS_INODE_EXTREF_KEY)
1325 ret = btrfs_find_name_in_ext_backref(path->nodes[0],
1326 path->slots[0], parent_id,
1327 name, namelen, NULL);
1328 else
1329 ret = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
1330 name, namelen, NULL);
1331
1332out:
1333 btrfs_free_path(path);
1334 return ret;
1335}
1336
Filipe Manana1f250e92018-02-28 15:56:10 +00001337/*
Chris Masone02119d2008-09-05 16:13:11 -04001338 * replay one inode back reference item found in the log tree.
1339 * eb, slot and key refer to the buffer and key found in the log tree.
1340 * root is the destination we are replaying into, and path is for temp
1341 * use by this function. (it should be released on return).
1342 */
1343static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1344 struct btrfs_root *root,
1345 struct btrfs_root *log,
1346 struct btrfs_path *path,
1347 struct extent_buffer *eb, int slot,
1348 struct btrfs_key *key)
1349{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001350 struct inode *dir = NULL;
1351 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001352 unsigned long ref_ptr;
1353 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001354 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001355 int namelen;
1356 int ret;
liuboc622ae62011-03-26 08:01:12 -04001357 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001358 int log_ref_ver = 0;
1359 u64 parent_objectid;
1360 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001361 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001362 int ref_struct_size;
1363
1364 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1365 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1366
1367 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1368 struct btrfs_inode_extref *r;
1369
1370 ref_struct_size = sizeof(struct btrfs_inode_extref);
1371 log_ref_ver = 1;
1372 r = (struct btrfs_inode_extref *)ref_ptr;
1373 parent_objectid = btrfs_inode_extref_parent(eb, r);
1374 } else {
1375 ref_struct_size = sizeof(struct btrfs_inode_ref);
1376 parent_objectid = key->offset;
1377 }
1378 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001379
Chris Masone02119d2008-09-05 16:13:11 -04001380 /*
1381 * it is possible that we didn't log all the parent directories
1382 * for a given inode. If we don't find the dir, just don't
1383 * copy the back ref in. The link count fixup code will take
1384 * care of the rest
1385 */
Mark Fashehf1863732012-08-08 11:32:27 -07001386 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001387 if (!dir) {
1388 ret = -ENOENT;
1389 goto out;
1390 }
Chris Masone02119d2008-09-05 16:13:11 -04001391
Mark Fashehf1863732012-08-08 11:32:27 -07001392 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001393 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001394 ret = -EIO;
1395 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001396 }
Chris Masone02119d2008-09-05 16:13:11 -04001397
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001398 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001399 if (log_ref_ver) {
Qu Wenruobae15d92017-11-08 08:54:26 +08001400 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1401 &ref_index, &parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001402 /*
1403 * parent object can change from one array
1404 * item to another.
1405 */
1406 if (!dir)
1407 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001408 if (!dir) {
1409 ret = -ENOENT;
1410 goto out;
1411 }
Mark Fashehf1863732012-08-08 11:32:27 -07001412 } else {
Qu Wenruobae15d92017-11-08 08:54:26 +08001413 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1414 &ref_index);
Mark Fashehf1863732012-08-08 11:32:27 -07001415 }
1416 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001417 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001418
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001419 /* if we already have a perfect match, we're done */
David Sterbaf85b7372017-01-20 14:54:07 +01001420 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1421 btrfs_ino(BTRFS_I(inode)), ref_index,
1422 name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001423 /*
1424 * look for a conflicting back reference in the
1425 * metadata. if we find one we have to unlink that name
1426 * of the file before we add our new link. Later on, we
1427 * overwrite any existing back reference, and we don't
1428 * want to create dangling pointers in the directory.
1429 */
Chris Masone02119d2008-09-05 16:13:11 -04001430
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001431 if (!search_done) {
1432 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001433 BTRFS_I(dir),
David Sterbad75eefd2017-02-10 20:20:19 +01001434 BTRFS_I(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001435 inode_objectid,
1436 parent_objectid,
1437 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001438 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001439 if (ret) {
1440 if (ret == 1)
1441 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001442 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001443 }
Chris Masone02119d2008-09-05 16:13:11 -04001444 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001445
Filipe Manana0d836392018-07-20 10:59:06 +01001446 /*
1447 * If a reference item already exists for this inode
1448 * with the same parent and name, but different index,
1449 * drop it and the corresponding directory index entries
1450 * from the parent before adding the new reference item
1451 * and dir index entries, otherwise we would fail with
1452 * -EEXIST returned from btrfs_add_link() below.
1453 */
1454 ret = btrfs_inode_ref_exists(inode, dir, key->type,
1455 name, namelen);
1456 if (ret > 0) {
1457 ret = btrfs_unlink_inode(trans, root,
1458 BTRFS_I(dir),
1459 BTRFS_I(inode),
1460 name, namelen);
1461 /*
1462 * If we dropped the link count to 0, bump it so
1463 * that later the iput() on the inode will not
1464 * free it. We will fixup the link count later.
1465 */
1466 if (!ret && inode->i_nlink == 0)
1467 inc_nlink(inode);
1468 }
1469 if (ret < 0)
1470 goto out;
1471
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001472 /* insert our name */
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001473 ret = btrfs_add_link(trans, BTRFS_I(dir),
1474 BTRFS_I(inode),
1475 name, namelen, 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001476 if (ret)
1477 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001478
1479 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001480 }
liuboc622ae62011-03-26 08:01:12 -04001481
Mark Fashehf1863732012-08-08 11:32:27 -07001482 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001483 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001484 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001485 if (log_ref_ver) {
1486 iput(dir);
1487 dir = NULL;
1488 }
Chris Masone02119d2008-09-05 16:13:11 -04001489 }
Chris Masone02119d2008-09-05 16:13:11 -04001490
Filipe Manana1f250e92018-02-28 15:56:10 +00001491 /*
1492 * Before we overwrite the inode reference item in the subvolume tree
1493 * with the item from the log tree, we must unlink all names from the
1494 * parent directory that are in the subvolume's tree inode reference
1495 * item, otherwise we end up with an inconsistent subvolume tree where
1496 * dir index entries exist for a name but there is no inode reference
1497 * item with the same name.
1498 */
1499 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1500 key);
1501 if (ret)
1502 goto out;
1503
Chris Masone02119d2008-09-05 16:13:11 -04001504 /* finally write the back reference in the inode */
1505 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001506out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001507 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001508 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001509 iput(dir);
1510 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001511 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001512}
1513
Yan, Zhengc71bf092009-11-12 09:34:40 +00001514static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001515 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001516{
1517 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001518
David Sterba9c4f61f2015-01-02 19:12:57 +01001519 ret = btrfs_insert_orphan_item(trans, root, ino);
1520 if (ret == -EEXIST)
1521 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001522
Yan, Zhengc71bf092009-11-12 09:34:40 +00001523 return ret;
1524}
1525
Mark Fashehf1863732012-08-08 11:32:27 -07001526static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001527 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001528{
Mark Fashehf1863732012-08-08 11:32:27 -07001529 int ret = 0;
1530 int name_len;
1531 unsigned int nlink = 0;
1532 u32 item_size;
1533 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001534 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001535 u64 offset = 0;
1536 unsigned long ptr;
1537 struct btrfs_inode_extref *extref;
1538 struct extent_buffer *leaf;
1539
1540 while (1) {
1541 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1542 &extref, &offset);
1543 if (ret)
1544 break;
1545
1546 leaf = path->nodes[0];
1547 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1548 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001549 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001550
1551 while (cur_offset < item_size) {
1552 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1553 name_len = btrfs_inode_extref_name_len(leaf, extref);
1554
1555 nlink++;
1556
1557 cur_offset += name_len + sizeof(*extref);
1558 }
1559
1560 offset++;
1561 btrfs_release_path(path);
1562 }
1563 btrfs_release_path(path);
1564
Filipe Manana2c2c4522015-01-13 16:40:04 +00001565 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001566 return ret;
1567 return nlink;
1568}
1569
1570static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001571 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001572{
Chris Masone02119d2008-09-05 16:13:11 -04001573 int ret;
1574 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001575 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001576 unsigned long ptr;
1577 unsigned long ptr_end;
1578 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001579 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001580
Li Zefan33345d012011-04-20 10:31:50 +08001581 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001582 key.type = BTRFS_INODE_REF_KEY;
1583 key.offset = (u64)-1;
1584
Chris Masond3977122009-01-05 21:25:51 -05001585 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001586 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1587 if (ret < 0)
1588 break;
1589 if (ret > 0) {
1590 if (path->slots[0] == 0)
1591 break;
1592 path->slots[0]--;
1593 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001594process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001595 btrfs_item_key_to_cpu(path->nodes[0], &key,
1596 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001597 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001598 key.type != BTRFS_INODE_REF_KEY)
1599 break;
1600 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1601 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1602 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001603 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001604 struct btrfs_inode_ref *ref;
1605
1606 ref = (struct btrfs_inode_ref *)ptr;
1607 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1608 ref);
1609 ptr = (unsigned long)(ref + 1) + name_len;
1610 nlink++;
1611 }
1612
1613 if (key.offset == 0)
1614 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001615 if (path->slots[0] > 0) {
1616 path->slots[0]--;
1617 goto process_slot;
1618 }
Chris Masone02119d2008-09-05 16:13:11 -04001619 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001620 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001621 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001622 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001623
1624 return nlink;
1625}
1626
1627/*
1628 * There are a few corners where the link count of the file can't
1629 * be properly maintained during replay. So, instead of adding
1630 * lots of complexity to the log code, we just scan the backrefs
1631 * for any file that has been through replay.
1632 *
1633 * The scan will update the link count on the inode to reflect the
1634 * number of back refs found. If it goes down to zero, the iput
1635 * will free the inode.
1636 */
1637static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1638 struct btrfs_root *root,
1639 struct inode *inode)
1640{
1641 struct btrfs_path *path;
1642 int ret;
1643 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001644 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001645
1646 path = btrfs_alloc_path();
1647 if (!path)
1648 return -ENOMEM;
1649
Nikolay Borisovf329e312017-01-18 00:31:50 +02001650 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001651 if (ret < 0)
1652 goto out;
1653
1654 nlink = ret;
1655
Nikolay Borisov36283652017-01-18 00:31:49 +02001656 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001657 if (ret < 0)
1658 goto out;
1659
1660 nlink += ret;
1661
1662 ret = 0;
1663
Chris Masone02119d2008-09-05 16:13:11 -04001664 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001665 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001666 btrfs_update_inode(trans, root, inode);
1667 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001668 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001669
Yan, Zhengc71bf092009-11-12 09:34:40 +00001670 if (inode->i_nlink == 0) {
1671 if (S_ISDIR(inode->i_mode)) {
1672 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001673 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001674 if (ret)
1675 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001676 }
Li Zefan33345d012011-04-20 10:31:50 +08001677 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001678 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001679
Mark Fashehf1863732012-08-08 11:32:27 -07001680out:
1681 btrfs_free_path(path);
1682 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001683}
1684
1685static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1686 struct btrfs_root *root,
1687 struct btrfs_path *path)
1688{
1689 int ret;
1690 struct btrfs_key key;
1691 struct inode *inode;
1692
1693 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1694 key.type = BTRFS_ORPHAN_ITEM_KEY;
1695 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001696 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001697 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1698 if (ret < 0)
1699 break;
1700
1701 if (ret == 1) {
1702 if (path->slots[0] == 0)
1703 break;
1704 path->slots[0]--;
1705 }
1706
1707 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1708 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1709 key.type != BTRFS_ORPHAN_ITEM_KEY)
1710 break;
1711
1712 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001713 if (ret)
1714 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001715
David Sterbab3b4aa72011-04-21 01:20:15 +02001716 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001717 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001718 if (!inode)
1719 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001720
1721 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001722 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001723 if (ret)
1724 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001725
Chris Mason12fcfd22009-03-24 10:24:20 -04001726 /*
1727 * fixup on a directory may create new entries,
1728 * make sure we always look for the highset possible
1729 * offset
1730 */
1731 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001732 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001733 ret = 0;
1734out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001735 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001736 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001737}
1738
1739
1740/*
1741 * record a given inode in the fixup dir so we can check its link
1742 * count when replay is done. The link count is incremented here
1743 * so the inode won't go away until we check it
1744 */
1745static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1746 struct btrfs_root *root,
1747 struct btrfs_path *path,
1748 u64 objectid)
1749{
1750 struct btrfs_key key;
1751 int ret = 0;
1752 struct inode *inode;
1753
1754 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001755 if (!inode)
1756 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001757
1758 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001759 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001760 key.offset = objectid;
1761
1762 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1763
David Sterbab3b4aa72011-04-21 01:20:15 +02001764 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001765 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001766 if (!inode->i_nlink)
1767 set_nlink(inode, 1);
1768 else
Zach Brown8b558c52013-10-16 12:10:34 -07001769 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001770 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001771 } else if (ret == -EEXIST) {
1772 ret = 0;
1773 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001774 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001775 }
1776 iput(inode);
1777
1778 return ret;
1779}
1780
1781/*
1782 * when replaying the log for a directory, we only insert names
1783 * for inodes that actually exist. This means an fsync on a directory
1784 * does not implicitly fsync all the new files in it
1785 */
1786static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1787 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001788 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001789 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001790 struct btrfs_key *location)
1791{
1792 struct inode *inode;
1793 struct inode *dir;
1794 int ret;
1795
1796 inode = read_one_inode(root, location->objectid);
1797 if (!inode)
1798 return -ENOENT;
1799
1800 dir = read_one_inode(root, dirid);
1801 if (!dir) {
1802 iput(inode);
1803 return -EIO;
1804 }
Josef Bacikd5554382013-09-11 14:17:00 -04001805
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001806 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1807 name_len, 1, index);
Chris Masone02119d2008-09-05 16:13:11 -04001808
1809 /* FIXME, put inode into FIXUP list */
1810
1811 iput(inode);
1812 iput(dir);
1813 return ret;
1814}
1815
1816/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001817 * Return true if an inode reference exists in the log for the given name,
1818 * inode and parent inode.
1819 */
1820static bool name_in_log_ref(struct btrfs_root *log_root,
1821 const char *name, const int name_len,
1822 const u64 dirid, const u64 ino)
1823{
1824 struct btrfs_key search_key;
1825
1826 search_key.objectid = ino;
1827 search_key.type = BTRFS_INODE_REF_KEY;
1828 search_key.offset = dirid;
1829 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1830 return true;
1831
1832 search_key.type = BTRFS_INODE_EXTREF_KEY;
1833 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1834 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1835 return true;
1836
1837 return false;
1838}
1839
1840/*
Chris Masone02119d2008-09-05 16:13:11 -04001841 * take a single entry in a log directory item and replay it into
1842 * the subvolume.
1843 *
1844 * if a conflicting item exists in the subdirectory already,
1845 * the inode it points to is unlinked and put into the link count
1846 * fix up tree.
1847 *
1848 * If a name from the log points to a file or directory that does
1849 * not exist in the FS, it is skipped. fsyncs on directories
1850 * do not force down inodes inside that directory, just changes to the
1851 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001852 *
1853 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1854 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001855 */
1856static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1857 struct btrfs_root *root,
1858 struct btrfs_path *path,
1859 struct extent_buffer *eb,
1860 struct btrfs_dir_item *di,
1861 struct btrfs_key *key)
1862{
1863 char *name;
1864 int name_len;
1865 struct btrfs_dir_item *dst_di;
1866 struct btrfs_key found_key;
1867 struct btrfs_key log_key;
1868 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001869 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001870 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001871 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001872 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001873 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001874
1875 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001876 if (!dir)
1877 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001878
1879 name_len = btrfs_dir_name_len(eb, di);
1880 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001881 if (!name) {
1882 ret = -ENOMEM;
1883 goto out;
1884 }
liubo2a29edc2011-01-26 06:22:08 +00001885
Chris Masone02119d2008-09-05 16:13:11 -04001886 log_type = btrfs_dir_type(eb, di);
1887 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1888 name_len);
1889
1890 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001891 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1892 if (exists == 0)
1893 exists = 1;
1894 else
1895 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001896 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001897
Chris Masone02119d2008-09-05 16:13:11 -04001898 if (key->type == BTRFS_DIR_ITEM_KEY) {
1899 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1900 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001901 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001902 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1903 key->objectid,
1904 key->offset, name,
1905 name_len, 1);
1906 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001907 /* Corruption */
1908 ret = -EINVAL;
1909 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001910 }
David Sterbac7040052011-04-19 18:00:01 +02001911 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001912 /* we need a sequence number to insert, so we only
1913 * do inserts for the BTRFS_DIR_INDEX_KEY types
1914 */
1915 if (key->type != BTRFS_DIR_INDEX_KEY)
1916 goto out;
1917 goto insert;
1918 }
1919
1920 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1921 /* the existing item matches the logged item */
1922 if (found_key.objectid == log_key.objectid &&
1923 found_key.type == log_key.type &&
1924 found_key.offset == log_key.offset &&
1925 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001926 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001927 goto out;
1928 }
1929
1930 /*
1931 * don't drop the conflicting directory entry if the inode
1932 * for the new entry doesn't exist
1933 */
Chris Mason4bef0842008-09-08 11:18:08 -04001934 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001935 goto out;
1936
Nikolay Borisov207e7d92017-01-18 00:31:45 +02001937 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001938 if (ret)
1939 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001940
1941 if (key->type == BTRFS_DIR_INDEX_KEY)
1942 goto insert;
1943out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001944 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001945 if (!ret && update_size) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02001946 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
Josef Bacikd5554382013-09-11 14:17:00 -04001947 ret = btrfs_update_inode(trans, root, dir);
1948 }
Chris Masone02119d2008-09-05 16:13:11 -04001949 kfree(name);
1950 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001951 if (!ret && name_added)
1952 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001953 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001954
1955insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001956 if (name_in_log_ref(root->log_root, name, name_len,
1957 key->objectid, log_key.objectid)) {
1958 /* The dentry will be added later. */
1959 ret = 0;
1960 update_size = false;
1961 goto out;
1962 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001963 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001964 ret = insert_one_name(trans, root, key->objectid, key->offset,
1965 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001966 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001967 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001968 if (!ret)
1969 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001970 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001971 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001972 goto out;
1973}
1974
1975/*
1976 * find all the names in a directory item and reconcile them into
1977 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1978 * one name in a directory item, but the same code gets used for
1979 * both directory index types
1980 */
1981static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1982 struct btrfs_root *root,
1983 struct btrfs_path *path,
1984 struct extent_buffer *eb, int slot,
1985 struct btrfs_key *key)
1986{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001987 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001988 u32 item_size = btrfs_item_size_nr(eb, slot);
1989 struct btrfs_dir_item *di;
1990 int name_len;
1991 unsigned long ptr;
1992 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001993 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001994
1995 ptr = btrfs_item_ptr_offset(eb, slot);
1996 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001997 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001998 di = (struct btrfs_dir_item *)ptr;
1999 name_len = btrfs_dir_name_len(eb, di);
2000 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01002001 if (ret < 0)
2002 break;
Chris Masone02119d2008-09-05 16:13:11 -04002003 ptr = (unsigned long)(di + 1);
2004 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002005
2006 /*
2007 * If this entry refers to a non-directory (directories can not
2008 * have a link count > 1) and it was added in the transaction
2009 * that was not committed, make sure we fixup the link count of
2010 * the inode it the entry points to. Otherwise something like
2011 * the following would result in a directory pointing to an
2012 * inode with a wrong link that does not account for this dir
2013 * entry:
2014 *
2015 * mkdir testdir
2016 * touch testdir/foo
2017 * touch testdir/bar
2018 * sync
2019 *
2020 * ln testdir/bar testdir/bar_link
2021 * ln testdir/foo testdir/foo_link
2022 * xfs_io -c "fsync" testdir/bar
2023 *
2024 * <power failure>
2025 *
2026 * mount fs, log replay happens
2027 *
2028 * File foo would remain with a link count of 1 when it has two
2029 * entries pointing to it in the directory testdir. This would
2030 * make it impossible to ever delete the parent directory has
2031 * it would result in stale dentries that can never be deleted.
2032 */
2033 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
2034 struct btrfs_key di_key;
2035
2036 if (!fixup_path) {
2037 fixup_path = btrfs_alloc_path();
2038 if (!fixup_path) {
2039 ret = -ENOMEM;
2040 break;
2041 }
2042 }
2043
2044 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2045 ret = link_to_fixup_dir(trans, root, fixup_path,
2046 di_key.objectid);
2047 if (ret)
2048 break;
2049 }
2050 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002051 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01002052 btrfs_free_path(fixup_path);
2053 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002054}
2055
2056/*
2057 * directory replay has two parts. There are the standard directory
2058 * items in the log copied from the subvolume, and range items
2059 * created in the log while the subvolume was logged.
2060 *
2061 * The range items tell us which parts of the key space the log
2062 * is authoritative for. During replay, if a key in the subvolume
2063 * directory is in a logged range item, but not actually in the log
2064 * that means it was deleted from the directory before the fsync
2065 * and should be removed.
2066 */
2067static noinline int find_dir_range(struct btrfs_root *root,
2068 struct btrfs_path *path,
2069 u64 dirid, int key_type,
2070 u64 *start_ret, u64 *end_ret)
2071{
2072 struct btrfs_key key;
2073 u64 found_end;
2074 struct btrfs_dir_log_item *item;
2075 int ret;
2076 int nritems;
2077
2078 if (*start_ret == (u64)-1)
2079 return 1;
2080
2081 key.objectid = dirid;
2082 key.type = key_type;
2083 key.offset = *start_ret;
2084
2085 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2086 if (ret < 0)
2087 goto out;
2088 if (ret > 0) {
2089 if (path->slots[0] == 0)
2090 goto out;
2091 path->slots[0]--;
2092 }
2093 if (ret != 0)
2094 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2095
2096 if (key.type != key_type || key.objectid != dirid) {
2097 ret = 1;
2098 goto next;
2099 }
2100 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2101 struct btrfs_dir_log_item);
2102 found_end = btrfs_dir_log_end(path->nodes[0], item);
2103
2104 if (*start_ret >= key.offset && *start_ret <= found_end) {
2105 ret = 0;
2106 *start_ret = key.offset;
2107 *end_ret = found_end;
2108 goto out;
2109 }
2110 ret = 1;
2111next:
2112 /* check the next slot in the tree to see if it is a valid item */
2113 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08002114 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04002115 if (path->slots[0] >= nritems) {
2116 ret = btrfs_next_leaf(root, path);
2117 if (ret)
2118 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002119 }
2120
2121 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2122
2123 if (key.type != key_type || key.objectid != dirid) {
2124 ret = 1;
2125 goto out;
2126 }
2127 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2128 struct btrfs_dir_log_item);
2129 found_end = btrfs_dir_log_end(path->nodes[0], item);
2130 *start_ret = key.offset;
2131 *end_ret = found_end;
2132 ret = 0;
2133out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002134 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002135 return ret;
2136}
2137
2138/*
2139 * this looks for a given directory item in the log. If the directory
2140 * item is not in the log, the item is removed and the inode it points
2141 * to is unlinked
2142 */
2143static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2144 struct btrfs_root *root,
2145 struct btrfs_root *log,
2146 struct btrfs_path *path,
2147 struct btrfs_path *log_path,
2148 struct inode *dir,
2149 struct btrfs_key *dir_key)
2150{
2151 int ret;
2152 struct extent_buffer *eb;
2153 int slot;
2154 u32 item_size;
2155 struct btrfs_dir_item *di;
2156 struct btrfs_dir_item *log_di;
2157 int name_len;
2158 unsigned long ptr;
2159 unsigned long ptr_end;
2160 char *name;
2161 struct inode *inode;
2162 struct btrfs_key location;
2163
2164again:
2165 eb = path->nodes[0];
2166 slot = path->slots[0];
2167 item_size = btrfs_item_size_nr(eb, slot);
2168 ptr = btrfs_item_ptr_offset(eb, slot);
2169 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002170 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002171 di = (struct btrfs_dir_item *)ptr;
2172 name_len = btrfs_dir_name_len(eb, di);
2173 name = kmalloc(name_len, GFP_NOFS);
2174 if (!name) {
2175 ret = -ENOMEM;
2176 goto out;
2177 }
2178 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2179 name_len);
2180 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002181 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002182 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2183 dir_key->objectid,
2184 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002185 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002186 log_di = btrfs_lookup_dir_index_item(trans, log,
2187 log_path,
2188 dir_key->objectid,
2189 dir_key->offset,
2190 name, name_len, 0);
2191 }
Al Viro8d9e2202018-07-29 23:04:46 +01002192 if (!log_di || log_di == ERR_PTR(-ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002193 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002194 btrfs_release_path(path);
2195 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002196 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002197 if (!inode) {
2198 kfree(name);
2199 return -EIO;
2200 }
Chris Masone02119d2008-09-05 16:13:11 -04002201
2202 ret = link_to_fixup_dir(trans, root,
2203 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002204 if (ret) {
2205 kfree(name);
2206 iput(inode);
2207 goto out;
2208 }
2209
Zach Brown8b558c52013-10-16 12:10:34 -07002210 inc_nlink(inode);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002211 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2212 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002213 if (!ret)
Nikolay Borisove5c304e62018-02-07 17:55:43 +02002214 ret = btrfs_run_delayed_items(trans);
Chris Masone02119d2008-09-05 16:13:11 -04002215 kfree(name);
2216 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002217 if (ret)
2218 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002219
2220 /* there might still be more names under this key
2221 * check and repeat if required
2222 */
2223 ret = btrfs_search_slot(NULL, root, dir_key, path,
2224 0, 0);
2225 if (ret == 0)
2226 goto again;
2227 ret = 0;
2228 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002229 } else if (IS_ERR(log_di)) {
2230 kfree(name);
2231 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002232 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002233 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002234 kfree(name);
2235
2236 ptr = (unsigned long)(di + 1);
2237 ptr += name_len;
2238 }
2239 ret = 0;
2240out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002241 btrfs_release_path(path);
2242 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002243 return ret;
2244}
2245
Filipe Manana4f764e52015-02-23 19:53:35 +00002246static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2247 struct btrfs_root *root,
2248 struct btrfs_root *log,
2249 struct btrfs_path *path,
2250 const u64 ino)
2251{
2252 struct btrfs_key search_key;
2253 struct btrfs_path *log_path;
2254 int i;
2255 int nritems;
2256 int ret;
2257
2258 log_path = btrfs_alloc_path();
2259 if (!log_path)
2260 return -ENOMEM;
2261
2262 search_key.objectid = ino;
2263 search_key.type = BTRFS_XATTR_ITEM_KEY;
2264 search_key.offset = 0;
2265again:
2266 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2267 if (ret < 0)
2268 goto out;
2269process_leaf:
2270 nritems = btrfs_header_nritems(path->nodes[0]);
2271 for (i = path->slots[0]; i < nritems; i++) {
2272 struct btrfs_key key;
2273 struct btrfs_dir_item *di;
2274 struct btrfs_dir_item *log_di;
2275 u32 total_size;
2276 u32 cur;
2277
2278 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2279 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2280 ret = 0;
2281 goto out;
2282 }
2283
2284 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2285 total_size = btrfs_item_size_nr(path->nodes[0], i);
2286 cur = 0;
2287 while (cur < total_size) {
2288 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2289 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2290 u32 this_len = sizeof(*di) + name_len + data_len;
2291 char *name;
2292
2293 name = kmalloc(name_len, GFP_NOFS);
2294 if (!name) {
2295 ret = -ENOMEM;
2296 goto out;
2297 }
2298 read_extent_buffer(path->nodes[0], name,
2299 (unsigned long)(di + 1), name_len);
2300
2301 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2302 name, name_len, 0);
2303 btrfs_release_path(log_path);
2304 if (!log_di) {
2305 /* Doesn't exist in log tree, so delete it. */
2306 btrfs_release_path(path);
2307 di = btrfs_lookup_xattr(trans, root, path, ino,
2308 name, name_len, -1);
2309 kfree(name);
2310 if (IS_ERR(di)) {
2311 ret = PTR_ERR(di);
2312 goto out;
2313 }
2314 ASSERT(di);
2315 ret = btrfs_delete_one_dir_name(trans, root,
2316 path, di);
2317 if (ret)
2318 goto out;
2319 btrfs_release_path(path);
2320 search_key = key;
2321 goto again;
2322 }
2323 kfree(name);
2324 if (IS_ERR(log_di)) {
2325 ret = PTR_ERR(log_di);
2326 goto out;
2327 }
2328 cur += this_len;
2329 di = (struct btrfs_dir_item *)((char *)di + this_len);
2330 }
2331 }
2332 ret = btrfs_next_leaf(root, path);
2333 if (ret > 0)
2334 ret = 0;
2335 else if (ret == 0)
2336 goto process_leaf;
2337out:
2338 btrfs_free_path(log_path);
2339 btrfs_release_path(path);
2340 return ret;
2341}
2342
2343
Chris Masone02119d2008-09-05 16:13:11 -04002344/*
2345 * deletion replay happens before we copy any new directory items
2346 * out of the log or out of backreferences from inodes. It
2347 * scans the log to find ranges of keys that log is authoritative for,
2348 * and then scans the directory to find items in those ranges that are
2349 * not present in the log.
2350 *
2351 * Anything we don't find in the log is unlinked and removed from the
2352 * directory.
2353 */
2354static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2355 struct btrfs_root *root,
2356 struct btrfs_root *log,
2357 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002358 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002359{
2360 u64 range_start;
2361 u64 range_end;
2362 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2363 int ret = 0;
2364 struct btrfs_key dir_key;
2365 struct btrfs_key found_key;
2366 struct btrfs_path *log_path;
2367 struct inode *dir;
2368
2369 dir_key.objectid = dirid;
2370 dir_key.type = BTRFS_DIR_ITEM_KEY;
2371 log_path = btrfs_alloc_path();
2372 if (!log_path)
2373 return -ENOMEM;
2374
2375 dir = read_one_inode(root, dirid);
2376 /* it isn't an error if the inode isn't there, that can happen
2377 * because we replay the deletes before we copy in the inode item
2378 * from the log
2379 */
2380 if (!dir) {
2381 btrfs_free_path(log_path);
2382 return 0;
2383 }
2384again:
2385 range_start = 0;
2386 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002387 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002388 if (del_all)
2389 range_end = (u64)-1;
2390 else {
2391 ret = find_dir_range(log, path, dirid, key_type,
2392 &range_start, &range_end);
2393 if (ret != 0)
2394 break;
2395 }
Chris Masone02119d2008-09-05 16:13:11 -04002396
2397 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002398 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002399 int nritems;
2400 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2401 0, 0);
2402 if (ret < 0)
2403 goto out;
2404
2405 nritems = btrfs_header_nritems(path->nodes[0]);
2406 if (path->slots[0] >= nritems) {
2407 ret = btrfs_next_leaf(root, path);
Liu Bob98def72018-04-03 01:59:48 +08002408 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002409 break;
Liu Bob98def72018-04-03 01:59:48 +08002410 else if (ret < 0)
2411 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002412 }
2413 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2414 path->slots[0]);
2415 if (found_key.objectid != dirid ||
2416 found_key.type != dir_key.type)
2417 goto next_type;
2418
2419 if (found_key.offset > range_end)
2420 break;
2421
2422 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002423 log_path, dir,
2424 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002425 if (ret)
2426 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002427 if (found_key.offset == (u64)-1)
2428 break;
2429 dir_key.offset = found_key.offset + 1;
2430 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002431 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002432 if (range_end == (u64)-1)
2433 break;
2434 range_start = range_end + 1;
2435 }
2436
2437next_type:
2438 ret = 0;
2439 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2440 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2441 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002442 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002443 goto again;
2444 }
2445out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002446 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002447 btrfs_free_path(log_path);
2448 iput(dir);
2449 return ret;
2450}
2451
2452/*
2453 * the process_func used to replay items from the log tree. This
2454 * gets called in two different stages. The first stage just looks
2455 * for inodes and makes sure they are all copied into the subvolume.
2456 *
2457 * The second stage copies all the other item types from the log into
2458 * the subvolume. The two stage approach is slower, but gets rid of
2459 * lots of complexity around inodes referencing other inodes that exist
2460 * only in the log (references come from either directory items or inode
2461 * back refs).
2462 */
2463static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +08002464 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -04002465{
2466 int nritems;
2467 struct btrfs_path *path;
2468 struct btrfs_root *root = wc->replay_dest;
2469 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002470 int i;
2471 int ret;
2472
Qu Wenruo581c1762018-03-29 09:08:11 +08002473 ret = btrfs_read_buffer(eb, gen, level, NULL);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002474 if (ret)
2475 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002476
2477 level = btrfs_header_level(eb);
2478
2479 if (level != 0)
2480 return 0;
2481
2482 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002483 if (!path)
2484 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002485
2486 nritems = btrfs_header_nritems(eb);
2487 for (i = 0; i < nritems; i++) {
2488 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002489
2490 /* inode keys are done during the first stage */
2491 if (key.type == BTRFS_INODE_ITEM_KEY &&
2492 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002493 struct btrfs_inode_item *inode_item;
2494 u32 mode;
2495
2496 inode_item = btrfs_item_ptr(eb, i,
2497 struct btrfs_inode_item);
Filipe Manana55f21e12018-10-08 11:12:55 +01002498 /*
2499 * If we have a tmpfile (O_TMPFILE) that got fsync'ed
2500 * and never got linked before the fsync, skip it, as
2501 * replaying it is pointless since it would be deleted
2502 * later. We skip logging tmpfiles, but it's always
2503 * possible we are replaying a log created with a kernel
2504 * that used to log tmpfiles.
2505 */
2506 if (btrfs_inode_nlink(eb, inode_item) == 0) {
2507 wc->ignore_cur_inode = true;
2508 continue;
2509 } else {
2510 wc->ignore_cur_inode = false;
2511 }
Filipe Manana4f764e52015-02-23 19:53:35 +00002512 ret = replay_xattr_deletes(wc->trans, root, log,
2513 path, key.objectid);
2514 if (ret)
2515 break;
Chris Masone02119d2008-09-05 16:13:11 -04002516 mode = btrfs_inode_mode(eb, inode_item);
2517 if (S_ISDIR(mode)) {
2518 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002519 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002520 if (ret)
2521 break;
Chris Masone02119d2008-09-05 16:13:11 -04002522 }
2523 ret = overwrite_item(wc->trans, root, path,
2524 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002525 if (ret)
2526 break;
Chris Masone02119d2008-09-05 16:13:11 -04002527
Filipe Manana471d5572018-04-05 22:55:12 +01002528 /*
2529 * Before replaying extents, truncate the inode to its
2530 * size. We need to do it now and not after log replay
2531 * because before an fsync we can have prealloc extents
2532 * added beyond the inode's i_size. If we did it after,
2533 * through orphan cleanup for example, we would drop
2534 * those prealloc extents just after replaying them.
Chris Masone02119d2008-09-05 16:13:11 -04002535 */
2536 if (S_ISREG(mode)) {
Filipe Manana471d5572018-04-05 22:55:12 +01002537 struct inode *inode;
2538 u64 from;
2539
2540 inode = read_one_inode(root, key.objectid);
2541 if (!inode) {
2542 ret = -EIO;
2543 break;
2544 }
2545 from = ALIGN(i_size_read(inode),
2546 root->fs_info->sectorsize);
2547 ret = btrfs_drop_extents(wc->trans, root, inode,
2548 from, (u64)-1, 1);
Filipe Manana471d5572018-04-05 22:55:12 +01002549 if (!ret) {
Filipe Manana55f21e12018-10-08 11:12:55 +01002550 /* Update the inode's nbytes. */
Filipe Manana471d5572018-04-05 22:55:12 +01002551 ret = btrfs_update_inode(wc->trans,
2552 root, inode);
2553 }
2554 iput(inode);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002555 if (ret)
2556 break;
Chris Masone02119d2008-09-05 16:13:11 -04002557 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002558
Chris Masone02119d2008-09-05 16:13:11 -04002559 ret = link_to_fixup_dir(wc->trans, root,
2560 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002561 if (ret)
2562 break;
Chris Masone02119d2008-09-05 16:13:11 -04002563 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002564
Filipe Manana55f21e12018-10-08 11:12:55 +01002565 if (wc->ignore_cur_inode)
2566 continue;
2567
Josef Bacikdd8e7212013-09-11 11:57:23 -04002568 if (key.type == BTRFS_DIR_INDEX_KEY &&
2569 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2570 ret = replay_one_dir_item(wc->trans, root, path,
2571 eb, i, &key);
2572 if (ret)
2573 break;
2574 }
2575
Chris Masone02119d2008-09-05 16:13:11 -04002576 if (wc->stage < LOG_WALK_REPLAY_ALL)
2577 continue;
2578
2579 /* these keys are simply copied */
2580 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2581 ret = overwrite_item(wc->trans, root, path,
2582 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002583 if (ret)
2584 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002585 } else if (key.type == BTRFS_INODE_REF_KEY ||
2586 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002587 ret = add_inode_ref(wc->trans, root, log, path,
2588 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002589 if (ret && ret != -ENOENT)
2590 break;
2591 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002592 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2593 ret = replay_one_extent(wc->trans, root, path,
2594 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002595 if (ret)
2596 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002597 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002598 ret = replay_one_dir_item(wc->trans, root, path,
2599 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002600 if (ret)
2601 break;
Chris Masone02119d2008-09-05 16:13:11 -04002602 }
2603 }
2604 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002605 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002606}
2607
Chris Masond3977122009-01-05 21:25:51 -05002608static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002609 struct btrfs_root *root,
2610 struct btrfs_path *path, int *level,
2611 struct walk_control *wc)
2612{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002613 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002614 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002615 u64 bytenr;
2616 u64 ptr_gen;
2617 struct extent_buffer *next;
2618 struct extent_buffer *cur;
2619 struct extent_buffer *parent;
2620 u32 blocksize;
2621 int ret = 0;
2622
2623 WARN_ON(*level < 0);
2624 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2625
Chris Masond3977122009-01-05 21:25:51 -05002626 while (*level > 0) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002627 struct btrfs_key first_key;
2628
Chris Masone02119d2008-09-05 16:13:11 -04002629 WARN_ON(*level < 0);
2630 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2631 cur = path->nodes[*level];
2632
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302633 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002634
2635 if (path->slots[*level] >=
2636 btrfs_header_nritems(cur))
2637 break;
2638
2639 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2640 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08002641 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002642 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002643
2644 parent = path->nodes[*level];
2645 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002646
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002647 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002648 if (IS_ERR(next))
2649 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002650
Chris Masone02119d2008-09-05 16:13:11 -04002651 if (*level == 1) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002652 ret = wc->process_func(root, next, wc, ptr_gen,
2653 *level - 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002654 if (ret) {
2655 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002656 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002657 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002658
Chris Masone02119d2008-09-05 16:13:11 -04002659 path->slots[*level]++;
2660 if (wc->free) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002661 ret = btrfs_read_buffer(next, ptr_gen,
2662 *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002663 if (ret) {
2664 free_extent_buffer(next);
2665 return ret;
2666 }
Chris Masone02119d2008-09-05 16:13:11 -04002667
Josef Bacik681ae502013-10-07 15:11:00 -04002668 if (trans) {
2669 btrfs_tree_lock(next);
2670 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002671 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002672 btrfs_wait_tree_block_writeback(next);
2673 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002674 } else {
2675 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2676 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002677 }
Chris Masone02119d2008-09-05 16:13:11 -04002678
Chris Masone02119d2008-09-05 16:13:11 -04002679 WARN_ON(root_owner !=
2680 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002681 ret = btrfs_free_and_pin_reserved_extent(
2682 fs_info, bytenr,
2683 blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002684 if (ret) {
2685 free_extent_buffer(next);
2686 return ret;
2687 }
Chris Masone02119d2008-09-05 16:13:11 -04002688 }
2689 free_extent_buffer(next);
2690 continue;
2691 }
Qu Wenruo581c1762018-03-29 09:08:11 +08002692 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002693 if (ret) {
2694 free_extent_buffer(next);
2695 return ret;
2696 }
Chris Masone02119d2008-09-05 16:13:11 -04002697
2698 WARN_ON(*level <= 0);
2699 if (path->nodes[*level-1])
2700 free_extent_buffer(path->nodes[*level-1]);
2701 path->nodes[*level-1] = next;
2702 *level = btrfs_header_level(next);
2703 path->slots[*level] = 0;
2704 cond_resched();
2705 }
2706 WARN_ON(*level < 0);
2707 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2708
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002709 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002710
2711 cond_resched();
2712 return 0;
2713}
2714
Chris Masond3977122009-01-05 21:25:51 -05002715static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002716 struct btrfs_root *root,
2717 struct btrfs_path *path, int *level,
2718 struct walk_control *wc)
2719{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002720 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002721 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002722 int i;
2723 int slot;
2724 int ret;
2725
Chris Masond3977122009-01-05 21:25:51 -05002726 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002727 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002728 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002729 path->slots[i]++;
2730 *level = i;
2731 WARN_ON(*level == 0);
2732 return 0;
2733 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002734 struct extent_buffer *parent;
2735 if (path->nodes[*level] == root->node)
2736 parent = path->nodes[*level];
2737 else
2738 parent = path->nodes[*level + 1];
2739
2740 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002741 ret = wc->process_func(root, path->nodes[*level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002742 btrfs_header_generation(path->nodes[*level]),
2743 *level);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002744 if (ret)
2745 return ret;
2746
Chris Masone02119d2008-09-05 16:13:11 -04002747 if (wc->free) {
2748 struct extent_buffer *next;
2749
2750 next = path->nodes[*level];
2751
Josef Bacik681ae502013-10-07 15:11:00 -04002752 if (trans) {
2753 btrfs_tree_lock(next);
2754 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002755 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002756 btrfs_wait_tree_block_writeback(next);
2757 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002758 } else {
2759 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2760 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002761 }
Chris Masone02119d2008-09-05 16:13:11 -04002762
Chris Masone02119d2008-09-05 16:13:11 -04002763 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002764 ret = btrfs_free_and_pin_reserved_extent(
2765 fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04002766 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002767 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002768 if (ret)
2769 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002770 }
2771 free_extent_buffer(path->nodes[*level]);
2772 path->nodes[*level] = NULL;
2773 *level = i + 1;
2774 }
2775 }
2776 return 1;
2777}
2778
2779/*
2780 * drop the reference count on the tree rooted at 'snap'. This traverses
2781 * the tree freeing any blocks that have a ref count of zero after being
2782 * decremented.
2783 */
2784static int walk_log_tree(struct btrfs_trans_handle *trans,
2785 struct btrfs_root *log, struct walk_control *wc)
2786{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002787 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002788 int ret = 0;
2789 int wret;
2790 int level;
2791 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002792 int orig_level;
2793
2794 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002795 if (!path)
2796 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002797
2798 level = btrfs_header_level(log->node);
2799 orig_level = level;
2800 path->nodes[level] = log->node;
2801 extent_buffer_get(log->node);
2802 path->slots[level] = 0;
2803
Chris Masond3977122009-01-05 21:25:51 -05002804 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002805 wret = walk_down_log_tree(trans, log, path, &level, wc);
2806 if (wret > 0)
2807 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002808 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002809 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002810 goto out;
2811 }
Chris Masone02119d2008-09-05 16:13:11 -04002812
2813 wret = walk_up_log_tree(trans, log, path, &level, wc);
2814 if (wret > 0)
2815 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002816 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002817 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002818 goto out;
2819 }
Chris Masone02119d2008-09-05 16:13:11 -04002820 }
2821
2822 /* was the root node processed? if not, catch it here */
2823 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002824 ret = wc->process_func(log, path->nodes[orig_level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002825 btrfs_header_generation(path->nodes[orig_level]),
2826 orig_level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002827 if (ret)
2828 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002829 if (wc->free) {
2830 struct extent_buffer *next;
2831
2832 next = path->nodes[orig_level];
2833
Josef Bacik681ae502013-10-07 15:11:00 -04002834 if (trans) {
2835 btrfs_tree_lock(next);
2836 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002837 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002838 btrfs_wait_tree_block_writeback(next);
2839 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002840 } else {
2841 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2842 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002843 }
Chris Masone02119d2008-09-05 16:13:11 -04002844
Chris Masone02119d2008-09-05 16:13:11 -04002845 WARN_ON(log->root_key.objectid !=
2846 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002847 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2848 next->start, next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002849 if (ret)
2850 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002851 }
2852 }
2853
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002854out:
Chris Masone02119d2008-09-05 16:13:11 -04002855 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002856 return ret;
2857}
2858
Yan Zheng7237f182009-01-21 12:54:03 -05002859/*
2860 * helper function to update the item for a given subvolumes log root
2861 * in the tree of log roots
2862 */
2863static int update_log_root(struct btrfs_trans_handle *trans,
Josef Bacikf7313de2019-09-30 16:27:25 -04002864 struct btrfs_root *log,
2865 struct btrfs_root_item *root_item)
Yan Zheng7237f182009-01-21 12:54:03 -05002866{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002867 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05002868 int ret;
2869
2870 if (log->log_transid == 1) {
2871 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002872 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Josef Bacikf7313de2019-09-30 16:27:25 -04002873 &log->root_key, root_item);
Yan Zheng7237f182009-01-21 12:54:03 -05002874 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002875 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Josef Bacikf7313de2019-09-30 16:27:25 -04002876 &log->root_key, root_item);
Yan Zheng7237f182009-01-21 12:54:03 -05002877 }
2878 return ret;
2879}
2880
Zhaolei60d53eb2015-08-17 18:44:46 +08002881static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002882{
2883 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002884 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002885
Yan Zheng7237f182009-01-21 12:54:03 -05002886 /*
2887 * we only allow two pending log transactions at a time,
2888 * so we know that if ours is more than 2 older than the
2889 * current transaction, we're done
2890 */
Liu Bo49e83f52017-09-01 16:14:30 -06002891 for (;;) {
Yan Zheng7237f182009-01-21 12:54:03 -05002892 prepare_to_wait(&root->log_commit_wait[index],
2893 &wait, TASK_UNINTERRUPTIBLE);
Liu Bo49e83f52017-09-01 16:14:30 -06002894
2895 if (!(root->log_transid_committed < transid &&
2896 atomic_read(&root->log_commit[index])))
2897 break;
2898
Yan Zheng7237f182009-01-21 12:54:03 -05002899 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002900 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002901 mutex_lock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002902 }
2903 finish_wait(&root->log_commit_wait[index], &wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002904}
2905
Zhaolei60d53eb2015-08-17 18:44:46 +08002906static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002907{
2908 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002909
Liu Bo49e83f52017-09-01 16:14:30 -06002910 for (;;) {
2911 prepare_to_wait(&root->log_writer_wait, &wait,
2912 TASK_UNINTERRUPTIBLE);
2913 if (!atomic_read(&root->log_writers))
2914 break;
2915
Yan Zheng7237f182009-01-21 12:54:03 -05002916 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002917 schedule();
Filipe Manana575849e2015-02-11 11:12:39 +00002918 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002919 }
Liu Bo49e83f52017-09-01 16:14:30 -06002920 finish_wait(&root->log_writer_wait, &wait);
Chris Masone02119d2008-09-05 16:13:11 -04002921}
2922
Miao Xie8b050d32014-02-20 18:08:58 +08002923static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2924 struct btrfs_log_ctx *ctx)
2925{
2926 if (!ctx)
2927 return;
2928
2929 mutex_lock(&root->log_mutex);
2930 list_del_init(&ctx->list);
2931 mutex_unlock(&root->log_mutex);
2932}
2933
2934/*
2935 * Invoked in log mutex context, or be sure there is no other task which
2936 * can access the list.
2937 */
2938static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2939 int index, int error)
2940{
2941 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002942 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002943
Chris Mason570dd452016-10-27 10:42:20 -07002944 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2945 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002946 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002947 }
Miao Xie8b050d32014-02-20 18:08:58 +08002948
2949 INIT_LIST_HEAD(&root->log_ctxs[index]);
2950}
2951
Chris Masone02119d2008-09-05 16:13:11 -04002952/*
2953 * btrfs_sync_log does sends a given tree log down to the disk and
2954 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002955 * you know that any inodes previously logged are safely on disk only
2956 * if it returns 0.
2957 *
2958 * Any other return value means you need to call btrfs_commit_transaction.
2959 * Some of the edge cases for fsyncing directories that have had unlinks
2960 * or renames done in the past mean that sometimes the only safe
2961 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2962 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002963 */
2964int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002965 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002966{
Yan Zheng7237f182009-01-21 12:54:03 -05002967 int index1;
2968 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002969 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002970 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002971 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002972 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002973 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Josef Bacikf7313de2019-09-30 16:27:25 -04002974 struct btrfs_root_item new_root_item;
Miao Xiebb14a592014-02-20 18:08:56 +08002975 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002976 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002977 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002978
Yan Zheng7237f182009-01-21 12:54:03 -05002979 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002980 log_transid = ctx->log_transid;
2981 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002982 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002983 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002984 }
Miao Xied1433de2014-02-20 18:08:59 +08002985
2986 index1 = log_transid % 2;
2987 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002988 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002989 mutex_unlock(&root->log_mutex);
2990 return ctx->log_ret;
2991 }
2992 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002993 atomic_set(&root->log_commit[index1], 1);
2994
2995 /* wait for previous tree log sync to complete */
2996 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002997 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002998
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002999 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06003000 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04003001 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003002 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08003003 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04003004 mutex_unlock(&root->log_mutex);
3005 schedule_timeout_uninterruptible(1);
3006 mutex_lock(&root->log_mutex);
3007 }
Zhaolei60d53eb2015-08-17 18:44:46 +08003008 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06003009 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04003010 break;
3011 }
Chris Masond0c803c2008-09-11 16:17:57 -04003012
Chris Mason12fcfd22009-03-24 10:24:20 -04003013 /* bail out if we need to do a full commit */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003014 if (btrfs_need_log_full_commit(fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003015 ret = -EAGAIN;
3016 mutex_unlock(&root->log_mutex);
3017 goto out;
3018 }
3019
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003020 if (log_transid % 2 == 0)
3021 mark = EXTENT_DIRTY;
3022 else
3023 mark = EXTENT_NEW;
3024
Chris Mason690587d2009-10-13 13:29:19 -04003025 /* we start IO on all the marked extents here, but we don't actually
3026 * wait for them until later.
3027 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00003028 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003029 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003030 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003031 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04003032 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003033 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003034 mutex_unlock(&root->log_mutex);
3035 goto out;
3036 }
Yan Zheng7237f182009-01-21 12:54:03 -05003037
Josef Bacikf7313de2019-09-30 16:27:25 -04003038 /*
3039 * We _must_ update under the root->log_mutex in order to make sure we
3040 * have a consistent view of the log root we are trying to commit at
3041 * this moment.
3042 *
3043 * We _must_ copy this into a local copy, because we are not holding the
3044 * log_root_tree->log_mutex yet. This is important because when we
3045 * commit the log_root_tree we must have a consistent view of the
3046 * log_root_tree when we update the super block to point at the
3047 * log_root_tree bytenr. If we update the log_root_tree here we'll race
3048 * with the commit and possibly point at the new block which we may not
3049 * have written out.
3050 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003051 btrfs_set_root_node(&log->root_item, log->node);
Josef Bacikf7313de2019-09-30 16:27:25 -04003052 memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
Yan Zheng7237f182009-01-21 12:54:03 -05003053
Yan Zheng7237f182009-01-21 12:54:03 -05003054 root->log_transid++;
3055 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04003056 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05003057 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003058 * IO has been started, blocks of the log tree have WRITTEN flag set
3059 * in their headers. new modifications of the log will be written to
3060 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05003061 */
3062 mutex_unlock(&root->log_mutex);
3063
Filipe Manana28a23592016-08-23 21:13:51 +01003064 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08003065
Yan Zheng7237f182009-01-21 12:54:03 -05003066 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06003067 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05003068 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08003069
3070 index2 = log_root_tree->log_transid % 2;
3071 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3072 root_log_ctx.log_transid = log_root_tree->log_transid;
3073
Yan Zheng7237f182009-01-21 12:54:03 -05003074 mutex_unlock(&log_root_tree->log_mutex);
3075
Yan Zheng7237f182009-01-21 12:54:03 -05003076 mutex_lock(&log_root_tree->log_mutex);
Josef Bacikf7313de2019-09-30 16:27:25 -04003077
3078 /*
3079 * Now we are safe to update the log_root_tree because we're under the
3080 * log_mutex, and we're a current writer so we're holding the commit
3081 * open until we drop the log_mutex.
3082 */
3083 ret = update_log_root(trans, log, &new_root_item);
3084
Yan Zheng7237f182009-01-21 12:54:03 -05003085 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +01003086 /* atomic_dec_and_test implies a barrier */
3087 cond_wake_up_nomb(&log_root_tree->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -05003088 }
3089
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003090 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08003091 if (!list_empty(&root_log_ctx.list))
3092 list_del_init(&root_log_ctx.list);
3093
Miao Xiec6adc9c2013-05-28 10:05:39 +00003094 blk_finish_plug(&plug);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003095 btrfs_set_log_full_commit(fs_info, trans);
Miao Xie995946d2014-04-02 19:51:06 +08003096
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003097 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003098 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003099 mutex_unlock(&log_root_tree->log_mutex);
3100 goto out;
3101 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003102 btrfs_wait_tree_log_extents(log, mark);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003103 mutex_unlock(&log_root_tree->log_mutex);
3104 ret = -EAGAIN;
3105 goto out;
3106 }
3107
Miao Xied1433de2014-02-20 18:08:59 +08003108 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08003109 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07003110 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08003111 mutex_unlock(&log_root_tree->log_mutex);
3112 ret = root_log_ctx.log_ret;
3113 goto out;
3114 }
Miao Xie8b050d32014-02-20 18:08:58 +08003115
Miao Xied1433de2014-02-20 18:08:59 +08003116 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05003117 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003118 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003119 ret = btrfs_wait_tree_log_extents(log, mark);
Zhaolei60d53eb2015-08-17 18:44:46 +08003120 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003121 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003122 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003123 if (!ret)
3124 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05003125 goto out;
3126 }
Miao Xied1433de2014-02-20 18:08:59 +08003127 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003128 atomic_set(&log_root_tree->log_commit[index2], 1);
3129
Chris Mason12fcfd22009-03-24 10:24:20 -04003130 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003131 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003132 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04003133 }
Yan Zheng7237f182009-01-21 12:54:03 -05003134
Zhaolei60d53eb2015-08-17 18:44:46 +08003135 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04003136
3137 /*
3138 * now that we've moved on to the tree of log tree roots,
3139 * check the full commit flag again
3140 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003141 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003142 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003143 btrfs_wait_tree_log_extents(log, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04003144 mutex_unlock(&log_root_tree->log_mutex);
3145 ret = -EAGAIN;
3146 goto out_wake_log_root;
3147 }
Yan Zheng7237f182009-01-21 12:54:03 -05003148
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003149 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00003150 &log_root_tree->dirty_log_pages,
3151 EXTENT_DIRTY | EXTENT_NEW);
3152 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003153 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003154 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003155 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003156 mutex_unlock(&log_root_tree->log_mutex);
3157 goto out_wake_log_root;
3158 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003159 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003160 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003161 ret = btrfs_wait_tree_log_extents(log_root_tree,
3162 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003163 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003164 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003165 mutex_unlock(&log_root_tree->log_mutex);
3166 goto out_wake_log_root;
3167 }
Chris Masone02119d2008-09-05 16:13:11 -04003168
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003169 btrfs_set_super_log_root(fs_info->super_for_commit,
3170 log_root_tree->node->start);
3171 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3172 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04003173
Yan Zheng7237f182009-01-21 12:54:03 -05003174 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05003175 mutex_unlock(&log_root_tree->log_mutex);
3176
3177 /*
3178 * nobody else is going to jump in and write the the ctree
3179 * super here because the log_commit atomic below is protecting
3180 * us. We must be called with a transaction handle pinning
3181 * the running transaction open, so a full commit can't hop
3182 * in and cause problems either.
3183 */
David Sterbaeece6a92017-02-10 19:04:32 +01003184 ret = write_all_supers(fs_info, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003185 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003186 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003187 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003188 goto out_wake_log_root;
3189 }
Yan Zheng7237f182009-01-21 12:54:03 -05003190
Chris Mason257c62e2009-10-13 13:21:08 -04003191 mutex_lock(&root->log_mutex);
3192 if (root->last_log_commit < log_transid)
3193 root->last_log_commit = log_transid;
3194 mutex_unlock(&root->log_mutex);
3195
Chris Mason12fcfd22009-03-24 10:24:20 -04003196out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07003197 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003198 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3199
Miao Xied1433de2014-02-20 18:08:59 +08003200 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003201 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003202 mutex_unlock(&log_root_tree->log_mutex);
3203
David Sterba33a9eca2015-10-10 18:35:10 +02003204 /*
David Sterba093258e2018-02-26 16:15:17 +01003205 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3206 * all the updates above are seen by the woken threads. It might not be
3207 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003208 */
David Sterba093258e2018-02-26 16:15:17 +01003209 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04003210out:
Miao Xied1433de2014-02-20 18:08:59 +08003211 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07003212 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08003213 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003214 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003215 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003216
David Sterba33a9eca2015-10-10 18:35:10 +02003217 /*
David Sterba093258e2018-02-26 16:15:17 +01003218 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3219 * all the updates above are seen by the woken threads. It might not be
3220 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003221 */
David Sterba093258e2018-02-26 16:15:17 +01003222 cond_wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003223 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003224}
3225
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003226static void free_log_tree(struct btrfs_trans_handle *trans,
3227 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003228{
3229 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003230 u64 start;
3231 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003232 struct walk_control wc = {
3233 .free = 1,
3234 .process_func = process_one_buffer
3235 };
3236
Josef Bacik681ae502013-10-07 15:11:00 -04003237 ret = walk_log_tree(trans, log, &wc);
Jeff Mahoneycdecd482018-09-06 16:59:33 -04003238 if (ret) {
3239 if (trans)
3240 btrfs_abort_transaction(trans, ret);
3241 else
3242 btrfs_handle_fs_error(log->fs_info, ret, NULL);
3243 }
Chris Masone02119d2008-09-05 16:13:11 -04003244
Chris Masond3977122009-01-05 21:25:51 -05003245 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003246 ret = find_first_extent_bit(&log->dirty_log_pages,
Liu Bo55237a52018-01-25 11:02:52 -07003247 0, &start, &end,
3248 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
Josef Bacike6138872012-09-27 17:07:30 -04003249 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003250 if (ret)
3251 break;
3252
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003253 clear_extent_bits(&log->dirty_log_pages, start, end,
Liu Bo55237a52018-01-25 11:02:52 -07003254 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Chris Masond0c803c2008-09-11 16:17:57 -04003255 }
3256
Yan Zheng7237f182009-01-21 12:54:03 -05003257 free_extent_buffer(log->node);
3258 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003259}
3260
3261/*
3262 * free all the extents used by the tree log. This should be called
3263 * at commit time of the full transaction
3264 */
3265int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3266{
3267 if (root->log_root) {
3268 free_log_tree(trans, root->log_root);
3269 root->log_root = NULL;
3270 }
3271 return 0;
3272}
3273
3274int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3275 struct btrfs_fs_info *fs_info)
3276{
3277 if (fs_info->log_root_tree) {
3278 free_log_tree(trans, fs_info->log_root_tree);
3279 fs_info->log_root_tree = NULL;
3280 }
Chris Masone02119d2008-09-05 16:13:11 -04003281 return 0;
3282}
3283
3284/*
Filipe Mananafffedf52019-06-19 13:05:39 +01003285 * Check if an inode was logged in the current transaction. We can't always rely
3286 * on an inode's logged_trans value, because it's an in-memory only field and
3287 * therefore not persisted. This means that its value is lost if the inode gets
3288 * evicted and loaded again from disk (in which case it has a value of 0, and
3289 * certainly it is smaller then any possible transaction ID), when that happens
3290 * the full_sync flag is set in the inode's runtime flags, so on that case we
3291 * assume eviction happened and ignore the logged_trans value, assuming the
3292 * worst case, that the inode was logged before in the current transaction.
3293 */
3294static bool inode_logged(struct btrfs_trans_handle *trans,
3295 struct btrfs_inode *inode)
3296{
3297 if (inode->logged_trans == trans->transid)
3298 return true;
3299
3300 if (inode->last_trans == trans->transid &&
3301 test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) &&
3302 !test_bit(BTRFS_FS_LOG_RECOVERING, &trans->fs_info->flags))
3303 return true;
3304
3305 return false;
3306}
3307
3308/*
Chris Masone02119d2008-09-05 16:13:11 -04003309 * If both a file and directory are logged, and unlinks or renames are
3310 * mixed in, we have a few interesting corners:
3311 *
3312 * create file X in dir Y
3313 * link file X to X.link in dir Y
3314 * fsync file X
3315 * unlink file X but leave X.link
3316 * fsync dir Y
3317 *
3318 * After a crash we would expect only X.link to exist. But file X
3319 * didn't get fsync'd again so the log has back refs for X and X.link.
3320 *
3321 * We solve this by removing directory entries and inode backrefs from the
3322 * log when a file that was logged in the current transaction is
3323 * unlinked. Any later fsync will include the updated log entries, and
3324 * we'll be able to reconstruct the proper directory items from backrefs.
3325 *
3326 * This optimizations allows us to avoid relogging the entire inode
3327 * or the entire directory.
3328 */
3329int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3330 struct btrfs_root *root,
3331 const char *name, int name_len,
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003332 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003333{
3334 struct btrfs_root *log;
3335 struct btrfs_dir_item *di;
3336 struct btrfs_path *path;
3337 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003338 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003339 int bytes_del = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003340 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003341
Filipe Mananafffedf52019-06-19 13:05:39 +01003342 if (!inode_logged(trans, dir))
Chris Mason3a5f1d42008-09-11 15:53:37 -04003343 return 0;
3344
Chris Masone02119d2008-09-05 16:13:11 -04003345 ret = join_running_log_trans(root);
3346 if (ret)
3347 return 0;
3348
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003349 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003350
3351 log = root->log_root;
3352 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003353 if (!path) {
3354 err = -ENOMEM;
3355 goto out_unlock;
3356 }
liubo2a29edc2011-01-26 06:22:08 +00003357
Li Zefan33345d012011-04-20 10:31:50 +08003358 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003359 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003360 if (IS_ERR(di)) {
3361 err = PTR_ERR(di);
3362 goto fail;
3363 }
3364 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003365 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3366 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003367 if (ret) {
3368 err = ret;
3369 goto fail;
3370 }
Chris Masone02119d2008-09-05 16:13:11 -04003371 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003372 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003373 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003374 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003375 if (IS_ERR(di)) {
3376 err = PTR_ERR(di);
3377 goto fail;
3378 }
3379 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003380 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3381 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003382 if (ret) {
3383 err = ret;
3384 goto fail;
3385 }
Chris Masone02119d2008-09-05 16:13:11 -04003386 }
3387
3388 /* update the directory size in the log to reflect the names
3389 * we have removed
3390 */
3391 if (bytes_del) {
3392 struct btrfs_key key;
3393
Li Zefan33345d012011-04-20 10:31:50 +08003394 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003395 key.offset = 0;
3396 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003397 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003398
3399 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003400 if (ret < 0) {
3401 err = ret;
3402 goto fail;
3403 }
Chris Masone02119d2008-09-05 16:13:11 -04003404 if (ret == 0) {
3405 struct btrfs_inode_item *item;
3406 u64 i_size;
3407
3408 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3409 struct btrfs_inode_item);
3410 i_size = btrfs_inode_size(path->nodes[0], item);
3411 if (i_size > bytes_del)
3412 i_size -= bytes_del;
3413 else
3414 i_size = 0;
3415 btrfs_set_inode_size(path->nodes[0], item, i_size);
3416 btrfs_mark_buffer_dirty(path->nodes[0]);
3417 } else
3418 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003419 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003420 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003421fail:
Chris Masone02119d2008-09-05 16:13:11 -04003422 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003423out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003424 mutex_unlock(&dir->log_mutex);
Josef Bacik2d037282020-08-10 17:31:16 -04003425 if (err == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003426 btrfs_set_log_full_commit(root->fs_info, trans);
Josef Bacik2d037282020-08-10 17:31:16 -04003427 err = 0;
3428 } else if (err < 0 && err != -ENOENT) {
3429 /* ENOENT can be returned if the entry hasn't been fsynced yet */
3430 btrfs_abort_transaction(trans, err);
3431 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003432
Chris Mason12fcfd22009-03-24 10:24:20 -04003433 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003434
Andi Kleen411fc6b2010-10-29 15:14:31 -04003435 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003436}
3437
3438/* see comments for btrfs_del_dir_entries_in_log */
3439int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3440 struct btrfs_root *root,
3441 const char *name, int name_len,
Nikolay Borisova491abb2017-01-18 00:31:33 +02003442 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003443{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003444 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003445 struct btrfs_root *log;
3446 u64 index;
3447 int ret;
3448
Filipe Mananafffedf52019-06-19 13:05:39 +01003449 if (!inode_logged(trans, inode))
Chris Mason3a5f1d42008-09-11 15:53:37 -04003450 return 0;
3451
Chris Masone02119d2008-09-05 16:13:11 -04003452 ret = join_running_log_trans(root);
3453 if (ret)
3454 return 0;
3455 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003456 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003457
Nikolay Borisova491abb2017-01-18 00:31:33 +02003458 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003459 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003460 mutex_unlock(&inode->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003461 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003462 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003463 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003464 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003465 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003466 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003467
Chris Masone02119d2008-09-05 16:13:11 -04003468 return ret;
3469}
3470
3471/*
3472 * creates a range item in the log for 'dirid'. first_offset and
3473 * last_offset tell us which parts of the key space the log should
3474 * be considered authoritative for.
3475 */
3476static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3477 struct btrfs_root *log,
3478 struct btrfs_path *path,
3479 int key_type, u64 dirid,
3480 u64 first_offset, u64 last_offset)
3481{
3482 int ret;
3483 struct btrfs_key key;
3484 struct btrfs_dir_log_item *item;
3485
3486 key.objectid = dirid;
3487 key.offset = first_offset;
3488 if (key_type == BTRFS_DIR_ITEM_KEY)
3489 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3490 else
3491 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3492 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003493 if (ret)
3494 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003495
3496 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3497 struct btrfs_dir_log_item);
3498 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3499 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003500 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003501 return 0;
3502}
3503
3504/*
3505 * log all the items included in the current transaction for a given
3506 * directory. This also creates the range items in the log tree required
3507 * to replay anything deleted before the fsync
3508 */
3509static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Nikolay Borisov684a5772017-01-18 00:31:41 +02003510 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003511 struct btrfs_path *path,
3512 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003513 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003514 u64 min_offset, u64 *last_offset_ret)
3515{
3516 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003517 struct btrfs_root *log = root->log_root;
3518 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003519 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003520 int ret;
3521 int i;
3522 int nritems;
3523 u64 first_offset = min_offset;
3524 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003525 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003526
3527 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003528
Li Zefan33345d012011-04-20 10:31:50 +08003529 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003530 min_key.type = key_type;
3531 min_key.offset = min_offset;
3532
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003533 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003534
3535 /*
3536 * we didn't find anything from this transaction, see if there
3537 * is anything at all
3538 */
Li Zefan33345d012011-04-20 10:31:50 +08003539 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3540 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003541 min_key.type = key_type;
3542 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003543 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003544 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3545 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003546 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003547 return ret;
3548 }
Li Zefan33345d012011-04-20 10:31:50 +08003549 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003550
3551 /* if ret == 0 there are items for this type,
3552 * create a range to tell us the last key of this type.
3553 * otherwise, there are no items in this directory after
3554 * *min_offset, and we create a range to indicate that.
3555 */
3556 if (ret == 0) {
3557 struct btrfs_key tmp;
3558 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3559 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003560 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003561 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003562 }
3563 goto done;
3564 }
3565
3566 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003567 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003568 if (ret == 0) {
3569 struct btrfs_key tmp;
3570 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3571 if (key_type == tmp.type) {
3572 first_offset = tmp.offset;
3573 ret = overwrite_item(trans, log, dst_path,
3574 path->nodes[0], path->slots[0],
3575 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003576 if (ret) {
3577 err = ret;
3578 goto done;
3579 }
Chris Masone02119d2008-09-05 16:13:11 -04003580 }
3581 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003582 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003583
Josef Bacikb57220c2019-03-06 17:13:04 -05003584 /*
3585 * Find the first key from this transaction again. See the note for
3586 * log_new_dir_dentries, if we're logging a directory recursively we
3587 * won't be holding its i_mutex, which means we can modify the directory
3588 * while we're logging it. If we remove an entry between our first
3589 * search and this search we'll not find the key again and can just
3590 * bail.
3591 */
Chris Masone02119d2008-09-05 16:13:11 -04003592 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Josef Bacikb57220c2019-03-06 17:13:04 -05003593 if (ret != 0)
Chris Masone02119d2008-09-05 16:13:11 -04003594 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003595
3596 /*
3597 * we have a block from this transaction, log every item in it
3598 * from our directory
3599 */
Chris Masond3977122009-01-05 21:25:51 -05003600 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003601 struct btrfs_key tmp;
3602 src = path->nodes[0];
3603 nritems = btrfs_header_nritems(src);
3604 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003605 struct btrfs_dir_item *di;
3606
Chris Masone02119d2008-09-05 16:13:11 -04003607 btrfs_item_key_to_cpu(src, &min_key, i);
3608
Li Zefan33345d012011-04-20 10:31:50 +08003609 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003610 goto done;
3611 ret = overwrite_item(trans, log, dst_path, src, i,
3612 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003613 if (ret) {
3614 err = ret;
3615 goto done;
3616 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003617
3618 /*
3619 * We must make sure that when we log a directory entry,
3620 * the corresponding inode, after log replay, has a
3621 * matching link count. For example:
3622 *
3623 * touch foo
3624 * mkdir mydir
3625 * sync
3626 * ln foo mydir/bar
3627 * xfs_io -c "fsync" mydir
3628 * <crash>
3629 * <mount fs and log replay>
3630 *
3631 * Would result in a fsync log that when replayed, our
3632 * file inode would have a link count of 1, but we get
3633 * two directory entries pointing to the same inode.
3634 * After removing one of the names, it would not be
3635 * possible to remove the other name, which resulted
3636 * always in stale file handle errors, and would not
3637 * be possible to rmdir the parent directory, since
3638 * its i_size could never decrement to the value
3639 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3640 */
3641 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3642 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3643 if (ctx &&
3644 (btrfs_dir_transid(src, di) == trans->transid ||
3645 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3646 tmp.type != BTRFS_ROOT_ITEM_KEY)
3647 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003648 }
3649 path->slots[0] = nritems;
3650
3651 /*
3652 * look ahead to the next item and see if it is also
3653 * from this directory and from this transaction
3654 */
3655 ret = btrfs_next_leaf(root, path);
Liu Bo80c0b422018-04-03 01:59:47 +08003656 if (ret) {
3657 if (ret == 1)
3658 last_offset = (u64)-1;
3659 else
3660 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003661 goto done;
3662 }
3663 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003664 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003665 last_offset = (u64)-1;
3666 goto done;
3667 }
3668 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3669 ret = overwrite_item(trans, log, dst_path,
3670 path->nodes[0], path->slots[0],
3671 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003672 if (ret)
3673 err = ret;
3674 else
3675 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003676 goto done;
3677 }
3678 }
3679done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003680 btrfs_release_path(path);
3681 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003682
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003683 if (err == 0) {
3684 *last_offset_ret = last_offset;
3685 /*
3686 * insert the log range keys to indicate where the log
3687 * is valid
3688 */
3689 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003690 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003691 if (ret)
3692 err = ret;
3693 }
3694 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003695}
3696
3697/*
3698 * logging directories is very similar to logging inodes, We find all the items
3699 * from the current transaction and write them to the log.
3700 *
3701 * The recovery code scans the directory in the subvolume, and if it finds a
3702 * key in the range logged that is not present in the log tree, then it means
3703 * that dir entry was unlinked during the transaction.
3704 *
3705 * In order for that scan to work, we must include one key smaller than
3706 * the smallest logged by this transaction and one key larger than the largest
3707 * key logged by this transaction.
3708 */
3709static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003710 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003711 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003712 struct btrfs_path *dst_path,
3713 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003714{
3715 u64 min_key;
3716 u64 max_key;
3717 int ret;
3718 int key_type = BTRFS_DIR_ITEM_KEY;
3719
3720again:
3721 min_key = 0;
3722 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003723 while (1) {
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003724 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3725 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003726 if (ret)
3727 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003728 if (max_key == (u64)-1)
3729 break;
3730 min_key = max_key + 1;
3731 }
3732
3733 if (key_type == BTRFS_DIR_ITEM_KEY) {
3734 key_type = BTRFS_DIR_INDEX_KEY;
3735 goto again;
3736 }
3737 return 0;
3738}
3739
3740/*
3741 * a helper function to drop items from the log before we relog an
3742 * inode. max_key_type indicates the highest item type to remove.
3743 * This cannot be run for file data extents because it does not
3744 * free the extents they point to.
3745 */
3746static int drop_objectid_items(struct btrfs_trans_handle *trans,
3747 struct btrfs_root *log,
3748 struct btrfs_path *path,
3749 u64 objectid, int max_key_type)
3750{
3751 int ret;
3752 struct btrfs_key key;
3753 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003754 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003755
3756 key.objectid = objectid;
3757 key.type = max_key_type;
3758 key.offset = (u64)-1;
3759
Chris Masond3977122009-01-05 21:25:51 -05003760 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003761 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003762 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003763 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003764 break;
3765
3766 if (path->slots[0] == 0)
3767 break;
3768
3769 path->slots[0]--;
3770 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3771 path->slots[0]);
3772
3773 if (found_key.objectid != objectid)
3774 break;
3775
Josef Bacik18ec90d2012-09-28 11:56:28 -04003776 found_key.offset = 0;
3777 found_key.type = 0;
3778 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3779 &start_slot);
3780
3781 ret = btrfs_del_items(trans, log, path, start_slot,
3782 path->slots[0] - start_slot + 1);
3783 /*
3784 * If start slot isn't 0 then we don't need to re-search, we've
3785 * found the last guy with the objectid in this tree.
3786 */
3787 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003788 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003789 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003790 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003791 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003792 if (ret > 0)
3793 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003794 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003795}
3796
Josef Bacik94edf4a2012-09-25 14:56:25 -04003797static void fill_inode_item(struct btrfs_trans_handle *trans,
3798 struct extent_buffer *leaf,
3799 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003800 struct inode *inode, int log_inode_only,
3801 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003802{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003803 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003804
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003805 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003806
3807 if (log_inode_only) {
3808 /* set the generation to zero so the recover code
3809 * can tell the difference between an logging
3810 * just to say 'this inode exists' and a logging
3811 * to say 'update this inode with these values'
3812 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003813 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003814 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003815 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003816 btrfs_set_token_inode_generation(leaf, item,
3817 BTRFS_I(inode)->generation,
3818 &token);
3819 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003820 }
3821
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003822 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3823 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3824 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3825 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3826
David Sterbaa937b972014-12-12 17:39:12 +01003827 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003828 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003829 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003830 inode->i_atime.tv_nsec, &token);
3831
David Sterbaa937b972014-12-12 17:39:12 +01003832 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003833 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003834 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003835 inode->i_mtime.tv_nsec, &token);
3836
David Sterbaa937b972014-12-12 17:39:12 +01003837 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003838 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003839 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003840 inode->i_ctime.tv_nsec, &token);
3841
3842 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3843 &token);
3844
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003845 btrfs_set_token_inode_sequence(leaf, item,
3846 inode_peek_iversion(inode), &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003847 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3848 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3849 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3850 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003851}
3852
Josef Bacika95249b2012-10-11 16:17:34 -04003853static int log_inode_item(struct btrfs_trans_handle *trans,
3854 struct btrfs_root *log, struct btrfs_path *path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003855 struct btrfs_inode *inode)
Josef Bacika95249b2012-10-11 16:17:34 -04003856{
3857 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003858 int ret;
3859
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003860 ret = btrfs_insert_empty_item(trans, log, path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003861 &inode->location, sizeof(*inode_item));
Josef Bacika95249b2012-10-11 16:17:34 -04003862 if (ret && ret != -EEXIST)
3863 return ret;
3864 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3865 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003866 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3867 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003868 btrfs_release_path(path);
3869 return 0;
3870}
3871
Filipe Manana01510492019-12-05 16:58:30 +00003872static int log_csums(struct btrfs_trans_handle *trans,
3873 struct btrfs_root *log_root,
3874 struct btrfs_ordered_sum *sums)
3875{
3876 int ret;
3877
3878 /*
3879 * Due to extent cloning, we might have logged a csum item that covers a
3880 * subrange of a cloned extent, and later we can end up logging a csum
3881 * item for a larger subrange of the same extent or the entire range.
3882 * This would leave csum items in the log tree that cover the same range
3883 * and break the searches for checksums in the log tree, resulting in
3884 * some checksums missing in the fs/subvolume tree. So just delete (or
3885 * trim and adjust) any existing csum items in the log for this range.
3886 */
3887 ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len);
3888 if (ret)
3889 return ret;
3890
3891 return btrfs_csum_file_blocks(trans, log_root, sums);
3892}
3893
Chris Mason31ff1cd2008-09-11 16:17:57 -04003894static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003895 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003896 struct btrfs_path *dst_path,
Filipe Manana587292a2019-11-19 12:07:33 +00003897 struct btrfs_path *src_path,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003898 int start_slot, int nr, int inode_only,
3899 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003900{
David Sterba3ffbd682018-06-29 10:56:42 +02003901 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003902 unsigned long src_offset;
3903 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003904 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003905 struct btrfs_file_extent_item *extent;
3906 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003907 struct extent_buffer *src = src_path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003908 int ret;
3909 struct btrfs_key *ins_keys;
3910 u32 *ins_sizes;
3911 char *ins_data;
3912 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003913 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003914 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Chris Masond20f7042008-12-08 16:58:54 -05003915
3916 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003917
3918 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3919 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003920 if (!ins_data)
3921 return -ENOMEM;
3922
Chris Mason31ff1cd2008-09-11 16:17:57 -04003923 ins_sizes = (u32 *)ins_data;
3924 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3925
3926 for (i = 0; i < nr; i++) {
3927 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3928 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3929 }
3930 ret = btrfs_insert_empty_items(trans, log, dst_path,
3931 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003932 if (ret) {
3933 kfree(ins_data);
3934 return ret;
3935 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003936
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003937 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003938 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3939 dst_path->slots[0]);
3940
3941 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3942
Josef Bacik94edf4a2012-09-25 14:56:25 -04003943 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003944 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3945 dst_path->slots[0],
3946 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003947 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01003948 &inode->vfs_inode,
3949 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003950 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003951 } else {
3952 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3953 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003954 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003955
Chris Mason31ff1cd2008-09-11 16:17:57 -04003956 /* take a reference on file data extents so that truncates
3957 * or deletes of this inode don't have to relog the inode
3958 * again
3959 */
David Sterba962a2982014-06-04 18:41:45 +02003960 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003961 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003962 int found_type;
3963 extent = btrfs_item_ptr(src, start_slot + i,
3964 struct btrfs_file_extent_item);
3965
liubo8e531cd2011-05-06 10:36:09 +08003966 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3967 continue;
3968
Chris Mason31ff1cd2008-09-11 16:17:57 -04003969 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003970 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003971 u64 ds, dl, cs, cl;
3972 ds = btrfs_file_extent_disk_bytenr(src,
3973 extent);
3974 /* ds == 0 is a hole */
3975 if (ds == 0)
3976 continue;
3977
3978 dl = btrfs_file_extent_disk_num_bytes(src,
3979 extent);
3980 cs = btrfs_file_extent_offset(src, extent);
3981 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003982 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003983 if (btrfs_file_extent_compression(src,
3984 extent)) {
3985 cs = 0;
3986 cl = dl;
3987 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003988
3989 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003990 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003991 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003992 &ordered_sums, 0);
Filipe Manana183af2d2020-07-29 10:17:50 +01003993 if (ret)
3994 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003995 }
3996 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003997 }
3998
3999 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004000 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04004001 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05004002
4003 /*
4004 * we have to do this after the loop above to avoid changing the
4005 * log tree while trying to change the log tree.
4006 */
Chris Masond3977122009-01-05 21:25:51 -05004007 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05004008 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4009 struct btrfs_ordered_sum,
4010 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004011 if (!ret)
Filipe Manana01510492019-12-05 16:58:30 +00004012 ret = log_csums(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05004013 list_del(&sums->list);
4014 kfree(sums);
4015 }
Josef Bacik16e75492013-10-22 12:18:51 -04004016
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004017 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004018}
4019
Josef Bacik5dc562c2012-08-17 13:14:17 -04004020static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4021{
4022 struct extent_map *em1, *em2;
4023
4024 em1 = list_entry(a, struct extent_map, list);
4025 em2 = list_entry(b, struct extent_map, list);
4026
4027 if (em1->start < em2->start)
4028 return -1;
4029 else if (em1->start > em2->start)
4030 return 1;
4031 return 0;
4032}
4033
Josef Bacike7175a62018-05-23 11:58:34 -04004034static int log_extent_csums(struct btrfs_trans_handle *trans,
4035 struct btrfs_inode *inode,
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004036 struct btrfs_root *log_root,
Josef Bacike7175a62018-05-23 11:58:34 -04004037 const struct extent_map *em)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004038{
Josef Bacik2ab28f32012-10-12 15:27:49 -04004039 u64 csum_offset;
4040 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004041 LIST_HEAD(ordered_sums);
4042 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004043
Josef Bacike7175a62018-05-23 11:58:34 -04004044 if (inode->flags & BTRFS_INODE_NODATASUM ||
4045 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
Filipe Manana8407f552014-09-05 15:14:39 +01004046 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04004047 return 0;
4048
Josef Bacike7175a62018-05-23 11:58:34 -04004049 /* If we're compressed we have to save the entire range of csums. */
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004050 if (em->compress_type) {
4051 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004052 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004053 } else {
Josef Bacike7175a62018-05-23 11:58:34 -04004054 csum_offset = em->mod_start - em->start;
4055 csum_len = em->mod_len;
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004056 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004057
Josef Bacik70c8a912012-10-11 16:54:30 -04004058 /* block start is already adjusted for the file extent offset. */
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004059 ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004060 em->block_start + csum_offset,
4061 em->block_start + csum_offset +
4062 csum_len - 1, &ordered_sums, 0);
4063 if (ret)
4064 return ret;
4065
4066 while (!list_empty(&ordered_sums)) {
4067 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4068 struct btrfs_ordered_sum,
4069 list);
4070 if (!ret)
Filipe Manana01510492019-12-05 16:58:30 +00004071 ret = log_csums(trans, log_root, sums);
Josef Bacik70c8a912012-10-11 16:54:30 -04004072 list_del(&sums->list);
4073 kfree(sums);
4074 }
4075
4076 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004077}
4078
Filipe Manana8407f552014-09-05 15:14:39 +01004079static int log_one_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004080 struct btrfs_inode *inode, struct btrfs_root *root,
Filipe Manana8407f552014-09-05 15:14:39 +01004081 const struct extent_map *em,
4082 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004083 struct btrfs_log_ctx *ctx)
4084{
4085 struct btrfs_root *log = root->log_root;
4086 struct btrfs_file_extent_item *fi;
4087 struct extent_buffer *leaf;
4088 struct btrfs_map_token token;
4089 struct btrfs_key key;
4090 u64 extent_offset = em->start - em->orig_start;
4091 u64 block_len;
4092 int ret;
4093 int extent_inserted = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004094
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004095 ret = log_extent_csums(trans, inode, log, em);
Filipe Manana8407f552014-09-05 15:14:39 +01004096 if (ret)
4097 return ret;
4098
Filipe Manana8407f552014-09-05 15:14:39 +01004099 btrfs_init_map_token(&token);
4100
Nikolay Borisov9d122622017-01-18 00:31:40 +02004101 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
Filipe Manana8407f552014-09-05 15:14:39 +01004102 em->start + em->len, NULL, 0, 1,
4103 sizeof(*fi), &extent_inserted);
4104 if (ret)
4105 return ret;
4106
4107 if (!extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004108 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004109 key.type = BTRFS_EXTENT_DATA_KEY;
4110 key.offset = em->start;
4111
4112 ret = btrfs_insert_empty_item(trans, log, path, &key,
4113 sizeof(*fi));
4114 if (ret)
4115 return ret;
4116 }
4117 leaf = path->nodes[0];
4118 fi = btrfs_item_ptr(leaf, path->slots[0],
4119 struct btrfs_file_extent_item);
4120
Josef Bacik50d9aa92014-11-21 14:52:38 -05004121 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004122 &token);
4123 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4124 btrfs_set_token_file_extent_type(leaf, fi,
4125 BTRFS_FILE_EXTENT_PREALLOC,
4126 &token);
4127 else
4128 btrfs_set_token_file_extent_type(leaf, fi,
4129 BTRFS_FILE_EXTENT_REG,
4130 &token);
4131
4132 block_len = max(em->block_len, em->orig_block_len);
4133 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4134 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4135 em->block_start,
4136 &token);
4137 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4138 &token);
4139 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4140 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4141 em->block_start -
4142 extent_offset, &token);
4143 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4144 &token);
4145 } else {
4146 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4147 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4148 &token);
4149 }
4150
4151 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4152 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4153 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4154 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4155 &token);
4156 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4157 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4158 btrfs_mark_buffer_dirty(leaf);
4159
4160 btrfs_release_path(path);
4161
4162 return ret;
4163}
4164
Filipe Manana31d11b82018-05-09 16:01:46 +01004165/*
4166 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4167 * lose them after doing a fast fsync and replaying the log. We scan the
4168 * subvolume's root instead of iterating the inode's extent map tree because
4169 * otherwise we can log incorrect extent items based on extent map conversion.
4170 * That can happen due to the fact that extent maps are merged when they
4171 * are not in the extent map tree's list of modified extents.
4172 */
4173static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4174 struct btrfs_inode *inode,
4175 struct btrfs_path *path)
4176{
4177 struct btrfs_root *root = inode->root;
4178 struct btrfs_key key;
4179 const u64 i_size = i_size_read(&inode->vfs_inode);
4180 const u64 ino = btrfs_ino(inode);
4181 struct btrfs_path *dst_path = NULL;
Filipe Manana587292a2019-11-19 12:07:33 +00004182 bool dropped_extents = false;
Filipe Manana00043452020-04-23 16:30:53 +01004183 u64 truncate_offset = i_size;
4184 struct extent_buffer *leaf;
4185 int slot;
Filipe Manana31d11b82018-05-09 16:01:46 +01004186 int ins_nr = 0;
4187 int start_slot;
4188 int ret;
4189
4190 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4191 return 0;
4192
4193 key.objectid = ino;
4194 key.type = BTRFS_EXTENT_DATA_KEY;
4195 key.offset = i_size;
4196 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4197 if (ret < 0)
4198 goto out;
4199
Filipe Manana00043452020-04-23 16:30:53 +01004200 /*
4201 * We must check if there is a prealloc extent that starts before the
4202 * i_size and crosses the i_size boundary. This is to ensure later we
4203 * truncate down to the end of that extent and not to the i_size, as
4204 * otherwise we end up losing part of the prealloc extent after a log
4205 * replay and with an implicit hole if there is another prealloc extent
4206 * that starts at an offset beyond i_size.
4207 */
4208 ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
4209 if (ret < 0)
4210 goto out;
4211
4212 if (ret == 0) {
4213 struct btrfs_file_extent_item *ei;
4214
4215 leaf = path->nodes[0];
4216 slot = path->slots[0];
4217 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4218
4219 if (btrfs_file_extent_type(leaf, ei) ==
4220 BTRFS_FILE_EXTENT_PREALLOC) {
4221 u64 extent_end;
4222
4223 btrfs_item_key_to_cpu(leaf, &key, slot);
4224 extent_end = key.offset +
4225 btrfs_file_extent_num_bytes(leaf, ei);
4226
4227 if (extent_end > i_size)
4228 truncate_offset = extent_end;
4229 }
4230 } else {
4231 ret = 0;
4232 }
4233
Filipe Manana31d11b82018-05-09 16:01:46 +01004234 while (true) {
Filipe Manana00043452020-04-23 16:30:53 +01004235 leaf = path->nodes[0];
4236 slot = path->slots[0];
Filipe Manana31d11b82018-05-09 16:01:46 +01004237
4238 if (slot >= btrfs_header_nritems(leaf)) {
4239 if (ins_nr > 0) {
4240 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana587292a2019-11-19 12:07:33 +00004241 start_slot, ins_nr, 1, 0);
Filipe Manana31d11b82018-05-09 16:01:46 +01004242 if (ret < 0)
4243 goto out;
4244 ins_nr = 0;
4245 }
4246 ret = btrfs_next_leaf(root, path);
4247 if (ret < 0)
4248 goto out;
4249 if (ret > 0) {
4250 ret = 0;
4251 break;
4252 }
4253 continue;
4254 }
4255
4256 btrfs_item_key_to_cpu(leaf, &key, slot);
4257 if (key.objectid > ino)
4258 break;
4259 if (WARN_ON_ONCE(key.objectid < ino) ||
4260 key.type < BTRFS_EXTENT_DATA_KEY ||
4261 key.offset < i_size) {
4262 path->slots[0]++;
4263 continue;
4264 }
Filipe Manana587292a2019-11-19 12:07:33 +00004265 if (!dropped_extents) {
Filipe Manana31d11b82018-05-09 16:01:46 +01004266 /*
4267 * Avoid logging extent items logged in past fsync calls
4268 * and leading to duplicate keys in the log tree.
4269 */
4270 do {
4271 ret = btrfs_truncate_inode_items(trans,
4272 root->log_root,
4273 &inode->vfs_inode,
Filipe Manana00043452020-04-23 16:30:53 +01004274 truncate_offset,
Filipe Manana31d11b82018-05-09 16:01:46 +01004275 BTRFS_EXTENT_DATA_KEY);
4276 } while (ret == -EAGAIN);
4277 if (ret)
4278 goto out;
Filipe Manana587292a2019-11-19 12:07:33 +00004279 dropped_extents = true;
Filipe Manana31d11b82018-05-09 16:01:46 +01004280 }
4281 if (ins_nr == 0)
4282 start_slot = slot;
4283 ins_nr++;
4284 path->slots[0]++;
4285 if (!dst_path) {
4286 dst_path = btrfs_alloc_path();
4287 if (!dst_path) {
4288 ret = -ENOMEM;
4289 goto out;
4290 }
4291 }
4292 }
4293 if (ins_nr > 0) {
Filipe Manana587292a2019-11-19 12:07:33 +00004294 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana31d11b82018-05-09 16:01:46 +01004295 start_slot, ins_nr, 1, 0);
4296 if (ret > 0)
4297 ret = 0;
4298 }
4299out:
4300 btrfs_release_path(path);
4301 btrfs_free_path(dst_path);
4302 return ret;
4303}
4304
Josef Bacik5dc562c2012-08-17 13:14:17 -04004305static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4306 struct btrfs_root *root,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004307 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004308 struct btrfs_path *path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004309 struct btrfs_log_ctx *ctx,
4310 const u64 start,
4311 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004312{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004313 struct extent_map *em, *n;
4314 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004315 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004316 u64 logged_start, logged_end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004317 u64 test_gen;
4318 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004319 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004320
4321 INIT_LIST_HEAD(&extents);
4322
Josef Bacik5dc562c2012-08-17 13:14:17 -04004323 write_lock(&tree->lock);
4324 test_gen = root->fs_info->last_trans_committed;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004325 logged_start = start;
4326 logged_end = end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004327
4328 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
Filipe Mananafa625a42018-10-29 09:42:06 +00004329 /*
4330 * Skip extents outside our logging range. It's important to do
4331 * it for correctness because if we don't ignore them, we may
4332 * log them before their ordered extent completes, and therefore
4333 * we could log them without logging their respective checksums
4334 * (the checksum items are added to the csum tree at the very
4335 * end of btrfs_finish_ordered_io()). Also leave such extents
4336 * outside of our range in the list, since we may have another
4337 * ranged fsync in the near future that needs them. If an extent
4338 * outside our range corresponds to a hole, log it to avoid
4339 * leaving gaps between extents (fsck will complain when we are
4340 * not using the NO_HOLES feature).
4341 */
4342 if ((em->start > end || em->start + em->len <= start) &&
4343 em->block_start != EXTENT_MAP_HOLE)
4344 continue;
4345
Josef Bacik5dc562c2012-08-17 13:14:17 -04004346 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004347 /*
4348 * Just an arbitrary number, this can be really CPU intensive
4349 * once we start getting a lot of extents, and really once we
4350 * have a bunch of extents we just want to commit since it will
4351 * be faster.
4352 */
4353 if (++num > 32768) {
4354 list_del_init(&tree->modified_extents);
4355 ret = -EFBIG;
4356 goto process;
4357 }
4358
Josef Bacik5dc562c2012-08-17 13:14:17 -04004359 if (em->generation <= test_gen)
4360 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004361
Filipe Manana31d11b82018-05-09 16:01:46 +01004362 /* We log prealloc extents beyond eof later. */
4363 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4364 em->start >= i_size_read(&inode->vfs_inode))
4365 continue;
4366
Josef Bacik8c6c5922017-08-29 10:11:39 -04004367 if (em->start < logged_start)
4368 logged_start = em->start;
4369 if ((em->start + em->len - 1) > logged_end)
4370 logged_end = em->start + em->len - 1;
4371
Josef Bacikff44c6e2012-09-14 12:59:20 -04004372 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004373 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004374 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004375 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004376 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004377 }
4378
4379 list_sort(NULL, &extents, extent_cmp);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004380process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004381 while (!list_empty(&extents)) {
4382 em = list_entry(extents.next, struct extent_map, list);
4383
4384 list_del_init(&em->list);
4385
4386 /*
4387 * If we had an error we just need to delete everybody from our
4388 * private list.
4389 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004390 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004391 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004392 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004393 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004394 }
4395
4396 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004397
Josef Bacika2120a42018-05-23 11:58:35 -04004398 ret = log_one_extent(trans, inode, root, em, path, ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004399 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004400 clear_em_logging(tree, em);
4401 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004402 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004403 WARN_ON(!list_empty(&extents));
4404 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004405
Josef Bacik5dc562c2012-08-17 13:14:17 -04004406 btrfs_release_path(path);
Filipe Manana31d11b82018-05-09 16:01:46 +01004407 if (!ret)
4408 ret = btrfs_log_prealloc_extents(trans, inode, path);
4409
Josef Bacik5dc562c2012-08-17 13:14:17 -04004410 return ret;
4411}
4412
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004413static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004414 struct btrfs_path *path, u64 *size_ret)
4415{
4416 struct btrfs_key key;
4417 int ret;
4418
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004419 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004420 key.type = BTRFS_INODE_ITEM_KEY;
4421 key.offset = 0;
4422
4423 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4424 if (ret < 0) {
4425 return ret;
4426 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004427 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004428 } else {
4429 struct btrfs_inode_item *item;
4430
4431 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4432 struct btrfs_inode_item);
4433 *size_ret = btrfs_inode_size(path->nodes[0], item);
Filipe Manana22dcb302019-03-04 14:06:12 +00004434 /*
4435 * If the in-memory inode's i_size is smaller then the inode
4436 * size stored in the btree, return the inode's i_size, so
4437 * that we get a correct inode size after replaying the log
4438 * when before a power failure we had a shrinking truncate
4439 * followed by addition of a new name (rename / new hard link).
4440 * Otherwise return the inode size from the btree, to avoid
4441 * data loss when replaying a log due to previously doing a
4442 * write that expands the inode's size and logging a new name
4443 * immediately after.
4444 */
4445 if (*size_ret > inode->vfs_inode.i_size)
4446 *size_ret = inode->vfs_inode.i_size;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004447 }
4448
4449 btrfs_release_path(path);
4450 return 0;
4451}
4452
Filipe Manana36283bf2015-06-20 00:44:51 +01004453/*
4454 * At the moment we always log all xattrs. This is to figure out at log replay
4455 * time which xattrs must have their deletion replayed. If a xattr is missing
4456 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4457 * because if a xattr is deleted, the inode is fsynced and a power failure
4458 * happens, causing the log to be replayed the next time the fs is mounted,
4459 * we want the xattr to not exist anymore (same behaviour as other filesystems
4460 * with a journal, ext3/4, xfs, f2fs, etc).
4461 */
4462static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4463 struct btrfs_root *root,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004464 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004465 struct btrfs_path *path,
4466 struct btrfs_path *dst_path)
4467{
4468 int ret;
4469 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004470 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004471 int ins_nr = 0;
4472 int start_slot = 0;
4473
4474 key.objectid = ino;
4475 key.type = BTRFS_XATTR_ITEM_KEY;
4476 key.offset = 0;
4477
4478 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4479 if (ret < 0)
4480 return ret;
4481
4482 while (true) {
4483 int slot = path->slots[0];
4484 struct extent_buffer *leaf = path->nodes[0];
4485 int nritems = btrfs_header_nritems(leaf);
4486
4487 if (slot >= nritems) {
4488 if (ins_nr > 0) {
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004489 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana587292a2019-11-19 12:07:33 +00004490 start_slot, ins_nr, 1, 0);
Filipe Manana36283bf2015-06-20 00:44:51 +01004491 if (ret < 0)
4492 return ret;
4493 ins_nr = 0;
4494 }
4495 ret = btrfs_next_leaf(root, path);
4496 if (ret < 0)
4497 return ret;
4498 else if (ret > 0)
4499 break;
4500 continue;
4501 }
4502
4503 btrfs_item_key_to_cpu(leaf, &key, slot);
4504 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4505 break;
4506
4507 if (ins_nr == 0)
4508 start_slot = slot;
4509 ins_nr++;
4510 path->slots[0]++;
4511 cond_resched();
4512 }
4513 if (ins_nr > 0) {
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004514 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana587292a2019-11-19 12:07:33 +00004515 start_slot, ins_nr, 1, 0);
Filipe Manana36283bf2015-06-20 00:44:51 +01004516 if (ret < 0)
4517 return ret;
4518 }
4519
4520 return 0;
4521}
4522
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004523/*
Filipe Manana587292a2019-11-19 12:07:33 +00004524 * When using the NO_HOLES feature if we punched a hole that causes the
4525 * deletion of entire leafs or all the extent items of the first leaf (the one
4526 * that contains the inode item and references) we may end up not processing
4527 * any extents, because there are no leafs with a generation matching the
4528 * current transaction that have extent items for our inode. So we need to find
4529 * if any holes exist and then log them. We also need to log holes after any
4530 * truncate operation that changes the inode's size.
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004531 */
Filipe Manana587292a2019-11-19 12:07:33 +00004532static int btrfs_log_holes(struct btrfs_trans_handle *trans,
4533 struct btrfs_root *root,
4534 struct btrfs_inode *inode,
4535 struct btrfs_path *path)
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004536{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004537 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004538 struct btrfs_key key;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004539 const u64 ino = btrfs_ino(inode);
4540 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Manana587292a2019-11-19 12:07:33 +00004541 u64 prev_extent_end = 0;
4542 int ret;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004543
Filipe Manana587292a2019-11-19 12:07:33 +00004544 if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004545 return 0;
4546
4547 key.objectid = ino;
4548 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana587292a2019-11-19 12:07:33 +00004549 key.offset = 0;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004550
4551 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004552 if (ret < 0)
4553 return ret;
4554
Filipe Manana587292a2019-11-19 12:07:33 +00004555 while (true) {
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004556 struct btrfs_file_extent_item *extent;
Filipe Manana587292a2019-11-19 12:07:33 +00004557 struct extent_buffer *leaf = path->nodes[0];
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004558 u64 len;
4559
Filipe Manana587292a2019-11-19 12:07:33 +00004560 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4561 ret = btrfs_next_leaf(root, path);
4562 if (ret < 0)
4563 return ret;
4564 if (ret > 0) {
4565 ret = 0;
4566 break;
4567 }
4568 leaf = path->nodes[0];
4569 }
4570
4571 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4572 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
4573 break;
4574
4575 /* We have a hole, log it. */
4576 if (prev_extent_end < key.offset) {
4577 const u64 hole_len = key.offset - prev_extent_end;
4578
4579 /*
4580 * Release the path to avoid deadlocks with other code
4581 * paths that search the root while holding locks on
4582 * leafs from the log root.
4583 */
4584 btrfs_release_path(path);
4585 ret = btrfs_insert_file_extent(trans, root->log_root,
4586 ino, prev_extent_end, 0,
4587 0, hole_len, 0, hole_len,
4588 0, 0, 0);
4589 if (ret < 0)
4590 return ret;
4591
4592 /*
4593 * Search for the same key again in the root. Since it's
4594 * an extent item and we are holding the inode lock, the
4595 * key must still exist. If it doesn't just emit warning
4596 * and return an error to fall back to a transaction
4597 * commit.
4598 */
4599 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4600 if (ret < 0)
4601 return ret;
4602 if (WARN_ON(ret > 0))
4603 return -ENOENT;
4604 leaf = path->nodes[0];
4605 }
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004606
4607 extent = btrfs_item_ptr(leaf, path->slots[0],
4608 struct btrfs_file_extent_item);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004609 if (btrfs_file_extent_type(leaf, extent) ==
Filipe Manana587292a2019-11-19 12:07:33 +00004610 BTRFS_FILE_EXTENT_INLINE) {
4611 len = btrfs_file_extent_ram_bytes(leaf, extent);
4612 prev_extent_end = ALIGN(key.offset + len,
4613 fs_info->sectorsize);
4614 } else {
4615 len = btrfs_file_extent_num_bytes(leaf, extent);
4616 prev_extent_end = key.offset + len;
4617 }
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004618
Filipe Manana587292a2019-11-19 12:07:33 +00004619 path->slots[0]++;
4620 cond_resched();
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004621 }
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004622
Filipe Manana587292a2019-11-19 12:07:33 +00004623 if (prev_extent_end < i_size) {
4624 u64 hole_len;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004625
Filipe Manana587292a2019-11-19 12:07:33 +00004626 btrfs_release_path(path);
4627 hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
4628 ret = btrfs_insert_file_extent(trans, root->log_root,
4629 ino, prev_extent_end, 0, 0,
4630 hole_len, 0, hole_len,
4631 0, 0, 0);
4632 if (ret < 0)
4633 return ret;
4634 }
4635
4636 return 0;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004637}
4638
Filipe Manana56f23fd2016-03-30 23:37:21 +01004639/*
4640 * When we are logging a new inode X, check if it doesn't have a reference that
4641 * matches the reference from some other inode Y created in a past transaction
4642 * and that was renamed in the current transaction. If we don't do this, then at
4643 * log replay time we can lose inode Y (and all its files if it's a directory):
4644 *
4645 * mkdir /mnt/x
4646 * echo "hello world" > /mnt/x/foobar
4647 * sync
4648 * mv /mnt/x /mnt/y
4649 * mkdir /mnt/x # or touch /mnt/x
4650 * xfs_io -c fsync /mnt/x
4651 * <power fail>
4652 * mount fs, trigger log replay
4653 *
4654 * After the log replay procedure, we would lose the first directory and all its
4655 * files (file foobar).
4656 * For the case where inode Y is not a directory we simply end up losing it:
4657 *
4658 * echo "123" > /mnt/foo
4659 * sync
4660 * mv /mnt/foo /mnt/bar
4661 * echo "abc" > /mnt/foo
4662 * xfs_io -c fsync /mnt/foo
4663 * <power fail>
4664 *
4665 * We also need this for cases where a snapshot entry is replaced by some other
4666 * entry (file or directory) otherwise we end up with an unreplayable log due to
4667 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4668 * if it were a regular entry:
4669 *
4670 * mkdir /mnt/x
4671 * btrfs subvolume snapshot /mnt /mnt/x/snap
4672 * btrfs subvolume delete /mnt/x/snap
4673 * rmdir /mnt/x
4674 * mkdir /mnt/x
4675 * fsync /mnt/x or fsync some new file inside it
4676 * <power fail>
4677 *
4678 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4679 * the same transaction.
4680 */
4681static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4682 const int slot,
4683 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004684 struct btrfs_inode *inode,
Filipe Manana44f714d2016-06-06 16:11:13 +01004685 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004686{
4687 int ret;
4688 struct btrfs_path *search_path;
4689 char *name = NULL;
4690 u32 name_len = 0;
4691 u32 item_size = btrfs_item_size_nr(eb, slot);
4692 u32 cur_offset = 0;
4693 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4694
4695 search_path = btrfs_alloc_path();
4696 if (!search_path)
4697 return -ENOMEM;
4698 search_path->search_commit_root = 1;
4699 search_path->skip_locking = 1;
4700
4701 while (cur_offset < item_size) {
4702 u64 parent;
4703 u32 this_name_len;
4704 u32 this_len;
4705 unsigned long name_ptr;
4706 struct btrfs_dir_item *di;
4707
4708 if (key->type == BTRFS_INODE_REF_KEY) {
4709 struct btrfs_inode_ref *iref;
4710
4711 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4712 parent = key->offset;
4713 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4714 name_ptr = (unsigned long)(iref + 1);
4715 this_len = sizeof(*iref) + this_name_len;
4716 } else {
4717 struct btrfs_inode_extref *extref;
4718
4719 extref = (struct btrfs_inode_extref *)(ptr +
4720 cur_offset);
4721 parent = btrfs_inode_extref_parent(eb, extref);
4722 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4723 name_ptr = (unsigned long)&extref->name;
4724 this_len = sizeof(*extref) + this_name_len;
4725 }
4726
4727 if (this_name_len > name_len) {
4728 char *new_name;
4729
4730 new_name = krealloc(name, this_name_len, GFP_NOFS);
4731 if (!new_name) {
4732 ret = -ENOMEM;
4733 goto out;
4734 }
4735 name_len = this_name_len;
4736 name = new_name;
4737 }
4738
4739 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004740 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4741 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004742 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004743 struct btrfs_key di_key;
4744
4745 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4746 di, &di_key);
4747 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4748 ret = 1;
4749 *other_ino = di_key.objectid;
4750 } else {
4751 ret = -EAGAIN;
4752 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004753 goto out;
4754 } else if (IS_ERR(di)) {
4755 ret = PTR_ERR(di);
4756 goto out;
4757 }
4758 btrfs_release_path(search_path);
4759
4760 cur_offset += this_len;
4761 }
4762 ret = 0;
4763out:
4764 btrfs_free_path(search_path);
4765 kfree(name);
4766 return ret;
4767}
4768
Chris Masone02119d2008-09-05 16:13:11 -04004769/* log a single inode in the tree log.
4770 * At least one parent directory for this inode must exist in the tree
4771 * or be logged already.
4772 *
4773 * Any items from this inode changed by the current transaction are copied
4774 * to the log tree. An extra reference is taken on any extents in this
4775 * file, allowing us to avoid a whole pile of corner cases around logging
4776 * blocks that have been removed from the tree.
4777 *
4778 * See LOG_INODE_ALL and related defines for a description of what inode_only
4779 * does.
4780 *
4781 * This handles both files and directories.
4782 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004783static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004784 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004785 int inode_only,
4786 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004787 const loff_t end,
4788 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004789{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004790 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004791 struct btrfs_path *path;
4792 struct btrfs_path *dst_path;
4793 struct btrfs_key min_key;
4794 struct btrfs_key max_key;
4795 struct btrfs_root *log = root->log_root;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004796 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004797 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004798 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004799 int ins_start_slot = 0;
4800 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004801 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004802 u64 ino = btrfs_ino(inode);
4803 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004804 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004805 bool need_log_inode_item = true;
Filipe Manana9a8fca62018-05-11 16:42:42 +01004806 bool xattrs_logged = false;
Chris Masone02119d2008-09-05 16:13:11 -04004807
Chris Masone02119d2008-09-05 16:13:11 -04004808 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004809 if (!path)
4810 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004811 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004812 if (!dst_path) {
4813 btrfs_free_path(path);
4814 return -ENOMEM;
4815 }
Chris Masone02119d2008-09-05 16:13:11 -04004816
Li Zefan33345d012011-04-20 10:31:50 +08004817 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004818 min_key.type = BTRFS_INODE_ITEM_KEY;
4819 min_key.offset = 0;
4820
Li Zefan33345d012011-04-20 10:31:50 +08004821 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004822
Chris Mason12fcfd22009-03-24 10:24:20 -04004823
Josef Bacik5dc562c2012-08-17 13:14:17 -04004824 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004825 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00004826 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004827 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08004828 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004829 max_key.type = BTRFS_XATTR_ITEM_KEY;
4830 else
4831 max_key.type = (u8)-1;
4832 max_key.offset = (u64)-1;
4833
Filipe Manana2c2c4522015-01-13 16:40:04 +00004834 /*
4835 * Only run delayed items if we are a dir or a new file.
4836 * Otherwise commit the delayed inode only, which is needed in
4837 * order for the log replay code to mark inodes for link count
4838 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4839 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004840 if (S_ISDIR(inode->vfs_inode.i_mode) ||
4841 inode->generation > fs_info->last_trans_committed)
4842 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004843 else
Nikolay Borisova59108a2017-01-18 00:31:48 +02004844 ret = btrfs_commit_inode_delayed_inode(inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004845
4846 if (ret) {
4847 btrfs_free_path(path);
4848 btrfs_free_path(dst_path);
4849 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004850 }
4851
Liu Bo781feef2016-11-30 16:20:25 -08004852 if (inode_only == LOG_OTHER_INODE) {
4853 inode_only = LOG_INODE_EXISTS;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004854 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08004855 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004856 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08004857 }
Chris Masone02119d2008-09-05 16:13:11 -04004858
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004859 /*
Chris Masone02119d2008-09-05 16:13:11 -04004860 * a brute force approach to making sure we get the most uptodate
4861 * copies of everything.
4862 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004863 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004864 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4865
Filipe Manana4f764e52015-02-23 19:53:35 +00004866 if (inode_only == LOG_INODE_EXISTS)
4867 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004868 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004869 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004870 if (inode_only == LOG_INODE_EXISTS) {
4871 /*
4872 * Make sure the new inode item we write to the log has
4873 * the same isize as the current one (if it exists).
4874 * This is necessary to prevent data loss after log
4875 * replay, and also to prevent doing a wrong expanding
4876 * truncate - for e.g. create file, write 4K into offset
4877 * 0, fsync, write 4K into offset 4096, add hard link,
4878 * fsync some other file (to sync log), power fail - if
4879 * we use the inode's current i_size, after log replay
4880 * we get a 8Kb file, with the last 4Kb extent as a hole
4881 * (zeroes), as if an expanding truncate happened,
4882 * instead of getting a file of 4Kb only.
4883 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004884 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004885 if (err)
4886 goto out_unlock;
4887 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004888 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004889 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00004890 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004891 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004892 ret = drop_objectid_items(trans, log, path, ino,
4893 max_key.type);
4894 } else {
4895 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004896 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00004897 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004898 &inode->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004899 while(1) {
4900 ret = btrfs_truncate_inode_items(trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004901 log, &inode->vfs_inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08004902 if (ret != -EAGAIN)
4903 break;
4904 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004905 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004906 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004907 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004908 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004909 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004910 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004911 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004912 ret = drop_objectid_items(trans, log, path, ino,
4913 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004914 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004915 if (inode_only == LOG_INODE_ALL)
4916 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004917 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004918 }
Josef Bacika95249b2012-10-11 16:17:34 -04004919
Chris Masone02119d2008-09-05 16:13:11 -04004920 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004921 if (ret) {
4922 err = ret;
4923 goto out_unlock;
4924 }
Chris Masone02119d2008-09-05 16:13:11 -04004925
Chris Masond3977122009-01-05 21:25:51 -05004926 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004927 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004928 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004929 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004930 if (ret < 0) {
4931 err = ret;
4932 goto out_unlock;
4933 }
Chris Masone02119d2008-09-05 16:13:11 -04004934 if (ret != 0)
4935 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004936again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004937 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004938 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004939 break;
4940 if (min_key.type > max_key.type)
4941 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004942
Filipe Mananae4545de2015-06-17 12:49:23 +01004943 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4944 need_log_inode_item = false;
4945
Filipe Manana56f23fd2016-03-30 23:37:21 +01004946 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4947 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
Nikolay Borisova59108a2017-01-18 00:31:48 +02004948 inode->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004949 u64 other_ino = 0;
4950
Filipe Manana56f23fd2016-03-30 23:37:21 +01004951 ret = btrfs_check_ref_name_override(path->nodes[0],
Nikolay Borisova59108a2017-01-18 00:31:48 +02004952 path->slots[0], &min_key, inode,
4953 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004954 if (ret < 0) {
4955 err = ret;
4956 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004957 } else if (ret > 0 && ctx &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004958 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004959 struct btrfs_key inode_key;
4960 struct inode *other_inode;
4961
4962 if (ins_nr > 0) {
4963 ins_nr++;
4964 } else {
4965 ins_nr = 1;
4966 ins_start_slot = path->slots[0];
4967 }
Nikolay Borisova59108a2017-01-18 00:31:48 +02004968 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana587292a2019-11-19 12:07:33 +00004969 ins_start_slot,
Filipe Manana44f714d2016-06-06 16:11:13 +01004970 ins_nr, inode_only,
4971 logged_isize);
4972 if (ret < 0) {
4973 err = ret;
4974 goto out_unlock;
4975 }
4976 ins_nr = 0;
4977 btrfs_release_path(path);
4978 inode_key.objectid = other_ino;
4979 inode_key.type = BTRFS_INODE_ITEM_KEY;
4980 inode_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004981 other_inode = btrfs_iget(fs_info->sb,
Filipe Manana44f714d2016-06-06 16:11:13 +01004982 &inode_key, root,
4983 NULL);
4984 /*
4985 * If the other inode that had a conflicting dir
4986 * entry was deleted in the current transaction,
4987 * we don't need to do more work nor fallback to
4988 * a transaction commit.
4989 */
Al Viro8d9e2202018-07-29 23:04:46 +01004990 if (other_inode == ERR_PTR(-ENOENT)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004991 goto next_key;
4992 } else if (IS_ERR(other_inode)) {
4993 err = PTR_ERR(other_inode);
4994 goto out_unlock;
4995 }
4996 /*
4997 * We are safe logging the other inode without
4998 * acquiring its i_mutex as long as we log with
4999 * the LOG_INODE_EXISTS mode. We're safe against
5000 * concurrent renames of the other inode as well
5001 * because during a rename we pin the log and
5002 * update the log with the new name before we
5003 * unpin it.
5004 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005005 err = btrfs_log_inode(trans, root,
5006 BTRFS_I(other_inode),
5007 LOG_OTHER_INODE, 0, LLONG_MAX,
5008 ctx);
Filipe Manana7cbd49c2019-09-10 15:26:49 +01005009 btrfs_add_delayed_iput(other_inode);
Filipe Manana44f714d2016-06-06 16:11:13 +01005010 if (err)
5011 goto out_unlock;
5012 else
5013 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01005014 }
5015 }
5016
Filipe Manana36283bf2015-06-20 00:44:51 +01005017 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5018 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
5019 if (ins_nr == 0)
5020 goto next_slot;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005021 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana587292a2019-11-19 12:07:33 +00005022 ins_start_slot,
Filipe Manana36283bf2015-06-20 00:44:51 +01005023 ins_nr, inode_only, logged_isize);
5024 if (ret < 0) {
5025 err = ret;
5026 goto out_unlock;
5027 }
5028 ins_nr = 0;
Filipe Manana36283bf2015-06-20 00:44:51 +01005029 goto next_slot;
5030 }
5031
Chris Mason31ff1cd2008-09-11 16:17:57 -04005032 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5033 ins_nr++;
5034 goto next_slot;
5035 } else if (!ins_nr) {
5036 ins_start_slot = path->slots[0];
5037 ins_nr = 1;
5038 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04005039 }
5040
Filipe Manana587292a2019-11-19 12:07:33 +00005041 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005042 ins_start_slot, ins_nr, inode_only,
5043 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005044 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005045 err = ret;
5046 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02005047 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005048 ins_nr = 1;
5049 ins_start_slot = path->slots[0];
5050next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04005051
Chris Mason3a5f1d42008-09-11 15:53:37 -04005052 nritems = btrfs_header_nritems(path->nodes[0]);
5053 path->slots[0]++;
5054 if (path->slots[0] < nritems) {
5055 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
5056 path->slots[0]);
5057 goto again;
5058 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005059 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005060 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana587292a2019-11-19 12:07:33 +00005061 ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005062 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005063 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005064 err = ret;
5065 goto out_unlock;
5066 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005067 ins_nr = 0;
5068 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005069 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01005070next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005071 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04005072 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005073 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04005074 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005075 min_key.offset = 0;
5076 } else {
Chris Masone02119d2008-09-05 16:13:11 -04005077 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005078 }
Chris Masone02119d2008-09-05 16:13:11 -04005079 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005080 if (ins_nr) {
Filipe Manana587292a2019-11-19 12:07:33 +00005081 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005082 ins_start_slot, ins_nr, inode_only,
5083 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005084 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005085 err = ret;
5086 goto out_unlock;
5087 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005088 ins_nr = 0;
5089 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005090
Filipe Manana36283bf2015-06-20 00:44:51 +01005091 btrfs_release_path(path);
5092 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005093 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01005094 if (err)
5095 goto out_unlock;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005096 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005097 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5098 btrfs_release_path(path);
5099 btrfs_release_path(dst_path);
Filipe Manana587292a2019-11-19 12:07:33 +00005100 err = btrfs_log_holes(trans, root, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005101 if (err)
5102 goto out_unlock;
5103 }
Josef Bacika95249b2012-10-11 16:17:34 -04005104log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04005105 btrfs_release_path(path);
5106 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01005107 if (need_log_inode_item) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005108 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana9a8fca62018-05-11 16:42:42 +01005109 if (!err && !xattrs_logged) {
5110 err = btrfs_log_all_xattrs(trans, root, inode, path,
5111 dst_path);
5112 btrfs_release_path(path);
5113 }
Filipe Mananae4545de2015-06-17 12:49:23 +01005114 if (err)
5115 goto out_unlock;
5116 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005117 if (fast_search) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005118 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Josef Bacika2120a42018-05-23 11:58:35 -04005119 ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005120 if (ret) {
5121 err = ret;
5122 goto out_unlock;
5123 }
Josef Bacikd006a042013-11-12 20:54:09 -05005124 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06005125 struct extent_map *em, *n;
5126
Filipe Manana49dae1b2014-09-06 22:34:39 +01005127 write_lock(&em_tree->lock);
5128 /*
5129 * We can't just remove every em if we're called for a ranged
5130 * fsync - that is, one that doesn't cover the whole possible
5131 * file range (0 to LLONG_MAX). This is because we can have
5132 * em's that fall outside the range we're logging and therefore
5133 * their ordered operations haven't completed yet
5134 * (btrfs_finish_ordered_io() not invoked yet). This means we
5135 * didn't get their respective file extent item in the fs/subvol
5136 * tree yet, and need to let the next fast fsync (one which
5137 * consults the list of modified extent maps) find the em so
5138 * that it logs a matching file extent item and waits for the
5139 * respective ordered operation to complete (if it's still
5140 * running).
5141 *
5142 * Removing every em outside the range we're logging would make
5143 * the next fast fsync not log their matching file extent items,
5144 * therefore making us lose data after a log replay.
5145 */
5146 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5147 list) {
5148 const u64 mod_end = em->mod_start + em->mod_len - 1;
5149
5150 if (em->mod_start >= start && mod_end <= end)
5151 list_del_init(&em->list);
5152 }
5153 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005154 }
5155
Nikolay Borisova59108a2017-01-18 00:31:48 +02005156 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5157 ret = log_directory_changes(trans, root, inode, path, dst_path,
5158 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005159 if (ret) {
5160 err = ret;
5161 goto out_unlock;
5162 }
Chris Masone02119d2008-09-05 16:13:11 -04005163 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005164
Filipe Manana110850f2019-06-07 11:25:24 +01005165 /*
5166 * Don't update last_log_commit if we logged that an inode exists after
5167 * it was loaded to memory (full_sync bit set).
5168 * This is to prevent data loss when we do a write to the inode, then
5169 * the inode gets evicted after all delalloc was flushed, then we log
5170 * it exists (due to a rename for example) and then fsync it. This last
5171 * fsync would do nothing (not logging the extents previously written).
5172 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005173 spin_lock(&inode->lock);
5174 inode->logged_trans = trans->transid;
Filipe Manana110850f2019-06-07 11:25:24 +01005175 if (inode_only != LOG_INODE_EXISTS ||
5176 !test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
5177 inode->last_log_commit = inode->last_sub_trans;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005178 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005179out_unlock:
Nikolay Borisova59108a2017-01-18 00:31:48 +02005180 mutex_unlock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04005181
5182 btrfs_free_path(path);
5183 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005184 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005185}
5186
Chris Mason12fcfd22009-03-24 10:24:20 -04005187/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005188 * Check if we must fallback to a transaction commit when logging an inode.
5189 * This must be called after logging the inode and is used only in the context
5190 * when fsyncing an inode requires the need to log some other inode - in which
5191 * case we can't lock the i_mutex of each other inode we need to log as that
5192 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5193 * log inodes up or down in the hierarchy) or rename operations for example. So
5194 * we take the log_mutex of the inode after we have logged it and then check for
5195 * its last_unlink_trans value - this is safe because any task setting
5196 * last_unlink_trans must take the log_mutex and it must do this before it does
5197 * the actual unlink operation, so if we do this check before a concurrent task
5198 * sets last_unlink_trans it means we've logged a consistent version/state of
5199 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005200 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005201 * we logged the inode or it might have also done the unlink).
5202 */
5203static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005204 struct btrfs_inode *inode)
Filipe Manana2be63d52016-02-12 11:34:23 +00005205{
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005206 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana2be63d52016-02-12 11:34:23 +00005207 bool ret = false;
5208
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005209 mutex_lock(&inode->log_mutex);
5210 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
Filipe Manana2be63d52016-02-12 11:34:23 +00005211 /*
5212 * Make sure any commits to the log are forced to be full
5213 * commits.
5214 */
5215 btrfs_set_log_full_commit(fs_info, trans);
5216 ret = true;
5217 }
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005218 mutex_unlock(&inode->log_mutex);
Filipe Manana2be63d52016-02-12 11:34:23 +00005219
5220 return ret;
5221}
5222
5223/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005224 * follow the dentry parent pointers up the chain and see if any
5225 * of the directories in it require a full commit before they can
5226 * be logged. Returns zero if nothing special needs to be done or 1 if
5227 * a full commit is required.
5228 */
5229static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005230 struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005231 struct dentry *parent,
5232 struct super_block *sb,
5233 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005234{
Chris Mason12fcfd22009-03-24 10:24:20 -04005235 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005236 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005237
Chris Masonaf4176b2009-03-24 10:24:31 -04005238 /*
5239 * for regular files, if its inode is already on disk, we don't
5240 * have to worry about the parents at all. This is because
5241 * we can use the last_unlink_trans field to record renames
5242 * and other fun in this file.
5243 */
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005244 if (S_ISREG(inode->vfs_inode.i_mode) &&
5245 inode->generation <= last_committed &&
5246 inode->last_unlink_trans <= last_committed)
5247 goto out;
Chris Masonaf4176b2009-03-24 10:24:31 -04005248
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005249 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005250 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005251 goto out;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005252 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005253 }
5254
5255 while (1) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005256 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005257 ret = 1;
5258 break;
5259 }
5260
Al Virofc640052016-04-10 01:33:30 -04005261 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005262 break;
5263
Filipe Manana44f714d2016-06-06 16:11:13 +01005264 if (IS_ROOT(parent)) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005265 inode = BTRFS_I(d_inode(parent));
5266 if (btrfs_must_commit_transaction(trans, inode))
Filipe Manana44f714d2016-06-06 16:11:13 +01005267 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005268 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005269 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005270
Josef Bacik6a912212010-11-20 09:48:00 +00005271 parent = dget_parent(parent);
5272 dput(old_parent);
5273 old_parent = parent;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005274 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005275
5276 }
Josef Bacik6a912212010-11-20 09:48:00 +00005277 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005278out:
Chris Masone02119d2008-09-05 16:13:11 -04005279 return ret;
5280}
5281
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005282struct btrfs_dir_list {
5283 u64 ino;
5284 struct list_head list;
5285};
5286
5287/*
5288 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5289 * details about the why it is needed.
5290 * This is a recursive operation - if an existing dentry corresponds to a
5291 * directory, that directory's new entries are logged too (same behaviour as
5292 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5293 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5294 * complains about the following circular lock dependency / possible deadlock:
5295 *
5296 * CPU0 CPU1
5297 * ---- ----
5298 * lock(&type->i_mutex_dir_key#3/2);
5299 * lock(sb_internal#2);
5300 * lock(&type->i_mutex_dir_key#3/2);
5301 * lock(&sb->s_type->i_mutex_key#14);
5302 *
5303 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5304 * sb_start_intwrite() in btrfs_start_transaction().
5305 * Not locking i_mutex of the inodes is still safe because:
5306 *
5307 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5308 * that while logging the inode new references (names) are added or removed
5309 * from the inode, leaving the logged inode item with a link count that does
5310 * not match the number of logged inode reference items. This is fine because
5311 * at log replay time we compute the real number of links and correct the
5312 * link count in the inode item (see replay_one_buffer() and
5313 * link_to_fixup_dir());
5314 *
5315 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5316 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5317 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5318 * has a size that doesn't match the sum of the lengths of all the logged
5319 * names. This does not result in a problem because if a dir_item key is
5320 * logged but its matching dir_index key is not logged, at log replay time we
5321 * don't use it to replay the respective name (see replay_one_name()). On the
5322 * other hand if only the dir_index key ends up being logged, the respective
5323 * name is added to the fs/subvol tree with both the dir_item and dir_index
5324 * keys created (see replay_one_name()).
5325 * The directory's inode item with a wrong i_size is not a problem as well,
5326 * since we don't use it at log replay time to set the i_size in the inode
5327 * item of the fs/subvol tree (see overwrite_item()).
5328 */
5329static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5330 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005331 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005332 struct btrfs_log_ctx *ctx)
5333{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005334 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005335 struct btrfs_root *log = root->log_root;
5336 struct btrfs_path *path;
5337 LIST_HEAD(dir_list);
5338 struct btrfs_dir_list *dir_elem;
5339 int ret = 0;
5340
5341 path = btrfs_alloc_path();
5342 if (!path)
5343 return -ENOMEM;
5344
5345 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5346 if (!dir_elem) {
5347 btrfs_free_path(path);
5348 return -ENOMEM;
5349 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005350 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005351 list_add_tail(&dir_elem->list, &dir_list);
5352
5353 while (!list_empty(&dir_list)) {
5354 struct extent_buffer *leaf;
5355 struct btrfs_key min_key;
5356 int nritems;
5357 int i;
5358
5359 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5360 list);
5361 if (ret)
5362 goto next_dir_inode;
5363
5364 min_key.objectid = dir_elem->ino;
5365 min_key.type = BTRFS_DIR_ITEM_KEY;
5366 min_key.offset = 0;
5367again:
5368 btrfs_release_path(path);
5369 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5370 if (ret < 0) {
5371 goto next_dir_inode;
5372 } else if (ret > 0) {
5373 ret = 0;
5374 goto next_dir_inode;
5375 }
5376
5377process_leaf:
5378 leaf = path->nodes[0];
5379 nritems = btrfs_header_nritems(leaf);
5380 for (i = path->slots[0]; i < nritems; i++) {
5381 struct btrfs_dir_item *di;
5382 struct btrfs_key di_key;
5383 struct inode *di_inode;
5384 struct btrfs_dir_list *new_dir_elem;
5385 int log_mode = LOG_INODE_EXISTS;
5386 int type;
5387
5388 btrfs_item_key_to_cpu(leaf, &min_key, i);
5389 if (min_key.objectid != dir_elem->ino ||
5390 min_key.type != BTRFS_DIR_ITEM_KEY)
5391 goto next_dir_inode;
5392
5393 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5394 type = btrfs_dir_type(leaf, di);
5395 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5396 type != BTRFS_FT_DIR)
5397 continue;
5398 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5399 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5400 continue;
5401
Robbie Koec125cf2016-10-28 10:48:26 +08005402 btrfs_release_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005403 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005404 if (IS_ERR(di_inode)) {
5405 ret = PTR_ERR(di_inode);
5406 goto next_dir_inode;
5407 }
5408
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005409 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
Filipe Manana7cbd49c2019-09-10 15:26:49 +01005410 btrfs_add_delayed_iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005411 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005412 }
5413
5414 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005415 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005416 log_mode = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005417 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005418 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005419 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005420 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005421 ret = 1;
Filipe Manana7cbd49c2019-09-10 15:26:49 +01005422 btrfs_add_delayed_iput(di_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005423 if (ret)
5424 goto next_dir_inode;
5425 if (ctx->log_new_dentries) {
5426 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5427 GFP_NOFS);
5428 if (!new_dir_elem) {
5429 ret = -ENOMEM;
5430 goto next_dir_inode;
5431 }
5432 new_dir_elem->ino = di_key.objectid;
5433 list_add_tail(&new_dir_elem->list, &dir_list);
5434 }
5435 break;
5436 }
5437 if (i == nritems) {
5438 ret = btrfs_next_leaf(log, path);
5439 if (ret < 0) {
5440 goto next_dir_inode;
5441 } else if (ret > 0) {
5442 ret = 0;
5443 goto next_dir_inode;
5444 }
5445 goto process_leaf;
5446 }
5447 if (min_key.offset < (u64)-1) {
5448 min_key.offset++;
5449 goto again;
5450 }
5451next_dir_inode:
5452 list_del(&dir_elem->list);
5453 kfree(dir_elem);
5454 }
5455
5456 btrfs_free_path(path);
5457 return ret;
5458}
5459
Filipe Manana18aa0922015-08-05 16:49:08 +01005460static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005461 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01005462 struct btrfs_log_ctx *ctx)
5463{
David Sterba3ffbd682018-06-29 10:56:42 +02005464 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana18aa0922015-08-05 16:49:08 +01005465 int ret;
5466 struct btrfs_path *path;
5467 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005468 struct btrfs_root *root = inode->root;
5469 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005470
5471 path = btrfs_alloc_path();
5472 if (!path)
5473 return -ENOMEM;
5474 path->skip_locking = 1;
5475 path->search_commit_root = 1;
5476
5477 key.objectid = ino;
5478 key.type = BTRFS_INODE_REF_KEY;
5479 key.offset = 0;
5480 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5481 if (ret < 0)
5482 goto out;
5483
5484 while (true) {
5485 struct extent_buffer *leaf = path->nodes[0];
5486 int slot = path->slots[0];
5487 u32 cur_offset = 0;
5488 u32 item_size;
5489 unsigned long ptr;
5490
5491 if (slot >= btrfs_header_nritems(leaf)) {
5492 ret = btrfs_next_leaf(root, path);
5493 if (ret < 0)
5494 goto out;
5495 else if (ret > 0)
5496 break;
5497 continue;
5498 }
5499
5500 btrfs_item_key_to_cpu(leaf, &key, slot);
5501 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5502 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5503 break;
5504
5505 item_size = btrfs_item_size_nr(leaf, slot);
5506 ptr = btrfs_item_ptr_offset(leaf, slot);
5507 while (cur_offset < item_size) {
5508 struct btrfs_key inode_key;
5509 struct inode *dir_inode;
5510
5511 inode_key.type = BTRFS_INODE_ITEM_KEY;
5512 inode_key.offset = 0;
5513
5514 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5515 struct btrfs_inode_extref *extref;
5516
5517 extref = (struct btrfs_inode_extref *)
5518 (ptr + cur_offset);
5519 inode_key.objectid = btrfs_inode_extref_parent(
5520 leaf, extref);
5521 cur_offset += sizeof(*extref);
5522 cur_offset += btrfs_inode_extref_name_len(leaf,
5523 extref);
5524 } else {
5525 inode_key.objectid = key.offset;
5526 cur_offset = item_size;
5527 }
5528
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005529 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
Filipe Manana18aa0922015-08-05 16:49:08 +01005530 root, NULL);
Filipe Mananad2c6df32018-10-09 15:05:29 +01005531 /*
5532 * If the parent inode was deleted, return an error to
5533 * fallback to a transaction commit. This is to prevent
5534 * getting an inode that was moved from one parent A to
5535 * a parent B, got its former parent A deleted and then
5536 * it got fsync'ed, from existing at both parents after
5537 * a log replay (and the old parent still existing).
5538 * Example:
5539 *
5540 * mkdir /mnt/A
5541 * mkdir /mnt/B
5542 * touch /mnt/B/bar
5543 * sync
5544 * mv /mnt/B/bar /mnt/A/bar
5545 * mv -T /mnt/A /mnt/B
5546 * fsync /mnt/B/bar
5547 * <power fail>
5548 *
5549 * If we ignore the old parent B which got deleted,
5550 * after a log replay we would have file bar linked
5551 * at both parents and the old parent B would still
5552 * exist.
5553 */
5554 if (IS_ERR(dir_inode)) {
5555 ret = PTR_ERR(dir_inode);
5556 goto out;
5557 }
Filipe Manana18aa0922015-08-05 16:49:08 +01005558
Filipe Manana657ed1a2016-04-06 17:11:56 +01005559 if (ctx)
5560 ctx->log_new_dentries = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005561 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Filipe Manana18aa0922015-08-05 16:49:08 +01005562 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005563 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005564 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005565 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005566 if (!ret && ctx && ctx->log_new_dentries)
5567 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01005568 BTRFS_I(dir_inode), ctx);
Filipe Manana7cbd49c2019-09-10 15:26:49 +01005569 btrfs_add_delayed_iput(dir_inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005570 if (ret)
5571 goto out;
5572 }
5573 path->slots[0]++;
5574 }
5575 ret = 0;
5576out:
5577 btrfs_free_path(path);
5578 return ret;
5579}
5580
Chris Masone02119d2008-09-05 16:13:11 -04005581/*
5582 * helper function around btrfs_log_inode to make sure newly created
5583 * parent directories also end up in the log. A minimal inode and backref
5584 * only logging is done of any parent directories that are older than
5585 * the last committed transaction
5586 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005587static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005588 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005589 struct dentry *parent,
5590 const loff_t start,
5591 const loff_t end,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07005592 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005593 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005594{
Nikolay Borisovf8822742018-02-27 17:37:17 +02005595 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005596 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04005597 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005598 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005599 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005600 u64 last_committed = fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005601 bool log_dentries = false;
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005602 struct btrfs_inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005603
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005604 sb = inode->vfs_inode.i_sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04005605
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005606 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005607 ret = 1;
5608 goto end_no_trans;
5609 }
5610
Miao Xie995946d2014-04-02 19:51:06 +08005611 /*
5612 * The prev transaction commit doesn't complete, we need do
5613 * full commit by ourselves.
5614 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005615 if (fs_info->last_trans_log_full_commit >
5616 fs_info->last_trans_committed) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005617 ret = 1;
5618 goto end_no_trans;
5619 }
5620
Nikolay Borisovf8822742018-02-27 17:37:17 +02005621 if (btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005622 ret = 1;
5623 goto end_no_trans;
5624 }
5625
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005626 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
5627 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04005628 if (ret)
5629 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005630
Filipe Manana55f21e12018-10-08 11:12:55 +01005631 /*
5632 * Skip already logged inodes or inodes corresponding to tmpfiles
5633 * (since logging them is pointless, a link count of 0 means they
5634 * will never be accessible).
5635 */
5636 if (btrfs_inode_in_log(inode, trans->transid) ||
5637 inode->vfs_inode.i_nlink == 0) {
Chris Mason257c62e2009-10-13 13:21:08 -04005638 ret = BTRFS_NO_LOG_SYNC;
5639 goto end_no_trans;
5640 }
5641
Miao Xie8b050d32014-02-20 18:08:58 +08005642 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005643 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005644 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005645
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005646 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005647 if (ret)
5648 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005649
Chris Masonaf4176b2009-03-24 10:24:31 -04005650 /*
5651 * for regular files, if its inode is already on disk, we don't
5652 * have to worry about the parents at all. This is because
5653 * we can use the last_unlink_trans field to record renames
5654 * and other fun in this file.
5655 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005656 if (S_ISREG(inode->vfs_inode.i_mode) &&
5657 inode->generation <= last_committed &&
5658 inode->last_unlink_trans <= last_committed) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005659 ret = 0;
5660 goto end_trans;
5661 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005662
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005663 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005664 log_dentries = true;
5665
Filipe Manana18aa0922015-08-05 16:49:08 +01005666 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005667 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005668 * inodes are fully logged. This is to prevent leaving dangling
5669 * directory index entries in directories that were our parents but are
5670 * not anymore. Not doing this results in old parent directory being
5671 * impossible to delete after log replay (rmdir will always fail with
5672 * error -ENOTEMPTY).
5673 *
5674 * Example 1:
5675 *
5676 * mkdir testdir
5677 * touch testdir/foo
5678 * ln testdir/foo testdir/bar
5679 * sync
5680 * unlink testdir/bar
5681 * xfs_io -c fsync testdir/foo
5682 * <power failure>
5683 * mount fs, triggers log replay
5684 *
5685 * If we don't log the parent directory (testdir), after log replay the
5686 * directory still has an entry pointing to the file inode using the bar
5687 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5688 * the file inode has a link count of 1.
5689 *
5690 * Example 2:
5691 *
5692 * mkdir testdir
5693 * touch foo
5694 * ln foo testdir/foo2
5695 * ln foo testdir/foo3
5696 * sync
5697 * unlink testdir/foo3
5698 * xfs_io -c fsync foo
5699 * <power failure>
5700 * mount fs, triggers log replay
5701 *
5702 * Similar as the first example, after log replay the parent directory
5703 * testdir still has an entry pointing to the inode file with name foo3
5704 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5705 * and has a link count of 2.
5706 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005707 if (inode->last_unlink_trans > last_committed) {
Filipe Manana18aa0922015-08-05 16:49:08 +01005708 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5709 if (ret)
5710 goto end_trans;
5711 }
5712
Filipe Manana10b04212018-11-28 14:54:28 +00005713 /*
5714 * If a new hard link was added to the inode in the current transaction
5715 * and its link count is now greater than 1, we need to fallback to a
5716 * transaction commit, otherwise we can end up not logging all its new
5717 * parents for all the hard links. Here just from the dentry used to
5718 * fsync, we can not visit the ancestor inodes for all the other hard
5719 * links to figure out if any is new, so we fallback to a transaction
5720 * commit (instead of adding a lot of complexity of scanning a btree,
5721 * since this scenario is not a common use case).
5722 */
5723 if (inode->vfs_inode.i_nlink > 1 &&
5724 inode->last_link_trans > last_committed) {
5725 ret = -EMLINK;
5726 goto end_trans;
5727 }
5728
Chris Masond3977122009-01-05 21:25:51 -05005729 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005730 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005731 break;
5732
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005733 inode = BTRFS_I(d_inode(parent));
5734 if (root != inode->root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005735 break;
5736
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005737 if (inode->generation > last_committed) {
5738 ret = btrfs_log_inode(trans, root, inode,
5739 LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005740 if (ret)
5741 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005742 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005743 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005744 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005745
Josef Bacik6a912212010-11-20 09:48:00 +00005746 parent = dget_parent(parent);
5747 dput(old_parent);
5748 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005749 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005750 if (log_dentries)
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005751 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005752 else
5753 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005754end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005755 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005756 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005757 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005758 ret = 1;
5759 }
Miao Xie8b050d32014-02-20 18:08:58 +08005760
5761 if (ret)
5762 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005763 btrfs_end_log_trans(root);
5764end_no_trans:
5765 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005766}
5767
5768/*
5769 * it is not safe to log dentry if the chunk root has added new
5770 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5771 * If this returns 1, you must commit the transaction to safely get your
5772 * data on disk.
5773 */
5774int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Nikolay Borisove5b84f7a2018-02-27 17:37:18 +02005775 struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005776 const loff_t start,
5777 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005778 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005779{
Josef Bacik6a912212010-11-20 09:48:00 +00005780 struct dentry *parent = dget_parent(dentry);
5781 int ret;
5782
Nikolay Borisovf8822742018-02-27 17:37:17 +02005783 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
5784 start, end, LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005785 dput(parent);
5786
5787 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005788}
5789
5790/*
5791 * should be called during mount to recover any replay any log trees
5792 * from the FS
5793 */
5794int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5795{
5796 int ret;
5797 struct btrfs_path *path;
5798 struct btrfs_trans_handle *trans;
5799 struct btrfs_key key;
5800 struct btrfs_key found_key;
5801 struct btrfs_key tmp_key;
5802 struct btrfs_root *log;
5803 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5804 struct walk_control wc = {
5805 .process_func = process_one_buffer,
5806 .stage = 0,
5807 };
5808
Chris Masone02119d2008-09-05 16:13:11 -04005809 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005810 if (!path)
5811 return -ENOMEM;
5812
Josef Bacikafcdd122016-09-02 15:40:02 -04005813 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005814
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005815 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005816 if (IS_ERR(trans)) {
5817 ret = PTR_ERR(trans);
5818 goto error;
5819 }
Chris Masone02119d2008-09-05 16:13:11 -04005820
5821 wc.trans = trans;
5822 wc.pin = 1;
5823
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005824 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005825 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005826 btrfs_handle_fs_error(fs_info, ret,
5827 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005828 goto error;
5829 }
Chris Masone02119d2008-09-05 16:13:11 -04005830
5831again:
5832 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5833 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005834 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005835
Chris Masond3977122009-01-05 21:25:51 -05005836 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005837 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005838
5839 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005840 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005841 "Couldn't find tree log root.");
5842 goto error;
5843 }
Chris Masone02119d2008-09-05 16:13:11 -04005844 if (ret > 0) {
5845 if (path->slots[0] == 0)
5846 break;
5847 path->slots[0]--;
5848 }
5849 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5850 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005851 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005852 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5853 break;
5854
Miao Xiecb517ea2013-05-15 07:48:19 +00005855 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005856 if (IS_ERR(log)) {
5857 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005858 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005859 "Couldn't read tree log root.");
5860 goto error;
5861 }
Chris Masone02119d2008-09-05 16:13:11 -04005862
5863 tmp_key.objectid = found_key.offset;
5864 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5865 tmp_key.offset = (u64)-1;
5866
5867 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005868 if (IS_ERR(wc.replay_dest)) {
5869 ret = PTR_ERR(wc.replay_dest);
Josef Bacikf5c97352019-12-06 09:37:17 -05005870
5871 /*
5872 * We didn't find the subvol, likely because it was
5873 * deleted. This is ok, simply skip this log and go to
5874 * the next one.
5875 *
5876 * We need to exclude the root because we can't have
5877 * other log replays overwriting this log as we'll read
5878 * it back in a few more times. This will keep our
5879 * block from being modified, and we'll just bail for
5880 * each subsequent pass.
5881 */
5882 if (ret == -ENOENT)
5883 ret = btrfs_pin_extent_for_log_replay(fs_info,
5884 log->node->start,
5885 log->node->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005886 free_extent_buffer(log->node);
5887 free_extent_buffer(log->commit_root);
5888 kfree(log);
Josef Bacikf5c97352019-12-06 09:37:17 -05005889
5890 if (!ret)
5891 goto next;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005892 btrfs_handle_fs_error(fs_info, ret,
5893 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005894 goto error;
5895 }
Chris Masone02119d2008-09-05 16:13:11 -04005896
Yan Zheng07d400a2009-01-06 11:42:00 -05005897 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005898 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005899 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005900
Josef Bacikb50c6e22013-04-25 15:55:30 -04005901 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005902 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5903 path);
Chris Masone02119d2008-09-05 16:13:11 -04005904 }
Chris Masone02119d2008-09-05 16:13:11 -04005905
Liu Bo900c9982018-01-25 11:02:56 -07005906 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5907 struct btrfs_root *root = wc.replay_dest;
5908
5909 btrfs_release_path(path);
5910
5911 /*
5912 * We have just replayed everything, and the highest
5913 * objectid of fs roots probably has changed in case
5914 * some inode_item's got replayed.
5915 *
5916 * root->objectid_mutex is not acquired as log replay
5917 * could only happen during mount.
5918 */
5919 ret = btrfs_find_highest_objectid(root,
5920 &root->highest_objectid);
5921 }
5922
Yan Zheng07d400a2009-01-06 11:42:00 -05005923 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005924 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005925 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005926 kfree(log);
5927
Josef Bacikb50c6e22013-04-25 15:55:30 -04005928 if (ret)
5929 goto error;
Josef Bacikf5c97352019-12-06 09:37:17 -05005930next:
Chris Masone02119d2008-09-05 16:13:11 -04005931 if (found_key.offset == 0)
5932 break;
Josef Bacikf5c97352019-12-06 09:37:17 -05005933 key.offset = found_key.offset - 1;
Chris Masone02119d2008-09-05 16:13:11 -04005934 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005935 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005936
5937 /* step one is to pin it all, step two is to replay just inodes */
5938 if (wc.pin) {
5939 wc.pin = 0;
5940 wc.process_func = replay_one_buffer;
5941 wc.stage = LOG_WALK_REPLAY_INODES;
5942 goto again;
5943 }
5944 /* step three is to replay everything */
5945 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5946 wc.stage++;
5947 goto again;
5948 }
5949
5950 btrfs_free_path(path);
5951
Josef Bacikabefa552013-04-24 16:40:05 -04005952 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005953 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04005954 if (ret)
5955 return ret;
5956
Chris Masone02119d2008-09-05 16:13:11 -04005957 free_extent_buffer(log_root_tree->node);
5958 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005959 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005960 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005961
Josef Bacikabefa552013-04-24 16:40:05 -04005962 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005963error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005964 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005965 btrfs_end_transaction(wc.trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005966 btrfs_free_path(path);
5967 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005968}
Chris Mason12fcfd22009-03-24 10:24:20 -04005969
5970/*
5971 * there are some corner cases where we want to force a full
5972 * commit instead of allowing a directory to be logged.
5973 *
5974 * They revolve around files there were unlinked from the directory, and
5975 * this function updates the parent directory so that a full commit is
5976 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005977 *
5978 * Must be called before the unlink operations (updates to the subvolume tree,
5979 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005980 */
5981void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005982 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005983 int for_rename)
5984{
5985 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005986 * when we're logging a file, if it hasn't been renamed
5987 * or unlinked, and its inode is fully committed on disk,
5988 * we don't have to worry about walking up the directory chain
5989 * to log its parents.
5990 *
5991 * So, we use the last_unlink_trans field to put this transid
5992 * into the file. When the file is logged we check it and
5993 * don't log the parents if the file is fully on disk.
5994 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005995 mutex_lock(&inode->log_mutex);
5996 inode->last_unlink_trans = trans->transid;
5997 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005998
5999 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006000 * if this directory was already logged any new
6001 * names for this file/dir will get recorded
6002 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006003 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006004 return;
6005
6006 /*
6007 * if the inode we're about to unlink was logged,
6008 * the log will be properly updated for any new names
6009 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006010 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006011 return;
6012
6013 /*
6014 * when renaming files across directories, if the directory
6015 * there we're unlinking from gets fsync'd later on, there's
6016 * no way to find the destination directory later and fsync it
6017 * properly. So, we have to be conservative and force commits
6018 * so the new name gets discovered.
6019 */
6020 if (for_rename)
6021 goto record;
6022
6023 /* we can safely do the unlink without any special recording */
6024 return;
6025
6026record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006027 mutex_lock(&dir->log_mutex);
6028 dir->last_unlink_trans = trans->transid;
6029 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04006030}
6031
6032/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006033 * Make sure that if someone attempts to fsync the parent directory of a deleted
6034 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6035 * that after replaying the log tree of the parent directory's root we will not
6036 * see the snapshot anymore and at log replay time we will not see any log tree
6037 * corresponding to the deleted snapshot's root, which could lead to replaying
6038 * it after replaying the log tree of the parent directory (which would replay
6039 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00006040 *
6041 * Must be called before the actual snapshot destroy operation (updates to the
6042 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006043 */
6044void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02006045 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006046{
Nikolay Borisov43663552017-01-18 00:31:29 +02006047 mutex_lock(&dir->log_mutex);
6048 dir->last_unlink_trans = trans->transid;
6049 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006050}
6051
6052/*
Chris Mason12fcfd22009-03-24 10:24:20 -04006053 * Call this after adding a new name for a file and it will properly
6054 * update the log to reflect the new name.
6055 *
Filipe Mananad4682ba2018-06-11 19:24:28 +01006056 * @ctx can not be NULL when @sync_log is false, and should be NULL when it's
6057 * true (because it's not used).
6058 *
6059 * Return value depends on whether @sync_log is true or false.
6060 * When true: returns BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6061 * committed by the caller, and BTRFS_DONT_NEED_TRANS_COMMIT
6062 * otherwise.
6063 * When false: returns BTRFS_DONT_NEED_LOG_SYNC if the caller does not need to
6064 * to sync the log, BTRFS_NEED_LOG_SYNC if it needs to sync the log,
6065 * or BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6066 * committed (without attempting to sync the log).
Chris Mason12fcfd22009-03-24 10:24:20 -04006067 */
6068int btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006069 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Filipe Mananad4682ba2018-06-11 19:24:28 +01006070 struct dentry *parent,
6071 bool sync_log, struct btrfs_log_ctx *ctx)
Chris Mason12fcfd22009-03-24 10:24:20 -04006072{
David Sterba3ffbd682018-06-29 10:56:42 +02006073 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006074 int ret;
Chris Mason12fcfd22009-03-24 10:24:20 -04006075
6076 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006077 * this will force the logging code to walk the dentry chain
6078 * up for the file
6079 */
Filipe Manana9a6509c2018-02-28 15:55:40 +00006080 if (!S_ISDIR(inode->vfs_inode.i_mode))
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006081 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04006082
6083 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006084 * if this inode hasn't been logged and directory we're renaming it
6085 * from hasn't been logged, we don't need to log it
6086 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006087 if (inode->logged_trans <= fs_info->last_trans_committed &&
6088 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
Filipe Mananad4682ba2018-06-11 19:24:28 +01006089 return sync_log ? BTRFS_DONT_NEED_TRANS_COMMIT :
6090 BTRFS_DONT_NEED_LOG_SYNC;
Chris Mason12fcfd22009-03-24 10:24:20 -04006091
Filipe Mananad4682ba2018-06-11 19:24:28 +01006092 if (sync_log) {
6093 struct btrfs_log_ctx ctx2;
6094
6095 btrfs_init_log_ctx(&ctx2, &inode->vfs_inode);
6096 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6097 LOG_INODE_EXISTS, &ctx2);
6098 if (ret == BTRFS_NO_LOG_SYNC)
6099 return BTRFS_DONT_NEED_TRANS_COMMIT;
6100 else if (ret)
6101 return BTRFS_NEED_TRANS_COMMIT;
6102
6103 ret = btrfs_sync_log(trans, inode->root, &ctx2);
6104 if (ret)
6105 return BTRFS_NEED_TRANS_COMMIT;
6106 return BTRFS_DONT_NEED_TRANS_COMMIT;
6107 }
6108
6109 ASSERT(ctx);
6110 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6111 LOG_INODE_EXISTS, ctx);
6112 if (ret == BTRFS_NO_LOG_SYNC)
6113 return BTRFS_DONT_NEED_LOG_SYNC;
6114 else if (ret)
6115 return BTRFS_NEED_TRANS_COMMIT;
6116
6117 return BTRFS_NEED_LOG_SYNC;
Chris Mason12fcfd22009-03-24 10:24:20 -04006118}
6119