blob: dbe26468bc90f64cd100e58fd9bd1a9aa6893c24 [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);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003425 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003426 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003427 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003428 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003429 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003430
Chris Mason12fcfd22009-03-24 10:24:20 -04003431 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003432
Andi Kleen411fc6b2010-10-29 15:14:31 -04003433 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003434}
3435
3436/* see comments for btrfs_del_dir_entries_in_log */
3437int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3438 struct btrfs_root *root,
3439 const char *name, int name_len,
Nikolay Borisova491abb2017-01-18 00:31:33 +02003440 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003441{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003442 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003443 struct btrfs_root *log;
3444 u64 index;
3445 int ret;
3446
Filipe Mananafffedf52019-06-19 13:05:39 +01003447 if (!inode_logged(trans, inode))
Chris Mason3a5f1d42008-09-11 15:53:37 -04003448 return 0;
3449
Chris Masone02119d2008-09-05 16:13:11 -04003450 ret = join_running_log_trans(root);
3451 if (ret)
3452 return 0;
3453 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003454 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003455
Nikolay Borisova491abb2017-01-18 00:31:33 +02003456 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003457 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003458 mutex_unlock(&inode->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003459 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003460 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003461 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003462 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003463 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003464 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003465
Chris Masone02119d2008-09-05 16:13:11 -04003466 return ret;
3467}
3468
3469/*
3470 * creates a range item in the log for 'dirid'. first_offset and
3471 * last_offset tell us which parts of the key space the log should
3472 * be considered authoritative for.
3473 */
3474static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3475 struct btrfs_root *log,
3476 struct btrfs_path *path,
3477 int key_type, u64 dirid,
3478 u64 first_offset, u64 last_offset)
3479{
3480 int ret;
3481 struct btrfs_key key;
3482 struct btrfs_dir_log_item *item;
3483
3484 key.objectid = dirid;
3485 key.offset = first_offset;
3486 if (key_type == BTRFS_DIR_ITEM_KEY)
3487 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3488 else
3489 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3490 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003491 if (ret)
3492 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003493
3494 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3495 struct btrfs_dir_log_item);
3496 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3497 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003498 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003499 return 0;
3500}
3501
3502/*
3503 * log all the items included in the current transaction for a given
3504 * directory. This also creates the range items in the log tree required
3505 * to replay anything deleted before the fsync
3506 */
3507static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Nikolay Borisov684a5772017-01-18 00:31:41 +02003508 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003509 struct btrfs_path *path,
3510 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003511 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003512 u64 min_offset, u64 *last_offset_ret)
3513{
3514 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003515 struct btrfs_root *log = root->log_root;
3516 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003517 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003518 int ret;
3519 int i;
3520 int nritems;
3521 u64 first_offset = min_offset;
3522 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003523 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003524
3525 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003526
Li Zefan33345d012011-04-20 10:31:50 +08003527 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003528 min_key.type = key_type;
3529 min_key.offset = min_offset;
3530
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003531 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003532
3533 /*
3534 * we didn't find anything from this transaction, see if there
3535 * is anything at all
3536 */
Li Zefan33345d012011-04-20 10:31:50 +08003537 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3538 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003539 min_key.type = key_type;
3540 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003541 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003542 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3543 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003544 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003545 return ret;
3546 }
Li Zefan33345d012011-04-20 10:31:50 +08003547 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003548
3549 /* if ret == 0 there are items for this type,
3550 * create a range to tell us the last key of this type.
3551 * otherwise, there are no items in this directory after
3552 * *min_offset, and we create a range to indicate that.
3553 */
3554 if (ret == 0) {
3555 struct btrfs_key tmp;
3556 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3557 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003558 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003559 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003560 }
3561 goto done;
3562 }
3563
3564 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003565 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003566 if (ret == 0) {
3567 struct btrfs_key tmp;
3568 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3569 if (key_type == tmp.type) {
3570 first_offset = tmp.offset;
3571 ret = overwrite_item(trans, log, dst_path,
3572 path->nodes[0], path->slots[0],
3573 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003574 if (ret) {
3575 err = ret;
3576 goto done;
3577 }
Chris Masone02119d2008-09-05 16:13:11 -04003578 }
3579 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003580 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003581
Josef Bacikb57220c2019-03-06 17:13:04 -05003582 /*
3583 * Find the first key from this transaction again. See the note for
3584 * log_new_dir_dentries, if we're logging a directory recursively we
3585 * won't be holding its i_mutex, which means we can modify the directory
3586 * while we're logging it. If we remove an entry between our first
3587 * search and this search we'll not find the key again and can just
3588 * bail.
3589 */
Chris Masone02119d2008-09-05 16:13:11 -04003590 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Josef Bacikb57220c2019-03-06 17:13:04 -05003591 if (ret != 0)
Chris Masone02119d2008-09-05 16:13:11 -04003592 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003593
3594 /*
3595 * we have a block from this transaction, log every item in it
3596 * from our directory
3597 */
Chris Masond3977122009-01-05 21:25:51 -05003598 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003599 struct btrfs_key tmp;
3600 src = path->nodes[0];
3601 nritems = btrfs_header_nritems(src);
3602 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003603 struct btrfs_dir_item *di;
3604
Chris Masone02119d2008-09-05 16:13:11 -04003605 btrfs_item_key_to_cpu(src, &min_key, i);
3606
Li Zefan33345d012011-04-20 10:31:50 +08003607 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003608 goto done;
3609 ret = overwrite_item(trans, log, dst_path, src, i,
3610 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003611 if (ret) {
3612 err = ret;
3613 goto done;
3614 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003615
3616 /*
3617 * We must make sure that when we log a directory entry,
3618 * the corresponding inode, after log replay, has a
3619 * matching link count. For example:
3620 *
3621 * touch foo
3622 * mkdir mydir
3623 * sync
3624 * ln foo mydir/bar
3625 * xfs_io -c "fsync" mydir
3626 * <crash>
3627 * <mount fs and log replay>
3628 *
3629 * Would result in a fsync log that when replayed, our
3630 * file inode would have a link count of 1, but we get
3631 * two directory entries pointing to the same inode.
3632 * After removing one of the names, it would not be
3633 * possible to remove the other name, which resulted
3634 * always in stale file handle errors, and would not
3635 * be possible to rmdir the parent directory, since
3636 * its i_size could never decrement to the value
3637 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3638 */
3639 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3640 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3641 if (ctx &&
3642 (btrfs_dir_transid(src, di) == trans->transid ||
3643 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3644 tmp.type != BTRFS_ROOT_ITEM_KEY)
3645 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003646 }
3647 path->slots[0] = nritems;
3648
3649 /*
3650 * look ahead to the next item and see if it is also
3651 * from this directory and from this transaction
3652 */
3653 ret = btrfs_next_leaf(root, path);
Liu Bo80c0b422018-04-03 01:59:47 +08003654 if (ret) {
3655 if (ret == 1)
3656 last_offset = (u64)-1;
3657 else
3658 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003659 goto done;
3660 }
3661 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003662 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003663 last_offset = (u64)-1;
3664 goto done;
3665 }
3666 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3667 ret = overwrite_item(trans, log, dst_path,
3668 path->nodes[0], path->slots[0],
3669 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003670 if (ret)
3671 err = ret;
3672 else
3673 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003674 goto done;
3675 }
3676 }
3677done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003678 btrfs_release_path(path);
3679 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003680
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003681 if (err == 0) {
3682 *last_offset_ret = last_offset;
3683 /*
3684 * insert the log range keys to indicate where the log
3685 * is valid
3686 */
3687 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003688 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003689 if (ret)
3690 err = ret;
3691 }
3692 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003693}
3694
3695/*
3696 * logging directories is very similar to logging inodes, We find all the items
3697 * from the current transaction and write them to the log.
3698 *
3699 * The recovery code scans the directory in the subvolume, and if it finds a
3700 * key in the range logged that is not present in the log tree, then it means
3701 * that dir entry was unlinked during the transaction.
3702 *
3703 * In order for that scan to work, we must include one key smaller than
3704 * the smallest logged by this transaction and one key larger than the largest
3705 * key logged by this transaction.
3706 */
3707static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003708 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003709 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003710 struct btrfs_path *dst_path,
3711 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003712{
3713 u64 min_key;
3714 u64 max_key;
3715 int ret;
3716 int key_type = BTRFS_DIR_ITEM_KEY;
3717
3718again:
3719 min_key = 0;
3720 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003721 while (1) {
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003722 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3723 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003724 if (ret)
3725 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003726 if (max_key == (u64)-1)
3727 break;
3728 min_key = max_key + 1;
3729 }
3730
3731 if (key_type == BTRFS_DIR_ITEM_KEY) {
3732 key_type = BTRFS_DIR_INDEX_KEY;
3733 goto again;
3734 }
3735 return 0;
3736}
3737
3738/*
3739 * a helper function to drop items from the log before we relog an
3740 * inode. max_key_type indicates the highest item type to remove.
3741 * This cannot be run for file data extents because it does not
3742 * free the extents they point to.
3743 */
3744static int drop_objectid_items(struct btrfs_trans_handle *trans,
3745 struct btrfs_root *log,
3746 struct btrfs_path *path,
3747 u64 objectid, int max_key_type)
3748{
3749 int ret;
3750 struct btrfs_key key;
3751 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003752 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003753
3754 key.objectid = objectid;
3755 key.type = max_key_type;
3756 key.offset = (u64)-1;
3757
Chris Masond3977122009-01-05 21:25:51 -05003758 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003759 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003760 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003761 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003762 break;
3763
3764 if (path->slots[0] == 0)
3765 break;
3766
3767 path->slots[0]--;
3768 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3769 path->slots[0]);
3770
3771 if (found_key.objectid != objectid)
3772 break;
3773
Josef Bacik18ec90d2012-09-28 11:56:28 -04003774 found_key.offset = 0;
3775 found_key.type = 0;
3776 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3777 &start_slot);
3778
3779 ret = btrfs_del_items(trans, log, path, start_slot,
3780 path->slots[0] - start_slot + 1);
3781 /*
3782 * If start slot isn't 0 then we don't need to re-search, we've
3783 * found the last guy with the objectid in this tree.
3784 */
3785 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003786 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003787 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003788 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003789 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003790 if (ret > 0)
3791 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003792 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003793}
3794
Josef Bacik94edf4a2012-09-25 14:56:25 -04003795static void fill_inode_item(struct btrfs_trans_handle *trans,
3796 struct extent_buffer *leaf,
3797 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003798 struct inode *inode, int log_inode_only,
3799 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003800{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003801 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003802
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003803 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003804
3805 if (log_inode_only) {
3806 /* set the generation to zero so the recover code
3807 * can tell the difference between an logging
3808 * just to say 'this inode exists' and a logging
3809 * to say 'update this inode with these values'
3810 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003811 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003812 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003813 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003814 btrfs_set_token_inode_generation(leaf, item,
3815 BTRFS_I(inode)->generation,
3816 &token);
3817 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003818 }
3819
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003820 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3821 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3822 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3823 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3824
David Sterbaa937b972014-12-12 17:39:12 +01003825 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003826 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003827 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003828 inode->i_atime.tv_nsec, &token);
3829
David Sterbaa937b972014-12-12 17:39:12 +01003830 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003831 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003832 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003833 inode->i_mtime.tv_nsec, &token);
3834
David Sterbaa937b972014-12-12 17:39:12 +01003835 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003836 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003837 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003838 inode->i_ctime.tv_nsec, &token);
3839
3840 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3841 &token);
3842
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003843 btrfs_set_token_inode_sequence(leaf, item,
3844 inode_peek_iversion(inode), &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003845 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3846 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3847 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3848 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003849}
3850
Josef Bacika95249b2012-10-11 16:17:34 -04003851static int log_inode_item(struct btrfs_trans_handle *trans,
3852 struct btrfs_root *log, struct btrfs_path *path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003853 struct btrfs_inode *inode)
Josef Bacika95249b2012-10-11 16:17:34 -04003854{
3855 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003856 int ret;
3857
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003858 ret = btrfs_insert_empty_item(trans, log, path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003859 &inode->location, sizeof(*inode_item));
Josef Bacika95249b2012-10-11 16:17:34 -04003860 if (ret && ret != -EEXIST)
3861 return ret;
3862 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3863 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003864 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3865 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003866 btrfs_release_path(path);
3867 return 0;
3868}
3869
Filipe Manana01510492019-12-05 16:58:30 +00003870static int log_csums(struct btrfs_trans_handle *trans,
3871 struct btrfs_root *log_root,
3872 struct btrfs_ordered_sum *sums)
3873{
3874 int ret;
3875
3876 /*
3877 * Due to extent cloning, we might have logged a csum item that covers a
3878 * subrange of a cloned extent, and later we can end up logging a csum
3879 * item for a larger subrange of the same extent or the entire range.
3880 * This would leave csum items in the log tree that cover the same range
3881 * and break the searches for checksums in the log tree, resulting in
3882 * some checksums missing in the fs/subvolume tree. So just delete (or
3883 * trim and adjust) any existing csum items in the log for this range.
3884 */
3885 ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len);
3886 if (ret)
3887 return ret;
3888
3889 return btrfs_csum_file_blocks(trans, log_root, sums);
3890}
3891
Chris Mason31ff1cd2008-09-11 16:17:57 -04003892static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003893 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003894 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003895 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003896 int start_slot, int nr, int inode_only,
3897 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003898{
David Sterba3ffbd682018-06-29 10:56:42 +02003899 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003900 unsigned long src_offset;
3901 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003902 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003903 struct btrfs_file_extent_item *extent;
3904 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003905 struct extent_buffer *src = src_path->nodes[0];
3906 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003907 int ret;
3908 struct btrfs_key *ins_keys;
3909 u32 *ins_sizes;
3910 char *ins_data;
3911 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003912 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003913 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003914 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003915 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003916 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003917
3918 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003919
3920 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3921 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003922 if (!ins_data)
3923 return -ENOMEM;
3924
Josef Bacik16e75492013-10-22 12:18:51 -04003925 first_key.objectid = (u64)-1;
3926
Chris Mason31ff1cd2008-09-11 16:17:57 -04003927 ins_sizes = (u32 *)ins_data;
3928 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3929
3930 for (i = 0; i < nr; i++) {
3931 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3932 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3933 }
3934 ret = btrfs_insert_empty_items(trans, log, dst_path,
3935 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003936 if (ret) {
3937 kfree(ins_data);
3938 return ret;
3939 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003940
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003941 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003942 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3943 dst_path->slots[0]);
3944
3945 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3946
Matthias Kaehlcke0dde10b2017-07-27 14:30:23 -07003947 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003948 last_key = ins_keys[i];
3949
Josef Bacik94edf4a2012-09-25 14:56:25 -04003950 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003951 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3952 dst_path->slots[0],
3953 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003954 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01003955 &inode->vfs_inode,
3956 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003957 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003958 } else {
3959 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3960 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003961 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003962
Josef Bacik16e75492013-10-22 12:18:51 -04003963 /*
3964 * We set need_find_last_extent here in case we know we were
3965 * processing other items and then walk into the first extent in
3966 * the inode. If we don't hit an extent then nothing changes,
3967 * we'll do the last search the next time around.
3968 */
3969 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3970 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003971 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003972 first_key = ins_keys[i];
3973 } else {
3974 need_find_last_extent = false;
3975 }
3976
Chris Mason31ff1cd2008-09-11 16:17:57 -04003977 /* take a reference on file data extents so that truncates
3978 * or deletes of this inode don't have to relog the inode
3979 * again
3980 */
David Sterba962a2982014-06-04 18:41:45 +02003981 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003982 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003983 int found_type;
3984 extent = btrfs_item_ptr(src, start_slot + i,
3985 struct btrfs_file_extent_item);
3986
liubo8e531cd2011-05-06 10:36:09 +08003987 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3988 continue;
3989
Chris Mason31ff1cd2008-09-11 16:17:57 -04003990 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003991 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003992 u64 ds, dl, cs, cl;
3993 ds = btrfs_file_extent_disk_bytenr(src,
3994 extent);
3995 /* ds == 0 is a hole */
3996 if (ds == 0)
3997 continue;
3998
3999 dl = btrfs_file_extent_disk_num_bytes(src,
4000 extent);
4001 cs = btrfs_file_extent_offset(src, extent);
4002 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07004003 extent);
Chris Mason580afd72008-12-08 19:15:39 -05004004 if (btrfs_file_extent_compression(src,
4005 extent)) {
4006 cs = 0;
4007 cl = dl;
4008 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004009
4010 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004011 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004012 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01004013 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04004014 if (ret) {
4015 btrfs_release_path(dst_path);
4016 kfree(ins_data);
4017 return ret;
4018 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004019 }
4020 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004021 }
4022
4023 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004024 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04004025 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05004026
4027 /*
4028 * we have to do this after the loop above to avoid changing the
4029 * log tree while trying to change the log tree.
4030 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004031 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004032 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05004033 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4034 struct btrfs_ordered_sum,
4035 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004036 if (!ret)
Filipe Manana01510492019-12-05 16:58:30 +00004037 ret = log_csums(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05004038 list_del(&sums->list);
4039 kfree(sums);
4040 }
Josef Bacik16e75492013-10-22 12:18:51 -04004041
4042 if (!has_extents)
4043 return ret;
4044
Filipe Manana74121f7c2014-08-07 12:00:44 +01004045 if (need_find_last_extent && *last_extent == first_key.offset) {
4046 /*
4047 * We don't have any leafs between our current one and the one
4048 * we processed before that can have file extent items for our
4049 * inode (and have a generation number smaller than our current
4050 * transaction id).
4051 */
4052 need_find_last_extent = false;
4053 }
4054
Josef Bacik16e75492013-10-22 12:18:51 -04004055 /*
4056 * Because we use btrfs_search_forward we could skip leaves that were
4057 * not modified and then assume *last_extent is valid when it really
4058 * isn't. So back up to the previous leaf and read the end of the last
4059 * extent before we go and fill in holes.
4060 */
4061 if (need_find_last_extent) {
4062 u64 len;
4063
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004064 ret = btrfs_prev_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04004065 if (ret < 0)
4066 return ret;
4067 if (ret)
4068 goto fill_holes;
4069 if (src_path->slots[0])
4070 src_path->slots[0]--;
4071 src = src_path->nodes[0];
4072 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004073 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04004074 key.type != BTRFS_EXTENT_DATA_KEY)
4075 goto fill_holes;
4076 extent = btrfs_item_ptr(src, src_path->slots[0],
4077 struct btrfs_file_extent_item);
4078 if (btrfs_file_extent_type(src, extent) ==
4079 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004080 len = btrfs_file_extent_ram_bytes(src, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04004081 *last_extent = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004082 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04004083 } else {
4084 len = btrfs_file_extent_num_bytes(src, extent);
4085 *last_extent = key.offset + len;
4086 }
4087 }
4088fill_holes:
4089 /* So we did prev_leaf, now we need to move to the next leaf, but a few
4090 * things could have happened
4091 *
4092 * 1) A merge could have happened, so we could currently be on a leaf
4093 * that holds what we were copying in the first place.
4094 * 2) A split could have happened, and now not all of the items we want
4095 * are on the same leaf.
4096 *
4097 * So we need to adjust how we search for holes, we need to drop the
4098 * path and re-search for the first extent key we found, and then walk
4099 * forward until we hit the last one we copied.
4100 */
4101 if (need_find_last_extent) {
4102 /* btrfs_prev_leaf could return 1 without releasing the path */
4103 btrfs_release_path(src_path);
David Sterbaf85b7372017-01-20 14:54:07 +01004104 ret = btrfs_search_slot(NULL, inode->root, &first_key,
4105 src_path, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004106 if (ret < 0)
4107 return ret;
4108 ASSERT(ret == 0);
4109 src = src_path->nodes[0];
4110 i = src_path->slots[0];
4111 } else {
4112 i = start_slot;
4113 }
4114
4115 /*
4116 * Ok so here we need to go through and fill in any holes we may have
4117 * to make sure that holes are punched for those areas in case they had
4118 * extents previously.
4119 */
4120 while (!done) {
4121 u64 offset, len;
4122 u64 extent_end;
4123
4124 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004125 ret = btrfs_next_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04004126 if (ret < 0)
4127 return ret;
4128 ASSERT(ret == 0);
4129 src = src_path->nodes[0];
4130 i = 0;
Filipe Manana8434ec42018-03-26 23:59:12 +01004131 need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04004132 }
4133
4134 btrfs_item_key_to_cpu(src, &key, i);
4135 if (!btrfs_comp_cpu_keys(&key, &last_key))
4136 done = true;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004137 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04004138 key.type != BTRFS_EXTENT_DATA_KEY) {
4139 i++;
4140 continue;
4141 }
4142 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
4143 if (btrfs_file_extent_type(src, extent) ==
4144 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004145 len = btrfs_file_extent_ram_bytes(src, extent);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004146 extent_end = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004147 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04004148 } else {
4149 len = btrfs_file_extent_num_bytes(src, extent);
4150 extent_end = key.offset + len;
4151 }
4152 i++;
4153
4154 if (*last_extent == key.offset) {
4155 *last_extent = extent_end;
4156 continue;
4157 }
4158 offset = *last_extent;
4159 len = key.offset - *last_extent;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004160 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01004161 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004162 if (ret)
4163 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01004164 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04004165 }
Filipe Manana4ee3fad2018-03-26 23:59:00 +01004166
4167 /*
4168 * Check if there is a hole between the last extent found in our leaf
4169 * and the first extent in the next leaf. If there is one, we need to
4170 * log an explicit hole so that at replay time we can punch the hole.
4171 */
4172 if (ret == 0 &&
4173 key.objectid == btrfs_ino(inode) &&
4174 key.type == BTRFS_EXTENT_DATA_KEY &&
4175 i == btrfs_header_nritems(src_path->nodes[0])) {
4176 ret = btrfs_next_leaf(inode->root, src_path);
4177 need_find_last_extent = true;
4178 if (ret > 0) {
4179 ret = 0;
4180 } else if (ret == 0) {
4181 btrfs_item_key_to_cpu(src_path->nodes[0], &key,
4182 src_path->slots[0]);
4183 if (key.objectid == btrfs_ino(inode) &&
4184 key.type == BTRFS_EXTENT_DATA_KEY &&
4185 *last_extent < key.offset) {
4186 const u64 len = key.offset - *last_extent;
4187
4188 ret = btrfs_insert_file_extent(trans, log,
4189 btrfs_ino(inode),
4190 *last_extent, 0,
4191 0, len, 0, len,
4192 0, 0, 0);
Filipe Manana4f9a7742019-05-06 16:43:51 +01004193 *last_extent += len;
Filipe Manana4ee3fad2018-03-26 23:59:00 +01004194 }
4195 }
4196 }
Josef Bacik16e75492013-10-22 12:18:51 -04004197 /*
4198 * Need to let the callers know we dropped the path so they should
4199 * re-search.
4200 */
4201 if (!ret && need_find_last_extent)
4202 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004203 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004204}
4205
Josef Bacik5dc562c2012-08-17 13:14:17 -04004206static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4207{
4208 struct extent_map *em1, *em2;
4209
4210 em1 = list_entry(a, struct extent_map, list);
4211 em2 = list_entry(b, struct extent_map, list);
4212
4213 if (em1->start < em2->start)
4214 return -1;
4215 else if (em1->start > em2->start)
4216 return 1;
4217 return 0;
4218}
4219
Josef Bacike7175a62018-05-23 11:58:34 -04004220static int log_extent_csums(struct btrfs_trans_handle *trans,
4221 struct btrfs_inode *inode,
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004222 struct btrfs_root *log_root,
Josef Bacike7175a62018-05-23 11:58:34 -04004223 const struct extent_map *em)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004224{
Josef Bacik2ab28f32012-10-12 15:27:49 -04004225 u64 csum_offset;
4226 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004227 LIST_HEAD(ordered_sums);
4228 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004229
Josef Bacike7175a62018-05-23 11:58:34 -04004230 if (inode->flags & BTRFS_INODE_NODATASUM ||
4231 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
Filipe Manana8407f552014-09-05 15:14:39 +01004232 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04004233 return 0;
4234
Josef Bacike7175a62018-05-23 11:58:34 -04004235 /* If we're compressed we have to save the entire range of csums. */
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004236 if (em->compress_type) {
4237 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004238 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004239 } else {
Josef Bacike7175a62018-05-23 11:58:34 -04004240 csum_offset = em->mod_start - em->start;
4241 csum_len = em->mod_len;
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004242 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004243
Josef Bacik70c8a912012-10-11 16:54:30 -04004244 /* block start is already adjusted for the file extent offset. */
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004245 ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004246 em->block_start + csum_offset,
4247 em->block_start + csum_offset +
4248 csum_len - 1, &ordered_sums, 0);
4249 if (ret)
4250 return ret;
4251
4252 while (!list_empty(&ordered_sums)) {
4253 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4254 struct btrfs_ordered_sum,
4255 list);
4256 if (!ret)
Filipe Manana01510492019-12-05 16:58:30 +00004257 ret = log_csums(trans, log_root, sums);
Josef Bacik70c8a912012-10-11 16:54:30 -04004258 list_del(&sums->list);
4259 kfree(sums);
4260 }
4261
4262 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004263}
4264
Filipe Manana8407f552014-09-05 15:14:39 +01004265static int log_one_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004266 struct btrfs_inode *inode, struct btrfs_root *root,
Filipe Manana8407f552014-09-05 15:14:39 +01004267 const struct extent_map *em,
4268 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004269 struct btrfs_log_ctx *ctx)
4270{
4271 struct btrfs_root *log = root->log_root;
4272 struct btrfs_file_extent_item *fi;
4273 struct extent_buffer *leaf;
4274 struct btrfs_map_token token;
4275 struct btrfs_key key;
4276 u64 extent_offset = em->start - em->orig_start;
4277 u64 block_len;
4278 int ret;
4279 int extent_inserted = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004280
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004281 ret = log_extent_csums(trans, inode, log, em);
Filipe Manana8407f552014-09-05 15:14:39 +01004282 if (ret)
4283 return ret;
4284
Filipe Manana8407f552014-09-05 15:14:39 +01004285 btrfs_init_map_token(&token);
4286
Nikolay Borisov9d122622017-01-18 00:31:40 +02004287 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
Filipe Manana8407f552014-09-05 15:14:39 +01004288 em->start + em->len, NULL, 0, 1,
4289 sizeof(*fi), &extent_inserted);
4290 if (ret)
4291 return ret;
4292
4293 if (!extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004294 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004295 key.type = BTRFS_EXTENT_DATA_KEY;
4296 key.offset = em->start;
4297
4298 ret = btrfs_insert_empty_item(trans, log, path, &key,
4299 sizeof(*fi));
4300 if (ret)
4301 return ret;
4302 }
4303 leaf = path->nodes[0];
4304 fi = btrfs_item_ptr(leaf, path->slots[0],
4305 struct btrfs_file_extent_item);
4306
Josef Bacik50d9aa92014-11-21 14:52:38 -05004307 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004308 &token);
4309 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4310 btrfs_set_token_file_extent_type(leaf, fi,
4311 BTRFS_FILE_EXTENT_PREALLOC,
4312 &token);
4313 else
4314 btrfs_set_token_file_extent_type(leaf, fi,
4315 BTRFS_FILE_EXTENT_REG,
4316 &token);
4317
4318 block_len = max(em->block_len, em->orig_block_len);
4319 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4320 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4321 em->block_start,
4322 &token);
4323 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4324 &token);
4325 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4326 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4327 em->block_start -
4328 extent_offset, &token);
4329 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4330 &token);
4331 } else {
4332 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4333 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4334 &token);
4335 }
4336
4337 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4338 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4339 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4340 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4341 &token);
4342 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4343 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4344 btrfs_mark_buffer_dirty(leaf);
4345
4346 btrfs_release_path(path);
4347
4348 return ret;
4349}
4350
Filipe Manana31d11b82018-05-09 16:01:46 +01004351/*
4352 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4353 * lose them after doing a fast fsync and replaying the log. We scan the
4354 * subvolume's root instead of iterating the inode's extent map tree because
4355 * otherwise we can log incorrect extent items based on extent map conversion.
4356 * That can happen due to the fact that extent maps are merged when they
4357 * are not in the extent map tree's list of modified extents.
4358 */
4359static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4360 struct btrfs_inode *inode,
4361 struct btrfs_path *path)
4362{
4363 struct btrfs_root *root = inode->root;
4364 struct btrfs_key key;
4365 const u64 i_size = i_size_read(&inode->vfs_inode);
4366 const u64 ino = btrfs_ino(inode);
4367 struct btrfs_path *dst_path = NULL;
4368 u64 last_extent = (u64)-1;
4369 int ins_nr = 0;
4370 int start_slot;
4371 int ret;
4372
4373 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4374 return 0;
4375
4376 key.objectid = ino;
4377 key.type = BTRFS_EXTENT_DATA_KEY;
4378 key.offset = i_size;
4379 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4380 if (ret < 0)
4381 goto out;
4382
4383 while (true) {
4384 struct extent_buffer *leaf = path->nodes[0];
4385 int slot = path->slots[0];
4386
4387 if (slot >= btrfs_header_nritems(leaf)) {
4388 if (ins_nr > 0) {
4389 ret = copy_items(trans, inode, dst_path, path,
4390 &last_extent, start_slot,
4391 ins_nr, 1, 0);
4392 if (ret < 0)
4393 goto out;
4394 ins_nr = 0;
4395 }
4396 ret = btrfs_next_leaf(root, path);
4397 if (ret < 0)
4398 goto out;
4399 if (ret > 0) {
4400 ret = 0;
4401 break;
4402 }
4403 continue;
4404 }
4405
4406 btrfs_item_key_to_cpu(leaf, &key, slot);
4407 if (key.objectid > ino)
4408 break;
4409 if (WARN_ON_ONCE(key.objectid < ino) ||
4410 key.type < BTRFS_EXTENT_DATA_KEY ||
4411 key.offset < i_size) {
4412 path->slots[0]++;
4413 continue;
4414 }
4415 if (last_extent == (u64)-1) {
4416 last_extent = key.offset;
4417 /*
4418 * Avoid logging extent items logged in past fsync calls
4419 * and leading to duplicate keys in the log tree.
4420 */
4421 do {
4422 ret = btrfs_truncate_inode_items(trans,
4423 root->log_root,
4424 &inode->vfs_inode,
4425 i_size,
4426 BTRFS_EXTENT_DATA_KEY);
4427 } while (ret == -EAGAIN);
4428 if (ret)
4429 goto out;
4430 }
4431 if (ins_nr == 0)
4432 start_slot = slot;
4433 ins_nr++;
4434 path->slots[0]++;
4435 if (!dst_path) {
4436 dst_path = btrfs_alloc_path();
4437 if (!dst_path) {
4438 ret = -ENOMEM;
4439 goto out;
4440 }
4441 }
4442 }
4443 if (ins_nr > 0) {
4444 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4445 start_slot, ins_nr, 1, 0);
4446 if (ret > 0)
4447 ret = 0;
4448 }
4449out:
4450 btrfs_release_path(path);
4451 btrfs_free_path(dst_path);
4452 return ret;
4453}
4454
Josef Bacik5dc562c2012-08-17 13:14:17 -04004455static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4456 struct btrfs_root *root,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004457 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004458 struct btrfs_path *path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004459 struct btrfs_log_ctx *ctx,
4460 const u64 start,
4461 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004462{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004463 struct extent_map *em, *n;
4464 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004465 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004466 u64 logged_start, logged_end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004467 u64 test_gen;
4468 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004469 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004470
4471 INIT_LIST_HEAD(&extents);
4472
Josef Bacik5dc562c2012-08-17 13:14:17 -04004473 write_lock(&tree->lock);
4474 test_gen = root->fs_info->last_trans_committed;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004475 logged_start = start;
4476 logged_end = end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004477
4478 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
Filipe Mananafa625a42018-10-29 09:42:06 +00004479 /*
4480 * Skip extents outside our logging range. It's important to do
4481 * it for correctness because if we don't ignore them, we may
4482 * log them before their ordered extent completes, and therefore
4483 * we could log them without logging their respective checksums
4484 * (the checksum items are added to the csum tree at the very
4485 * end of btrfs_finish_ordered_io()). Also leave such extents
4486 * outside of our range in the list, since we may have another
4487 * ranged fsync in the near future that needs them. If an extent
4488 * outside our range corresponds to a hole, log it to avoid
4489 * leaving gaps between extents (fsck will complain when we are
4490 * not using the NO_HOLES feature).
4491 */
4492 if ((em->start > end || em->start + em->len <= start) &&
4493 em->block_start != EXTENT_MAP_HOLE)
4494 continue;
4495
Josef Bacik5dc562c2012-08-17 13:14:17 -04004496 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004497 /*
4498 * Just an arbitrary number, this can be really CPU intensive
4499 * once we start getting a lot of extents, and really once we
4500 * have a bunch of extents we just want to commit since it will
4501 * be faster.
4502 */
4503 if (++num > 32768) {
4504 list_del_init(&tree->modified_extents);
4505 ret = -EFBIG;
4506 goto process;
4507 }
4508
Josef Bacik5dc562c2012-08-17 13:14:17 -04004509 if (em->generation <= test_gen)
4510 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004511
Filipe Manana31d11b82018-05-09 16:01:46 +01004512 /* We log prealloc extents beyond eof later. */
4513 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4514 em->start >= i_size_read(&inode->vfs_inode))
4515 continue;
4516
Josef Bacik8c6c5922017-08-29 10:11:39 -04004517 if (em->start < logged_start)
4518 logged_start = em->start;
4519 if ((em->start + em->len - 1) > logged_end)
4520 logged_end = em->start + em->len - 1;
4521
Josef Bacikff44c6e2012-09-14 12:59:20 -04004522 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004523 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004524 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004525 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004526 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004527 }
4528
4529 list_sort(NULL, &extents, extent_cmp);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004530process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004531 while (!list_empty(&extents)) {
4532 em = list_entry(extents.next, struct extent_map, list);
4533
4534 list_del_init(&em->list);
4535
4536 /*
4537 * If we had an error we just need to delete everybody from our
4538 * private list.
4539 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004540 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004541 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004542 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004543 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004544 }
4545
4546 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004547
Josef Bacika2120a42018-05-23 11:58:35 -04004548 ret = log_one_extent(trans, inode, root, em, path, ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004549 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004550 clear_em_logging(tree, em);
4551 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004552 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004553 WARN_ON(!list_empty(&extents));
4554 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004555
Josef Bacik5dc562c2012-08-17 13:14:17 -04004556 btrfs_release_path(path);
Filipe Manana31d11b82018-05-09 16:01:46 +01004557 if (!ret)
4558 ret = btrfs_log_prealloc_extents(trans, inode, path);
4559
Josef Bacik5dc562c2012-08-17 13:14:17 -04004560 return ret;
4561}
4562
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004563static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004564 struct btrfs_path *path, u64 *size_ret)
4565{
4566 struct btrfs_key key;
4567 int ret;
4568
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004569 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004570 key.type = BTRFS_INODE_ITEM_KEY;
4571 key.offset = 0;
4572
4573 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4574 if (ret < 0) {
4575 return ret;
4576 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004577 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004578 } else {
4579 struct btrfs_inode_item *item;
4580
4581 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4582 struct btrfs_inode_item);
4583 *size_ret = btrfs_inode_size(path->nodes[0], item);
Filipe Manana22dcb302019-03-04 14:06:12 +00004584 /*
4585 * If the in-memory inode's i_size is smaller then the inode
4586 * size stored in the btree, return the inode's i_size, so
4587 * that we get a correct inode size after replaying the log
4588 * when before a power failure we had a shrinking truncate
4589 * followed by addition of a new name (rename / new hard link).
4590 * Otherwise return the inode size from the btree, to avoid
4591 * data loss when replaying a log due to previously doing a
4592 * write that expands the inode's size and logging a new name
4593 * immediately after.
4594 */
4595 if (*size_ret > inode->vfs_inode.i_size)
4596 *size_ret = inode->vfs_inode.i_size;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004597 }
4598
4599 btrfs_release_path(path);
4600 return 0;
4601}
4602
Filipe Manana36283bf2015-06-20 00:44:51 +01004603/*
4604 * At the moment we always log all xattrs. This is to figure out at log replay
4605 * time which xattrs must have their deletion replayed. If a xattr is missing
4606 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4607 * because if a xattr is deleted, the inode is fsynced and a power failure
4608 * happens, causing the log to be replayed the next time the fs is mounted,
4609 * we want the xattr to not exist anymore (same behaviour as other filesystems
4610 * with a journal, ext3/4, xfs, f2fs, etc).
4611 */
4612static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4613 struct btrfs_root *root,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004614 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004615 struct btrfs_path *path,
4616 struct btrfs_path *dst_path)
4617{
4618 int ret;
4619 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004620 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004621 int ins_nr = 0;
4622 int start_slot = 0;
4623
4624 key.objectid = ino;
4625 key.type = BTRFS_XATTR_ITEM_KEY;
4626 key.offset = 0;
4627
4628 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4629 if (ret < 0)
4630 return ret;
4631
4632 while (true) {
4633 int slot = path->slots[0];
4634 struct extent_buffer *leaf = path->nodes[0];
4635 int nritems = btrfs_header_nritems(leaf);
4636
4637 if (slot >= nritems) {
4638 if (ins_nr > 0) {
4639 u64 last_extent = 0;
4640
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004641 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004642 &last_extent, start_slot,
4643 ins_nr, 1, 0);
4644 /* can't be 1, extent items aren't processed */
4645 ASSERT(ret <= 0);
4646 if (ret < 0)
4647 return ret;
4648 ins_nr = 0;
4649 }
4650 ret = btrfs_next_leaf(root, path);
4651 if (ret < 0)
4652 return ret;
4653 else if (ret > 0)
4654 break;
4655 continue;
4656 }
4657
4658 btrfs_item_key_to_cpu(leaf, &key, slot);
4659 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4660 break;
4661
4662 if (ins_nr == 0)
4663 start_slot = slot;
4664 ins_nr++;
4665 path->slots[0]++;
4666 cond_resched();
4667 }
4668 if (ins_nr > 0) {
4669 u64 last_extent = 0;
4670
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004671 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004672 &last_extent, start_slot,
4673 ins_nr, 1, 0);
4674 /* can't be 1, extent items aren't processed */
4675 ASSERT(ret <= 0);
4676 if (ret < 0)
4677 return ret;
4678 }
4679
4680 return 0;
4681}
4682
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004683/*
4684 * If the no holes feature is enabled we need to make sure any hole between the
4685 * last extent and the i_size of our inode is explicitly marked in the log. This
4686 * is to make sure that doing something like:
4687 *
4688 * 1) create file with 128Kb of data
4689 * 2) truncate file to 64Kb
4690 * 3) truncate file to 256Kb
4691 * 4) fsync file
4692 * 5) <crash/power failure>
4693 * 6) mount fs and trigger log replay
4694 *
4695 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4696 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4697 * file correspond to a hole. The presence of explicit holes in a log tree is
4698 * what guarantees that log replay will remove/adjust file extent items in the
4699 * fs/subvol tree.
4700 *
4701 * Here we do not need to care about holes between extents, that is already done
4702 * by copy_items(). We also only need to do this in the full sync path, where we
4703 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4704 * lookup the list of modified extent maps and if any represents a hole, we
4705 * insert a corresponding extent representing a hole in the log tree.
4706 */
4707static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4708 struct btrfs_root *root,
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004709 struct btrfs_inode *inode,
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004710 struct btrfs_path *path)
4711{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004712 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004713 int ret;
4714 struct btrfs_key key;
4715 u64 hole_start;
4716 u64 hole_size;
4717 struct extent_buffer *leaf;
4718 struct btrfs_root *log = root->log_root;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004719 const u64 ino = btrfs_ino(inode);
4720 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004721
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004722 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004723 return 0;
4724
4725 key.objectid = ino;
4726 key.type = BTRFS_EXTENT_DATA_KEY;
4727 key.offset = (u64)-1;
4728
4729 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4730 ASSERT(ret != 0);
4731 if (ret < 0)
4732 return ret;
4733
4734 ASSERT(path->slots[0] > 0);
4735 path->slots[0]--;
4736 leaf = path->nodes[0];
4737 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4738
4739 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4740 /* inode does not have any extents */
4741 hole_start = 0;
4742 hole_size = i_size;
4743 } else {
4744 struct btrfs_file_extent_item *extent;
4745 u64 len;
4746
4747 /*
4748 * If there's an extent beyond i_size, an explicit hole was
4749 * already inserted by copy_items().
4750 */
4751 if (key.offset >= i_size)
4752 return 0;
4753
4754 extent = btrfs_item_ptr(leaf, path->slots[0],
4755 struct btrfs_file_extent_item);
4756
4757 if (btrfs_file_extent_type(leaf, extent) ==
Filipe Mananafd1b2532019-03-19 17:18:13 +00004758 BTRFS_FILE_EXTENT_INLINE)
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004759 return 0;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004760
4761 len = btrfs_file_extent_num_bytes(leaf, extent);
4762 /* Last extent goes beyond i_size, no need to log a hole. */
4763 if (key.offset + len > i_size)
4764 return 0;
4765 hole_start = key.offset + len;
4766 hole_size = i_size - hole_start;
4767 }
4768 btrfs_release_path(path);
4769
4770 /* Last extent ends at i_size. */
4771 if (hole_size == 0)
4772 return 0;
4773
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004774 hole_size = ALIGN(hole_size, fs_info->sectorsize);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004775 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4776 hole_size, 0, hole_size, 0, 0, 0);
4777 return ret;
4778}
4779
Filipe Manana56f23fd2016-03-30 23:37:21 +01004780/*
4781 * When we are logging a new inode X, check if it doesn't have a reference that
4782 * matches the reference from some other inode Y created in a past transaction
4783 * and that was renamed in the current transaction. If we don't do this, then at
4784 * log replay time we can lose inode Y (and all its files if it's a directory):
4785 *
4786 * mkdir /mnt/x
4787 * echo "hello world" > /mnt/x/foobar
4788 * sync
4789 * mv /mnt/x /mnt/y
4790 * mkdir /mnt/x # or touch /mnt/x
4791 * xfs_io -c fsync /mnt/x
4792 * <power fail>
4793 * mount fs, trigger log replay
4794 *
4795 * After the log replay procedure, we would lose the first directory and all its
4796 * files (file foobar).
4797 * For the case where inode Y is not a directory we simply end up losing it:
4798 *
4799 * echo "123" > /mnt/foo
4800 * sync
4801 * mv /mnt/foo /mnt/bar
4802 * echo "abc" > /mnt/foo
4803 * xfs_io -c fsync /mnt/foo
4804 * <power fail>
4805 *
4806 * We also need this for cases where a snapshot entry is replaced by some other
4807 * entry (file or directory) otherwise we end up with an unreplayable log due to
4808 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4809 * if it were a regular entry:
4810 *
4811 * mkdir /mnt/x
4812 * btrfs subvolume snapshot /mnt /mnt/x/snap
4813 * btrfs subvolume delete /mnt/x/snap
4814 * rmdir /mnt/x
4815 * mkdir /mnt/x
4816 * fsync /mnt/x or fsync some new file inside it
4817 * <power fail>
4818 *
4819 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4820 * the same transaction.
4821 */
4822static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4823 const int slot,
4824 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004825 struct btrfs_inode *inode,
Filipe Manana44f714d2016-06-06 16:11:13 +01004826 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004827{
4828 int ret;
4829 struct btrfs_path *search_path;
4830 char *name = NULL;
4831 u32 name_len = 0;
4832 u32 item_size = btrfs_item_size_nr(eb, slot);
4833 u32 cur_offset = 0;
4834 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4835
4836 search_path = btrfs_alloc_path();
4837 if (!search_path)
4838 return -ENOMEM;
4839 search_path->search_commit_root = 1;
4840 search_path->skip_locking = 1;
4841
4842 while (cur_offset < item_size) {
4843 u64 parent;
4844 u32 this_name_len;
4845 u32 this_len;
4846 unsigned long name_ptr;
4847 struct btrfs_dir_item *di;
4848
4849 if (key->type == BTRFS_INODE_REF_KEY) {
4850 struct btrfs_inode_ref *iref;
4851
4852 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4853 parent = key->offset;
4854 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4855 name_ptr = (unsigned long)(iref + 1);
4856 this_len = sizeof(*iref) + this_name_len;
4857 } else {
4858 struct btrfs_inode_extref *extref;
4859
4860 extref = (struct btrfs_inode_extref *)(ptr +
4861 cur_offset);
4862 parent = btrfs_inode_extref_parent(eb, extref);
4863 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4864 name_ptr = (unsigned long)&extref->name;
4865 this_len = sizeof(*extref) + this_name_len;
4866 }
4867
4868 if (this_name_len > name_len) {
4869 char *new_name;
4870
4871 new_name = krealloc(name, this_name_len, GFP_NOFS);
4872 if (!new_name) {
4873 ret = -ENOMEM;
4874 goto out;
4875 }
4876 name_len = this_name_len;
4877 name = new_name;
4878 }
4879
4880 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004881 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4882 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004883 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004884 struct btrfs_key di_key;
4885
4886 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4887 di, &di_key);
4888 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4889 ret = 1;
4890 *other_ino = di_key.objectid;
4891 } else {
4892 ret = -EAGAIN;
4893 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004894 goto out;
4895 } else if (IS_ERR(di)) {
4896 ret = PTR_ERR(di);
4897 goto out;
4898 }
4899 btrfs_release_path(search_path);
4900
4901 cur_offset += this_len;
4902 }
4903 ret = 0;
4904out:
4905 btrfs_free_path(search_path);
4906 kfree(name);
4907 return ret;
4908}
4909
Chris Masone02119d2008-09-05 16:13:11 -04004910/* log a single inode in the tree log.
4911 * At least one parent directory for this inode must exist in the tree
4912 * or be logged already.
4913 *
4914 * Any items from this inode changed by the current transaction are copied
4915 * to the log tree. An extra reference is taken on any extents in this
4916 * file, allowing us to avoid a whole pile of corner cases around logging
4917 * blocks that have been removed from the tree.
4918 *
4919 * See LOG_INODE_ALL and related defines for a description of what inode_only
4920 * does.
4921 *
4922 * This handles both files and directories.
4923 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004924static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004925 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004926 int inode_only,
4927 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004928 const loff_t end,
4929 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004930{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004931 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004932 struct btrfs_path *path;
4933 struct btrfs_path *dst_path;
4934 struct btrfs_key min_key;
4935 struct btrfs_key max_key;
4936 struct btrfs_root *log = root->log_root;
Josef Bacik16e75492013-10-22 12:18:51 -04004937 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004938 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004939 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004940 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004941 int ins_start_slot = 0;
4942 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004943 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004944 u64 ino = btrfs_ino(inode);
4945 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004946 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004947 bool need_log_inode_item = true;
Filipe Manana9a8fca62018-05-11 16:42:42 +01004948 bool xattrs_logged = false;
Chris Masone02119d2008-09-05 16:13:11 -04004949
Chris Masone02119d2008-09-05 16:13:11 -04004950 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004951 if (!path)
4952 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004953 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004954 if (!dst_path) {
4955 btrfs_free_path(path);
4956 return -ENOMEM;
4957 }
Chris Masone02119d2008-09-05 16:13:11 -04004958
Li Zefan33345d012011-04-20 10:31:50 +08004959 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004960 min_key.type = BTRFS_INODE_ITEM_KEY;
4961 min_key.offset = 0;
4962
Li Zefan33345d012011-04-20 10:31:50 +08004963 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004964
Chris Mason12fcfd22009-03-24 10:24:20 -04004965
Josef Bacik5dc562c2012-08-17 13:14:17 -04004966 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004967 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00004968 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004969 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08004970 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004971 max_key.type = BTRFS_XATTR_ITEM_KEY;
4972 else
4973 max_key.type = (u8)-1;
4974 max_key.offset = (u64)-1;
4975
Filipe Manana2c2c4522015-01-13 16:40:04 +00004976 /*
4977 * Only run delayed items if we are a dir or a new file.
4978 * Otherwise commit the delayed inode only, which is needed in
4979 * order for the log replay code to mark inodes for link count
4980 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4981 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004982 if (S_ISDIR(inode->vfs_inode.i_mode) ||
4983 inode->generation > fs_info->last_trans_committed)
4984 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004985 else
Nikolay Borisova59108a2017-01-18 00:31:48 +02004986 ret = btrfs_commit_inode_delayed_inode(inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004987
4988 if (ret) {
4989 btrfs_free_path(path);
4990 btrfs_free_path(dst_path);
4991 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004992 }
4993
Liu Bo781feef2016-11-30 16:20:25 -08004994 if (inode_only == LOG_OTHER_INODE) {
4995 inode_only = LOG_INODE_EXISTS;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004996 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08004997 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004998 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08004999 }
Chris Masone02119d2008-09-05 16:13:11 -04005000
Filipe Manana5e33a2b2016-02-25 23:19:38 +00005001 /*
Chris Masone02119d2008-09-05 16:13:11 -04005002 * a brute force approach to making sure we get the most uptodate
5003 * copies of everything.
5004 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005005 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04005006 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
5007
Filipe Manana4f764e52015-02-23 19:53:35 +00005008 if (inode_only == LOG_INODE_EXISTS)
5009 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08005010 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04005011 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005012 if (inode_only == LOG_INODE_EXISTS) {
5013 /*
5014 * Make sure the new inode item we write to the log has
5015 * the same isize as the current one (if it exists).
5016 * This is necessary to prevent data loss after log
5017 * replay, and also to prevent doing a wrong expanding
5018 * truncate - for e.g. create file, write 4K into offset
5019 * 0, fsync, write 4K into offset 4096, add hard link,
5020 * fsync some other file (to sync log), power fail - if
5021 * we use the inode's current i_size, after log replay
5022 * we get a 8Kb file, with the last 4Kb extent as a hole
5023 * (zeroes), as if an expanding truncate happened,
5024 * instead of getting a file of 4Kb only.
5025 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005026 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005027 if (err)
5028 goto out_unlock;
5029 }
Filipe Mananaa7429942015-02-13 16:56:14 +00005030 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005031 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00005032 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005033 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00005034 ret = drop_objectid_items(trans, log, path, ino,
5035 max_key.type);
5036 } else {
5037 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005038 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00005039 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005040 &inode->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08005041 while(1) {
5042 ret = btrfs_truncate_inode_items(trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005043 log, &inode->vfs_inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005044 if (ret != -EAGAIN)
5045 break;
5046 }
Filipe Mananaa7429942015-02-13 16:56:14 +00005047 }
Filipe Manana4f764e52015-02-23 19:53:35 +00005048 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005049 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05005050 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005051 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04005052 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00005053 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04005054 ret = drop_objectid_items(trans, log, path, ino,
5055 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005056 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00005057 if (inode_only == LOG_INODE_ALL)
5058 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04005059 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005060 }
Josef Bacika95249b2012-10-11 16:17:34 -04005061
Chris Masone02119d2008-09-05 16:13:11 -04005062 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005063 if (ret) {
5064 err = ret;
5065 goto out_unlock;
5066 }
Chris Masone02119d2008-09-05 16:13:11 -04005067
Chris Masond3977122009-01-05 21:25:51 -05005068 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04005069 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01005070 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005071 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07005072 if (ret < 0) {
5073 err = ret;
5074 goto out_unlock;
5075 }
Chris Masone02119d2008-09-05 16:13:11 -04005076 if (ret != 0)
5077 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04005078again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04005079 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08005080 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04005081 break;
5082 if (min_key.type > max_key.type)
5083 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005084
Filipe Mananae4545de2015-06-17 12:49:23 +01005085 if (min_key.type == BTRFS_INODE_ITEM_KEY)
5086 need_log_inode_item = false;
5087
Filipe Manana56f23fd2016-03-30 23:37:21 +01005088 if ((min_key.type == BTRFS_INODE_REF_KEY ||
5089 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
Nikolay Borisova59108a2017-01-18 00:31:48 +02005090 inode->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005091 u64 other_ino = 0;
5092
Filipe Manana56f23fd2016-03-30 23:37:21 +01005093 ret = btrfs_check_ref_name_override(path->nodes[0],
Nikolay Borisova59108a2017-01-18 00:31:48 +02005094 path->slots[0], &min_key, inode,
5095 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01005096 if (ret < 0) {
5097 err = ret;
5098 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01005099 } else if (ret > 0 && ctx &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005100 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005101 struct btrfs_key inode_key;
5102 struct inode *other_inode;
5103
5104 if (ins_nr > 0) {
5105 ins_nr++;
5106 } else {
5107 ins_nr = 1;
5108 ins_start_slot = path->slots[0];
5109 }
Nikolay Borisova59108a2017-01-18 00:31:48 +02005110 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana44f714d2016-06-06 16:11:13 +01005111 &last_extent, ins_start_slot,
5112 ins_nr, inode_only,
5113 logged_isize);
5114 if (ret < 0) {
5115 err = ret;
5116 goto out_unlock;
5117 }
5118 ins_nr = 0;
5119 btrfs_release_path(path);
5120 inode_key.objectid = other_ino;
5121 inode_key.type = BTRFS_INODE_ITEM_KEY;
5122 inode_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005123 other_inode = btrfs_iget(fs_info->sb,
Filipe Manana44f714d2016-06-06 16:11:13 +01005124 &inode_key, root,
5125 NULL);
5126 /*
5127 * If the other inode that had a conflicting dir
5128 * entry was deleted in the current transaction,
5129 * we don't need to do more work nor fallback to
5130 * a transaction commit.
5131 */
Al Viro8d9e2202018-07-29 23:04:46 +01005132 if (other_inode == ERR_PTR(-ENOENT)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005133 goto next_key;
5134 } else if (IS_ERR(other_inode)) {
5135 err = PTR_ERR(other_inode);
5136 goto out_unlock;
5137 }
5138 /*
5139 * We are safe logging the other inode without
5140 * acquiring its i_mutex as long as we log with
5141 * the LOG_INODE_EXISTS mode. We're safe against
5142 * concurrent renames of the other inode as well
5143 * because during a rename we pin the log and
5144 * update the log with the new name before we
5145 * unpin it.
5146 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005147 err = btrfs_log_inode(trans, root,
5148 BTRFS_I(other_inode),
5149 LOG_OTHER_INODE, 0, LLONG_MAX,
5150 ctx);
Filipe Manana7cbd49c2019-09-10 15:26:49 +01005151 btrfs_add_delayed_iput(other_inode);
Filipe Manana44f714d2016-06-06 16:11:13 +01005152 if (err)
5153 goto out_unlock;
5154 else
5155 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01005156 }
5157 }
5158
Filipe Manana36283bf2015-06-20 00:44:51 +01005159 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5160 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
5161 if (ins_nr == 0)
5162 goto next_slot;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005163 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01005164 &last_extent, ins_start_slot,
5165 ins_nr, inode_only, logged_isize);
5166 if (ret < 0) {
5167 err = ret;
5168 goto out_unlock;
5169 }
5170 ins_nr = 0;
5171 if (ret) {
5172 btrfs_release_path(path);
5173 continue;
5174 }
5175 goto next_slot;
5176 }
5177
Chris Mason31ff1cd2008-09-11 16:17:57 -04005178 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5179 ins_nr++;
5180 goto next_slot;
5181 } else if (!ins_nr) {
5182 ins_start_slot = path->slots[0];
5183 ins_nr = 1;
5184 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04005185 }
5186
Nikolay Borisova59108a2017-01-18 00:31:48 +02005187 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005188 ins_start_slot, ins_nr, inode_only,
5189 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005190 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005191 err = ret;
5192 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02005193 }
5194 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04005195 ins_nr = 0;
5196 btrfs_release_path(path);
5197 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005198 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005199 ins_nr = 1;
5200 ins_start_slot = path->slots[0];
5201next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04005202
Chris Mason3a5f1d42008-09-11 15:53:37 -04005203 nritems = btrfs_header_nritems(path->nodes[0]);
5204 path->slots[0]++;
5205 if (path->slots[0] < nritems) {
5206 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
5207 path->slots[0]);
5208 goto again;
5209 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005210 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005211 ret = copy_items(trans, inode, dst_path, path,
Josef Bacik16e75492013-10-22 12:18:51 -04005212 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005213 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005214 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005215 err = ret;
5216 goto out_unlock;
5217 }
Josef Bacik16e75492013-10-22 12:18:51 -04005218 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005219 ins_nr = 0;
5220 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005221 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01005222next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005223 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04005224 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005225 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04005226 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005227 min_key.offset = 0;
5228 } else {
Chris Masone02119d2008-09-05 16:13:11 -04005229 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005230 }
Chris Masone02119d2008-09-05 16:13:11 -04005231 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005232 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005233 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005234 ins_start_slot, ins_nr, inode_only,
5235 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005236 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005237 err = ret;
5238 goto out_unlock;
5239 }
Josef Bacik16e75492013-10-22 12:18:51 -04005240 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005241 ins_nr = 0;
5242 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005243
Filipe Manana36283bf2015-06-20 00:44:51 +01005244 btrfs_release_path(path);
5245 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005246 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01005247 if (err)
5248 goto out_unlock;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005249 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005250 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5251 btrfs_release_path(path);
5252 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005253 err = btrfs_log_trailing_hole(trans, root, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005254 if (err)
5255 goto out_unlock;
5256 }
Josef Bacika95249b2012-10-11 16:17:34 -04005257log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04005258 btrfs_release_path(path);
5259 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01005260 if (need_log_inode_item) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005261 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana9a8fca62018-05-11 16:42:42 +01005262 if (!err && !xattrs_logged) {
5263 err = btrfs_log_all_xattrs(trans, root, inode, path,
5264 dst_path);
5265 btrfs_release_path(path);
5266 }
Filipe Mananae4545de2015-06-17 12:49:23 +01005267 if (err)
5268 goto out_unlock;
5269 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005270 if (fast_search) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005271 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Josef Bacika2120a42018-05-23 11:58:35 -04005272 ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005273 if (ret) {
5274 err = ret;
5275 goto out_unlock;
5276 }
Josef Bacikd006a042013-11-12 20:54:09 -05005277 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06005278 struct extent_map *em, *n;
5279
Filipe Manana49dae1b2014-09-06 22:34:39 +01005280 write_lock(&em_tree->lock);
5281 /*
5282 * We can't just remove every em if we're called for a ranged
5283 * fsync - that is, one that doesn't cover the whole possible
5284 * file range (0 to LLONG_MAX). This is because we can have
5285 * em's that fall outside the range we're logging and therefore
5286 * their ordered operations haven't completed yet
5287 * (btrfs_finish_ordered_io() not invoked yet). This means we
5288 * didn't get their respective file extent item in the fs/subvol
5289 * tree yet, and need to let the next fast fsync (one which
5290 * consults the list of modified extent maps) find the em so
5291 * that it logs a matching file extent item and waits for the
5292 * respective ordered operation to complete (if it's still
5293 * running).
5294 *
5295 * Removing every em outside the range we're logging would make
5296 * the next fast fsync not log their matching file extent items,
5297 * therefore making us lose data after a log replay.
5298 */
5299 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5300 list) {
5301 const u64 mod_end = em->mod_start + em->mod_len - 1;
5302
5303 if (em->mod_start >= start && mod_end <= end)
5304 list_del_init(&em->list);
5305 }
5306 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005307 }
5308
Nikolay Borisova59108a2017-01-18 00:31:48 +02005309 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5310 ret = log_directory_changes(trans, root, inode, path, dst_path,
5311 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005312 if (ret) {
5313 err = ret;
5314 goto out_unlock;
5315 }
Chris Masone02119d2008-09-05 16:13:11 -04005316 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005317
Filipe Manana110850f2019-06-07 11:25:24 +01005318 /*
5319 * Don't update last_log_commit if we logged that an inode exists after
5320 * it was loaded to memory (full_sync bit set).
5321 * This is to prevent data loss when we do a write to the inode, then
5322 * the inode gets evicted after all delalloc was flushed, then we log
5323 * it exists (due to a rename for example) and then fsync it. This last
5324 * fsync would do nothing (not logging the extents previously written).
5325 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005326 spin_lock(&inode->lock);
5327 inode->logged_trans = trans->transid;
Filipe Manana110850f2019-06-07 11:25:24 +01005328 if (inode_only != LOG_INODE_EXISTS ||
5329 !test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
5330 inode->last_log_commit = inode->last_sub_trans;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005331 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005332out_unlock:
Nikolay Borisova59108a2017-01-18 00:31:48 +02005333 mutex_unlock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04005334
5335 btrfs_free_path(path);
5336 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005337 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005338}
5339
Chris Mason12fcfd22009-03-24 10:24:20 -04005340/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005341 * Check if we must fallback to a transaction commit when logging an inode.
5342 * This must be called after logging the inode and is used only in the context
5343 * when fsyncing an inode requires the need to log some other inode - in which
5344 * case we can't lock the i_mutex of each other inode we need to log as that
5345 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5346 * log inodes up or down in the hierarchy) or rename operations for example. So
5347 * we take the log_mutex of the inode after we have logged it and then check for
5348 * its last_unlink_trans value - this is safe because any task setting
5349 * last_unlink_trans must take the log_mutex and it must do this before it does
5350 * the actual unlink operation, so if we do this check before a concurrent task
5351 * sets last_unlink_trans it means we've logged a consistent version/state of
5352 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005353 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005354 * we logged the inode or it might have also done the unlink).
5355 */
5356static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005357 struct btrfs_inode *inode)
Filipe Manana2be63d52016-02-12 11:34:23 +00005358{
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005359 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana2be63d52016-02-12 11:34:23 +00005360 bool ret = false;
5361
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005362 mutex_lock(&inode->log_mutex);
5363 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
Filipe Manana2be63d52016-02-12 11:34:23 +00005364 /*
5365 * Make sure any commits to the log are forced to be full
5366 * commits.
5367 */
5368 btrfs_set_log_full_commit(fs_info, trans);
5369 ret = true;
5370 }
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005371 mutex_unlock(&inode->log_mutex);
Filipe Manana2be63d52016-02-12 11:34:23 +00005372
5373 return ret;
5374}
5375
5376/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005377 * follow the dentry parent pointers up the chain and see if any
5378 * of the directories in it require a full commit before they can
5379 * be logged. Returns zero if nothing special needs to be done or 1 if
5380 * a full commit is required.
5381 */
5382static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005383 struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005384 struct dentry *parent,
5385 struct super_block *sb,
5386 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005387{
Chris Mason12fcfd22009-03-24 10:24:20 -04005388 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005389 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005390
Chris Masonaf4176b2009-03-24 10:24:31 -04005391 /*
5392 * for regular files, if its inode is already on disk, we don't
5393 * have to worry about the parents at all. This is because
5394 * we can use the last_unlink_trans field to record renames
5395 * and other fun in this file.
5396 */
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005397 if (S_ISREG(inode->vfs_inode.i_mode) &&
5398 inode->generation <= last_committed &&
5399 inode->last_unlink_trans <= last_committed)
5400 goto out;
Chris Masonaf4176b2009-03-24 10:24:31 -04005401
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005402 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005403 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005404 goto out;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005405 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005406 }
5407
5408 while (1) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005409 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005410 ret = 1;
5411 break;
5412 }
5413
Al Virofc640052016-04-10 01:33:30 -04005414 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005415 break;
5416
Filipe Manana44f714d2016-06-06 16:11:13 +01005417 if (IS_ROOT(parent)) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005418 inode = BTRFS_I(d_inode(parent));
5419 if (btrfs_must_commit_transaction(trans, inode))
Filipe Manana44f714d2016-06-06 16:11:13 +01005420 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005421 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005422 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005423
Josef Bacik6a912212010-11-20 09:48:00 +00005424 parent = dget_parent(parent);
5425 dput(old_parent);
5426 old_parent = parent;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005427 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005428
5429 }
Josef Bacik6a912212010-11-20 09:48:00 +00005430 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005431out:
Chris Masone02119d2008-09-05 16:13:11 -04005432 return ret;
5433}
5434
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005435struct btrfs_dir_list {
5436 u64 ino;
5437 struct list_head list;
5438};
5439
5440/*
5441 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5442 * details about the why it is needed.
5443 * This is a recursive operation - if an existing dentry corresponds to a
5444 * directory, that directory's new entries are logged too (same behaviour as
5445 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5446 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5447 * complains about the following circular lock dependency / possible deadlock:
5448 *
5449 * CPU0 CPU1
5450 * ---- ----
5451 * lock(&type->i_mutex_dir_key#3/2);
5452 * lock(sb_internal#2);
5453 * lock(&type->i_mutex_dir_key#3/2);
5454 * lock(&sb->s_type->i_mutex_key#14);
5455 *
5456 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5457 * sb_start_intwrite() in btrfs_start_transaction().
5458 * Not locking i_mutex of the inodes is still safe because:
5459 *
5460 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5461 * that while logging the inode new references (names) are added or removed
5462 * from the inode, leaving the logged inode item with a link count that does
5463 * not match the number of logged inode reference items. This is fine because
5464 * at log replay time we compute the real number of links and correct the
5465 * link count in the inode item (see replay_one_buffer() and
5466 * link_to_fixup_dir());
5467 *
5468 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5469 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5470 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5471 * has a size that doesn't match the sum of the lengths of all the logged
5472 * names. This does not result in a problem because if a dir_item key is
5473 * logged but its matching dir_index key is not logged, at log replay time we
5474 * don't use it to replay the respective name (see replay_one_name()). On the
5475 * other hand if only the dir_index key ends up being logged, the respective
5476 * name is added to the fs/subvol tree with both the dir_item and dir_index
5477 * keys created (see replay_one_name()).
5478 * The directory's inode item with a wrong i_size is not a problem as well,
5479 * since we don't use it at log replay time to set the i_size in the inode
5480 * item of the fs/subvol tree (see overwrite_item()).
5481 */
5482static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5483 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005484 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005485 struct btrfs_log_ctx *ctx)
5486{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005487 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005488 struct btrfs_root *log = root->log_root;
5489 struct btrfs_path *path;
5490 LIST_HEAD(dir_list);
5491 struct btrfs_dir_list *dir_elem;
5492 int ret = 0;
5493
5494 path = btrfs_alloc_path();
5495 if (!path)
5496 return -ENOMEM;
5497
5498 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5499 if (!dir_elem) {
5500 btrfs_free_path(path);
5501 return -ENOMEM;
5502 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005503 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005504 list_add_tail(&dir_elem->list, &dir_list);
5505
5506 while (!list_empty(&dir_list)) {
5507 struct extent_buffer *leaf;
5508 struct btrfs_key min_key;
5509 int nritems;
5510 int i;
5511
5512 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5513 list);
5514 if (ret)
5515 goto next_dir_inode;
5516
5517 min_key.objectid = dir_elem->ino;
5518 min_key.type = BTRFS_DIR_ITEM_KEY;
5519 min_key.offset = 0;
5520again:
5521 btrfs_release_path(path);
5522 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5523 if (ret < 0) {
5524 goto next_dir_inode;
5525 } else if (ret > 0) {
5526 ret = 0;
5527 goto next_dir_inode;
5528 }
5529
5530process_leaf:
5531 leaf = path->nodes[0];
5532 nritems = btrfs_header_nritems(leaf);
5533 for (i = path->slots[0]; i < nritems; i++) {
5534 struct btrfs_dir_item *di;
5535 struct btrfs_key di_key;
5536 struct inode *di_inode;
5537 struct btrfs_dir_list *new_dir_elem;
5538 int log_mode = LOG_INODE_EXISTS;
5539 int type;
5540
5541 btrfs_item_key_to_cpu(leaf, &min_key, i);
5542 if (min_key.objectid != dir_elem->ino ||
5543 min_key.type != BTRFS_DIR_ITEM_KEY)
5544 goto next_dir_inode;
5545
5546 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5547 type = btrfs_dir_type(leaf, di);
5548 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5549 type != BTRFS_FT_DIR)
5550 continue;
5551 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5552 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5553 continue;
5554
Robbie Koec125cf2016-10-28 10:48:26 +08005555 btrfs_release_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005556 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005557 if (IS_ERR(di_inode)) {
5558 ret = PTR_ERR(di_inode);
5559 goto next_dir_inode;
5560 }
5561
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005562 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
Filipe Manana7cbd49c2019-09-10 15:26:49 +01005563 btrfs_add_delayed_iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005564 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005565 }
5566
5567 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005568 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005569 log_mode = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005570 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005571 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005572 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005573 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005574 ret = 1;
Filipe Manana7cbd49c2019-09-10 15:26:49 +01005575 btrfs_add_delayed_iput(di_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005576 if (ret)
5577 goto next_dir_inode;
5578 if (ctx->log_new_dentries) {
5579 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5580 GFP_NOFS);
5581 if (!new_dir_elem) {
5582 ret = -ENOMEM;
5583 goto next_dir_inode;
5584 }
5585 new_dir_elem->ino = di_key.objectid;
5586 list_add_tail(&new_dir_elem->list, &dir_list);
5587 }
5588 break;
5589 }
5590 if (i == nritems) {
5591 ret = btrfs_next_leaf(log, path);
5592 if (ret < 0) {
5593 goto next_dir_inode;
5594 } else if (ret > 0) {
5595 ret = 0;
5596 goto next_dir_inode;
5597 }
5598 goto process_leaf;
5599 }
5600 if (min_key.offset < (u64)-1) {
5601 min_key.offset++;
5602 goto again;
5603 }
5604next_dir_inode:
5605 list_del(&dir_elem->list);
5606 kfree(dir_elem);
5607 }
5608
5609 btrfs_free_path(path);
5610 return ret;
5611}
5612
Filipe Manana18aa0922015-08-05 16:49:08 +01005613static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005614 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01005615 struct btrfs_log_ctx *ctx)
5616{
David Sterba3ffbd682018-06-29 10:56:42 +02005617 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana18aa0922015-08-05 16:49:08 +01005618 int ret;
5619 struct btrfs_path *path;
5620 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005621 struct btrfs_root *root = inode->root;
5622 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005623
5624 path = btrfs_alloc_path();
5625 if (!path)
5626 return -ENOMEM;
5627 path->skip_locking = 1;
5628 path->search_commit_root = 1;
5629
5630 key.objectid = ino;
5631 key.type = BTRFS_INODE_REF_KEY;
5632 key.offset = 0;
5633 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5634 if (ret < 0)
5635 goto out;
5636
5637 while (true) {
5638 struct extent_buffer *leaf = path->nodes[0];
5639 int slot = path->slots[0];
5640 u32 cur_offset = 0;
5641 u32 item_size;
5642 unsigned long ptr;
5643
5644 if (slot >= btrfs_header_nritems(leaf)) {
5645 ret = btrfs_next_leaf(root, path);
5646 if (ret < 0)
5647 goto out;
5648 else if (ret > 0)
5649 break;
5650 continue;
5651 }
5652
5653 btrfs_item_key_to_cpu(leaf, &key, slot);
5654 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5655 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5656 break;
5657
5658 item_size = btrfs_item_size_nr(leaf, slot);
5659 ptr = btrfs_item_ptr_offset(leaf, slot);
5660 while (cur_offset < item_size) {
5661 struct btrfs_key inode_key;
5662 struct inode *dir_inode;
5663
5664 inode_key.type = BTRFS_INODE_ITEM_KEY;
5665 inode_key.offset = 0;
5666
5667 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5668 struct btrfs_inode_extref *extref;
5669
5670 extref = (struct btrfs_inode_extref *)
5671 (ptr + cur_offset);
5672 inode_key.objectid = btrfs_inode_extref_parent(
5673 leaf, extref);
5674 cur_offset += sizeof(*extref);
5675 cur_offset += btrfs_inode_extref_name_len(leaf,
5676 extref);
5677 } else {
5678 inode_key.objectid = key.offset;
5679 cur_offset = item_size;
5680 }
5681
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005682 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
Filipe Manana18aa0922015-08-05 16:49:08 +01005683 root, NULL);
Filipe Mananad2c6df32018-10-09 15:05:29 +01005684 /*
5685 * If the parent inode was deleted, return an error to
5686 * fallback to a transaction commit. This is to prevent
5687 * getting an inode that was moved from one parent A to
5688 * a parent B, got its former parent A deleted and then
5689 * it got fsync'ed, from existing at both parents after
5690 * a log replay (and the old parent still existing).
5691 * Example:
5692 *
5693 * mkdir /mnt/A
5694 * mkdir /mnt/B
5695 * touch /mnt/B/bar
5696 * sync
5697 * mv /mnt/B/bar /mnt/A/bar
5698 * mv -T /mnt/A /mnt/B
5699 * fsync /mnt/B/bar
5700 * <power fail>
5701 *
5702 * If we ignore the old parent B which got deleted,
5703 * after a log replay we would have file bar linked
5704 * at both parents and the old parent B would still
5705 * exist.
5706 */
5707 if (IS_ERR(dir_inode)) {
5708 ret = PTR_ERR(dir_inode);
5709 goto out;
5710 }
Filipe Manana18aa0922015-08-05 16:49:08 +01005711
Filipe Manana657ed1a2016-04-06 17:11:56 +01005712 if (ctx)
5713 ctx->log_new_dentries = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005714 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Filipe Manana18aa0922015-08-05 16:49:08 +01005715 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005716 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005717 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005718 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005719 if (!ret && ctx && ctx->log_new_dentries)
5720 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01005721 BTRFS_I(dir_inode), ctx);
Filipe Manana7cbd49c2019-09-10 15:26:49 +01005722 btrfs_add_delayed_iput(dir_inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005723 if (ret)
5724 goto out;
5725 }
5726 path->slots[0]++;
5727 }
5728 ret = 0;
5729out:
5730 btrfs_free_path(path);
5731 return ret;
5732}
5733
Chris Masone02119d2008-09-05 16:13:11 -04005734/*
5735 * helper function around btrfs_log_inode to make sure newly created
5736 * parent directories also end up in the log. A minimal inode and backref
5737 * only logging is done of any parent directories that are older than
5738 * the last committed transaction
5739 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005740static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005741 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005742 struct dentry *parent,
5743 const loff_t start,
5744 const loff_t end,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07005745 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005746 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005747{
Nikolay Borisovf8822742018-02-27 17:37:17 +02005748 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005749 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04005750 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005751 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005752 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005753 u64 last_committed = fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005754 bool log_dentries = false;
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005755 struct btrfs_inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005756
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005757 sb = inode->vfs_inode.i_sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04005758
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005759 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005760 ret = 1;
5761 goto end_no_trans;
5762 }
5763
Miao Xie995946d2014-04-02 19:51:06 +08005764 /*
5765 * The prev transaction commit doesn't complete, we need do
5766 * full commit by ourselves.
5767 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005768 if (fs_info->last_trans_log_full_commit >
5769 fs_info->last_trans_committed) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005770 ret = 1;
5771 goto end_no_trans;
5772 }
5773
Nikolay Borisovf8822742018-02-27 17:37:17 +02005774 if (btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005775 ret = 1;
5776 goto end_no_trans;
5777 }
5778
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005779 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
5780 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04005781 if (ret)
5782 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005783
Filipe Manana55f21e12018-10-08 11:12:55 +01005784 /*
5785 * Skip already logged inodes or inodes corresponding to tmpfiles
5786 * (since logging them is pointless, a link count of 0 means they
5787 * will never be accessible).
5788 */
5789 if (btrfs_inode_in_log(inode, trans->transid) ||
5790 inode->vfs_inode.i_nlink == 0) {
Chris Mason257c62e2009-10-13 13:21:08 -04005791 ret = BTRFS_NO_LOG_SYNC;
5792 goto end_no_trans;
5793 }
5794
Miao Xie8b050d32014-02-20 18:08:58 +08005795 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005796 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005797 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005798
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005799 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005800 if (ret)
5801 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005802
Chris Masonaf4176b2009-03-24 10:24:31 -04005803 /*
5804 * for regular files, if its inode is already on disk, we don't
5805 * have to worry about the parents at all. This is because
5806 * we can use the last_unlink_trans field to record renames
5807 * and other fun in this file.
5808 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005809 if (S_ISREG(inode->vfs_inode.i_mode) &&
5810 inode->generation <= last_committed &&
5811 inode->last_unlink_trans <= last_committed) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005812 ret = 0;
5813 goto end_trans;
5814 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005815
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005816 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005817 log_dentries = true;
5818
Filipe Manana18aa0922015-08-05 16:49:08 +01005819 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005820 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005821 * inodes are fully logged. This is to prevent leaving dangling
5822 * directory index entries in directories that were our parents but are
5823 * not anymore. Not doing this results in old parent directory being
5824 * impossible to delete after log replay (rmdir will always fail with
5825 * error -ENOTEMPTY).
5826 *
5827 * Example 1:
5828 *
5829 * mkdir testdir
5830 * touch testdir/foo
5831 * ln testdir/foo testdir/bar
5832 * sync
5833 * unlink testdir/bar
5834 * xfs_io -c fsync testdir/foo
5835 * <power failure>
5836 * mount fs, triggers log replay
5837 *
5838 * If we don't log the parent directory (testdir), after log replay the
5839 * directory still has an entry pointing to the file inode using the bar
5840 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5841 * the file inode has a link count of 1.
5842 *
5843 * Example 2:
5844 *
5845 * mkdir testdir
5846 * touch foo
5847 * ln foo testdir/foo2
5848 * ln foo testdir/foo3
5849 * sync
5850 * unlink testdir/foo3
5851 * xfs_io -c fsync foo
5852 * <power failure>
5853 * mount fs, triggers log replay
5854 *
5855 * Similar as the first example, after log replay the parent directory
5856 * testdir still has an entry pointing to the inode file with name foo3
5857 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5858 * and has a link count of 2.
5859 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005860 if (inode->last_unlink_trans > last_committed) {
Filipe Manana18aa0922015-08-05 16:49:08 +01005861 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5862 if (ret)
5863 goto end_trans;
5864 }
5865
Filipe Manana10b04212018-11-28 14:54:28 +00005866 /*
5867 * If a new hard link was added to the inode in the current transaction
5868 * and its link count is now greater than 1, we need to fallback to a
5869 * transaction commit, otherwise we can end up not logging all its new
5870 * parents for all the hard links. Here just from the dentry used to
5871 * fsync, we can not visit the ancestor inodes for all the other hard
5872 * links to figure out if any is new, so we fallback to a transaction
5873 * commit (instead of adding a lot of complexity of scanning a btree,
5874 * since this scenario is not a common use case).
5875 */
5876 if (inode->vfs_inode.i_nlink > 1 &&
5877 inode->last_link_trans > last_committed) {
5878 ret = -EMLINK;
5879 goto end_trans;
5880 }
5881
Chris Masond3977122009-01-05 21:25:51 -05005882 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005883 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005884 break;
5885
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005886 inode = BTRFS_I(d_inode(parent));
5887 if (root != inode->root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005888 break;
5889
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005890 if (inode->generation > last_committed) {
5891 ret = btrfs_log_inode(trans, root, inode,
5892 LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005893 if (ret)
5894 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005895 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005896 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005897 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005898
Josef Bacik6a912212010-11-20 09:48:00 +00005899 parent = dget_parent(parent);
5900 dput(old_parent);
5901 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005902 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005903 if (log_dentries)
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005904 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005905 else
5906 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005907end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005908 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005909 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005910 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005911 ret = 1;
5912 }
Miao Xie8b050d32014-02-20 18:08:58 +08005913
5914 if (ret)
5915 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005916 btrfs_end_log_trans(root);
5917end_no_trans:
5918 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005919}
5920
5921/*
5922 * it is not safe to log dentry if the chunk root has added new
5923 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5924 * If this returns 1, you must commit the transaction to safely get your
5925 * data on disk.
5926 */
5927int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Nikolay Borisove5b84f7a2018-02-27 17:37:18 +02005928 struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005929 const loff_t start,
5930 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005931 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005932{
Josef Bacik6a912212010-11-20 09:48:00 +00005933 struct dentry *parent = dget_parent(dentry);
5934 int ret;
5935
Nikolay Borisovf8822742018-02-27 17:37:17 +02005936 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
5937 start, end, LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005938 dput(parent);
5939
5940 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005941}
5942
5943/*
5944 * should be called during mount to recover any replay any log trees
5945 * from the FS
5946 */
5947int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5948{
5949 int ret;
5950 struct btrfs_path *path;
5951 struct btrfs_trans_handle *trans;
5952 struct btrfs_key key;
5953 struct btrfs_key found_key;
5954 struct btrfs_key tmp_key;
5955 struct btrfs_root *log;
5956 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5957 struct walk_control wc = {
5958 .process_func = process_one_buffer,
5959 .stage = 0,
5960 };
5961
Chris Masone02119d2008-09-05 16:13:11 -04005962 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005963 if (!path)
5964 return -ENOMEM;
5965
Josef Bacikafcdd122016-09-02 15:40:02 -04005966 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005967
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005968 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005969 if (IS_ERR(trans)) {
5970 ret = PTR_ERR(trans);
5971 goto error;
5972 }
Chris Masone02119d2008-09-05 16:13:11 -04005973
5974 wc.trans = trans;
5975 wc.pin = 1;
5976
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005977 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005978 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005979 btrfs_handle_fs_error(fs_info, ret,
5980 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005981 goto error;
5982 }
Chris Masone02119d2008-09-05 16:13:11 -04005983
5984again:
5985 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5986 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005987 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005988
Chris Masond3977122009-01-05 21:25:51 -05005989 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005990 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005991
5992 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005993 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005994 "Couldn't find tree log root.");
5995 goto error;
5996 }
Chris Masone02119d2008-09-05 16:13:11 -04005997 if (ret > 0) {
5998 if (path->slots[0] == 0)
5999 break;
6000 path->slots[0]--;
6001 }
6002 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
6003 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02006004 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006005 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
6006 break;
6007
Miao Xiecb517ea2013-05-15 07:48:19 +00006008 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006009 if (IS_ERR(log)) {
6010 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08006011 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006012 "Couldn't read tree log root.");
6013 goto error;
6014 }
Chris Masone02119d2008-09-05 16:13:11 -04006015
6016 tmp_key.objectid = found_key.offset;
6017 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
6018 tmp_key.offset = (u64)-1;
6019
6020 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006021 if (IS_ERR(wc.replay_dest)) {
6022 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006023 free_extent_buffer(log->node);
6024 free_extent_buffer(log->commit_root);
6025 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006026 btrfs_handle_fs_error(fs_info, ret,
6027 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006028 goto error;
6029 }
Chris Masone02119d2008-09-05 16:13:11 -04006030
Yan Zheng07d400a2009-01-06 11:42:00 -05006031 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006032 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04006033 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04006034
Josef Bacikb50c6e22013-04-25 15:55:30 -04006035 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04006036 ret = fixup_inode_link_counts(trans, wc.replay_dest,
6037 path);
Chris Masone02119d2008-09-05 16:13:11 -04006038 }
Chris Masone02119d2008-09-05 16:13:11 -04006039
Liu Bo900c9982018-01-25 11:02:56 -07006040 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
6041 struct btrfs_root *root = wc.replay_dest;
6042
6043 btrfs_release_path(path);
6044
6045 /*
6046 * We have just replayed everything, and the highest
6047 * objectid of fs roots probably has changed in case
6048 * some inode_item's got replayed.
6049 *
6050 * root->objectid_mutex is not acquired as log replay
6051 * could only happen during mount.
6052 */
6053 ret = btrfs_find_highest_objectid(root,
6054 &root->highest_objectid);
6055 }
6056
Chris Masone02119d2008-09-05 16:13:11 -04006057 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05006058 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04006059 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04006060 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04006061 kfree(log);
6062
Josef Bacikb50c6e22013-04-25 15:55:30 -04006063 if (ret)
6064 goto error;
6065
Chris Masone02119d2008-09-05 16:13:11 -04006066 if (found_key.offset == 0)
6067 break;
6068 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006069 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006070
6071 /* step one is to pin it all, step two is to replay just inodes */
6072 if (wc.pin) {
6073 wc.pin = 0;
6074 wc.process_func = replay_one_buffer;
6075 wc.stage = LOG_WALK_REPLAY_INODES;
6076 goto again;
6077 }
6078 /* step three is to replay everything */
6079 if (wc.stage < LOG_WALK_REPLAY_ALL) {
6080 wc.stage++;
6081 goto again;
6082 }
6083
6084 btrfs_free_path(path);
6085
Josef Bacikabefa552013-04-24 16:40:05 -04006086 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006087 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04006088 if (ret)
6089 return ret;
6090
Chris Masone02119d2008-09-05 16:13:11 -04006091 free_extent_buffer(log_root_tree->node);
6092 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04006093 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04006094 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006095
Josef Bacikabefa552013-04-24 16:40:05 -04006096 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006097error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04006098 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006099 btrfs_end_transaction(wc.trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006100 btrfs_free_path(path);
6101 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006102}
Chris Mason12fcfd22009-03-24 10:24:20 -04006103
6104/*
6105 * there are some corner cases where we want to force a full
6106 * commit instead of allowing a directory to be logged.
6107 *
6108 * They revolve around files there were unlinked from the directory, and
6109 * this function updates the parent directory so that a full commit is
6110 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00006111 *
6112 * Must be called before the unlink operations (updates to the subvolume tree,
6113 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04006114 */
6115void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006116 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04006117 int for_rename)
6118{
6119 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006120 * when we're logging a file, if it hasn't been renamed
6121 * or unlinked, and its inode is fully committed on disk,
6122 * we don't have to worry about walking up the directory chain
6123 * to log its parents.
6124 *
6125 * So, we use the last_unlink_trans field to put this transid
6126 * into the file. When the file is logged we check it and
6127 * don't log the parents if the file is fully on disk.
6128 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006129 mutex_lock(&inode->log_mutex);
6130 inode->last_unlink_trans = trans->transid;
6131 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04006132
6133 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006134 * if this directory was already logged any new
6135 * names for this file/dir will get recorded
6136 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006137 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006138 return;
6139
6140 /*
6141 * if the inode we're about to unlink was logged,
6142 * the log will be properly updated for any new names
6143 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006144 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006145 return;
6146
6147 /*
6148 * when renaming files across directories, if the directory
6149 * there we're unlinking from gets fsync'd later on, there's
6150 * no way to find the destination directory later and fsync it
6151 * properly. So, we have to be conservative and force commits
6152 * so the new name gets discovered.
6153 */
6154 if (for_rename)
6155 goto record;
6156
6157 /* we can safely do the unlink without any special recording */
6158 return;
6159
6160record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006161 mutex_lock(&dir->log_mutex);
6162 dir->last_unlink_trans = trans->transid;
6163 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04006164}
6165
6166/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006167 * Make sure that if someone attempts to fsync the parent directory of a deleted
6168 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6169 * that after replaying the log tree of the parent directory's root we will not
6170 * see the snapshot anymore and at log replay time we will not see any log tree
6171 * corresponding to the deleted snapshot's root, which could lead to replaying
6172 * it after replaying the log tree of the parent directory (which would replay
6173 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00006174 *
6175 * Must be called before the actual snapshot destroy operation (updates to the
6176 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006177 */
6178void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02006179 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006180{
Nikolay Borisov43663552017-01-18 00:31:29 +02006181 mutex_lock(&dir->log_mutex);
6182 dir->last_unlink_trans = trans->transid;
6183 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006184}
6185
6186/*
Chris Mason12fcfd22009-03-24 10:24:20 -04006187 * Call this after adding a new name for a file and it will properly
6188 * update the log to reflect the new name.
6189 *
Filipe Mananad4682ba2018-06-11 19:24:28 +01006190 * @ctx can not be NULL when @sync_log is false, and should be NULL when it's
6191 * true (because it's not used).
6192 *
6193 * Return value depends on whether @sync_log is true or false.
6194 * When true: returns BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6195 * committed by the caller, and BTRFS_DONT_NEED_TRANS_COMMIT
6196 * otherwise.
6197 * When false: returns BTRFS_DONT_NEED_LOG_SYNC if the caller does not need to
6198 * to sync the log, BTRFS_NEED_LOG_SYNC if it needs to sync the log,
6199 * or BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6200 * committed (without attempting to sync the log).
Chris Mason12fcfd22009-03-24 10:24:20 -04006201 */
6202int btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006203 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Filipe Mananad4682ba2018-06-11 19:24:28 +01006204 struct dentry *parent,
6205 bool sync_log, struct btrfs_log_ctx *ctx)
Chris Mason12fcfd22009-03-24 10:24:20 -04006206{
David Sterba3ffbd682018-06-29 10:56:42 +02006207 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006208 int ret;
Chris Mason12fcfd22009-03-24 10:24:20 -04006209
6210 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006211 * this will force the logging code to walk the dentry chain
6212 * up for the file
6213 */
Filipe Manana9a6509c2018-02-28 15:55:40 +00006214 if (!S_ISDIR(inode->vfs_inode.i_mode))
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006215 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04006216
6217 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006218 * if this inode hasn't been logged and directory we're renaming it
6219 * from hasn't been logged, we don't need to log it
6220 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006221 if (inode->logged_trans <= fs_info->last_trans_committed &&
6222 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
Filipe Mananad4682ba2018-06-11 19:24:28 +01006223 return sync_log ? BTRFS_DONT_NEED_TRANS_COMMIT :
6224 BTRFS_DONT_NEED_LOG_SYNC;
Chris Mason12fcfd22009-03-24 10:24:20 -04006225
Filipe Mananad4682ba2018-06-11 19:24:28 +01006226 if (sync_log) {
6227 struct btrfs_log_ctx ctx2;
6228
6229 btrfs_init_log_ctx(&ctx2, &inode->vfs_inode);
6230 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6231 LOG_INODE_EXISTS, &ctx2);
6232 if (ret == BTRFS_NO_LOG_SYNC)
6233 return BTRFS_DONT_NEED_TRANS_COMMIT;
6234 else if (ret)
6235 return BTRFS_NEED_TRANS_COMMIT;
6236
6237 ret = btrfs_sync_log(trans, inode->root, &ctx2);
6238 if (ret)
6239 return BTRFS_NEED_TRANS_COMMIT;
6240 return BTRFS_DONT_NEED_TRANS_COMMIT;
6241 }
6242
6243 ASSERT(ctx);
6244 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6245 LOG_INODE_EXISTS, ctx);
6246 if (ret == BTRFS_NO_LOG_SYNC)
6247 return BTRFS_DONT_NEED_LOG_SYNC;
6248 else if (ret)
6249 return BTRFS_NEED_TRANS_COMMIT;
6250
6251 return BTRFS_NEED_LOG_SYNC;
Chris Mason12fcfd22009-03-24 10:24:20 -04006252}
6253