blob: 561884f60d35c36e11928e28e5007901fc695198 [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
Filipe Mananaa3baaf02019-02-13 12:14:09 +000030#define LOG_OTHER_INODE_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040031
32/*
Chris Mason12fcfd22009-03-24 10:24:20 -040033 * directory trouble cases
34 *
35 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
36 * log, we must force a full commit before doing an fsync of the directory
37 * where the unlink was done.
38 * ---> record transid of last unlink/rename per directory
39 *
40 * mkdir foo/some_dir
41 * normal commit
42 * rename foo/some_dir foo2/some_dir
43 * mkdir foo/some_dir
44 * fsync foo/some_dir/some_file
45 *
46 * The fsync above will unlink the original some_dir without recording
47 * it in its new location (foo2). After a crash, some_dir will be gone
48 * unless the fsync of some_file forces a full commit
49 *
50 * 2) we must log any new names for any file or dir that is in the fsync
51 * log. ---> check inode while renaming/linking.
52 *
53 * 2a) we must log any new names for any file or dir during rename
54 * when the directory they are being removed from was logged.
55 * ---> check inode and old parent dir during rename
56 *
57 * 2a is actually the more important variant. With the extra logging
58 * a crash might unlink the old name without recreating the new one
59 *
60 * 3) after a crash, we must go through any directories with a link count
61 * of zero and redo the rm -rf
62 *
63 * mkdir f1/foo
64 * normal commit
65 * rm -rf f1/foo
66 * fsync(f1)
67 *
68 * The directory f1 was fully removed from the FS, but fsync was never
69 * called on f1, only its parent dir. After a crash the rm -rf must
70 * be replayed. This must be able to recurse down the entire
71 * directory tree. The inode link count fixup code takes care of the
72 * ugly details.
73 */
74
75/*
Chris Masone02119d2008-09-05 16:13:11 -040076 * stages for the tree walking. The first
77 * stage (0) is to only pin down the blocks we find
78 * the second stage (1) is to make sure that all the inodes
79 * we find in the log are created in the subvolume.
80 *
81 * The last stage is to deal with directories and links and extents
82 * and all the other fun semantics
83 */
84#define LOG_WALK_PIN_ONLY 0
85#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040086#define LOG_WALK_REPLAY_DIR_INDEX 2
87#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040088
Chris Mason12fcfd22009-03-24 10:24:20 -040089static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +020090 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +010091 int inode_only,
92 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +010093 const loff_t end,
94 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -050095static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
96 struct btrfs_root *root,
97 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -040098static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
99 struct btrfs_root *root,
100 struct btrfs_root *log,
101 struct btrfs_path *path,
102 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400103
104/*
105 * tree logging is a special write ahead log used to make sure that
106 * fsyncs and O_SYNCs can happen without doing full tree commits.
107 *
108 * Full tree commits are expensive because they require commonly
109 * modified blocks to be recowed, creating many dirty pages in the
110 * extent tree an 4x-6x higher write load than ext3.
111 *
112 * Instead of doing a tree commit on every fsync, we use the
113 * key ranges and transaction ids to find items for a given file or directory
114 * that have changed in this transaction. Those items are copied into
115 * a special tree (one per subvolume root), that tree is written to disk
116 * and then the fsync is considered complete.
117 *
118 * After a crash, items are copied out of the log-tree back into the
119 * subvolume tree. Any file data extents found are recorded in the extent
120 * allocation tree, and the log-tree freed.
121 *
122 * The log tree is read three times, once to pin down all the extents it is
123 * using in ram and once, once to create all the inodes logged in the tree
124 * and once to do all the other items.
125 */
126
127/*
Chris Masone02119d2008-09-05 16:13:11 -0400128 * start a sub transaction and setup the log tree
129 * this increments the log tree writer count to make the people
130 * syncing the tree wait for us to finish
131 */
132static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800133 struct btrfs_root *root,
134 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400135{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400136 struct btrfs_fs_info *fs_info = root->fs_info;
Zhaolei34eb2a52015-08-17 18:44:45 +0800137 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500138
139 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800140
Yan Zheng7237f182009-01-21 12:54:03 -0500141 if (root->log_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400142 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800143 ret = -EAGAIN;
144 goto out;
145 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800146
Josef Bacikff782e02009-10-08 15:30:04 -0400147 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800148 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800149 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400150 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800151 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400152 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800153 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400154 mutex_lock(&fs_info->tree_log_mutex);
155 if (!fs_info->log_root_tree)
156 ret = btrfs_init_log_root_tree(trans, fs_info);
157 mutex_unlock(&fs_info->tree_log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800158 if (ret)
159 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400160
Chris Masone02119d2008-09-05 16:13:11 -0400161 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400162 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800163 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800164
165 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
166 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400167 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800168
Miao Xie2ecb7922012-09-06 04:04:27 -0600169 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500170 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800171 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800172 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800173 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800174 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800175 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800176
Miao Xiee87ac132014-02-20 18:08:53 +0800177out:
Yan Zheng7237f182009-01-21 12:54:03 -0500178 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800179 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400180}
181
182/*
183 * returns 0 if there was a log transaction running and we were able
184 * to join, or returns -ENOENT if there were not transactions
185 * in progress
186 */
187static int join_running_log_trans(struct btrfs_root *root)
188{
189 int ret = -ENOENT;
190
191 smp_mb();
192 if (!root->log_root)
193 return -ENOENT;
194
Yan Zheng7237f182009-01-21 12:54:03 -0500195 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400196 if (root->log_root) {
197 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500198 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400199 }
Yan Zheng7237f182009-01-21 12:54:03 -0500200 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400201 return ret;
202}
203
204/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400205 * This either makes the current running log transaction wait
206 * until you call btrfs_end_log_trans() or it makes any future
207 * log transactions wait until you call btrfs_end_log_trans()
208 */
zhong jiang45128b02018-08-17 00:37:15 +0800209void btrfs_pin_log_trans(struct btrfs_root *root)
Chris Mason12fcfd22009-03-24 10:24:20 -0400210{
Chris Mason12fcfd22009-03-24 10:24:20 -0400211 mutex_lock(&root->log_mutex);
212 atomic_inc(&root->log_writers);
213 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -0400214}
215
216/*
Chris Masone02119d2008-09-05 16:13:11 -0400217 * indicate we're done making changes to the log tree
218 * and wake up anyone waiting to do a sync
219 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100220void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400221{
Yan Zheng7237f182009-01-21 12:54:03 -0500222 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +0100223 /* atomic_dec_and_test implies a barrier */
224 cond_wake_up_nomb(&root->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -0500225 }
Chris Masone02119d2008-09-05 16:13:11 -0400226}
227
228
229/*
230 * the walk control struct is used to pass state down the chain when
231 * processing the log tree. The stage field tells us which part
232 * of the log tree processing we are currently doing. The others
233 * are state fields used for that specific part
234 */
235struct walk_control {
236 /* should we free the extent on disk when done? This is used
237 * at transaction commit time while freeing a log tree
238 */
239 int free;
240
241 /* should we write out the extent buffer? This is used
242 * while flushing the log tree to disk during a sync
243 */
244 int write;
245
246 /* should we wait for the extent buffer io to finish? Also used
247 * while flushing the log tree to disk for a sync
248 */
249 int wait;
250
251 /* pin only walk, we record which extents on disk belong to the
252 * log trees
253 */
254 int pin;
255
256 /* what stage of the replay code we're currently in */
257 int stage;
258
Filipe Mananaf2d72f42018-10-08 11:12:55 +0100259 /*
260 * Ignore any items from the inode currently being processed. Needs
261 * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
262 * the LOG_WALK_REPLAY_INODES stage.
263 */
264 bool ignore_cur_inode;
265
Chris Masone02119d2008-09-05 16:13:11 -0400266 /* the root we are currently replaying */
267 struct btrfs_root *replay_dest;
268
269 /* the trans handle for the current replay */
270 struct btrfs_trans_handle *trans;
271
272 /* the function that gets used to process blocks we find in the
273 * tree. Note the extent_buffer might not be up to date when it is
274 * passed in, and it must be checked or read if you need the data
275 * inside it
276 */
277 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800278 struct walk_control *wc, u64 gen, int level);
Chris Masone02119d2008-09-05 16:13:11 -0400279};
280
281/*
282 * process_func used to pin down extents, write them or wait on them
283 */
284static int process_one_buffer(struct btrfs_root *log,
285 struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800286 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -0400287{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400288 struct btrfs_fs_info *fs_info = log->fs_info;
Josef Bacikb50c6e22013-04-25 15:55:30 -0400289 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400290
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400291 /*
292 * If this fs is mixed then we need to be able to process the leaves to
293 * pin down any logged extents, so we have to read the block.
294 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400295 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Qu Wenruo581c1762018-03-29 09:08:11 +0800296 ret = btrfs_read_buffer(eb, gen, level, NULL);
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400297 if (ret)
298 return ret;
299 }
300
Josef Bacikb50c6e22013-04-25 15:55:30 -0400301 if (wc->pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400302 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
303 eb->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400304
305 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400306 if (wc->pin && btrfs_header_level(eb) == 0)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400307 ret = btrfs_exclude_logged_extents(fs_info, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400308 if (wc->write)
309 btrfs_write_tree_block(eb);
310 if (wc->wait)
311 btrfs_wait_tree_block_writeback(eb);
312 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400313 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400314}
315
316/*
317 * Item overwrite used by replay and tree logging. eb, slot and key all refer
318 * to the src data we are copying out.
319 *
320 * root is the tree we are copying into, and path is a scratch
321 * path for use in this function (it should be released on entry and
322 * will be released on exit).
323 *
324 * If the key is already in the destination tree the existing item is
325 * overwritten. If the existing item isn't big enough, it is extended.
326 * If it is too large, it is truncated.
327 *
328 * If the key isn't in the destination yet, a new item is inserted.
329 */
330static noinline int overwrite_item(struct btrfs_trans_handle *trans,
331 struct btrfs_root *root,
332 struct btrfs_path *path,
333 struct extent_buffer *eb, int slot,
334 struct btrfs_key *key)
335{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400336 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400337 int ret;
338 u32 item_size;
339 u64 saved_i_size = 0;
340 int save_old_i_size = 0;
341 unsigned long src_ptr;
342 unsigned long dst_ptr;
343 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000344 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400345
346 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
347 overwrite_root = 1;
348
349 item_size = btrfs_item_size_nr(eb, slot);
350 src_ptr = btrfs_item_ptr_offset(eb, slot);
351
352 /* look for the key in the destination tree */
353 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000354 if (ret < 0)
355 return ret;
356
Chris Masone02119d2008-09-05 16:13:11 -0400357 if (ret == 0) {
358 char *src_copy;
359 char *dst_copy;
360 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
361 path->slots[0]);
362 if (dst_size != item_size)
363 goto insert;
364
365 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200366 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400367 return 0;
368 }
369 dst_copy = kmalloc(item_size, GFP_NOFS);
370 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000371 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200372 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000373 kfree(dst_copy);
374 kfree(src_copy);
375 return -ENOMEM;
376 }
Chris Masone02119d2008-09-05 16:13:11 -0400377
378 read_extent_buffer(eb, src_copy, src_ptr, item_size);
379
380 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
381 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
382 item_size);
383 ret = memcmp(dst_copy, src_copy, item_size);
384
385 kfree(dst_copy);
386 kfree(src_copy);
387 /*
388 * they have the same contents, just return, this saves
389 * us from cowing blocks in the destination tree and doing
390 * extra writes that may not have been done by a previous
391 * sync
392 */
393 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200394 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400395 return 0;
396 }
397
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000398 /*
399 * We need to load the old nbytes into the inode so when we
400 * replay the extents we've logged we get the right nbytes.
401 */
402 if (inode_item) {
403 struct btrfs_inode_item *item;
404 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400405 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000406
407 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
408 struct btrfs_inode_item);
409 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
410 item = btrfs_item_ptr(eb, slot,
411 struct btrfs_inode_item);
412 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400413
414 /*
415 * If this is a directory we need to reset the i_size to
416 * 0 so that we can set it up properly when replaying
417 * the rest of the items in this log.
418 */
419 mode = btrfs_inode_mode(eb, item);
420 if (S_ISDIR(mode))
421 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000422 }
423 } else if (inode_item) {
424 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400425 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000426
427 /*
428 * New inode, set nbytes to 0 so that the nbytes comes out
429 * properly when we replay the extents.
430 */
431 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
432 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400433
434 /*
435 * If this is a directory we need to reset the i_size to 0 so
436 * that we can set it up properly when replaying the rest of
437 * the items in this log.
438 */
439 mode = btrfs_inode_mode(eb, item);
440 if (S_ISDIR(mode))
441 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400442 }
443insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200444 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400445 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000446 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400447 ret = btrfs_insert_empty_item(trans, root, path,
448 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000449 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400450
451 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000452 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400453 u32 found_size;
454 found_size = btrfs_item_size_nr(path->nodes[0],
455 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100456 if (found_size > item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400457 btrfs_truncate_item(fs_info, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100458 else if (found_size < item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400459 btrfs_extend_item(fs_info, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100460 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400461 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400462 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400463 }
464 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
465 path->slots[0]);
466
467 /* don't overwrite an existing inode if the generation number
468 * was logged as zero. This is done when the tree logging code
469 * is just logging an inode to make sure it exists after recovery.
470 *
471 * Also, don't overwrite i_size on directories during replay.
472 * log replay inserts and removes directory items based on the
473 * state of the tree found in the subvolume, and i_size is modified
474 * as it goes
475 */
476 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
477 struct btrfs_inode_item *src_item;
478 struct btrfs_inode_item *dst_item;
479
480 src_item = (struct btrfs_inode_item *)src_ptr;
481 dst_item = (struct btrfs_inode_item *)dst_ptr;
482
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000483 if (btrfs_inode_generation(eb, src_item) == 0) {
484 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000485 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000486
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000487 /*
488 * For regular files an ino_size == 0 is used only when
489 * logging that an inode exists, as part of a directory
490 * fsync, and the inode wasn't fsynced before. In this
491 * case don't set the size of the inode in the fs/subvol
492 * tree, otherwise we would be throwing valid data away.
493 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000494 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000495 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
496 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000497 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000498
499 btrfs_init_map_token(&token);
500 btrfs_set_token_inode_size(dst_eb, dst_item,
501 ino_size, &token);
502 }
Chris Masone02119d2008-09-05 16:13:11 -0400503 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000504 }
Chris Masone02119d2008-09-05 16:13:11 -0400505
506 if (overwrite_root &&
507 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
508 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
509 save_old_i_size = 1;
510 saved_i_size = btrfs_inode_size(path->nodes[0],
511 dst_item);
512 }
513 }
514
515 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
516 src_ptr, item_size);
517
518 if (save_old_i_size) {
519 struct btrfs_inode_item *dst_item;
520 dst_item = (struct btrfs_inode_item *)dst_ptr;
521 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
522 }
523
524 /* make sure the generation is filled in */
525 if (key->type == BTRFS_INODE_ITEM_KEY) {
526 struct btrfs_inode_item *dst_item;
527 dst_item = (struct btrfs_inode_item *)dst_ptr;
528 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
529 btrfs_set_inode_generation(path->nodes[0], dst_item,
530 trans->transid);
531 }
532 }
533no_copy:
534 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200535 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400536 return 0;
537}
538
539/*
540 * simple helper to read an inode off the disk from a given root
541 * This can only be called for subvolume roots and not for the log
542 */
543static noinline struct inode *read_one_inode(struct btrfs_root *root,
544 u64 objectid)
545{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400546 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400547 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400548
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400549 key.objectid = objectid;
550 key.type = BTRFS_INODE_ITEM_KEY;
551 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000552 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Al Viro2e19f1f2018-07-29 23:04:45 +0100553 if (IS_ERR(inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400554 inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -0400555 return inode;
556}
557
558/* replays a single extent in 'eb' at 'slot' with 'key' into the
559 * subvolume 'root'. path is released on entry and should be released
560 * on exit.
561 *
562 * extents in the log tree have not been allocated out of the extent
563 * tree yet. So, this completes the allocation, taking a reference
564 * as required if the extent already exists or creating a new extent
565 * if it isn't in the extent allocation tree yet.
566 *
567 * The extent is inserted into the file, dropping any existing extents
568 * from the file that overlap the new one.
569 */
570static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
571 struct btrfs_root *root,
572 struct btrfs_path *path,
573 struct extent_buffer *eb, int slot,
574 struct btrfs_key *key)
575{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400576 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400577 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400578 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400579 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000580 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400581 struct btrfs_file_extent_item *item;
582 struct inode *inode = NULL;
583 unsigned long size;
584 int ret = 0;
585
586 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
587 found_type = btrfs_file_extent_type(eb, item);
588
Yan Zhengd899e052008-10-30 14:25:28 -0400589 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000590 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
591 nbytes = btrfs_file_extent_num_bytes(eb, item);
592 extent_end = start + nbytes;
593
594 /*
595 * We don't add to the inodes nbytes if we are prealloc or a
596 * hole.
597 */
598 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
599 nbytes = 0;
600 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +0800601 size = btrfs_file_extent_ram_bytes(eb, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000602 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400603 extent_end = ALIGN(start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400604 fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400605 } else {
606 ret = 0;
607 goto out;
608 }
609
610 inode = read_one_inode(root, key->objectid);
611 if (!inode) {
612 ret = -EIO;
613 goto out;
614 }
615
616 /*
617 * first check to see if we already have this extent in the
618 * file. This must be done before the btrfs_drop_extents run
619 * so we don't try to drop this extent.
620 */
David Sterbaf85b7372017-01-20 14:54:07 +0100621 ret = btrfs_lookup_file_extent(trans, root, path,
622 btrfs_ino(BTRFS_I(inode)), start, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400623
Yan Zhengd899e052008-10-30 14:25:28 -0400624 if (ret == 0 &&
625 (found_type == BTRFS_FILE_EXTENT_REG ||
626 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400627 struct btrfs_file_extent_item cmp1;
628 struct btrfs_file_extent_item cmp2;
629 struct btrfs_file_extent_item *existing;
630 struct extent_buffer *leaf;
631
632 leaf = path->nodes[0];
633 existing = btrfs_item_ptr(leaf, path->slots[0],
634 struct btrfs_file_extent_item);
635
636 read_extent_buffer(eb, &cmp1, (unsigned long)item,
637 sizeof(cmp1));
638 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
639 sizeof(cmp2));
640
641 /*
642 * we already have a pointer to this exact extent,
643 * we don't have to do anything
644 */
645 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200646 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400647 goto out;
648 }
649 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200650 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400651
652 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400653 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400654 if (ret)
655 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400656
Yan Zheng07d400a2009-01-06 11:42:00 -0500657 if (found_type == BTRFS_FILE_EXTENT_REG ||
658 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400659 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500660 unsigned long dest_offset;
661 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400662
Filipe Manana3168021c2017-02-01 14:58:02 +0000663 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
664 btrfs_fs_incompat(fs_info, NO_HOLES))
665 goto update_inode;
666
Yan Zheng07d400a2009-01-06 11:42:00 -0500667 ret = btrfs_insert_empty_item(trans, root, path, key,
668 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400669 if (ret)
670 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500671 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
672 path->slots[0]);
673 copy_extent_buffer(path->nodes[0], eb, dest_offset,
674 (unsigned long)item, sizeof(*item));
675
676 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
677 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
678 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400679 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500680
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800681 /*
682 * Manually record dirty extent, as here we did a shallow
683 * file extent item copy and skip normal backref update,
684 * but modifying extent tree all by ourselves.
685 * So need to manually record dirty extent for qgroup,
686 * as the owner of the file extent changed from log tree
687 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
688 */
Lu Fengqia95f3aa2018-07-18 16:28:03 +0800689 ret = btrfs_qgroup_trace_extent(trans,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800690 btrfs_file_extent_disk_bytenr(eb, item),
691 btrfs_file_extent_disk_num_bytes(eb, item),
692 GFP_NOFS);
693 if (ret < 0)
694 goto out;
695
Yan Zheng07d400a2009-01-06 11:42:00 -0500696 if (ins.objectid > 0) {
697 u64 csum_start;
698 u64 csum_end;
699 LIST_HEAD(ordered_sums);
700 /*
701 * is this extent already allocated in the extent
702 * allocation tree? If so, just add a reference
703 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400704 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500705 ins.offset);
706 if (ret == 0) {
Josef Bacik84f7d8e2017-09-29 15:43:49 -0400707 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng07d400a2009-01-06 11:42:00 -0500708 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400709 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100710 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400711 if (ret)
712 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500713 } else {
714 /*
715 * insert the extent pointer in the extent
716 * allocation tree
717 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400718 ret = btrfs_alloc_logged_file_extent(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400719 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400720 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400721 if (ret)
722 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500723 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200724 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500725
726 if (btrfs_file_extent_compression(eb, item)) {
727 csum_start = ins.objectid;
728 csum_end = csum_start + ins.offset;
729 } else {
730 csum_start = ins.objectid +
731 btrfs_file_extent_offset(eb, item);
732 csum_end = csum_start +
733 btrfs_file_extent_num_bytes(eb, item);
734 }
735
736 ret = btrfs_lookup_csums_range(root->log_root,
737 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100738 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400739 if (ret)
740 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100741 /*
742 * Now delete all existing cums in the csum root that
743 * cover our range. We do this because we can have an
744 * extent that is completely referenced by one file
745 * extent item and partially referenced by another
746 * file extent item (like after using the clone or
747 * extent_same ioctls). In this case if we end up doing
748 * the replay of the one that partially references the
749 * extent first, and we do not do the csum deletion
750 * below, we can get 2 csum items in the csum tree that
751 * overlap each other. For example, imagine our log has
752 * the two following file extent items:
753 *
754 * key (257 EXTENT_DATA 409600)
755 * extent data disk byte 12845056 nr 102400
756 * extent data offset 20480 nr 20480 ram 102400
757 *
758 * key (257 EXTENT_DATA 819200)
759 * extent data disk byte 12845056 nr 102400
760 * extent data offset 0 nr 102400 ram 102400
761 *
762 * Where the second one fully references the 100K extent
763 * that starts at disk byte 12845056, and the log tree
764 * has a single csum item that covers the entire range
765 * of the extent:
766 *
767 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
768 *
769 * After the first file extent item is replayed, the
770 * csum tree gets the following csum item:
771 *
772 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
773 *
774 * Which covers the 20K sub-range starting at offset 20K
775 * of our extent. Now when we replay the second file
776 * extent item, if we do not delete existing csum items
777 * that cover any of its blocks, we end up getting two
778 * csum items in our csum tree that overlap each other:
779 *
780 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
781 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
782 *
783 * Which is a problem, because after this anyone trying
784 * to lookup up for the checksum of any block of our
785 * extent starting at an offset of 40K or higher, will
786 * end up looking at the second csum item only, which
787 * does not contain the checksum for any block starting
788 * at offset 40K or higher of our extent.
789 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500790 while (!list_empty(&ordered_sums)) {
791 struct btrfs_ordered_sum *sums;
792 sums = list_entry(ordered_sums.next,
793 struct btrfs_ordered_sum,
794 list);
Josef Bacik36508602013-04-25 16:23:32 -0400795 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400796 ret = btrfs_del_csums(trans, fs_info,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400797 sums->bytenr,
798 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100799 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400800 ret = btrfs_csum_file_blocks(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400801 fs_info->csum_root, sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500802 list_del(&sums->list);
803 kfree(sums);
804 }
Josef Bacik36508602013-04-25 16:23:32 -0400805 if (ret)
806 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500807 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200808 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500809 }
810 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
811 /* inline extents are easy, we just overwrite them */
812 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400813 if (ret)
814 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500815 }
816
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000817 inode_add_bytes(inode, nbytes);
Filipe Manana3168021c2017-02-01 14:58:02 +0000818update_inode:
Tsutomu Itohb9959292012-06-25 21:25:22 -0600819 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400820out:
821 if (inode)
822 iput(inode);
823 return ret;
824}
825
826/*
827 * when cleaning up conflicts between the directory names in the
828 * subvolume, directory names in the log and directory names in the
829 * inode back references, we may have to unlink inodes from directories.
830 *
831 * This is a helper function to do the unlink of a specific directory
832 * item
833 */
834static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
835 struct btrfs_root *root,
836 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200837 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400838 struct btrfs_dir_item *di)
839{
840 struct inode *inode;
841 char *name;
842 int name_len;
843 struct extent_buffer *leaf;
844 struct btrfs_key location;
845 int ret;
846
847 leaf = path->nodes[0];
848
849 btrfs_dir_item_key_to_cpu(leaf, di, &location);
850 name_len = btrfs_dir_name_len(leaf, di);
851 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000852 if (!name)
853 return -ENOMEM;
854
Chris Masone02119d2008-09-05 16:13:11 -0400855 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200856 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400857
858 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000859 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400860 ret = -EIO;
861 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000862 }
Chris Masone02119d2008-09-05 16:13:11 -0400863
Yan Zhengec051c02009-01-05 15:43:42 -0500864 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400865 if (ret)
866 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400867
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200868 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
869 name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400870 if (ret)
871 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100872 else
Nikolay Borisove5c304e62018-02-07 17:55:43 +0200873 ret = btrfs_run_delayed_items(trans);
Josef Bacik36508602013-04-25 16:23:32 -0400874out:
875 kfree(name);
876 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400877 return ret;
878}
879
880/*
881 * helper function to see if a given name and sequence number found
882 * in an inode back reference are already in a directory and correctly
883 * point to this inode
884 */
885static noinline int inode_in_dir(struct btrfs_root *root,
886 struct btrfs_path *path,
887 u64 dirid, u64 objectid, u64 index,
888 const char *name, int name_len)
889{
890 struct btrfs_dir_item *di;
891 struct btrfs_key location;
892 int match = 0;
893
894 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
895 index, name, name_len, 0);
896 if (di && !IS_ERR(di)) {
897 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
898 if (location.objectid != objectid)
899 goto out;
900 } else
901 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200902 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400903
904 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
905 if (di && !IS_ERR(di)) {
906 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
907 if (location.objectid != objectid)
908 goto out;
909 } else
910 goto out;
911 match = 1;
912out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200913 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400914 return match;
915}
916
917/*
918 * helper function to check a log tree for a named back reference in
919 * an inode. This is used to decide if a back reference that is
920 * found in the subvolume conflicts with what we find in the log.
921 *
922 * inode backreferences may have multiple refs in a single item,
923 * during replay we process one reference at a time, and we don't
924 * want to delete valid links to a file from the subvolume if that
925 * link is also in the log.
926 */
927static noinline int backref_in_log(struct btrfs_root *log,
928 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700929 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000930 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400931{
932 struct btrfs_path *path;
933 struct btrfs_inode_ref *ref;
934 unsigned long ptr;
935 unsigned long ptr_end;
936 unsigned long name_ptr;
937 int found_name_len;
938 int item_size;
939 int ret;
940 int match = 0;
941
942 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000943 if (!path)
944 return -ENOMEM;
945
Chris Masone02119d2008-09-05 16:13:11 -0400946 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
947 if (ret != 0)
948 goto out;
949
Chris Masone02119d2008-09-05 16:13:11 -0400950 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700951
952 if (key->type == BTRFS_INODE_EXTREF_KEY) {
Filipe Manana1f250e92018-02-28 15:56:10 +0000953 if (btrfs_find_name_in_ext_backref(path->nodes[0],
954 path->slots[0],
955 ref_objectid,
Mark Fashehf1863732012-08-08 11:32:27 -0700956 name, namelen, NULL))
957 match = 1;
958
959 goto out;
960 }
961
962 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400963 ptr_end = ptr + item_size;
964 while (ptr < ptr_end) {
965 ref = (struct btrfs_inode_ref *)ptr;
966 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
967 if (found_name_len == namelen) {
968 name_ptr = (unsigned long)(ref + 1);
969 ret = memcmp_extent_buffer(path->nodes[0], name,
970 name_ptr, namelen);
971 if (ret == 0) {
972 match = 1;
973 goto out;
974 }
975 }
976 ptr = (unsigned long)(ref + 1) + found_name_len;
977 }
978out:
979 btrfs_free_path(path);
980 return match;
981}
982
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700983static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
984 struct btrfs_root *root,
985 struct btrfs_path *path,
986 struct btrfs_root *log_root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +0200987 struct btrfs_inode *dir,
988 struct btrfs_inode *inode,
Mark Fashehf1863732012-08-08 11:32:27 -0700989 u64 inode_objectid, u64 parent_objectid,
990 u64 ref_index, char *name, int namelen,
991 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700992{
993 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700994 char *victim_name;
995 int victim_name_len;
996 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700997 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700998 struct btrfs_key search_key;
999 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001000
Mark Fashehf1863732012-08-08 11:32:27 -07001001again:
1002 /* Search old style refs */
1003 search_key.objectid = inode_objectid;
1004 search_key.type = BTRFS_INODE_REF_KEY;
1005 search_key.offset = parent_objectid;
1006 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001007 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001008 struct btrfs_inode_ref *victim_ref;
1009 unsigned long ptr;
1010 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001011
1012 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001013
1014 /* are we trying to overwrite a back ref for the root directory
1015 * if so, just jump out, we're done
1016 */
Mark Fashehf1863732012-08-08 11:32:27 -07001017 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001018 return 1;
1019
1020 /* check all the names in this back reference to see
1021 * if they are in the log. if so, we allow them to stay
1022 * otherwise they must be unlinked as a conflict
1023 */
1024 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1025 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1026 while (ptr < ptr_end) {
1027 victim_ref = (struct btrfs_inode_ref *)ptr;
1028 victim_name_len = btrfs_inode_ref_name_len(leaf,
1029 victim_ref);
1030 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001031 if (!victim_name)
1032 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001033
1034 read_extent_buffer(leaf, victim_name,
1035 (unsigned long)(victim_ref + 1),
1036 victim_name_len);
1037
Mark Fashehf1863732012-08-08 11:32:27 -07001038 if (!backref_in_log(log_root, &search_key,
1039 parent_objectid,
1040 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001041 victim_name_len)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001042 inc_nlink(&inode->vfs_inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001043 btrfs_release_path(path);
1044
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001045 ret = btrfs_unlink_inode(trans, root, dir, inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001046 victim_name, victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001047 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001048 if (ret)
1049 return ret;
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001050 ret = btrfs_run_delayed_items(trans);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001051 if (ret)
1052 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001053 *search_done = 1;
1054 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001055 }
1056 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001057
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001058 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1059 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001060
1061 /*
1062 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001063 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001064 */
1065 *search_done = 1;
1066 }
1067 btrfs_release_path(path);
1068
Mark Fashehf1863732012-08-08 11:32:27 -07001069 /* Same search but for extended refs */
1070 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1071 inode_objectid, parent_objectid, 0,
1072 0);
1073 if (!IS_ERR_OR_NULL(extref)) {
1074 u32 item_size;
1075 u32 cur_offset = 0;
1076 unsigned long base;
1077 struct inode *victim_parent;
1078
1079 leaf = path->nodes[0];
1080
1081 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1082 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1083
1084 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001085 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001086
1087 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1088
1089 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1090 goto next;
1091
1092 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001093 if (!victim_name)
1094 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001095 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1096 victim_name_len);
1097
1098 search_key.objectid = inode_objectid;
1099 search_key.type = BTRFS_INODE_EXTREF_KEY;
1100 search_key.offset = btrfs_extref_hash(parent_objectid,
1101 victim_name,
1102 victim_name_len);
1103 ret = 0;
1104 if (!backref_in_log(log_root, &search_key,
1105 parent_objectid, victim_name,
1106 victim_name_len)) {
1107 ret = -ENOENT;
1108 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001109 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001110 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001111 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001112 btrfs_release_path(path);
1113
1114 ret = btrfs_unlink_inode(trans, root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001115 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001116 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001117 victim_name,
1118 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001119 if (!ret)
1120 ret = btrfs_run_delayed_items(
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001121 trans);
Mark Fashehf1863732012-08-08 11:32:27 -07001122 }
Mark Fashehf1863732012-08-08 11:32:27 -07001123 iput(victim_parent);
1124 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001125 if (ret)
1126 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001127 *search_done = 1;
1128 goto again;
1129 }
1130 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001131next:
1132 cur_offset += victim_name_len + sizeof(*extref);
1133 }
1134 *search_done = 1;
1135 }
1136 btrfs_release_path(path);
1137
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001138 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001139 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001140 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001141 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001142 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001143 if (ret)
1144 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001145 }
1146 btrfs_release_path(path);
1147
Andrea Gelmini52042d82018-11-28 12:05:13 +01001148 /* look for a conflicting name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001149 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001150 name, namelen, 0);
1151 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001152 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001153 if (ret)
1154 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001155 }
1156 btrfs_release_path(path);
1157
1158 return 0;
1159}
Chris Masone02119d2008-09-05 16:13:11 -04001160
Qu Wenruobae15d92017-11-08 08:54:26 +08001161static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1162 u32 *namelen, char **name, u64 *index,
1163 u64 *parent_objectid)
Mark Fashehf1863732012-08-08 11:32:27 -07001164{
1165 struct btrfs_inode_extref *extref;
1166
1167 extref = (struct btrfs_inode_extref *)ref_ptr;
1168
1169 *namelen = btrfs_inode_extref_name_len(eb, extref);
1170 *name = kmalloc(*namelen, GFP_NOFS);
1171 if (*name == NULL)
1172 return -ENOMEM;
1173
1174 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1175 *namelen);
1176
Filipe Manana1f250e92018-02-28 15:56:10 +00001177 if (index)
1178 *index = btrfs_inode_extref_index(eb, extref);
Mark Fashehf1863732012-08-08 11:32:27 -07001179 if (parent_objectid)
1180 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1181
1182 return 0;
1183}
1184
Qu Wenruobae15d92017-11-08 08:54:26 +08001185static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1186 u32 *namelen, char **name, u64 *index)
Mark Fashehf1863732012-08-08 11:32:27 -07001187{
1188 struct btrfs_inode_ref *ref;
1189
1190 ref = (struct btrfs_inode_ref *)ref_ptr;
1191
1192 *namelen = btrfs_inode_ref_name_len(eb, ref);
1193 *name = kmalloc(*namelen, GFP_NOFS);
1194 if (*name == NULL)
1195 return -ENOMEM;
1196
1197 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1198
Filipe Manana1f250e92018-02-28 15:56:10 +00001199 if (index)
1200 *index = btrfs_inode_ref_index(eb, ref);
Mark Fashehf1863732012-08-08 11:32:27 -07001201
1202 return 0;
1203}
1204
Chris Masone02119d2008-09-05 16:13:11 -04001205/*
Filipe Manana1f250e92018-02-28 15:56:10 +00001206 * Take an inode reference item from the log tree and iterate all names from the
1207 * inode reference item in the subvolume tree with the same key (if it exists).
1208 * For any name that is not in the inode reference item from the log tree, do a
1209 * proper unlink of that name (that is, remove its entry from the inode
1210 * reference item and both dir index keys).
1211 */
1212static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1213 struct btrfs_root *root,
1214 struct btrfs_path *path,
1215 struct btrfs_inode *inode,
1216 struct extent_buffer *log_eb,
1217 int log_slot,
1218 struct btrfs_key *key)
1219{
1220 int ret;
1221 unsigned long ref_ptr;
1222 unsigned long ref_end;
1223 struct extent_buffer *eb;
1224
1225again:
1226 btrfs_release_path(path);
1227 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1228 if (ret > 0) {
1229 ret = 0;
1230 goto out;
1231 }
1232 if (ret < 0)
1233 goto out;
1234
1235 eb = path->nodes[0];
1236 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1237 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1238 while (ref_ptr < ref_end) {
1239 char *name = NULL;
1240 int namelen;
1241 u64 parent_id;
1242
1243 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1244 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1245 NULL, &parent_id);
1246 } else {
1247 parent_id = key->offset;
1248 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1249 NULL);
1250 }
1251 if (ret)
1252 goto out;
1253
1254 if (key->type == BTRFS_INODE_EXTREF_KEY)
1255 ret = btrfs_find_name_in_ext_backref(log_eb, log_slot,
1256 parent_id, name,
1257 namelen, NULL);
1258 else
1259 ret = btrfs_find_name_in_backref(log_eb, log_slot, name,
1260 namelen, NULL);
1261
1262 if (!ret) {
1263 struct inode *dir;
1264
1265 btrfs_release_path(path);
1266 dir = read_one_inode(root, parent_id);
1267 if (!dir) {
1268 ret = -ENOENT;
1269 kfree(name);
1270 goto out;
1271 }
1272 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1273 inode, name, namelen);
1274 kfree(name);
1275 iput(dir);
1276 if (ret)
1277 goto out;
1278 goto again;
1279 }
1280
1281 kfree(name);
1282 ref_ptr += namelen;
1283 if (key->type == BTRFS_INODE_EXTREF_KEY)
1284 ref_ptr += sizeof(struct btrfs_inode_extref);
1285 else
1286 ref_ptr += sizeof(struct btrfs_inode_ref);
1287 }
1288 ret = 0;
1289 out:
1290 btrfs_release_path(path);
1291 return ret;
1292}
1293
Filipe Manana0d836392018-07-20 10:59:06 +01001294static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
1295 const u8 ref_type, const char *name,
1296 const int namelen)
1297{
1298 struct btrfs_key key;
1299 struct btrfs_path *path;
1300 const u64 parent_id = btrfs_ino(BTRFS_I(dir));
1301 int ret;
1302
1303 path = btrfs_alloc_path();
1304 if (!path)
1305 return -ENOMEM;
1306
1307 key.objectid = btrfs_ino(BTRFS_I(inode));
1308 key.type = ref_type;
1309 if (key.type == BTRFS_INODE_REF_KEY)
1310 key.offset = parent_id;
1311 else
1312 key.offset = btrfs_extref_hash(parent_id, name, namelen);
1313
1314 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
1315 if (ret < 0)
1316 goto out;
1317 if (ret > 0) {
1318 ret = 0;
1319 goto out;
1320 }
1321 if (key.type == BTRFS_INODE_EXTREF_KEY)
1322 ret = btrfs_find_name_in_ext_backref(path->nodes[0],
1323 path->slots[0], parent_id,
1324 name, namelen, NULL);
1325 else
1326 ret = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
1327 name, namelen, NULL);
1328
1329out:
1330 btrfs_free_path(path);
1331 return ret;
1332}
1333
Filipe Manana6b5fc432019-02-13 12:14:03 +00001334static int add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1335 struct inode *dir, struct inode *inode, const char *name,
1336 int namelen, u64 ref_index)
1337{
1338 struct btrfs_dir_item *dir_item;
1339 struct btrfs_key key;
1340 struct btrfs_path *path;
1341 struct inode *other_inode = NULL;
1342 int ret;
1343
1344 path = btrfs_alloc_path();
1345 if (!path)
1346 return -ENOMEM;
1347
1348 dir_item = btrfs_lookup_dir_item(NULL, root, path,
1349 btrfs_ino(BTRFS_I(dir)),
1350 name, namelen, 0);
1351 if (!dir_item) {
1352 btrfs_release_path(path);
1353 goto add_link;
1354 } else if (IS_ERR(dir_item)) {
1355 ret = PTR_ERR(dir_item);
1356 goto out;
1357 }
1358
1359 /*
1360 * Our inode's dentry collides with the dentry of another inode which is
1361 * in the log but not yet processed since it has a higher inode number.
1362 * So delete that other dentry.
1363 */
1364 btrfs_dir_item_key_to_cpu(path->nodes[0], dir_item, &key);
1365 btrfs_release_path(path);
1366 other_inode = read_one_inode(root, key.objectid);
1367 if (!other_inode) {
1368 ret = -ENOENT;
1369 goto out;
1370 }
1371 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), BTRFS_I(other_inode),
1372 name, namelen);
1373 if (ret)
1374 goto out;
1375 /*
1376 * If we dropped the link count to 0, bump it so that later the iput()
1377 * on the inode will not free it. We will fixup the link count later.
1378 */
1379 if (other_inode->i_nlink == 0)
1380 inc_nlink(other_inode);
1381
1382 ret = btrfs_run_delayed_items(trans);
1383 if (ret)
1384 goto out;
1385add_link:
1386 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
1387 name, namelen, 0, ref_index);
1388out:
1389 iput(other_inode);
1390 btrfs_free_path(path);
1391
1392 return ret;
1393}
1394
Filipe Manana1f250e92018-02-28 15:56:10 +00001395/*
Chris Masone02119d2008-09-05 16:13:11 -04001396 * replay one inode back reference item found in the log tree.
1397 * eb, slot and key refer to the buffer and key found in the log tree.
1398 * root is the destination we are replaying into, and path is for temp
1399 * use by this function. (it should be released on return).
1400 */
1401static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1402 struct btrfs_root *root,
1403 struct btrfs_root *log,
1404 struct btrfs_path *path,
1405 struct extent_buffer *eb, int slot,
1406 struct btrfs_key *key)
1407{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001408 struct inode *dir = NULL;
1409 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001410 unsigned long ref_ptr;
1411 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001412 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001413 int namelen;
1414 int ret;
liuboc622ae62011-03-26 08:01:12 -04001415 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001416 int log_ref_ver = 0;
1417 u64 parent_objectid;
1418 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001419 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001420 int ref_struct_size;
1421
1422 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1423 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1424
1425 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1426 struct btrfs_inode_extref *r;
1427
1428 ref_struct_size = sizeof(struct btrfs_inode_extref);
1429 log_ref_ver = 1;
1430 r = (struct btrfs_inode_extref *)ref_ptr;
1431 parent_objectid = btrfs_inode_extref_parent(eb, r);
1432 } else {
1433 ref_struct_size = sizeof(struct btrfs_inode_ref);
1434 parent_objectid = key->offset;
1435 }
1436 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001437
Chris Masone02119d2008-09-05 16:13:11 -04001438 /*
1439 * it is possible that we didn't log all the parent directories
1440 * for a given inode. If we don't find the dir, just don't
1441 * copy the back ref in. The link count fixup code will take
1442 * care of the rest
1443 */
Mark Fashehf1863732012-08-08 11:32:27 -07001444 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001445 if (!dir) {
1446 ret = -ENOENT;
1447 goto out;
1448 }
Chris Masone02119d2008-09-05 16:13:11 -04001449
Mark Fashehf1863732012-08-08 11:32:27 -07001450 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001451 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001452 ret = -EIO;
1453 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001454 }
Chris Masone02119d2008-09-05 16:13:11 -04001455
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001456 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001457 if (log_ref_ver) {
Qu Wenruobae15d92017-11-08 08:54:26 +08001458 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1459 &ref_index, &parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001460 /*
1461 * parent object can change from one array
1462 * item to another.
1463 */
1464 if (!dir)
1465 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001466 if (!dir) {
1467 ret = -ENOENT;
1468 goto out;
1469 }
Mark Fashehf1863732012-08-08 11:32:27 -07001470 } else {
Qu Wenruobae15d92017-11-08 08:54:26 +08001471 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1472 &ref_index);
Mark Fashehf1863732012-08-08 11:32:27 -07001473 }
1474 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001475 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001476
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001477 /* if we already have a perfect match, we're done */
David Sterbaf85b7372017-01-20 14:54:07 +01001478 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1479 btrfs_ino(BTRFS_I(inode)), ref_index,
1480 name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001481 /*
1482 * look for a conflicting back reference in the
1483 * metadata. if we find one we have to unlink that name
1484 * of the file before we add our new link. Later on, we
1485 * overwrite any existing back reference, and we don't
1486 * want to create dangling pointers in the directory.
1487 */
Chris Masone02119d2008-09-05 16:13:11 -04001488
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001489 if (!search_done) {
1490 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001491 BTRFS_I(dir),
David Sterbad75eefd2017-02-10 20:20:19 +01001492 BTRFS_I(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001493 inode_objectid,
1494 parent_objectid,
1495 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001496 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001497 if (ret) {
1498 if (ret == 1)
1499 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001500 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001501 }
Chris Masone02119d2008-09-05 16:13:11 -04001502 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001503
Filipe Manana0d836392018-07-20 10:59:06 +01001504 /*
1505 * If a reference item already exists for this inode
1506 * with the same parent and name, but different index,
1507 * drop it and the corresponding directory index entries
1508 * from the parent before adding the new reference item
1509 * and dir index entries, otherwise we would fail with
1510 * -EEXIST returned from btrfs_add_link() below.
1511 */
1512 ret = btrfs_inode_ref_exists(inode, dir, key->type,
1513 name, namelen);
1514 if (ret > 0) {
1515 ret = btrfs_unlink_inode(trans, root,
1516 BTRFS_I(dir),
1517 BTRFS_I(inode),
1518 name, namelen);
1519 /*
1520 * If we dropped the link count to 0, bump it so
1521 * that later the iput() on the inode will not
1522 * free it. We will fixup the link count later.
1523 */
1524 if (!ret && inode->i_nlink == 0)
1525 inc_nlink(inode);
1526 }
1527 if (ret < 0)
1528 goto out;
1529
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001530 /* insert our name */
Filipe Manana6b5fc432019-02-13 12:14:03 +00001531 ret = add_link(trans, root, dir, inode, name, namelen,
1532 ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001533 if (ret)
1534 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001535
1536 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001537 }
liuboc622ae62011-03-26 08:01:12 -04001538
Mark Fashehf1863732012-08-08 11:32:27 -07001539 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001540 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001541 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001542 if (log_ref_ver) {
1543 iput(dir);
1544 dir = NULL;
1545 }
Chris Masone02119d2008-09-05 16:13:11 -04001546 }
Chris Masone02119d2008-09-05 16:13:11 -04001547
Filipe Manana1f250e92018-02-28 15:56:10 +00001548 /*
1549 * Before we overwrite the inode reference item in the subvolume tree
1550 * with the item from the log tree, we must unlink all names from the
1551 * parent directory that are in the subvolume's tree inode reference
1552 * item, otherwise we end up with an inconsistent subvolume tree where
1553 * dir index entries exist for a name but there is no inode reference
1554 * item with the same name.
1555 */
1556 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1557 key);
1558 if (ret)
1559 goto out;
1560
Chris Masone02119d2008-09-05 16:13:11 -04001561 /* finally write the back reference in the inode */
1562 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001563out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001564 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001565 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001566 iput(dir);
1567 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001568 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001569}
1570
Yan, Zhengc71bf092009-11-12 09:34:40 +00001571static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001572 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001573{
1574 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001575
David Sterba9c4f61f2015-01-02 19:12:57 +01001576 ret = btrfs_insert_orphan_item(trans, root, ino);
1577 if (ret == -EEXIST)
1578 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001579
Yan, Zhengc71bf092009-11-12 09:34:40 +00001580 return ret;
1581}
1582
Mark Fashehf1863732012-08-08 11:32:27 -07001583static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001584 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001585{
Mark Fashehf1863732012-08-08 11:32:27 -07001586 int ret = 0;
1587 int name_len;
1588 unsigned int nlink = 0;
1589 u32 item_size;
1590 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001591 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001592 u64 offset = 0;
1593 unsigned long ptr;
1594 struct btrfs_inode_extref *extref;
1595 struct extent_buffer *leaf;
1596
1597 while (1) {
1598 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1599 &extref, &offset);
1600 if (ret)
1601 break;
1602
1603 leaf = path->nodes[0];
1604 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1605 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001606 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001607
1608 while (cur_offset < item_size) {
1609 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1610 name_len = btrfs_inode_extref_name_len(leaf, extref);
1611
1612 nlink++;
1613
1614 cur_offset += name_len + sizeof(*extref);
1615 }
1616
1617 offset++;
1618 btrfs_release_path(path);
1619 }
1620 btrfs_release_path(path);
1621
Filipe Manana2c2c4522015-01-13 16:40:04 +00001622 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001623 return ret;
1624 return nlink;
1625}
1626
1627static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001628 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001629{
Chris Masone02119d2008-09-05 16:13:11 -04001630 int ret;
1631 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001632 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001633 unsigned long ptr;
1634 unsigned long ptr_end;
1635 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001636 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001637
Li Zefan33345d012011-04-20 10:31:50 +08001638 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001639 key.type = BTRFS_INODE_REF_KEY;
1640 key.offset = (u64)-1;
1641
Chris Masond3977122009-01-05 21:25:51 -05001642 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001643 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1644 if (ret < 0)
1645 break;
1646 if (ret > 0) {
1647 if (path->slots[0] == 0)
1648 break;
1649 path->slots[0]--;
1650 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001651process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001652 btrfs_item_key_to_cpu(path->nodes[0], &key,
1653 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001654 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001655 key.type != BTRFS_INODE_REF_KEY)
1656 break;
1657 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1658 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1659 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001660 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001661 struct btrfs_inode_ref *ref;
1662
1663 ref = (struct btrfs_inode_ref *)ptr;
1664 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1665 ref);
1666 ptr = (unsigned long)(ref + 1) + name_len;
1667 nlink++;
1668 }
1669
1670 if (key.offset == 0)
1671 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001672 if (path->slots[0] > 0) {
1673 path->slots[0]--;
1674 goto process_slot;
1675 }
Chris Masone02119d2008-09-05 16:13:11 -04001676 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001677 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001678 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001679 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001680
1681 return nlink;
1682}
1683
1684/*
1685 * There are a few corners where the link count of the file can't
1686 * be properly maintained during replay. So, instead of adding
1687 * lots of complexity to the log code, we just scan the backrefs
1688 * for any file that has been through replay.
1689 *
1690 * The scan will update the link count on the inode to reflect the
1691 * number of back refs found. If it goes down to zero, the iput
1692 * will free the inode.
1693 */
1694static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1695 struct btrfs_root *root,
1696 struct inode *inode)
1697{
1698 struct btrfs_path *path;
1699 int ret;
1700 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001701 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001702
1703 path = btrfs_alloc_path();
1704 if (!path)
1705 return -ENOMEM;
1706
Nikolay Borisovf329e312017-01-18 00:31:50 +02001707 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001708 if (ret < 0)
1709 goto out;
1710
1711 nlink = ret;
1712
Nikolay Borisov36283652017-01-18 00:31:49 +02001713 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001714 if (ret < 0)
1715 goto out;
1716
1717 nlink += ret;
1718
1719 ret = 0;
1720
Chris Masone02119d2008-09-05 16:13:11 -04001721 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001722 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001723 btrfs_update_inode(trans, root, inode);
1724 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001725 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001726
Yan, Zhengc71bf092009-11-12 09:34:40 +00001727 if (inode->i_nlink == 0) {
1728 if (S_ISDIR(inode->i_mode)) {
1729 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001730 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001731 if (ret)
1732 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001733 }
Li Zefan33345d012011-04-20 10:31:50 +08001734 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001735 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001736
Mark Fashehf1863732012-08-08 11:32:27 -07001737out:
1738 btrfs_free_path(path);
1739 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001740}
1741
1742static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1743 struct btrfs_root *root,
1744 struct btrfs_path *path)
1745{
1746 int ret;
1747 struct btrfs_key key;
1748 struct inode *inode;
1749
1750 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1751 key.type = BTRFS_ORPHAN_ITEM_KEY;
1752 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001753 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001754 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1755 if (ret < 0)
1756 break;
1757
1758 if (ret == 1) {
1759 if (path->slots[0] == 0)
1760 break;
1761 path->slots[0]--;
1762 }
1763
1764 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1765 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1766 key.type != BTRFS_ORPHAN_ITEM_KEY)
1767 break;
1768
1769 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001770 if (ret)
1771 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001772
David Sterbab3b4aa72011-04-21 01:20:15 +02001773 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001774 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001775 if (!inode)
1776 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001777
1778 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001779 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001780 if (ret)
1781 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001782
Chris Mason12fcfd22009-03-24 10:24:20 -04001783 /*
1784 * fixup on a directory may create new entries,
1785 * make sure we always look for the highset possible
1786 * offset
1787 */
1788 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001789 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001790 ret = 0;
1791out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001792 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001793 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001794}
1795
1796
1797/*
1798 * record a given inode in the fixup dir so we can check its link
1799 * count when replay is done. The link count is incremented here
1800 * so the inode won't go away until we check it
1801 */
1802static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1803 struct btrfs_root *root,
1804 struct btrfs_path *path,
1805 u64 objectid)
1806{
1807 struct btrfs_key key;
1808 int ret = 0;
1809 struct inode *inode;
1810
1811 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001812 if (!inode)
1813 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001814
1815 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001816 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001817 key.offset = objectid;
1818
1819 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1820
David Sterbab3b4aa72011-04-21 01:20:15 +02001821 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001822 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001823 if (!inode->i_nlink)
1824 set_nlink(inode, 1);
1825 else
Zach Brown8b558c52013-10-16 12:10:34 -07001826 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001827 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001828 } else if (ret == -EEXIST) {
1829 ret = 0;
1830 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001831 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001832 }
1833 iput(inode);
1834
1835 return ret;
1836}
1837
1838/*
1839 * when replaying the log for a directory, we only insert names
1840 * for inodes that actually exist. This means an fsync on a directory
1841 * does not implicitly fsync all the new files in it
1842 */
1843static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1844 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001845 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001846 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001847 struct btrfs_key *location)
1848{
1849 struct inode *inode;
1850 struct inode *dir;
1851 int ret;
1852
1853 inode = read_one_inode(root, location->objectid);
1854 if (!inode)
1855 return -ENOENT;
1856
1857 dir = read_one_inode(root, dirid);
1858 if (!dir) {
1859 iput(inode);
1860 return -EIO;
1861 }
Josef Bacikd5554382013-09-11 14:17:00 -04001862
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001863 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1864 name_len, 1, index);
Chris Masone02119d2008-09-05 16:13:11 -04001865
1866 /* FIXME, put inode into FIXUP list */
1867
1868 iput(inode);
1869 iput(dir);
1870 return ret;
1871}
1872
1873/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001874 * Return true if an inode reference exists in the log for the given name,
1875 * inode and parent inode.
1876 */
1877static bool name_in_log_ref(struct btrfs_root *log_root,
1878 const char *name, const int name_len,
1879 const u64 dirid, const u64 ino)
1880{
1881 struct btrfs_key search_key;
1882
1883 search_key.objectid = ino;
1884 search_key.type = BTRFS_INODE_REF_KEY;
1885 search_key.offset = dirid;
1886 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1887 return true;
1888
1889 search_key.type = BTRFS_INODE_EXTREF_KEY;
1890 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1891 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1892 return true;
1893
1894 return false;
1895}
1896
1897/*
Chris Masone02119d2008-09-05 16:13:11 -04001898 * take a single entry in a log directory item and replay it into
1899 * the subvolume.
1900 *
1901 * if a conflicting item exists in the subdirectory already,
1902 * the inode it points to is unlinked and put into the link count
1903 * fix up tree.
1904 *
1905 * If a name from the log points to a file or directory that does
1906 * not exist in the FS, it is skipped. fsyncs on directories
1907 * do not force down inodes inside that directory, just changes to the
1908 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001909 *
1910 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1911 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001912 */
1913static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1914 struct btrfs_root *root,
1915 struct btrfs_path *path,
1916 struct extent_buffer *eb,
1917 struct btrfs_dir_item *di,
1918 struct btrfs_key *key)
1919{
1920 char *name;
1921 int name_len;
1922 struct btrfs_dir_item *dst_di;
1923 struct btrfs_key found_key;
1924 struct btrfs_key log_key;
1925 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001926 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001927 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001928 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001929 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001930 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001931
1932 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001933 if (!dir)
1934 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001935
1936 name_len = btrfs_dir_name_len(eb, di);
1937 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001938 if (!name) {
1939 ret = -ENOMEM;
1940 goto out;
1941 }
liubo2a29edc2011-01-26 06:22:08 +00001942
Chris Masone02119d2008-09-05 16:13:11 -04001943 log_type = btrfs_dir_type(eb, di);
1944 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1945 name_len);
1946
1947 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001948 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1949 if (exists == 0)
1950 exists = 1;
1951 else
1952 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001953 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001954
Chris Masone02119d2008-09-05 16:13:11 -04001955 if (key->type == BTRFS_DIR_ITEM_KEY) {
1956 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1957 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001958 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001959 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1960 key->objectid,
1961 key->offset, name,
1962 name_len, 1);
1963 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001964 /* Corruption */
1965 ret = -EINVAL;
1966 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001967 }
David Sterbac7040052011-04-19 18:00:01 +02001968 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001969 /* we need a sequence number to insert, so we only
1970 * do inserts for the BTRFS_DIR_INDEX_KEY types
1971 */
1972 if (key->type != BTRFS_DIR_INDEX_KEY)
1973 goto out;
1974 goto insert;
1975 }
1976
1977 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1978 /* the existing item matches the logged item */
1979 if (found_key.objectid == log_key.objectid &&
1980 found_key.type == log_key.type &&
1981 found_key.offset == log_key.offset &&
1982 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001983 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001984 goto out;
1985 }
1986
1987 /*
1988 * don't drop the conflicting directory entry if the inode
1989 * for the new entry doesn't exist
1990 */
Chris Mason4bef0842008-09-08 11:18:08 -04001991 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001992 goto out;
1993
Nikolay Borisov207e7d92017-01-18 00:31:45 +02001994 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001995 if (ret)
1996 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001997
1998 if (key->type == BTRFS_DIR_INDEX_KEY)
1999 goto insert;
2000out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002001 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04002002 if (!ret && update_size) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02002003 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
Josef Bacikd5554382013-09-11 14:17:00 -04002004 ret = btrfs_update_inode(trans, root, dir);
2005 }
Chris Masone02119d2008-09-05 16:13:11 -04002006 kfree(name);
2007 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01002008 if (!ret && name_added)
2009 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04002010 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002011
2012insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00002013 if (name_in_log_ref(root->log_root, name, name_len,
2014 key->objectid, log_key.objectid)) {
2015 /* The dentry will be added later. */
2016 ret = 0;
2017 update_size = false;
2018 goto out;
2019 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002020 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08002021 ret = insert_one_name(trans, root, key->objectid, key->offset,
2022 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00002023 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04002024 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002025 if (!ret)
2026 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04002027 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04002028 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002029 goto out;
2030}
2031
2032/*
2033 * find all the names in a directory item and reconcile them into
2034 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
2035 * one name in a directory item, but the same code gets used for
2036 * both directory index types
2037 */
2038static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
2039 struct btrfs_root *root,
2040 struct btrfs_path *path,
2041 struct extent_buffer *eb, int slot,
2042 struct btrfs_key *key)
2043{
Filipe Mananabb53eda2015-07-15 23:26:43 +01002044 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002045 u32 item_size = btrfs_item_size_nr(eb, slot);
2046 struct btrfs_dir_item *di;
2047 int name_len;
2048 unsigned long ptr;
2049 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002050 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04002051
2052 ptr = btrfs_item_ptr_offset(eb, slot);
2053 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002054 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002055 di = (struct btrfs_dir_item *)ptr;
2056 name_len = btrfs_dir_name_len(eb, di);
2057 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01002058 if (ret < 0)
2059 break;
Chris Masone02119d2008-09-05 16:13:11 -04002060 ptr = (unsigned long)(di + 1);
2061 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002062
2063 /*
2064 * If this entry refers to a non-directory (directories can not
2065 * have a link count > 1) and it was added in the transaction
2066 * that was not committed, make sure we fixup the link count of
2067 * the inode it the entry points to. Otherwise something like
2068 * the following would result in a directory pointing to an
2069 * inode with a wrong link that does not account for this dir
2070 * entry:
2071 *
2072 * mkdir testdir
2073 * touch testdir/foo
2074 * touch testdir/bar
2075 * sync
2076 *
2077 * ln testdir/bar testdir/bar_link
2078 * ln testdir/foo testdir/foo_link
2079 * xfs_io -c "fsync" testdir/bar
2080 *
2081 * <power failure>
2082 *
2083 * mount fs, log replay happens
2084 *
2085 * File foo would remain with a link count of 1 when it has two
2086 * entries pointing to it in the directory testdir. This would
2087 * make it impossible to ever delete the parent directory has
2088 * it would result in stale dentries that can never be deleted.
2089 */
2090 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
2091 struct btrfs_key di_key;
2092
2093 if (!fixup_path) {
2094 fixup_path = btrfs_alloc_path();
2095 if (!fixup_path) {
2096 ret = -ENOMEM;
2097 break;
2098 }
2099 }
2100
2101 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2102 ret = link_to_fixup_dir(trans, root, fixup_path,
2103 di_key.objectid);
2104 if (ret)
2105 break;
2106 }
2107 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002108 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01002109 btrfs_free_path(fixup_path);
2110 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002111}
2112
2113/*
2114 * directory replay has two parts. There are the standard directory
2115 * items in the log copied from the subvolume, and range items
2116 * created in the log while the subvolume was logged.
2117 *
2118 * The range items tell us which parts of the key space the log
2119 * is authoritative for. During replay, if a key in the subvolume
2120 * directory is in a logged range item, but not actually in the log
2121 * that means it was deleted from the directory before the fsync
2122 * and should be removed.
2123 */
2124static noinline int find_dir_range(struct btrfs_root *root,
2125 struct btrfs_path *path,
2126 u64 dirid, int key_type,
2127 u64 *start_ret, u64 *end_ret)
2128{
2129 struct btrfs_key key;
2130 u64 found_end;
2131 struct btrfs_dir_log_item *item;
2132 int ret;
2133 int nritems;
2134
2135 if (*start_ret == (u64)-1)
2136 return 1;
2137
2138 key.objectid = dirid;
2139 key.type = key_type;
2140 key.offset = *start_ret;
2141
2142 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2143 if (ret < 0)
2144 goto out;
2145 if (ret > 0) {
2146 if (path->slots[0] == 0)
2147 goto out;
2148 path->slots[0]--;
2149 }
2150 if (ret != 0)
2151 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2152
2153 if (key.type != key_type || key.objectid != dirid) {
2154 ret = 1;
2155 goto next;
2156 }
2157 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2158 struct btrfs_dir_log_item);
2159 found_end = btrfs_dir_log_end(path->nodes[0], item);
2160
2161 if (*start_ret >= key.offset && *start_ret <= found_end) {
2162 ret = 0;
2163 *start_ret = key.offset;
2164 *end_ret = found_end;
2165 goto out;
2166 }
2167 ret = 1;
2168next:
2169 /* check the next slot in the tree to see if it is a valid item */
2170 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08002171 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04002172 if (path->slots[0] >= nritems) {
2173 ret = btrfs_next_leaf(root, path);
2174 if (ret)
2175 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002176 }
2177
2178 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2179
2180 if (key.type != key_type || key.objectid != dirid) {
2181 ret = 1;
2182 goto out;
2183 }
2184 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2185 struct btrfs_dir_log_item);
2186 found_end = btrfs_dir_log_end(path->nodes[0], item);
2187 *start_ret = key.offset;
2188 *end_ret = found_end;
2189 ret = 0;
2190out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002191 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002192 return ret;
2193}
2194
2195/*
2196 * this looks for a given directory item in the log. If the directory
2197 * item is not in the log, the item is removed and the inode it points
2198 * to is unlinked
2199 */
2200static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2201 struct btrfs_root *root,
2202 struct btrfs_root *log,
2203 struct btrfs_path *path,
2204 struct btrfs_path *log_path,
2205 struct inode *dir,
2206 struct btrfs_key *dir_key)
2207{
2208 int ret;
2209 struct extent_buffer *eb;
2210 int slot;
2211 u32 item_size;
2212 struct btrfs_dir_item *di;
2213 struct btrfs_dir_item *log_di;
2214 int name_len;
2215 unsigned long ptr;
2216 unsigned long ptr_end;
2217 char *name;
2218 struct inode *inode;
2219 struct btrfs_key location;
2220
2221again:
2222 eb = path->nodes[0];
2223 slot = path->slots[0];
2224 item_size = btrfs_item_size_nr(eb, slot);
2225 ptr = btrfs_item_ptr_offset(eb, slot);
2226 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002227 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002228 di = (struct btrfs_dir_item *)ptr;
2229 name_len = btrfs_dir_name_len(eb, di);
2230 name = kmalloc(name_len, GFP_NOFS);
2231 if (!name) {
2232 ret = -ENOMEM;
2233 goto out;
2234 }
2235 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2236 name_len);
2237 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002238 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002239 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2240 dir_key->objectid,
2241 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002242 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002243 log_di = btrfs_lookup_dir_index_item(trans, log,
2244 log_path,
2245 dir_key->objectid,
2246 dir_key->offset,
2247 name, name_len, 0);
2248 }
Al Viro8d9e2202018-07-29 23:04:46 +01002249 if (!log_di || log_di == ERR_PTR(-ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002250 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002251 btrfs_release_path(path);
2252 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002253 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002254 if (!inode) {
2255 kfree(name);
2256 return -EIO;
2257 }
Chris Masone02119d2008-09-05 16:13:11 -04002258
2259 ret = link_to_fixup_dir(trans, root,
2260 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002261 if (ret) {
2262 kfree(name);
2263 iput(inode);
2264 goto out;
2265 }
2266
Zach Brown8b558c52013-10-16 12:10:34 -07002267 inc_nlink(inode);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002268 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2269 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002270 if (!ret)
Nikolay Borisove5c304e62018-02-07 17:55:43 +02002271 ret = btrfs_run_delayed_items(trans);
Chris Masone02119d2008-09-05 16:13:11 -04002272 kfree(name);
2273 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002274 if (ret)
2275 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002276
2277 /* there might still be more names under this key
2278 * check and repeat if required
2279 */
2280 ret = btrfs_search_slot(NULL, root, dir_key, path,
2281 0, 0);
2282 if (ret == 0)
2283 goto again;
2284 ret = 0;
2285 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002286 } else if (IS_ERR(log_di)) {
2287 kfree(name);
2288 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002289 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002290 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002291 kfree(name);
2292
2293 ptr = (unsigned long)(di + 1);
2294 ptr += name_len;
2295 }
2296 ret = 0;
2297out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002298 btrfs_release_path(path);
2299 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002300 return ret;
2301}
2302
Filipe Manana4f764e52015-02-23 19:53:35 +00002303static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2304 struct btrfs_root *root,
2305 struct btrfs_root *log,
2306 struct btrfs_path *path,
2307 const u64 ino)
2308{
2309 struct btrfs_key search_key;
2310 struct btrfs_path *log_path;
2311 int i;
2312 int nritems;
2313 int ret;
2314
2315 log_path = btrfs_alloc_path();
2316 if (!log_path)
2317 return -ENOMEM;
2318
2319 search_key.objectid = ino;
2320 search_key.type = BTRFS_XATTR_ITEM_KEY;
2321 search_key.offset = 0;
2322again:
2323 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2324 if (ret < 0)
2325 goto out;
2326process_leaf:
2327 nritems = btrfs_header_nritems(path->nodes[0]);
2328 for (i = path->slots[0]; i < nritems; i++) {
2329 struct btrfs_key key;
2330 struct btrfs_dir_item *di;
2331 struct btrfs_dir_item *log_di;
2332 u32 total_size;
2333 u32 cur;
2334
2335 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2336 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2337 ret = 0;
2338 goto out;
2339 }
2340
2341 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2342 total_size = btrfs_item_size_nr(path->nodes[0], i);
2343 cur = 0;
2344 while (cur < total_size) {
2345 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2346 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2347 u32 this_len = sizeof(*di) + name_len + data_len;
2348 char *name;
2349
2350 name = kmalloc(name_len, GFP_NOFS);
2351 if (!name) {
2352 ret = -ENOMEM;
2353 goto out;
2354 }
2355 read_extent_buffer(path->nodes[0], name,
2356 (unsigned long)(di + 1), name_len);
2357
2358 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2359 name, name_len, 0);
2360 btrfs_release_path(log_path);
2361 if (!log_di) {
2362 /* Doesn't exist in log tree, so delete it. */
2363 btrfs_release_path(path);
2364 di = btrfs_lookup_xattr(trans, root, path, ino,
2365 name, name_len, -1);
2366 kfree(name);
2367 if (IS_ERR(di)) {
2368 ret = PTR_ERR(di);
2369 goto out;
2370 }
2371 ASSERT(di);
2372 ret = btrfs_delete_one_dir_name(trans, root,
2373 path, di);
2374 if (ret)
2375 goto out;
2376 btrfs_release_path(path);
2377 search_key = key;
2378 goto again;
2379 }
2380 kfree(name);
2381 if (IS_ERR(log_di)) {
2382 ret = PTR_ERR(log_di);
2383 goto out;
2384 }
2385 cur += this_len;
2386 di = (struct btrfs_dir_item *)((char *)di + this_len);
2387 }
2388 }
2389 ret = btrfs_next_leaf(root, path);
2390 if (ret > 0)
2391 ret = 0;
2392 else if (ret == 0)
2393 goto process_leaf;
2394out:
2395 btrfs_free_path(log_path);
2396 btrfs_release_path(path);
2397 return ret;
2398}
2399
2400
Chris Masone02119d2008-09-05 16:13:11 -04002401/*
2402 * deletion replay happens before we copy any new directory items
2403 * out of the log or out of backreferences from inodes. It
2404 * scans the log to find ranges of keys that log is authoritative for,
2405 * and then scans the directory to find items in those ranges that are
2406 * not present in the log.
2407 *
2408 * Anything we don't find in the log is unlinked and removed from the
2409 * directory.
2410 */
2411static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2412 struct btrfs_root *root,
2413 struct btrfs_root *log,
2414 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002415 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002416{
2417 u64 range_start;
2418 u64 range_end;
2419 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2420 int ret = 0;
2421 struct btrfs_key dir_key;
2422 struct btrfs_key found_key;
2423 struct btrfs_path *log_path;
2424 struct inode *dir;
2425
2426 dir_key.objectid = dirid;
2427 dir_key.type = BTRFS_DIR_ITEM_KEY;
2428 log_path = btrfs_alloc_path();
2429 if (!log_path)
2430 return -ENOMEM;
2431
2432 dir = read_one_inode(root, dirid);
2433 /* it isn't an error if the inode isn't there, that can happen
2434 * because we replay the deletes before we copy in the inode item
2435 * from the log
2436 */
2437 if (!dir) {
2438 btrfs_free_path(log_path);
2439 return 0;
2440 }
2441again:
2442 range_start = 0;
2443 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002444 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002445 if (del_all)
2446 range_end = (u64)-1;
2447 else {
2448 ret = find_dir_range(log, path, dirid, key_type,
2449 &range_start, &range_end);
2450 if (ret != 0)
2451 break;
2452 }
Chris Masone02119d2008-09-05 16:13:11 -04002453
2454 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002455 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002456 int nritems;
2457 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2458 0, 0);
2459 if (ret < 0)
2460 goto out;
2461
2462 nritems = btrfs_header_nritems(path->nodes[0]);
2463 if (path->slots[0] >= nritems) {
2464 ret = btrfs_next_leaf(root, path);
Liu Bob98def72018-04-03 01:59:48 +08002465 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002466 break;
Liu Bob98def72018-04-03 01:59:48 +08002467 else if (ret < 0)
2468 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002469 }
2470 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2471 path->slots[0]);
2472 if (found_key.objectid != dirid ||
2473 found_key.type != dir_key.type)
2474 goto next_type;
2475
2476 if (found_key.offset > range_end)
2477 break;
2478
2479 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002480 log_path, dir,
2481 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002482 if (ret)
2483 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002484 if (found_key.offset == (u64)-1)
2485 break;
2486 dir_key.offset = found_key.offset + 1;
2487 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002488 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002489 if (range_end == (u64)-1)
2490 break;
2491 range_start = range_end + 1;
2492 }
2493
2494next_type:
2495 ret = 0;
2496 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2497 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2498 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002499 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002500 goto again;
2501 }
2502out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002503 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002504 btrfs_free_path(log_path);
2505 iput(dir);
2506 return ret;
2507}
2508
2509/*
2510 * the process_func used to replay items from the log tree. This
2511 * gets called in two different stages. The first stage just looks
2512 * for inodes and makes sure they are all copied into the subvolume.
2513 *
2514 * The second stage copies all the other item types from the log into
2515 * the subvolume. The two stage approach is slower, but gets rid of
2516 * lots of complexity around inodes referencing other inodes that exist
2517 * only in the log (references come from either directory items or inode
2518 * back refs).
2519 */
2520static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +08002521 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -04002522{
2523 int nritems;
2524 struct btrfs_path *path;
2525 struct btrfs_root *root = wc->replay_dest;
2526 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002527 int i;
2528 int ret;
2529
Qu Wenruo581c1762018-03-29 09:08:11 +08002530 ret = btrfs_read_buffer(eb, gen, level, NULL);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002531 if (ret)
2532 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002533
2534 level = btrfs_header_level(eb);
2535
2536 if (level != 0)
2537 return 0;
2538
2539 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002540 if (!path)
2541 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002542
2543 nritems = btrfs_header_nritems(eb);
2544 for (i = 0; i < nritems; i++) {
2545 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002546
2547 /* inode keys are done during the first stage */
2548 if (key.type == BTRFS_INODE_ITEM_KEY &&
2549 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002550 struct btrfs_inode_item *inode_item;
2551 u32 mode;
2552
2553 inode_item = btrfs_item_ptr(eb, i,
2554 struct btrfs_inode_item);
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002555 /*
2556 * If we have a tmpfile (O_TMPFILE) that got fsync'ed
2557 * and never got linked before the fsync, skip it, as
2558 * replaying it is pointless since it would be deleted
2559 * later. We skip logging tmpfiles, but it's always
2560 * possible we are replaying a log created with a kernel
2561 * that used to log tmpfiles.
2562 */
2563 if (btrfs_inode_nlink(eb, inode_item) == 0) {
2564 wc->ignore_cur_inode = true;
2565 continue;
2566 } else {
2567 wc->ignore_cur_inode = false;
2568 }
Filipe Manana4f764e52015-02-23 19:53:35 +00002569 ret = replay_xattr_deletes(wc->trans, root, log,
2570 path, key.objectid);
2571 if (ret)
2572 break;
Chris Masone02119d2008-09-05 16:13:11 -04002573 mode = btrfs_inode_mode(eb, inode_item);
2574 if (S_ISDIR(mode)) {
2575 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002576 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002577 if (ret)
2578 break;
Chris Masone02119d2008-09-05 16:13:11 -04002579 }
2580 ret = overwrite_item(wc->trans, root, path,
2581 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002582 if (ret)
2583 break;
Chris Masone02119d2008-09-05 16:13:11 -04002584
Filipe Manana471d5572018-04-05 22:55:12 +01002585 /*
2586 * Before replaying extents, truncate the inode to its
2587 * size. We need to do it now and not after log replay
2588 * because before an fsync we can have prealloc extents
2589 * added beyond the inode's i_size. If we did it after,
2590 * through orphan cleanup for example, we would drop
2591 * those prealloc extents just after replaying them.
Chris Masone02119d2008-09-05 16:13:11 -04002592 */
2593 if (S_ISREG(mode)) {
Filipe Manana471d5572018-04-05 22:55:12 +01002594 struct inode *inode;
2595 u64 from;
2596
2597 inode = read_one_inode(root, key.objectid);
2598 if (!inode) {
2599 ret = -EIO;
2600 break;
2601 }
2602 from = ALIGN(i_size_read(inode),
2603 root->fs_info->sectorsize);
2604 ret = btrfs_drop_extents(wc->trans, root, inode,
2605 from, (u64)-1, 1);
Filipe Manana471d5572018-04-05 22:55:12 +01002606 if (!ret) {
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002607 /* Update the inode's nbytes. */
Filipe Manana471d5572018-04-05 22:55:12 +01002608 ret = btrfs_update_inode(wc->trans,
2609 root, inode);
2610 }
2611 iput(inode);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002612 if (ret)
2613 break;
Chris Masone02119d2008-09-05 16:13:11 -04002614 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002615
Chris Masone02119d2008-09-05 16:13:11 -04002616 ret = link_to_fixup_dir(wc->trans, root,
2617 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002618 if (ret)
2619 break;
Chris Masone02119d2008-09-05 16:13:11 -04002620 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002621
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002622 if (wc->ignore_cur_inode)
2623 continue;
2624
Josef Bacikdd8e7212013-09-11 11:57:23 -04002625 if (key.type == BTRFS_DIR_INDEX_KEY &&
2626 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2627 ret = replay_one_dir_item(wc->trans, root, path,
2628 eb, i, &key);
2629 if (ret)
2630 break;
2631 }
2632
Chris Masone02119d2008-09-05 16:13:11 -04002633 if (wc->stage < LOG_WALK_REPLAY_ALL)
2634 continue;
2635
2636 /* these keys are simply copied */
2637 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2638 ret = overwrite_item(wc->trans, root, path,
2639 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002640 if (ret)
2641 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002642 } else if (key.type == BTRFS_INODE_REF_KEY ||
2643 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002644 ret = add_inode_ref(wc->trans, root, log, path,
2645 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002646 if (ret && ret != -ENOENT)
2647 break;
2648 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002649 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2650 ret = replay_one_extent(wc->trans, root, path,
2651 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002652 if (ret)
2653 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002654 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002655 ret = replay_one_dir_item(wc->trans, root, path,
2656 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002657 if (ret)
2658 break;
Chris Masone02119d2008-09-05 16:13:11 -04002659 }
2660 }
2661 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002662 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002663}
2664
Chris Masond3977122009-01-05 21:25:51 -05002665static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002666 struct btrfs_root *root,
2667 struct btrfs_path *path, int *level,
2668 struct walk_control *wc)
2669{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002670 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002671 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002672 u64 bytenr;
2673 u64 ptr_gen;
2674 struct extent_buffer *next;
2675 struct extent_buffer *cur;
2676 struct extent_buffer *parent;
2677 u32 blocksize;
2678 int ret = 0;
2679
2680 WARN_ON(*level < 0);
2681 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2682
Chris Masond3977122009-01-05 21:25:51 -05002683 while (*level > 0) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002684 struct btrfs_key first_key;
2685
Chris Masone02119d2008-09-05 16:13:11 -04002686 WARN_ON(*level < 0);
2687 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2688 cur = path->nodes[*level];
2689
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302690 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002691
2692 if (path->slots[*level] >=
2693 btrfs_header_nritems(cur))
2694 break;
2695
2696 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2697 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08002698 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002699 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002700
2701 parent = path->nodes[*level];
2702 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002703
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002704 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002705 if (IS_ERR(next))
2706 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002707
Chris Masone02119d2008-09-05 16:13:11 -04002708 if (*level == 1) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002709 ret = wc->process_func(root, next, wc, ptr_gen,
2710 *level - 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002711 if (ret) {
2712 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002713 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002714 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002715
Chris Masone02119d2008-09-05 16:13:11 -04002716 path->slots[*level]++;
2717 if (wc->free) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002718 ret = btrfs_read_buffer(next, ptr_gen,
2719 *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002720 if (ret) {
2721 free_extent_buffer(next);
2722 return ret;
2723 }
Chris Masone02119d2008-09-05 16:13:11 -04002724
Josef Bacik681ae502013-10-07 15:11:00 -04002725 if (trans) {
2726 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02002727 btrfs_set_lock_blocking_write(next);
David Sterba7c302b42017-02-10 18:47:57 +01002728 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002729 btrfs_wait_tree_block_writeback(next);
2730 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002731 } else {
2732 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2733 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002734 }
Chris Masone02119d2008-09-05 16:13:11 -04002735
Chris Masone02119d2008-09-05 16:13:11 -04002736 WARN_ON(root_owner !=
2737 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002738 ret = btrfs_free_and_pin_reserved_extent(
2739 fs_info, bytenr,
2740 blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002741 if (ret) {
2742 free_extent_buffer(next);
2743 return ret;
2744 }
Chris Masone02119d2008-09-05 16:13:11 -04002745 }
2746 free_extent_buffer(next);
2747 continue;
2748 }
Qu Wenruo581c1762018-03-29 09:08:11 +08002749 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002750 if (ret) {
2751 free_extent_buffer(next);
2752 return ret;
2753 }
Chris Masone02119d2008-09-05 16:13:11 -04002754
2755 WARN_ON(*level <= 0);
2756 if (path->nodes[*level-1])
2757 free_extent_buffer(path->nodes[*level-1]);
2758 path->nodes[*level-1] = next;
2759 *level = btrfs_header_level(next);
2760 path->slots[*level] = 0;
2761 cond_resched();
2762 }
2763 WARN_ON(*level < 0);
2764 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2765
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002766 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002767
2768 cond_resched();
2769 return 0;
2770}
2771
Chris Masond3977122009-01-05 21:25:51 -05002772static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002773 struct btrfs_root *root,
2774 struct btrfs_path *path, int *level,
2775 struct walk_control *wc)
2776{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002777 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002778 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002779 int i;
2780 int slot;
2781 int ret;
2782
Chris Masond3977122009-01-05 21:25:51 -05002783 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002784 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002785 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002786 path->slots[i]++;
2787 *level = i;
2788 WARN_ON(*level == 0);
2789 return 0;
2790 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002791 struct extent_buffer *parent;
2792 if (path->nodes[*level] == root->node)
2793 parent = path->nodes[*level];
2794 else
2795 parent = path->nodes[*level + 1];
2796
2797 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002798 ret = wc->process_func(root, path->nodes[*level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002799 btrfs_header_generation(path->nodes[*level]),
2800 *level);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002801 if (ret)
2802 return ret;
2803
Chris Masone02119d2008-09-05 16:13:11 -04002804 if (wc->free) {
2805 struct extent_buffer *next;
2806
2807 next = path->nodes[*level];
2808
Josef Bacik681ae502013-10-07 15:11:00 -04002809 if (trans) {
2810 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02002811 btrfs_set_lock_blocking_write(next);
David Sterba7c302b42017-02-10 18:47:57 +01002812 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002813 btrfs_wait_tree_block_writeback(next);
2814 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002815 } else {
2816 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2817 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002818 }
Chris Masone02119d2008-09-05 16:13:11 -04002819
Chris Masone02119d2008-09-05 16:13:11 -04002820 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002821 ret = btrfs_free_and_pin_reserved_extent(
2822 fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04002823 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002824 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002825 if (ret)
2826 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002827 }
2828 free_extent_buffer(path->nodes[*level]);
2829 path->nodes[*level] = NULL;
2830 *level = i + 1;
2831 }
2832 }
2833 return 1;
2834}
2835
2836/*
2837 * drop the reference count on the tree rooted at 'snap'. This traverses
2838 * the tree freeing any blocks that have a ref count of zero after being
2839 * decremented.
2840 */
2841static int walk_log_tree(struct btrfs_trans_handle *trans,
2842 struct btrfs_root *log, struct walk_control *wc)
2843{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002844 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002845 int ret = 0;
2846 int wret;
2847 int level;
2848 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002849 int orig_level;
2850
2851 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002852 if (!path)
2853 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002854
2855 level = btrfs_header_level(log->node);
2856 orig_level = level;
2857 path->nodes[level] = log->node;
2858 extent_buffer_get(log->node);
2859 path->slots[level] = 0;
2860
Chris Masond3977122009-01-05 21:25:51 -05002861 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002862 wret = walk_down_log_tree(trans, log, path, &level, wc);
2863 if (wret > 0)
2864 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002865 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002866 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002867 goto out;
2868 }
Chris Masone02119d2008-09-05 16:13:11 -04002869
2870 wret = walk_up_log_tree(trans, log, path, &level, wc);
2871 if (wret > 0)
2872 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002873 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002874 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002875 goto out;
2876 }
Chris Masone02119d2008-09-05 16:13:11 -04002877 }
2878
2879 /* was the root node processed? if not, catch it here */
2880 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002881 ret = wc->process_func(log, path->nodes[orig_level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002882 btrfs_header_generation(path->nodes[orig_level]),
2883 orig_level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002884 if (ret)
2885 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002886 if (wc->free) {
2887 struct extent_buffer *next;
2888
2889 next = path->nodes[orig_level];
2890
Josef Bacik681ae502013-10-07 15:11:00 -04002891 if (trans) {
2892 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02002893 btrfs_set_lock_blocking_write(next);
David Sterba7c302b42017-02-10 18:47:57 +01002894 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002895 btrfs_wait_tree_block_writeback(next);
2896 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002897 } else {
2898 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2899 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002900 }
Chris Masone02119d2008-09-05 16:13:11 -04002901
Chris Masone02119d2008-09-05 16:13:11 -04002902 WARN_ON(log->root_key.objectid !=
2903 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002904 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2905 next->start, next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002906 if (ret)
2907 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002908 }
2909 }
2910
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002911out:
Chris Masone02119d2008-09-05 16:13:11 -04002912 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002913 return ret;
2914}
2915
Yan Zheng7237f182009-01-21 12:54:03 -05002916/*
2917 * helper function to update the item for a given subvolumes log root
2918 * in the tree of log roots
2919 */
2920static int update_log_root(struct btrfs_trans_handle *trans,
2921 struct btrfs_root *log)
2922{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002923 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05002924 int ret;
2925
2926 if (log->log_transid == 1) {
2927 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002928 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002929 &log->root_key, &log->root_item);
2930 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002931 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002932 &log->root_key, &log->root_item);
2933 }
2934 return ret;
2935}
2936
Zhaolei60d53eb2015-08-17 18:44:46 +08002937static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002938{
2939 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002940 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002941
Yan Zheng7237f182009-01-21 12:54:03 -05002942 /*
2943 * we only allow two pending log transactions at a time,
2944 * so we know that if ours is more than 2 older than the
2945 * current transaction, we're done
2946 */
Liu Bo49e83f52017-09-01 16:14:30 -06002947 for (;;) {
Yan Zheng7237f182009-01-21 12:54:03 -05002948 prepare_to_wait(&root->log_commit_wait[index],
2949 &wait, TASK_UNINTERRUPTIBLE);
Liu Bo49e83f52017-09-01 16:14:30 -06002950
2951 if (!(root->log_transid_committed < transid &&
2952 atomic_read(&root->log_commit[index])))
2953 break;
2954
Yan Zheng7237f182009-01-21 12:54:03 -05002955 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002956 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002957 mutex_lock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002958 }
2959 finish_wait(&root->log_commit_wait[index], &wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002960}
2961
Zhaolei60d53eb2015-08-17 18:44:46 +08002962static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002963{
2964 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002965
Liu Bo49e83f52017-09-01 16:14:30 -06002966 for (;;) {
2967 prepare_to_wait(&root->log_writer_wait, &wait,
2968 TASK_UNINTERRUPTIBLE);
2969 if (!atomic_read(&root->log_writers))
2970 break;
2971
Yan Zheng7237f182009-01-21 12:54:03 -05002972 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002973 schedule();
Filipe Manana575849e2015-02-11 11:12:39 +00002974 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002975 }
Liu Bo49e83f52017-09-01 16:14:30 -06002976 finish_wait(&root->log_writer_wait, &wait);
Chris Masone02119d2008-09-05 16:13:11 -04002977}
2978
Miao Xie8b050d32014-02-20 18:08:58 +08002979static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2980 struct btrfs_log_ctx *ctx)
2981{
2982 if (!ctx)
2983 return;
2984
2985 mutex_lock(&root->log_mutex);
2986 list_del_init(&ctx->list);
2987 mutex_unlock(&root->log_mutex);
2988}
2989
2990/*
2991 * Invoked in log mutex context, or be sure there is no other task which
2992 * can access the list.
2993 */
2994static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2995 int index, int error)
2996{
2997 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002998 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002999
Chris Mason570dd452016-10-27 10:42:20 -07003000 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
3001 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08003002 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07003003 }
Miao Xie8b050d32014-02-20 18:08:58 +08003004
3005 INIT_LIST_HEAD(&root->log_ctxs[index]);
3006}
3007
Chris Masone02119d2008-09-05 16:13:11 -04003008/*
3009 * btrfs_sync_log does sends a given tree log down to the disk and
3010 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04003011 * you know that any inodes previously logged are safely on disk only
3012 * if it returns 0.
3013 *
3014 * Any other return value means you need to call btrfs_commit_transaction.
3015 * Some of the edge cases for fsyncing directories that have had unlinks
3016 * or renames done in the past mean that sometimes the only safe
3017 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
3018 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04003019 */
3020int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08003021 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003022{
Yan Zheng7237f182009-01-21 12:54:03 -05003023 int index1;
3024 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003025 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04003026 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003027 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003028 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003029 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08003030 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08003031 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00003032 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04003033
Yan Zheng7237f182009-01-21 12:54:03 -05003034 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08003035 log_transid = ctx->log_transid;
3036 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05003037 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003038 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04003039 }
Miao Xied1433de2014-02-20 18:08:59 +08003040
3041 index1 = log_transid % 2;
3042 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003043 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08003044 mutex_unlock(&root->log_mutex);
3045 return ctx->log_ret;
3046 }
3047 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003048 atomic_set(&root->log_commit[index1], 1);
3049
3050 /* wait for previous tree log sync to complete */
3051 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08003052 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08003053
Yan, Zheng86df7eb2009-10-14 09:24:59 -04003054 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06003055 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04003056 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003057 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08003058 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04003059 mutex_unlock(&root->log_mutex);
3060 schedule_timeout_uninterruptible(1);
3061 mutex_lock(&root->log_mutex);
3062 }
Zhaolei60d53eb2015-08-17 18:44:46 +08003063 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06003064 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04003065 break;
3066 }
Chris Masond0c803c2008-09-11 16:17:57 -04003067
Chris Mason12fcfd22009-03-24 10:24:20 -04003068 /* bail out if we need to do a full commit */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003069 if (btrfs_need_log_full_commit(fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003070 ret = -EAGAIN;
3071 mutex_unlock(&root->log_mutex);
3072 goto out;
3073 }
3074
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003075 if (log_transid % 2 == 0)
3076 mark = EXTENT_DIRTY;
3077 else
3078 mark = EXTENT_NEW;
3079
Chris Mason690587d2009-10-13 13:29:19 -04003080 /* we start IO on all the marked extents here, but we don't actually
3081 * wait for them until later.
3082 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00003083 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003084 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003085 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003086 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04003087 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003088 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003089 mutex_unlock(&root->log_mutex);
3090 goto out;
3091 }
Yan Zheng7237f182009-01-21 12:54:03 -05003092
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003093 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05003094
Yan Zheng7237f182009-01-21 12:54:03 -05003095 root->log_transid++;
3096 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04003097 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05003098 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003099 * IO has been started, blocks of the log tree have WRITTEN flag set
3100 * in their headers. new modifications of the log will be written to
3101 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05003102 */
3103 mutex_unlock(&root->log_mutex);
3104
Filipe Manana28a23592016-08-23 21:13:51 +01003105 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08003106
Yan Zheng7237f182009-01-21 12:54:03 -05003107 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06003108 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05003109 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08003110
3111 index2 = log_root_tree->log_transid % 2;
3112 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3113 root_log_ctx.log_transid = log_root_tree->log_transid;
3114
Yan Zheng7237f182009-01-21 12:54:03 -05003115 mutex_unlock(&log_root_tree->log_mutex);
3116
3117 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05003118
3119 mutex_lock(&log_root_tree->log_mutex);
3120 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +01003121 /* atomic_dec_and_test implies a barrier */
3122 cond_wake_up_nomb(&log_root_tree->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -05003123 }
3124
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003125 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08003126 if (!list_empty(&root_log_ctx.list))
3127 list_del_init(&root_log_ctx.list);
3128
Miao Xiec6adc9c2013-05-28 10:05:39 +00003129 blk_finish_plug(&plug);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003130 btrfs_set_log_full_commit(fs_info, trans);
Miao Xie995946d2014-04-02 19:51:06 +08003131
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003132 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003133 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003134 mutex_unlock(&log_root_tree->log_mutex);
3135 goto out;
3136 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003137 btrfs_wait_tree_log_extents(log, mark);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003138 mutex_unlock(&log_root_tree->log_mutex);
3139 ret = -EAGAIN;
3140 goto out;
3141 }
3142
Miao Xied1433de2014-02-20 18:08:59 +08003143 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08003144 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07003145 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08003146 mutex_unlock(&log_root_tree->log_mutex);
3147 ret = root_log_ctx.log_ret;
3148 goto out;
3149 }
Miao Xie8b050d32014-02-20 18:08:58 +08003150
Miao Xied1433de2014-02-20 18:08:59 +08003151 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05003152 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003153 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003154 ret = btrfs_wait_tree_log_extents(log, mark);
Zhaolei60d53eb2015-08-17 18:44:46 +08003155 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003156 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003157 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003158 if (!ret)
3159 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05003160 goto out;
3161 }
Miao Xied1433de2014-02-20 18:08:59 +08003162 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003163 atomic_set(&log_root_tree->log_commit[index2], 1);
3164
Chris Mason12fcfd22009-03-24 10:24:20 -04003165 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003166 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003167 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04003168 }
Yan Zheng7237f182009-01-21 12:54:03 -05003169
Zhaolei60d53eb2015-08-17 18:44:46 +08003170 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04003171
3172 /*
3173 * now that we've moved on to the tree of log tree roots,
3174 * check the full commit flag again
3175 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003176 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003177 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003178 btrfs_wait_tree_log_extents(log, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04003179 mutex_unlock(&log_root_tree->log_mutex);
3180 ret = -EAGAIN;
3181 goto out_wake_log_root;
3182 }
Yan Zheng7237f182009-01-21 12:54:03 -05003183
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003184 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00003185 &log_root_tree->dirty_log_pages,
3186 EXTENT_DIRTY | EXTENT_NEW);
3187 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003188 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003189 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003190 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003191 mutex_unlock(&log_root_tree->log_mutex);
3192 goto out_wake_log_root;
3193 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003194 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003195 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003196 ret = btrfs_wait_tree_log_extents(log_root_tree,
3197 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003198 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003199 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003200 mutex_unlock(&log_root_tree->log_mutex);
3201 goto out_wake_log_root;
3202 }
Chris Masone02119d2008-09-05 16:13:11 -04003203
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003204 btrfs_set_super_log_root(fs_info->super_for_commit,
3205 log_root_tree->node->start);
3206 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3207 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04003208
Yan Zheng7237f182009-01-21 12:54:03 -05003209 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05003210 mutex_unlock(&log_root_tree->log_mutex);
3211
3212 /*
Andrea Gelmini52042d82018-11-28 12:05:13 +01003213 * Nobody else is going to jump in and write the ctree
Yan Zheng7237f182009-01-21 12:54:03 -05003214 * super here because the log_commit atomic below is protecting
3215 * us. We must be called with a transaction handle pinning
3216 * the running transaction open, so a full commit can't hop
3217 * in and cause problems either.
3218 */
David Sterbaeece6a92017-02-10 19:04:32 +01003219 ret = write_all_supers(fs_info, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003220 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003221 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003222 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003223 goto out_wake_log_root;
3224 }
Yan Zheng7237f182009-01-21 12:54:03 -05003225
Chris Mason257c62e2009-10-13 13:21:08 -04003226 mutex_lock(&root->log_mutex);
3227 if (root->last_log_commit < log_transid)
3228 root->last_log_commit = log_transid;
3229 mutex_unlock(&root->log_mutex);
3230
Chris Mason12fcfd22009-03-24 10:24:20 -04003231out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07003232 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003233 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3234
Miao Xied1433de2014-02-20 18:08:59 +08003235 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003236 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003237 mutex_unlock(&log_root_tree->log_mutex);
3238
David Sterba33a9eca2015-10-10 18:35:10 +02003239 /*
David Sterba093258e2018-02-26 16:15:17 +01003240 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3241 * all the updates above are seen by the woken threads. It might not be
3242 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003243 */
David Sterba093258e2018-02-26 16:15:17 +01003244 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04003245out:
Miao Xied1433de2014-02-20 18:08:59 +08003246 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07003247 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08003248 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003249 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003250 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003251
David Sterba33a9eca2015-10-10 18:35:10 +02003252 /*
David Sterba093258e2018-02-26 16:15:17 +01003253 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3254 * all the updates above are seen by the woken threads. It might not be
3255 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003256 */
David Sterba093258e2018-02-26 16:15:17 +01003257 cond_wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003258 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003259}
3260
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003261static void free_log_tree(struct btrfs_trans_handle *trans,
3262 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003263{
3264 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04003265 struct walk_control wc = {
3266 .free = 1,
3267 .process_func = process_one_buffer
3268 };
3269
Josef Bacik681ae502013-10-07 15:11:00 -04003270 ret = walk_log_tree(trans, log, &wc);
Jeff Mahoney374b0e22018-09-06 16:59:33 -04003271 if (ret) {
3272 if (trans)
3273 btrfs_abort_transaction(trans, ret);
3274 else
3275 btrfs_handle_fs_error(log->fs_info, ret, NULL);
3276 }
Chris Masone02119d2008-09-05 16:13:11 -04003277
Filipe Manana59b07132018-11-09 10:43:08 +00003278 clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
3279 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Yan Zheng7237f182009-01-21 12:54:03 -05003280 free_extent_buffer(log->node);
3281 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003282}
3283
3284/*
3285 * free all the extents used by the tree log. This should be called
3286 * at commit time of the full transaction
3287 */
3288int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3289{
3290 if (root->log_root) {
3291 free_log_tree(trans, root->log_root);
3292 root->log_root = NULL;
3293 }
3294 return 0;
3295}
3296
3297int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3298 struct btrfs_fs_info *fs_info)
3299{
3300 if (fs_info->log_root_tree) {
3301 free_log_tree(trans, fs_info->log_root_tree);
3302 fs_info->log_root_tree = NULL;
3303 }
Chris Masone02119d2008-09-05 16:13:11 -04003304 return 0;
3305}
3306
3307/*
Chris Masone02119d2008-09-05 16:13:11 -04003308 * If both a file and directory are logged, and unlinks or renames are
3309 * mixed in, we have a few interesting corners:
3310 *
3311 * create file X in dir Y
3312 * link file X to X.link in dir Y
3313 * fsync file X
3314 * unlink file X but leave X.link
3315 * fsync dir Y
3316 *
3317 * After a crash we would expect only X.link to exist. But file X
3318 * didn't get fsync'd again so the log has back refs for X and X.link.
3319 *
3320 * We solve this by removing directory entries and inode backrefs from the
3321 * log when a file that was logged in the current transaction is
3322 * unlinked. Any later fsync will include the updated log entries, and
3323 * we'll be able to reconstruct the proper directory items from backrefs.
3324 *
3325 * This optimizations allows us to avoid relogging the entire inode
3326 * or the entire directory.
3327 */
3328int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3329 struct btrfs_root *root,
3330 const char *name, int name_len,
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003331 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003332{
3333 struct btrfs_root *log;
3334 struct btrfs_dir_item *di;
3335 struct btrfs_path *path;
3336 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003337 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003338 int bytes_del = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003339 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003340
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003341 if (dir->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003342 return 0;
3343
Chris Masone02119d2008-09-05 16:13:11 -04003344 ret = join_running_log_trans(root);
3345 if (ret)
3346 return 0;
3347
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003348 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003349
3350 log = root->log_root;
3351 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003352 if (!path) {
3353 err = -ENOMEM;
3354 goto out_unlock;
3355 }
liubo2a29edc2011-01-26 06:22:08 +00003356
Li Zefan33345d012011-04-20 10:31:50 +08003357 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003358 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003359 if (IS_ERR(di)) {
3360 err = PTR_ERR(di);
3361 goto fail;
3362 }
3363 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003364 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3365 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003366 if (ret) {
3367 err = ret;
3368 goto fail;
3369 }
Chris Masone02119d2008-09-05 16:13:11 -04003370 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003371 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003372 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003373 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003374 if (IS_ERR(di)) {
3375 err = PTR_ERR(di);
3376 goto fail;
3377 }
3378 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003379 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3380 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003381 if (ret) {
3382 err = ret;
3383 goto fail;
3384 }
Chris Masone02119d2008-09-05 16:13:11 -04003385 }
3386
3387 /* update the directory size in the log to reflect the names
3388 * we have removed
3389 */
3390 if (bytes_del) {
3391 struct btrfs_key key;
3392
Li Zefan33345d012011-04-20 10:31:50 +08003393 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003394 key.offset = 0;
3395 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003396 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003397
3398 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003399 if (ret < 0) {
3400 err = ret;
3401 goto fail;
3402 }
Chris Masone02119d2008-09-05 16:13:11 -04003403 if (ret == 0) {
3404 struct btrfs_inode_item *item;
3405 u64 i_size;
3406
3407 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3408 struct btrfs_inode_item);
3409 i_size = btrfs_inode_size(path->nodes[0], item);
3410 if (i_size > bytes_del)
3411 i_size -= bytes_del;
3412 else
3413 i_size = 0;
3414 btrfs_set_inode_size(path->nodes[0], item, i_size);
3415 btrfs_mark_buffer_dirty(path->nodes[0]);
3416 } else
3417 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003418 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003419 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003420fail:
Chris Masone02119d2008-09-05 16:13:11 -04003421 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003422out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003423 mutex_unlock(&dir->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003424 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003425 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003426 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003427 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003428 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003429
Chris Mason12fcfd22009-03-24 10:24:20 -04003430 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003431
Andi Kleen411fc6b2010-10-29 15:14:31 -04003432 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003433}
3434
3435/* see comments for btrfs_del_dir_entries_in_log */
3436int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3437 struct btrfs_root *root,
3438 const char *name, int name_len,
Nikolay Borisova491abb2017-01-18 00:31:33 +02003439 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003440{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003441 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003442 struct btrfs_root *log;
3443 u64 index;
3444 int ret;
3445
Nikolay Borisova491abb2017-01-18 00:31:33 +02003446 if (inode->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003447 return 0;
3448
Chris Masone02119d2008-09-05 16:13:11 -04003449 ret = join_running_log_trans(root);
3450 if (ret)
3451 return 0;
3452 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003453 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003454
Nikolay Borisova491abb2017-01-18 00:31:33 +02003455 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003456 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003457 mutex_unlock(&inode->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003458 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003459 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003460 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003461 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003462 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003463 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003464
Chris Masone02119d2008-09-05 16:13:11 -04003465 return ret;
3466}
3467
3468/*
3469 * creates a range item in the log for 'dirid'. first_offset and
3470 * last_offset tell us which parts of the key space the log should
3471 * be considered authoritative for.
3472 */
3473static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3474 struct btrfs_root *log,
3475 struct btrfs_path *path,
3476 int key_type, u64 dirid,
3477 u64 first_offset, u64 last_offset)
3478{
3479 int ret;
3480 struct btrfs_key key;
3481 struct btrfs_dir_log_item *item;
3482
3483 key.objectid = dirid;
3484 key.offset = first_offset;
3485 if (key_type == BTRFS_DIR_ITEM_KEY)
3486 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3487 else
3488 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3489 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003490 if (ret)
3491 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003492
3493 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3494 struct btrfs_dir_log_item);
3495 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3496 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003497 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003498 return 0;
3499}
3500
3501/*
3502 * log all the items included in the current transaction for a given
3503 * directory. This also creates the range items in the log tree required
3504 * to replay anything deleted before the fsync
3505 */
3506static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Nikolay Borisov684a5772017-01-18 00:31:41 +02003507 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003508 struct btrfs_path *path,
3509 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003510 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003511 u64 min_offset, u64 *last_offset_ret)
3512{
3513 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003514 struct btrfs_root *log = root->log_root;
3515 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003516 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003517 int ret;
3518 int i;
3519 int nritems;
3520 u64 first_offset = min_offset;
3521 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003522 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003523
3524 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003525
Li Zefan33345d012011-04-20 10:31:50 +08003526 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003527 min_key.type = key_type;
3528 min_key.offset = min_offset;
3529
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003530 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003531
3532 /*
3533 * we didn't find anything from this transaction, see if there
3534 * is anything at all
3535 */
Li Zefan33345d012011-04-20 10:31:50 +08003536 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3537 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003538 min_key.type = key_type;
3539 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003540 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003541 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3542 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003543 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003544 return ret;
3545 }
Li Zefan33345d012011-04-20 10:31:50 +08003546 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003547
3548 /* if ret == 0 there are items for this type,
3549 * create a range to tell us the last key of this type.
3550 * otherwise, there are no items in this directory after
3551 * *min_offset, and we create a range to indicate that.
3552 */
3553 if (ret == 0) {
3554 struct btrfs_key tmp;
3555 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3556 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003557 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003558 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003559 }
3560 goto done;
3561 }
3562
3563 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003564 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003565 if (ret == 0) {
3566 struct btrfs_key tmp;
3567 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3568 if (key_type == tmp.type) {
3569 first_offset = tmp.offset;
3570 ret = overwrite_item(trans, log, dst_path,
3571 path->nodes[0], path->slots[0],
3572 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003573 if (ret) {
3574 err = ret;
3575 goto done;
3576 }
Chris Masone02119d2008-09-05 16:13:11 -04003577 }
3578 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003579 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003580
Josef Bacik2cc83342019-03-06 17:13:04 -05003581 /*
3582 * Find the first key from this transaction again. See the note for
3583 * log_new_dir_dentries, if we're logging a directory recursively we
3584 * won't be holding its i_mutex, which means we can modify the directory
3585 * while we're logging it. If we remove an entry between our first
3586 * search and this search we'll not find the key again and can just
3587 * bail.
3588 */
Chris Masone02119d2008-09-05 16:13:11 -04003589 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Josef Bacik2cc83342019-03-06 17:13:04 -05003590 if (ret != 0)
Chris Masone02119d2008-09-05 16:13:11 -04003591 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003592
3593 /*
3594 * we have a block from this transaction, log every item in it
3595 * from our directory
3596 */
Chris Masond3977122009-01-05 21:25:51 -05003597 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003598 struct btrfs_key tmp;
3599 src = path->nodes[0];
3600 nritems = btrfs_header_nritems(src);
3601 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003602 struct btrfs_dir_item *di;
3603
Chris Masone02119d2008-09-05 16:13:11 -04003604 btrfs_item_key_to_cpu(src, &min_key, i);
3605
Li Zefan33345d012011-04-20 10:31:50 +08003606 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003607 goto done;
3608 ret = overwrite_item(trans, log, dst_path, src, i,
3609 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003610 if (ret) {
3611 err = ret;
3612 goto done;
3613 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003614
3615 /*
3616 * We must make sure that when we log a directory entry,
3617 * the corresponding inode, after log replay, has a
3618 * matching link count. For example:
3619 *
3620 * touch foo
3621 * mkdir mydir
3622 * sync
3623 * ln foo mydir/bar
3624 * xfs_io -c "fsync" mydir
3625 * <crash>
3626 * <mount fs and log replay>
3627 *
3628 * Would result in a fsync log that when replayed, our
3629 * file inode would have a link count of 1, but we get
3630 * two directory entries pointing to the same inode.
3631 * After removing one of the names, it would not be
3632 * possible to remove the other name, which resulted
3633 * always in stale file handle errors, and would not
3634 * be possible to rmdir the parent directory, since
3635 * its i_size could never decrement to the value
3636 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3637 */
3638 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3639 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3640 if (ctx &&
3641 (btrfs_dir_transid(src, di) == trans->transid ||
3642 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3643 tmp.type != BTRFS_ROOT_ITEM_KEY)
3644 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003645 }
3646 path->slots[0] = nritems;
3647
3648 /*
3649 * look ahead to the next item and see if it is also
3650 * from this directory and from this transaction
3651 */
3652 ret = btrfs_next_leaf(root, path);
Liu Bo80c0b422018-04-03 01:59:47 +08003653 if (ret) {
3654 if (ret == 1)
3655 last_offset = (u64)-1;
3656 else
3657 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003658 goto done;
3659 }
3660 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003661 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003662 last_offset = (u64)-1;
3663 goto done;
3664 }
3665 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3666 ret = overwrite_item(trans, log, dst_path,
3667 path->nodes[0], path->slots[0],
3668 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003669 if (ret)
3670 err = ret;
3671 else
3672 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003673 goto done;
3674 }
3675 }
3676done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003677 btrfs_release_path(path);
3678 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003679
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003680 if (err == 0) {
3681 *last_offset_ret = last_offset;
3682 /*
3683 * insert the log range keys to indicate where the log
3684 * is valid
3685 */
3686 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003687 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003688 if (ret)
3689 err = ret;
3690 }
3691 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003692}
3693
3694/*
3695 * logging directories is very similar to logging inodes, We find all the items
3696 * from the current transaction and write them to the log.
3697 *
3698 * The recovery code scans the directory in the subvolume, and if it finds a
3699 * key in the range logged that is not present in the log tree, then it means
3700 * that dir entry was unlinked during the transaction.
3701 *
3702 * In order for that scan to work, we must include one key smaller than
3703 * the smallest logged by this transaction and one key larger than the largest
3704 * key logged by this transaction.
3705 */
3706static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003707 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003708 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003709 struct btrfs_path *dst_path,
3710 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003711{
3712 u64 min_key;
3713 u64 max_key;
3714 int ret;
3715 int key_type = BTRFS_DIR_ITEM_KEY;
3716
3717again:
3718 min_key = 0;
3719 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003720 while (1) {
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003721 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3722 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003723 if (ret)
3724 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003725 if (max_key == (u64)-1)
3726 break;
3727 min_key = max_key + 1;
3728 }
3729
3730 if (key_type == BTRFS_DIR_ITEM_KEY) {
3731 key_type = BTRFS_DIR_INDEX_KEY;
3732 goto again;
3733 }
3734 return 0;
3735}
3736
3737/*
3738 * a helper function to drop items from the log before we relog an
3739 * inode. max_key_type indicates the highest item type to remove.
3740 * This cannot be run for file data extents because it does not
3741 * free the extents they point to.
3742 */
3743static int drop_objectid_items(struct btrfs_trans_handle *trans,
3744 struct btrfs_root *log,
3745 struct btrfs_path *path,
3746 u64 objectid, int max_key_type)
3747{
3748 int ret;
3749 struct btrfs_key key;
3750 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003751 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003752
3753 key.objectid = objectid;
3754 key.type = max_key_type;
3755 key.offset = (u64)-1;
3756
Chris Masond3977122009-01-05 21:25:51 -05003757 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003758 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003759 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003760 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003761 break;
3762
3763 if (path->slots[0] == 0)
3764 break;
3765
3766 path->slots[0]--;
3767 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3768 path->slots[0]);
3769
3770 if (found_key.objectid != objectid)
3771 break;
3772
Josef Bacik18ec90d2012-09-28 11:56:28 -04003773 found_key.offset = 0;
3774 found_key.type = 0;
3775 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3776 &start_slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00003777 if (ret < 0)
3778 break;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003779
3780 ret = btrfs_del_items(trans, log, path, start_slot,
3781 path->slots[0] - start_slot + 1);
3782 /*
3783 * If start slot isn't 0 then we don't need to re-search, we've
3784 * found the last guy with the objectid in this tree.
3785 */
3786 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003787 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003788 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003789 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003790 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003791 if (ret > 0)
3792 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003793 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003794}
3795
Josef Bacik94edf4a2012-09-25 14:56:25 -04003796static void fill_inode_item(struct btrfs_trans_handle *trans,
3797 struct extent_buffer *leaf,
3798 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003799 struct inode *inode, int log_inode_only,
3800 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003801{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003802 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003803
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003804 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003805
3806 if (log_inode_only) {
3807 /* set the generation to zero so the recover code
3808 * can tell the difference between an logging
3809 * just to say 'this inode exists' and a logging
3810 * to say 'update this inode with these values'
3811 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003812 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003813 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003814 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003815 btrfs_set_token_inode_generation(leaf, item,
3816 BTRFS_I(inode)->generation,
3817 &token);
3818 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003819 }
3820
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003821 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3822 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3823 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3824 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3825
David Sterbaa937b972014-12-12 17:39:12 +01003826 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003827 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003828 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003829 inode->i_atime.tv_nsec, &token);
3830
David Sterbaa937b972014-12-12 17:39:12 +01003831 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003832 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003833 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003834 inode->i_mtime.tv_nsec, &token);
3835
David Sterbaa937b972014-12-12 17:39:12 +01003836 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003837 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003838 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003839 inode->i_ctime.tv_nsec, &token);
3840
3841 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3842 &token);
3843
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003844 btrfs_set_token_inode_sequence(leaf, item,
3845 inode_peek_iversion(inode), &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003846 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3847 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3848 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3849 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003850}
3851
Josef Bacika95249b2012-10-11 16:17:34 -04003852static int log_inode_item(struct btrfs_trans_handle *trans,
3853 struct btrfs_root *log, struct btrfs_path *path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003854 struct btrfs_inode *inode)
Josef Bacika95249b2012-10-11 16:17:34 -04003855{
3856 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003857 int ret;
3858
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003859 ret = btrfs_insert_empty_item(trans, log, path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003860 &inode->location, sizeof(*inode_item));
Josef Bacika95249b2012-10-11 16:17:34 -04003861 if (ret && ret != -EEXIST)
3862 return ret;
3863 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3864 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003865 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3866 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003867 btrfs_release_path(path);
3868 return 0;
3869}
3870
Chris Mason31ff1cd2008-09-11 16:17:57 -04003871static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003872 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003873 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003874 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003875 int start_slot, int nr, int inode_only,
3876 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003877{
David Sterba3ffbd682018-06-29 10:56:42 +02003878 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003879 unsigned long src_offset;
3880 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003881 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003882 struct btrfs_file_extent_item *extent;
3883 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003884 struct extent_buffer *src = src_path->nodes[0];
3885 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003886 int ret;
3887 struct btrfs_key *ins_keys;
3888 u32 *ins_sizes;
3889 char *ins_data;
3890 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003891 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003892 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003893 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003894 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003895 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003896
3897 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003898
3899 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3900 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003901 if (!ins_data)
3902 return -ENOMEM;
3903
Josef Bacik16e75492013-10-22 12:18:51 -04003904 first_key.objectid = (u64)-1;
3905
Chris Mason31ff1cd2008-09-11 16:17:57 -04003906 ins_sizes = (u32 *)ins_data;
3907 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3908
3909 for (i = 0; i < nr; i++) {
3910 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3911 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3912 }
3913 ret = btrfs_insert_empty_items(trans, log, dst_path,
3914 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003915 if (ret) {
3916 kfree(ins_data);
3917 return ret;
3918 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003919
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003920 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003921 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3922 dst_path->slots[0]);
3923
3924 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3925
Matthias Kaehlcke0dde10b2017-07-27 14:30:23 -07003926 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003927 last_key = ins_keys[i];
3928
Josef Bacik94edf4a2012-09-25 14:56:25 -04003929 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003930 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3931 dst_path->slots[0],
3932 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003933 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01003934 &inode->vfs_inode,
3935 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003936 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003937 } else {
3938 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3939 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003940 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003941
Josef Bacik16e75492013-10-22 12:18:51 -04003942 /*
3943 * We set need_find_last_extent here in case we know we were
3944 * processing other items and then walk into the first extent in
3945 * the inode. If we don't hit an extent then nothing changes,
3946 * we'll do the last search the next time around.
3947 */
3948 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3949 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003950 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003951 first_key = ins_keys[i];
3952 } else {
3953 need_find_last_extent = false;
3954 }
3955
Chris Mason31ff1cd2008-09-11 16:17:57 -04003956 /* take a reference on file data extents so that truncates
3957 * or deletes of this inode don't have to relog the inode
3958 * again
3959 */
David Sterba962a2982014-06-04 18:41:45 +02003960 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003961 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003962 int found_type;
3963 extent = btrfs_item_ptr(src, start_slot + i,
3964 struct btrfs_file_extent_item);
3965
liubo8e531cd2011-05-06 10:36:09 +08003966 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3967 continue;
3968
Chris Mason31ff1cd2008-09-11 16:17:57 -04003969 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003970 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003971 u64 ds, dl, cs, cl;
3972 ds = btrfs_file_extent_disk_bytenr(src,
3973 extent);
3974 /* ds == 0 is a hole */
3975 if (ds == 0)
3976 continue;
3977
3978 dl = btrfs_file_extent_disk_num_bytes(src,
3979 extent);
3980 cs = btrfs_file_extent_offset(src, extent);
3981 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003982 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003983 if (btrfs_file_extent_compression(src,
3984 extent)) {
3985 cs = 0;
3986 cl = dl;
3987 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003988
3989 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003990 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003991 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003992 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003993 if (ret) {
3994 btrfs_release_path(dst_path);
3995 kfree(ins_data);
3996 return ret;
3997 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003998 }
3999 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004000 }
4001
4002 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004003 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04004004 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05004005
4006 /*
4007 * we have to do this after the loop above to avoid changing the
4008 * log tree while trying to change the log tree.
4009 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004010 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004011 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05004012 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4013 struct btrfs_ordered_sum,
4014 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004015 if (!ret)
4016 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05004017 list_del(&sums->list);
4018 kfree(sums);
4019 }
Josef Bacik16e75492013-10-22 12:18:51 -04004020
4021 if (!has_extents)
4022 return ret;
4023
Filipe Manana74121f7c2014-08-07 12:00:44 +01004024 if (need_find_last_extent && *last_extent == first_key.offset) {
4025 /*
4026 * We don't have any leafs between our current one and the one
4027 * we processed before that can have file extent items for our
4028 * inode (and have a generation number smaller than our current
4029 * transaction id).
4030 */
4031 need_find_last_extent = false;
4032 }
4033
Josef Bacik16e75492013-10-22 12:18:51 -04004034 /*
4035 * Because we use btrfs_search_forward we could skip leaves that were
4036 * not modified and then assume *last_extent is valid when it really
4037 * isn't. So back up to the previous leaf and read the end of the last
4038 * extent before we go and fill in holes.
4039 */
4040 if (need_find_last_extent) {
4041 u64 len;
4042
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004043 ret = btrfs_prev_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04004044 if (ret < 0)
4045 return ret;
4046 if (ret)
4047 goto fill_holes;
4048 if (src_path->slots[0])
4049 src_path->slots[0]--;
4050 src = src_path->nodes[0];
4051 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004052 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04004053 key.type != BTRFS_EXTENT_DATA_KEY)
4054 goto fill_holes;
4055 extent = btrfs_item_ptr(src, src_path->slots[0],
4056 struct btrfs_file_extent_item);
4057 if (btrfs_file_extent_type(src, extent) ==
4058 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004059 len = btrfs_file_extent_ram_bytes(src, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04004060 *last_extent = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004061 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04004062 } else {
4063 len = btrfs_file_extent_num_bytes(src, extent);
4064 *last_extent = key.offset + len;
4065 }
4066 }
4067fill_holes:
4068 /* So we did prev_leaf, now we need to move to the next leaf, but a few
4069 * things could have happened
4070 *
4071 * 1) A merge could have happened, so we could currently be on a leaf
4072 * that holds what we were copying in the first place.
4073 * 2) A split could have happened, and now not all of the items we want
4074 * are on the same leaf.
4075 *
4076 * So we need to adjust how we search for holes, we need to drop the
4077 * path and re-search for the first extent key we found, and then walk
4078 * forward until we hit the last one we copied.
4079 */
4080 if (need_find_last_extent) {
4081 /* btrfs_prev_leaf could return 1 without releasing the path */
4082 btrfs_release_path(src_path);
David Sterbaf85b7372017-01-20 14:54:07 +01004083 ret = btrfs_search_slot(NULL, inode->root, &first_key,
4084 src_path, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004085 if (ret < 0)
4086 return ret;
4087 ASSERT(ret == 0);
4088 src = src_path->nodes[0];
4089 i = src_path->slots[0];
4090 } else {
4091 i = start_slot;
4092 }
4093
4094 /*
4095 * Ok so here we need to go through and fill in any holes we may have
4096 * to make sure that holes are punched for those areas in case they had
4097 * extents previously.
4098 */
4099 while (!done) {
4100 u64 offset, len;
4101 u64 extent_end;
4102
4103 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004104 ret = btrfs_next_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04004105 if (ret < 0)
4106 return ret;
4107 ASSERT(ret == 0);
4108 src = src_path->nodes[0];
4109 i = 0;
Filipe Manana8434ec42018-03-26 23:59:12 +01004110 need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04004111 }
4112
4113 btrfs_item_key_to_cpu(src, &key, i);
4114 if (!btrfs_comp_cpu_keys(&key, &last_key))
4115 done = true;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004116 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04004117 key.type != BTRFS_EXTENT_DATA_KEY) {
4118 i++;
4119 continue;
4120 }
4121 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
4122 if (btrfs_file_extent_type(src, extent) ==
4123 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004124 len = btrfs_file_extent_ram_bytes(src, extent);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004125 extent_end = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004126 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04004127 } else {
4128 len = btrfs_file_extent_num_bytes(src, extent);
4129 extent_end = key.offset + len;
4130 }
4131 i++;
4132
4133 if (*last_extent == key.offset) {
4134 *last_extent = extent_end;
4135 continue;
4136 }
4137 offset = *last_extent;
4138 len = key.offset - *last_extent;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004139 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01004140 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004141 if (ret)
4142 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01004143 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04004144 }
Filipe Manana4ee3fad2018-03-26 23:59:00 +01004145
4146 /*
4147 * Check if there is a hole between the last extent found in our leaf
4148 * and the first extent in the next leaf. If there is one, we need to
4149 * log an explicit hole so that at replay time we can punch the hole.
4150 */
4151 if (ret == 0 &&
4152 key.objectid == btrfs_ino(inode) &&
4153 key.type == BTRFS_EXTENT_DATA_KEY &&
4154 i == btrfs_header_nritems(src_path->nodes[0])) {
4155 ret = btrfs_next_leaf(inode->root, src_path);
4156 need_find_last_extent = true;
4157 if (ret > 0) {
4158 ret = 0;
4159 } else if (ret == 0) {
4160 btrfs_item_key_to_cpu(src_path->nodes[0], &key,
4161 src_path->slots[0]);
4162 if (key.objectid == btrfs_ino(inode) &&
4163 key.type == BTRFS_EXTENT_DATA_KEY &&
4164 *last_extent < key.offset) {
4165 const u64 len = key.offset - *last_extent;
4166
4167 ret = btrfs_insert_file_extent(trans, log,
4168 btrfs_ino(inode),
4169 *last_extent, 0,
4170 0, len, 0, len,
4171 0, 0, 0);
4172 }
4173 }
4174 }
Josef Bacik16e75492013-10-22 12:18:51 -04004175 /*
4176 * Need to let the callers know we dropped the path so they should
4177 * re-search.
4178 */
4179 if (!ret && need_find_last_extent)
4180 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004181 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004182}
4183
Josef Bacik5dc562c2012-08-17 13:14:17 -04004184static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4185{
4186 struct extent_map *em1, *em2;
4187
4188 em1 = list_entry(a, struct extent_map, list);
4189 em2 = list_entry(b, struct extent_map, list);
4190
4191 if (em1->start < em2->start)
4192 return -1;
4193 else if (em1->start > em2->start)
4194 return 1;
4195 return 0;
4196}
4197
Josef Bacike7175a62018-05-23 11:58:34 -04004198static int log_extent_csums(struct btrfs_trans_handle *trans,
4199 struct btrfs_inode *inode,
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004200 struct btrfs_root *log_root,
Josef Bacike7175a62018-05-23 11:58:34 -04004201 const struct extent_map *em)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004202{
Josef Bacik2ab28f32012-10-12 15:27:49 -04004203 u64 csum_offset;
4204 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004205 LIST_HEAD(ordered_sums);
4206 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004207
Josef Bacike7175a62018-05-23 11:58:34 -04004208 if (inode->flags & BTRFS_INODE_NODATASUM ||
4209 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
Filipe Manana8407f552014-09-05 15:14:39 +01004210 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04004211 return 0;
4212
Josef Bacike7175a62018-05-23 11:58:34 -04004213 /* If we're compressed we have to save the entire range of csums. */
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004214 if (em->compress_type) {
4215 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004216 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004217 } else {
Josef Bacike7175a62018-05-23 11:58:34 -04004218 csum_offset = em->mod_start - em->start;
4219 csum_len = em->mod_len;
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004220 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004221
Josef Bacik70c8a912012-10-11 16:54:30 -04004222 /* block start is already adjusted for the file extent offset. */
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004223 ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004224 em->block_start + csum_offset,
4225 em->block_start + csum_offset +
4226 csum_len - 1, &ordered_sums, 0);
4227 if (ret)
4228 return ret;
4229
4230 while (!list_empty(&ordered_sums)) {
4231 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4232 struct btrfs_ordered_sum,
4233 list);
4234 if (!ret)
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004235 ret = btrfs_csum_file_blocks(trans, log_root, sums);
Josef Bacik70c8a912012-10-11 16:54:30 -04004236 list_del(&sums->list);
4237 kfree(sums);
4238 }
4239
4240 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004241}
4242
Filipe Manana8407f552014-09-05 15:14:39 +01004243static int log_one_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004244 struct btrfs_inode *inode, struct btrfs_root *root,
Filipe Manana8407f552014-09-05 15:14:39 +01004245 const struct extent_map *em,
4246 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004247 struct btrfs_log_ctx *ctx)
4248{
4249 struct btrfs_root *log = root->log_root;
4250 struct btrfs_file_extent_item *fi;
4251 struct extent_buffer *leaf;
4252 struct btrfs_map_token token;
4253 struct btrfs_key key;
4254 u64 extent_offset = em->start - em->orig_start;
4255 u64 block_len;
4256 int ret;
4257 int extent_inserted = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004258
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004259 ret = log_extent_csums(trans, inode, log, em);
Filipe Manana8407f552014-09-05 15:14:39 +01004260 if (ret)
4261 return ret;
4262
Filipe Manana8407f552014-09-05 15:14:39 +01004263 btrfs_init_map_token(&token);
4264
Nikolay Borisov9d122622017-01-18 00:31:40 +02004265 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
Filipe Manana8407f552014-09-05 15:14:39 +01004266 em->start + em->len, NULL, 0, 1,
4267 sizeof(*fi), &extent_inserted);
4268 if (ret)
4269 return ret;
4270
4271 if (!extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004272 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004273 key.type = BTRFS_EXTENT_DATA_KEY;
4274 key.offset = em->start;
4275
4276 ret = btrfs_insert_empty_item(trans, log, path, &key,
4277 sizeof(*fi));
4278 if (ret)
4279 return ret;
4280 }
4281 leaf = path->nodes[0];
4282 fi = btrfs_item_ptr(leaf, path->slots[0],
4283 struct btrfs_file_extent_item);
4284
Josef Bacik50d9aa92014-11-21 14:52:38 -05004285 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004286 &token);
4287 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4288 btrfs_set_token_file_extent_type(leaf, fi,
4289 BTRFS_FILE_EXTENT_PREALLOC,
4290 &token);
4291 else
4292 btrfs_set_token_file_extent_type(leaf, fi,
4293 BTRFS_FILE_EXTENT_REG,
4294 &token);
4295
4296 block_len = max(em->block_len, em->orig_block_len);
4297 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4298 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4299 em->block_start,
4300 &token);
4301 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4302 &token);
4303 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4304 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4305 em->block_start -
4306 extent_offset, &token);
4307 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4308 &token);
4309 } else {
4310 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4311 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4312 &token);
4313 }
4314
4315 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4316 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4317 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4318 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4319 &token);
4320 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4321 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4322 btrfs_mark_buffer_dirty(leaf);
4323
4324 btrfs_release_path(path);
4325
4326 return ret;
4327}
4328
Filipe Manana31d11b82018-05-09 16:01:46 +01004329/*
4330 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4331 * lose them after doing a fast fsync and replaying the log. We scan the
4332 * subvolume's root instead of iterating the inode's extent map tree because
4333 * otherwise we can log incorrect extent items based on extent map conversion.
4334 * That can happen due to the fact that extent maps are merged when they
4335 * are not in the extent map tree's list of modified extents.
4336 */
4337static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4338 struct btrfs_inode *inode,
4339 struct btrfs_path *path)
4340{
4341 struct btrfs_root *root = inode->root;
4342 struct btrfs_key key;
4343 const u64 i_size = i_size_read(&inode->vfs_inode);
4344 const u64 ino = btrfs_ino(inode);
4345 struct btrfs_path *dst_path = NULL;
4346 u64 last_extent = (u64)-1;
4347 int ins_nr = 0;
4348 int start_slot;
4349 int ret;
4350
4351 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4352 return 0;
4353
4354 key.objectid = ino;
4355 key.type = BTRFS_EXTENT_DATA_KEY;
4356 key.offset = i_size;
4357 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4358 if (ret < 0)
4359 goto out;
4360
4361 while (true) {
4362 struct extent_buffer *leaf = path->nodes[0];
4363 int slot = path->slots[0];
4364
4365 if (slot >= btrfs_header_nritems(leaf)) {
4366 if (ins_nr > 0) {
4367 ret = copy_items(trans, inode, dst_path, path,
4368 &last_extent, start_slot,
4369 ins_nr, 1, 0);
4370 if (ret < 0)
4371 goto out;
4372 ins_nr = 0;
4373 }
4374 ret = btrfs_next_leaf(root, path);
4375 if (ret < 0)
4376 goto out;
4377 if (ret > 0) {
4378 ret = 0;
4379 break;
4380 }
4381 continue;
4382 }
4383
4384 btrfs_item_key_to_cpu(leaf, &key, slot);
4385 if (key.objectid > ino)
4386 break;
4387 if (WARN_ON_ONCE(key.objectid < ino) ||
4388 key.type < BTRFS_EXTENT_DATA_KEY ||
4389 key.offset < i_size) {
4390 path->slots[0]++;
4391 continue;
4392 }
4393 if (last_extent == (u64)-1) {
4394 last_extent = key.offset;
4395 /*
4396 * Avoid logging extent items logged in past fsync calls
4397 * and leading to duplicate keys in the log tree.
4398 */
4399 do {
4400 ret = btrfs_truncate_inode_items(trans,
4401 root->log_root,
4402 &inode->vfs_inode,
4403 i_size,
4404 BTRFS_EXTENT_DATA_KEY);
4405 } while (ret == -EAGAIN);
4406 if (ret)
4407 goto out;
4408 }
4409 if (ins_nr == 0)
4410 start_slot = slot;
4411 ins_nr++;
4412 path->slots[0]++;
4413 if (!dst_path) {
4414 dst_path = btrfs_alloc_path();
4415 if (!dst_path) {
4416 ret = -ENOMEM;
4417 goto out;
4418 }
4419 }
4420 }
4421 if (ins_nr > 0) {
4422 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4423 start_slot, ins_nr, 1, 0);
4424 if (ret > 0)
4425 ret = 0;
4426 }
4427out:
4428 btrfs_release_path(path);
4429 btrfs_free_path(dst_path);
4430 return ret;
4431}
4432
Josef Bacik5dc562c2012-08-17 13:14:17 -04004433static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4434 struct btrfs_root *root,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004435 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004436 struct btrfs_path *path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004437 struct btrfs_log_ctx *ctx,
4438 const u64 start,
4439 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004440{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004441 struct extent_map *em, *n;
4442 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004443 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004444 u64 test_gen;
4445 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004446 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004447
4448 INIT_LIST_HEAD(&extents);
4449
Josef Bacik5dc562c2012-08-17 13:14:17 -04004450 write_lock(&tree->lock);
4451 test_gen = root->fs_info->last_trans_committed;
4452
4453 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
Filipe Manana008c6752018-10-29 09:42:06 +00004454 /*
4455 * Skip extents outside our logging range. It's important to do
4456 * it for correctness because if we don't ignore them, we may
4457 * log them before their ordered extent completes, and therefore
4458 * we could log them without logging their respective checksums
4459 * (the checksum items are added to the csum tree at the very
4460 * end of btrfs_finish_ordered_io()). Also leave such extents
4461 * outside of our range in the list, since we may have another
4462 * ranged fsync in the near future that needs them. If an extent
4463 * outside our range corresponds to a hole, log it to avoid
4464 * leaving gaps between extents (fsck will complain when we are
4465 * not using the NO_HOLES feature).
4466 */
4467 if ((em->start > end || em->start + em->len <= start) &&
4468 em->block_start != EXTENT_MAP_HOLE)
4469 continue;
4470
Josef Bacik5dc562c2012-08-17 13:14:17 -04004471 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004472 /*
4473 * Just an arbitrary number, this can be really CPU intensive
4474 * once we start getting a lot of extents, and really once we
4475 * have a bunch of extents we just want to commit since it will
4476 * be faster.
4477 */
4478 if (++num > 32768) {
4479 list_del_init(&tree->modified_extents);
4480 ret = -EFBIG;
4481 goto process;
4482 }
4483
Josef Bacik5dc562c2012-08-17 13:14:17 -04004484 if (em->generation <= test_gen)
4485 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004486
Filipe Manana31d11b82018-05-09 16:01:46 +01004487 /* We log prealloc extents beyond eof later. */
4488 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4489 em->start >= i_size_read(&inode->vfs_inode))
4490 continue;
4491
Josef Bacikff44c6e2012-09-14 12:59:20 -04004492 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004493 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004494 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004495 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004496 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004497 }
4498
4499 list_sort(NULL, &extents, extent_cmp);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004500process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004501 while (!list_empty(&extents)) {
4502 em = list_entry(extents.next, struct extent_map, list);
4503
4504 list_del_init(&em->list);
4505
4506 /*
4507 * If we had an error we just need to delete everybody from our
4508 * private list.
4509 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004510 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004511 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004512 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004513 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004514 }
4515
4516 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004517
Josef Bacika2120a42018-05-23 11:58:35 -04004518 ret = log_one_extent(trans, inode, root, em, path, ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004519 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004520 clear_em_logging(tree, em);
4521 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004522 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004523 WARN_ON(!list_empty(&extents));
4524 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004525
Josef Bacik5dc562c2012-08-17 13:14:17 -04004526 btrfs_release_path(path);
Filipe Manana31d11b82018-05-09 16:01:46 +01004527 if (!ret)
4528 ret = btrfs_log_prealloc_extents(trans, inode, path);
4529
Josef Bacik5dc562c2012-08-17 13:14:17 -04004530 return ret;
4531}
4532
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004533static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004534 struct btrfs_path *path, u64 *size_ret)
4535{
4536 struct btrfs_key key;
4537 int ret;
4538
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004539 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004540 key.type = BTRFS_INODE_ITEM_KEY;
4541 key.offset = 0;
4542
4543 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4544 if (ret < 0) {
4545 return ret;
4546 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004547 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004548 } else {
4549 struct btrfs_inode_item *item;
4550
4551 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4552 struct btrfs_inode_item);
4553 *size_ret = btrfs_inode_size(path->nodes[0], item);
Filipe Mananabf504112019-03-04 14:06:12 +00004554 /*
4555 * If the in-memory inode's i_size is smaller then the inode
4556 * size stored in the btree, return the inode's i_size, so
4557 * that we get a correct inode size after replaying the log
4558 * when before a power failure we had a shrinking truncate
4559 * followed by addition of a new name (rename / new hard link).
4560 * Otherwise return the inode size from the btree, to avoid
4561 * data loss when replaying a log due to previously doing a
4562 * write that expands the inode's size and logging a new name
4563 * immediately after.
4564 */
4565 if (*size_ret > inode->vfs_inode.i_size)
4566 *size_ret = inode->vfs_inode.i_size;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004567 }
4568
4569 btrfs_release_path(path);
4570 return 0;
4571}
4572
Filipe Manana36283bf2015-06-20 00:44:51 +01004573/*
4574 * At the moment we always log all xattrs. This is to figure out at log replay
4575 * time which xattrs must have their deletion replayed. If a xattr is missing
4576 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4577 * because if a xattr is deleted, the inode is fsynced and a power failure
4578 * happens, causing the log to be replayed the next time the fs is mounted,
4579 * we want the xattr to not exist anymore (same behaviour as other filesystems
4580 * with a journal, ext3/4, xfs, f2fs, etc).
4581 */
4582static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4583 struct btrfs_root *root,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004584 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004585 struct btrfs_path *path,
4586 struct btrfs_path *dst_path)
4587{
4588 int ret;
4589 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004590 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004591 int ins_nr = 0;
4592 int start_slot = 0;
4593
4594 key.objectid = ino;
4595 key.type = BTRFS_XATTR_ITEM_KEY;
4596 key.offset = 0;
4597
4598 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4599 if (ret < 0)
4600 return ret;
4601
4602 while (true) {
4603 int slot = path->slots[0];
4604 struct extent_buffer *leaf = path->nodes[0];
4605 int nritems = btrfs_header_nritems(leaf);
4606
4607 if (slot >= nritems) {
4608 if (ins_nr > 0) {
4609 u64 last_extent = 0;
4610
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004611 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004612 &last_extent, start_slot,
4613 ins_nr, 1, 0);
4614 /* can't be 1, extent items aren't processed */
4615 ASSERT(ret <= 0);
4616 if (ret < 0)
4617 return ret;
4618 ins_nr = 0;
4619 }
4620 ret = btrfs_next_leaf(root, path);
4621 if (ret < 0)
4622 return ret;
4623 else if (ret > 0)
4624 break;
4625 continue;
4626 }
4627
4628 btrfs_item_key_to_cpu(leaf, &key, slot);
4629 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4630 break;
4631
4632 if (ins_nr == 0)
4633 start_slot = slot;
4634 ins_nr++;
4635 path->slots[0]++;
4636 cond_resched();
4637 }
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 }
4649
4650 return 0;
4651}
4652
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004653/*
4654 * If the no holes feature is enabled we need to make sure any hole between the
4655 * last extent and the i_size of our inode is explicitly marked in the log. This
4656 * is to make sure that doing something like:
4657 *
4658 * 1) create file with 128Kb of data
4659 * 2) truncate file to 64Kb
4660 * 3) truncate file to 256Kb
4661 * 4) fsync file
4662 * 5) <crash/power failure>
4663 * 6) mount fs and trigger log replay
4664 *
4665 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4666 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4667 * file correspond to a hole. The presence of explicit holes in a log tree is
4668 * what guarantees that log replay will remove/adjust file extent items in the
4669 * fs/subvol tree.
4670 *
4671 * Here we do not need to care about holes between extents, that is already done
4672 * by copy_items(). We also only need to do this in the full sync path, where we
4673 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4674 * lookup the list of modified extent maps and if any represents a hole, we
4675 * insert a corresponding extent representing a hole in the log tree.
4676 */
4677static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4678 struct btrfs_root *root,
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004679 struct btrfs_inode *inode,
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004680 struct btrfs_path *path)
4681{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004682 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004683 int ret;
4684 struct btrfs_key key;
4685 u64 hole_start;
4686 u64 hole_size;
4687 struct extent_buffer *leaf;
4688 struct btrfs_root *log = root->log_root;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004689 const u64 ino = btrfs_ino(inode);
4690 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004691
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004692 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004693 return 0;
4694
4695 key.objectid = ino;
4696 key.type = BTRFS_EXTENT_DATA_KEY;
4697 key.offset = (u64)-1;
4698
4699 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4700 ASSERT(ret != 0);
4701 if (ret < 0)
4702 return ret;
4703
4704 ASSERT(path->slots[0] > 0);
4705 path->slots[0]--;
4706 leaf = path->nodes[0];
4707 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4708
4709 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4710 /* inode does not have any extents */
4711 hole_start = 0;
4712 hole_size = i_size;
4713 } else {
4714 struct btrfs_file_extent_item *extent;
4715 u64 len;
4716
4717 /*
4718 * If there's an extent beyond i_size, an explicit hole was
4719 * already inserted by copy_items().
4720 */
4721 if (key.offset >= i_size)
4722 return 0;
4723
4724 extent = btrfs_item_ptr(leaf, path->slots[0],
4725 struct btrfs_file_extent_item);
4726
4727 if (btrfs_file_extent_type(leaf, extent) ==
Filipe Manana0ccc3872019-03-19 17:18:13 +00004728 BTRFS_FILE_EXTENT_INLINE)
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004729 return 0;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004730
4731 len = btrfs_file_extent_num_bytes(leaf, extent);
4732 /* Last extent goes beyond i_size, no need to log a hole. */
4733 if (key.offset + len > i_size)
4734 return 0;
4735 hole_start = key.offset + len;
4736 hole_size = i_size - hole_start;
4737 }
4738 btrfs_release_path(path);
4739
4740 /* Last extent ends at i_size. */
4741 if (hole_size == 0)
4742 return 0;
4743
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004744 hole_size = ALIGN(hole_size, fs_info->sectorsize);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004745 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4746 hole_size, 0, hole_size, 0, 0, 0);
4747 return ret;
4748}
4749
Filipe Manana56f23fd2016-03-30 23:37:21 +01004750/*
4751 * When we are logging a new inode X, check if it doesn't have a reference that
4752 * matches the reference from some other inode Y created in a past transaction
4753 * and that was renamed in the current transaction. If we don't do this, then at
4754 * log replay time we can lose inode Y (and all its files if it's a directory):
4755 *
4756 * mkdir /mnt/x
4757 * echo "hello world" > /mnt/x/foobar
4758 * sync
4759 * mv /mnt/x /mnt/y
4760 * mkdir /mnt/x # or touch /mnt/x
4761 * xfs_io -c fsync /mnt/x
4762 * <power fail>
4763 * mount fs, trigger log replay
4764 *
4765 * After the log replay procedure, we would lose the first directory and all its
4766 * files (file foobar).
4767 * For the case where inode Y is not a directory we simply end up losing it:
4768 *
4769 * echo "123" > /mnt/foo
4770 * sync
4771 * mv /mnt/foo /mnt/bar
4772 * echo "abc" > /mnt/foo
4773 * xfs_io -c fsync /mnt/foo
4774 * <power fail>
4775 *
4776 * We also need this for cases where a snapshot entry is replaced by some other
4777 * entry (file or directory) otherwise we end up with an unreplayable log due to
4778 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4779 * if it were a regular entry:
4780 *
4781 * mkdir /mnt/x
4782 * btrfs subvolume snapshot /mnt /mnt/x/snap
4783 * btrfs subvolume delete /mnt/x/snap
4784 * rmdir /mnt/x
4785 * mkdir /mnt/x
4786 * fsync /mnt/x or fsync some new file inside it
4787 * <power fail>
4788 *
4789 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4790 * the same transaction.
4791 */
4792static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4793 const int slot,
4794 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004795 struct btrfs_inode *inode,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004796 u64 *other_ino, u64 *other_parent)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004797{
4798 int ret;
4799 struct btrfs_path *search_path;
4800 char *name = NULL;
4801 u32 name_len = 0;
4802 u32 item_size = btrfs_item_size_nr(eb, slot);
4803 u32 cur_offset = 0;
4804 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4805
4806 search_path = btrfs_alloc_path();
4807 if (!search_path)
4808 return -ENOMEM;
4809 search_path->search_commit_root = 1;
4810 search_path->skip_locking = 1;
4811
4812 while (cur_offset < item_size) {
4813 u64 parent;
4814 u32 this_name_len;
4815 u32 this_len;
4816 unsigned long name_ptr;
4817 struct btrfs_dir_item *di;
4818
4819 if (key->type == BTRFS_INODE_REF_KEY) {
4820 struct btrfs_inode_ref *iref;
4821
4822 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4823 parent = key->offset;
4824 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4825 name_ptr = (unsigned long)(iref + 1);
4826 this_len = sizeof(*iref) + this_name_len;
4827 } else {
4828 struct btrfs_inode_extref *extref;
4829
4830 extref = (struct btrfs_inode_extref *)(ptr +
4831 cur_offset);
4832 parent = btrfs_inode_extref_parent(eb, extref);
4833 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4834 name_ptr = (unsigned long)&extref->name;
4835 this_len = sizeof(*extref) + this_name_len;
4836 }
4837
4838 if (this_name_len > name_len) {
4839 char *new_name;
4840
4841 new_name = krealloc(name, this_name_len, GFP_NOFS);
4842 if (!new_name) {
4843 ret = -ENOMEM;
4844 goto out;
4845 }
4846 name_len = this_name_len;
4847 name = new_name;
4848 }
4849
4850 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004851 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4852 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004853 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004854 struct btrfs_key di_key;
4855
4856 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4857 di, &di_key);
4858 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
Filipe Manana6b5fc432019-02-13 12:14:03 +00004859 if (di_key.objectid != key->objectid) {
4860 ret = 1;
4861 *other_ino = di_key.objectid;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004862 *other_parent = parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004863 } else {
4864 ret = 0;
4865 }
Filipe Manana44f714d2016-06-06 16:11:13 +01004866 } else {
4867 ret = -EAGAIN;
4868 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004869 goto out;
4870 } else if (IS_ERR(di)) {
4871 ret = PTR_ERR(di);
4872 goto out;
4873 }
4874 btrfs_release_path(search_path);
4875
4876 cur_offset += this_len;
4877 }
4878 ret = 0;
4879out:
4880 btrfs_free_path(search_path);
4881 kfree(name);
4882 return ret;
4883}
4884
Filipe Manana6b5fc432019-02-13 12:14:03 +00004885struct btrfs_ino_list {
4886 u64 ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004887 u64 parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004888 struct list_head list;
4889};
4890
4891static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
4892 struct btrfs_root *root,
4893 struct btrfs_path *path,
4894 struct btrfs_log_ctx *ctx,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004895 u64 ino, u64 parent)
Filipe Manana6b5fc432019-02-13 12:14:03 +00004896{
4897 struct btrfs_ino_list *ino_elem;
4898 LIST_HEAD(inode_list);
4899 int ret = 0;
4900
4901 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
4902 if (!ino_elem)
4903 return -ENOMEM;
4904 ino_elem->ino = ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004905 ino_elem->parent = parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004906 list_add_tail(&ino_elem->list, &inode_list);
4907
4908 while (!list_empty(&inode_list)) {
4909 struct btrfs_fs_info *fs_info = root->fs_info;
4910 struct btrfs_key key;
4911 struct inode *inode;
4912
4913 ino_elem = list_first_entry(&inode_list, struct btrfs_ino_list,
4914 list);
4915 ino = ino_elem->ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004916 parent = ino_elem->parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004917 list_del(&ino_elem->list);
4918 kfree(ino_elem);
4919 if (ret)
4920 continue;
4921
4922 btrfs_release_path(path);
4923
4924 key.objectid = ino;
4925 key.type = BTRFS_INODE_ITEM_KEY;
4926 key.offset = 0;
4927 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4928 /*
4929 * If the other inode that had a conflicting dir entry was
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004930 * deleted in the current transaction, we need to log its parent
4931 * directory.
Filipe Manana6b5fc432019-02-13 12:14:03 +00004932 */
4933 if (IS_ERR(inode)) {
4934 ret = PTR_ERR(inode);
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004935 if (ret == -ENOENT) {
4936 key.objectid = parent;
4937 inode = btrfs_iget(fs_info->sb, &key, root,
4938 NULL);
4939 if (IS_ERR(inode)) {
4940 ret = PTR_ERR(inode);
4941 } else {
4942 ret = btrfs_log_inode(trans, root,
4943 BTRFS_I(inode),
4944 LOG_OTHER_INODE_ALL,
4945 0, LLONG_MAX, ctx);
4946 iput(inode);
4947 }
4948 }
Filipe Manana6b5fc432019-02-13 12:14:03 +00004949 continue;
4950 }
4951 /*
4952 * We are safe logging the other inode without acquiring its
4953 * lock as long as we log with the LOG_INODE_EXISTS mode. We
4954 * are safe against concurrent renames of the other inode as
4955 * well because during a rename we pin the log and update the
4956 * log with the new name before we unpin it.
4957 */
4958 ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
4959 LOG_OTHER_INODE, 0, LLONG_MAX, ctx);
4960 if (ret) {
4961 iput(inode);
4962 continue;
4963 }
4964
4965 key.objectid = ino;
4966 key.type = BTRFS_INODE_REF_KEY;
4967 key.offset = 0;
4968 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4969 if (ret < 0) {
4970 iput(inode);
4971 continue;
4972 }
4973
4974 while (true) {
4975 struct extent_buffer *leaf = path->nodes[0];
4976 int slot = path->slots[0];
4977 u64 other_ino = 0;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00004978 u64 other_parent = 0;
Filipe Manana6b5fc432019-02-13 12:14:03 +00004979
4980 if (slot >= btrfs_header_nritems(leaf)) {
4981 ret = btrfs_next_leaf(root, path);
4982 if (ret < 0) {
4983 break;
4984 } else if (ret > 0) {
4985 ret = 0;
4986 break;
4987 }
4988 continue;
4989 }
4990
4991 btrfs_item_key_to_cpu(leaf, &key, slot);
4992 if (key.objectid != ino ||
4993 (key.type != BTRFS_INODE_REF_KEY &&
4994 key.type != BTRFS_INODE_EXTREF_KEY)) {
4995 ret = 0;
4996 break;
4997 }
4998
4999 ret = btrfs_check_ref_name_override(leaf, slot, &key,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005000 BTRFS_I(inode), &other_ino,
5001 &other_parent);
Filipe Manana6b5fc432019-02-13 12:14:03 +00005002 if (ret < 0)
5003 break;
5004 if (ret > 0) {
5005 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
5006 if (!ino_elem) {
5007 ret = -ENOMEM;
5008 break;
5009 }
5010 ino_elem->ino = other_ino;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005011 ino_elem->parent = other_parent;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005012 list_add_tail(&ino_elem->list, &inode_list);
5013 ret = 0;
5014 }
5015 path->slots[0]++;
5016 }
5017 iput(inode);
5018 }
5019
5020 return ret;
5021}
5022
Chris Masone02119d2008-09-05 16:13:11 -04005023/* log a single inode in the tree log.
5024 * At least one parent directory for this inode must exist in the tree
5025 * or be logged already.
5026 *
5027 * Any items from this inode changed by the current transaction are copied
5028 * to the log tree. An extra reference is taken on any extents in this
5029 * file, allowing us to avoid a whole pile of corner cases around logging
5030 * blocks that have been removed from the tree.
5031 *
5032 * See LOG_INODE_ALL and related defines for a description of what inode_only
5033 * does.
5034 *
5035 * This handles both files and directories.
5036 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005037static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005038 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005039 int inode_only,
5040 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01005041 const loff_t end,
5042 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005043{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005044 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04005045 struct btrfs_path *path;
5046 struct btrfs_path *dst_path;
5047 struct btrfs_key min_key;
5048 struct btrfs_key max_key;
5049 struct btrfs_root *log = root->log_root;
Josef Bacik16e75492013-10-22 12:18:51 -04005050 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005051 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005052 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04005053 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005054 int ins_start_slot = 0;
5055 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005056 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005057 u64 ino = btrfs_ino(inode);
5058 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005059 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01005060 bool need_log_inode_item = true;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005061 bool xattrs_logged = false;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005062 bool recursive_logging = false;
Chris Masone02119d2008-09-05 16:13:11 -04005063
Chris Masone02119d2008-09-05 16:13:11 -04005064 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00005065 if (!path)
5066 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04005067 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00005068 if (!dst_path) {
5069 btrfs_free_path(path);
5070 return -ENOMEM;
5071 }
Chris Masone02119d2008-09-05 16:13:11 -04005072
Li Zefan33345d012011-04-20 10:31:50 +08005073 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04005074 min_key.type = BTRFS_INODE_ITEM_KEY;
5075 min_key.offset = 0;
5076
Li Zefan33345d012011-04-20 10:31:50 +08005077 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04005078
Chris Mason12fcfd22009-03-24 10:24:20 -04005079
Josef Bacik5dc562c2012-08-17 13:14:17 -04005080 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005081 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00005082 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005083 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08005084 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04005085 max_key.type = BTRFS_XATTR_ITEM_KEY;
5086 else
5087 max_key.type = (u8)-1;
5088 max_key.offset = (u64)-1;
5089
Filipe Manana2c2c4522015-01-13 16:40:04 +00005090 /*
5091 * Only run delayed items if we are a dir or a new file.
5092 * Otherwise commit the delayed inode only, which is needed in
5093 * order for the log replay code to mark inodes for link count
5094 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
5095 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005096 if (S_ISDIR(inode->vfs_inode.i_mode) ||
5097 inode->generation > fs_info->last_trans_committed)
5098 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00005099 else
Nikolay Borisova59108a2017-01-18 00:31:48 +02005100 ret = btrfs_commit_inode_delayed_inode(inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00005101
5102 if (ret) {
5103 btrfs_free_path(path);
5104 btrfs_free_path(dst_path);
5105 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08005106 }
5107
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005108 if (inode_only == LOG_OTHER_INODE || inode_only == LOG_OTHER_INODE_ALL) {
5109 recursive_logging = true;
5110 if (inode_only == LOG_OTHER_INODE)
5111 inode_only = LOG_INODE_EXISTS;
5112 else
5113 inode_only = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005114 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08005115 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005116 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08005117 }
Chris Masone02119d2008-09-05 16:13:11 -04005118
Filipe Manana5e33a2b2016-02-25 23:19:38 +00005119 /*
Chris Masone02119d2008-09-05 16:13:11 -04005120 * a brute force approach to making sure we get the most uptodate
5121 * copies of everything.
5122 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005123 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04005124 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
5125
Filipe Manana4f764e52015-02-23 19:53:35 +00005126 if (inode_only == LOG_INODE_EXISTS)
5127 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08005128 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04005129 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005130 if (inode_only == LOG_INODE_EXISTS) {
5131 /*
5132 * Make sure the new inode item we write to the log has
5133 * the same isize as the current one (if it exists).
5134 * This is necessary to prevent data loss after log
5135 * replay, and also to prevent doing a wrong expanding
5136 * truncate - for e.g. create file, write 4K into offset
5137 * 0, fsync, write 4K into offset 4096, add hard link,
5138 * fsync some other file (to sync log), power fail - if
5139 * we use the inode's current i_size, after log replay
5140 * we get a 8Kb file, with the last 4Kb extent as a hole
5141 * (zeroes), as if an expanding truncate happened,
5142 * instead of getting a file of 4Kb only.
5143 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005144 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005145 if (err)
5146 goto out_unlock;
5147 }
Filipe Mananaa7429942015-02-13 16:56:14 +00005148 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005149 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00005150 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005151 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00005152 ret = drop_objectid_items(trans, log, path, ino,
5153 max_key.type);
5154 } else {
5155 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005156 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00005157 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005158 &inode->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08005159 while(1) {
5160 ret = btrfs_truncate_inode_items(trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005161 log, &inode->vfs_inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005162 if (ret != -EAGAIN)
5163 break;
5164 }
Filipe Mananaa7429942015-02-13 16:56:14 +00005165 }
Filipe Manana4f764e52015-02-23 19:53:35 +00005166 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02005167 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05005168 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00005169 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04005170 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00005171 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04005172 ret = drop_objectid_items(trans, log, path, ino,
5173 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005174 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00005175 if (inode_only == LOG_INODE_ALL)
5176 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04005177 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005178 }
Josef Bacika95249b2012-10-11 16:17:34 -04005179
Chris Masone02119d2008-09-05 16:13:11 -04005180 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005181 if (ret) {
5182 err = ret;
5183 goto out_unlock;
5184 }
Chris Masone02119d2008-09-05 16:13:11 -04005185
Chris Masond3977122009-01-05 21:25:51 -05005186 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04005187 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01005188 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005189 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07005190 if (ret < 0) {
5191 err = ret;
5192 goto out_unlock;
5193 }
Chris Masone02119d2008-09-05 16:13:11 -04005194 if (ret != 0)
5195 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04005196again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04005197 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08005198 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04005199 break;
5200 if (min_key.type > max_key.type)
5201 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005202
Filipe Mananae4545de2015-06-17 12:49:23 +01005203 if (min_key.type == BTRFS_INODE_ITEM_KEY)
5204 need_log_inode_item = false;
5205
Filipe Manana56f23fd2016-03-30 23:37:21 +01005206 if ((min_key.type == BTRFS_INODE_REF_KEY ||
5207 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
Filipe Manana6b5fc432019-02-13 12:14:03 +00005208 inode->generation == trans->transid &&
5209 !recursive_logging) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005210 u64 other_ino = 0;
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005211 u64 other_parent = 0;
Filipe Manana44f714d2016-06-06 16:11:13 +01005212
Filipe Manana56f23fd2016-03-30 23:37:21 +01005213 ret = btrfs_check_ref_name_override(path->nodes[0],
Nikolay Borisova59108a2017-01-18 00:31:48 +02005214 path->slots[0], &min_key, inode,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005215 &other_ino, &other_parent);
Filipe Manana56f23fd2016-03-30 23:37:21 +01005216 if (ret < 0) {
5217 err = ret;
5218 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01005219 } else if (ret > 0 && ctx &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005220 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005221 if (ins_nr > 0) {
5222 ins_nr++;
5223 } else {
5224 ins_nr = 1;
5225 ins_start_slot = path->slots[0];
5226 }
Nikolay Borisova59108a2017-01-18 00:31:48 +02005227 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana44f714d2016-06-06 16:11:13 +01005228 &last_extent, ins_start_slot,
5229 ins_nr, inode_only,
5230 logged_isize);
5231 if (ret < 0) {
5232 err = ret;
5233 goto out_unlock;
5234 }
5235 ins_nr = 0;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005236
5237 err = log_conflicting_inodes(trans, root, path,
Filipe Mananaa3baaf02019-02-13 12:14:09 +00005238 ctx, other_ino, other_parent);
Filipe Manana44f714d2016-06-06 16:11:13 +01005239 if (err)
5240 goto out_unlock;
Filipe Manana6b5fc432019-02-13 12:14:03 +00005241 btrfs_release_path(path);
5242 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01005243 }
5244 }
5245
Filipe Manana36283bf2015-06-20 00:44:51 +01005246 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5247 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
5248 if (ins_nr == 0)
5249 goto next_slot;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005250 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01005251 &last_extent, ins_start_slot,
5252 ins_nr, inode_only, logged_isize);
5253 if (ret < 0) {
5254 err = ret;
5255 goto out_unlock;
5256 }
5257 ins_nr = 0;
5258 if (ret) {
5259 btrfs_release_path(path);
5260 continue;
5261 }
5262 goto next_slot;
5263 }
5264
Chris Mason31ff1cd2008-09-11 16:17:57 -04005265 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5266 ins_nr++;
5267 goto next_slot;
5268 } else if (!ins_nr) {
5269 ins_start_slot = path->slots[0];
5270 ins_nr = 1;
5271 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04005272 }
5273
Nikolay Borisova59108a2017-01-18 00:31:48 +02005274 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005275 ins_start_slot, ins_nr, inode_only,
5276 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005277 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005278 err = ret;
5279 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02005280 }
5281 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04005282 ins_nr = 0;
5283 btrfs_release_path(path);
5284 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005285 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005286 ins_nr = 1;
5287 ins_start_slot = path->slots[0];
5288next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04005289
Chris Mason3a5f1d42008-09-11 15:53:37 -04005290 nritems = btrfs_header_nritems(path->nodes[0]);
5291 path->slots[0]++;
5292 if (path->slots[0] < nritems) {
5293 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
5294 path->slots[0]);
5295 goto again;
5296 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005297 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005298 ret = copy_items(trans, inode, dst_path, path,
Josef Bacik16e75492013-10-22 12:18:51 -04005299 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005300 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005301 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005302 err = ret;
5303 goto out_unlock;
5304 }
Josef Bacik16e75492013-10-22 12:18:51 -04005305 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005306 ins_nr = 0;
5307 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005308 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01005309next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005310 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04005311 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005312 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04005313 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005314 min_key.offset = 0;
5315 } else {
Chris Masone02119d2008-09-05 16:13:11 -04005316 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005317 }
Chris Masone02119d2008-09-05 16:13:11 -04005318 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005319 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005320 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005321 ins_start_slot, ins_nr, inode_only,
5322 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005323 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005324 err = ret;
5325 goto out_unlock;
5326 }
Josef Bacik16e75492013-10-22 12:18:51 -04005327 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005328 ins_nr = 0;
5329 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005330
Filipe Manana36283bf2015-06-20 00:44:51 +01005331 btrfs_release_path(path);
5332 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005333 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01005334 if (err)
5335 goto out_unlock;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005336 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005337 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5338 btrfs_release_path(path);
5339 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005340 err = btrfs_log_trailing_hole(trans, root, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005341 if (err)
5342 goto out_unlock;
5343 }
Josef Bacika95249b2012-10-11 16:17:34 -04005344log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04005345 btrfs_release_path(path);
5346 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01005347 if (need_log_inode_item) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005348 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana9a8fca62018-05-11 16:42:42 +01005349 if (!err && !xattrs_logged) {
5350 err = btrfs_log_all_xattrs(trans, root, inode, path,
5351 dst_path);
5352 btrfs_release_path(path);
5353 }
Filipe Mananae4545de2015-06-17 12:49:23 +01005354 if (err)
5355 goto out_unlock;
5356 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005357 if (fast_search) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005358 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Josef Bacika2120a42018-05-23 11:58:35 -04005359 ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005360 if (ret) {
5361 err = ret;
5362 goto out_unlock;
5363 }
Josef Bacikd006a042013-11-12 20:54:09 -05005364 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06005365 struct extent_map *em, *n;
5366
Filipe Manana49dae1b2014-09-06 22:34:39 +01005367 write_lock(&em_tree->lock);
5368 /*
5369 * We can't just remove every em if we're called for a ranged
5370 * fsync - that is, one that doesn't cover the whole possible
5371 * file range (0 to LLONG_MAX). This is because we can have
5372 * em's that fall outside the range we're logging and therefore
5373 * their ordered operations haven't completed yet
5374 * (btrfs_finish_ordered_io() not invoked yet). This means we
5375 * didn't get their respective file extent item in the fs/subvol
5376 * tree yet, and need to let the next fast fsync (one which
5377 * consults the list of modified extent maps) find the em so
5378 * that it logs a matching file extent item and waits for the
5379 * respective ordered operation to complete (if it's still
5380 * running).
5381 *
5382 * Removing every em outside the range we're logging would make
5383 * the next fast fsync not log their matching file extent items,
5384 * therefore making us lose data after a log replay.
5385 */
5386 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5387 list) {
5388 const u64 mod_end = em->mod_start + em->mod_len - 1;
5389
5390 if (em->mod_start >= start && mod_end <= end)
5391 list_del_init(&em->list);
5392 }
5393 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005394 }
5395
Nikolay Borisova59108a2017-01-18 00:31:48 +02005396 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5397 ret = log_directory_changes(trans, root, inode, path, dst_path,
5398 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005399 if (ret) {
5400 err = ret;
5401 goto out_unlock;
5402 }
Chris Masone02119d2008-09-05 16:13:11 -04005403 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005404
Nikolay Borisova59108a2017-01-18 00:31:48 +02005405 spin_lock(&inode->lock);
5406 inode->logged_trans = trans->transid;
5407 inode->last_log_commit = inode->last_sub_trans;
5408 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005409out_unlock:
Nikolay Borisova59108a2017-01-18 00:31:48 +02005410 mutex_unlock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04005411
5412 btrfs_free_path(path);
5413 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005414 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005415}
5416
Chris Mason12fcfd22009-03-24 10:24:20 -04005417/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005418 * Check if we must fallback to a transaction commit when logging an inode.
5419 * This must be called after logging the inode and is used only in the context
5420 * when fsyncing an inode requires the need to log some other inode - in which
5421 * case we can't lock the i_mutex of each other inode we need to log as that
5422 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5423 * log inodes up or down in the hierarchy) or rename operations for example. So
5424 * we take the log_mutex of the inode after we have logged it and then check for
5425 * its last_unlink_trans value - this is safe because any task setting
5426 * last_unlink_trans must take the log_mutex and it must do this before it does
5427 * the actual unlink operation, so if we do this check before a concurrent task
5428 * sets last_unlink_trans it means we've logged a consistent version/state of
5429 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005430 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005431 * we logged the inode or it might have also done the unlink).
5432 */
5433static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005434 struct btrfs_inode *inode)
Filipe Manana2be63d52016-02-12 11:34:23 +00005435{
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005436 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana2be63d52016-02-12 11:34:23 +00005437 bool ret = false;
5438
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005439 mutex_lock(&inode->log_mutex);
5440 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
Filipe Manana2be63d52016-02-12 11:34:23 +00005441 /*
5442 * Make sure any commits to the log are forced to be full
5443 * commits.
5444 */
5445 btrfs_set_log_full_commit(fs_info, trans);
5446 ret = true;
5447 }
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005448 mutex_unlock(&inode->log_mutex);
Filipe Manana2be63d52016-02-12 11:34:23 +00005449
5450 return ret;
5451}
5452
5453/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005454 * follow the dentry parent pointers up the chain and see if any
5455 * of the directories in it require a full commit before they can
5456 * be logged. Returns zero if nothing special needs to be done or 1 if
5457 * a full commit is required.
5458 */
5459static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005460 struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005461 struct dentry *parent,
5462 struct super_block *sb,
5463 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005464{
Chris Mason12fcfd22009-03-24 10:24:20 -04005465 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005466 struct dentry *old_parent = NULL;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005467 struct btrfs_inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005468
Chris Masonaf4176b2009-03-24 10:24:31 -04005469 /*
5470 * for regular files, if its inode is already on disk, we don't
5471 * have to worry about the parents at all. This is because
5472 * we can use the last_unlink_trans field to record renames
5473 * and other fun in this file.
5474 */
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005475 if (S_ISREG(inode->vfs_inode.i_mode) &&
5476 inode->generation <= last_committed &&
5477 inode->last_unlink_trans <= last_committed)
5478 goto out;
Chris Masonaf4176b2009-03-24 10:24:31 -04005479
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005480 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005481 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005482 goto out;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005483 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005484 }
5485
5486 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005487 /*
5488 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005489 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005490 * logged_trans so that further down in the log code we don't
5491 * think this inode has already been logged.
5492 */
5493 if (inode != orig_inode)
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005494 inode->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005495 smp_mb();
5496
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005497 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005498 ret = 1;
5499 break;
5500 }
5501
Al Virofc640052016-04-10 01:33:30 -04005502 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005503 break;
5504
Filipe Manana44f714d2016-06-06 16:11:13 +01005505 if (IS_ROOT(parent)) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005506 inode = BTRFS_I(d_inode(parent));
5507 if (btrfs_must_commit_transaction(trans, inode))
Filipe Manana44f714d2016-06-06 16:11:13 +01005508 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005509 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005510 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005511
Josef Bacik6a912212010-11-20 09:48:00 +00005512 parent = dget_parent(parent);
5513 dput(old_parent);
5514 old_parent = parent;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005515 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005516
5517 }
Josef Bacik6a912212010-11-20 09:48:00 +00005518 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005519out:
Chris Masone02119d2008-09-05 16:13:11 -04005520 return ret;
5521}
5522
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005523struct btrfs_dir_list {
5524 u64 ino;
5525 struct list_head list;
5526};
5527
5528/*
5529 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5530 * details about the why it is needed.
5531 * This is a recursive operation - if an existing dentry corresponds to a
5532 * directory, that directory's new entries are logged too (same behaviour as
5533 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5534 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5535 * complains about the following circular lock dependency / possible deadlock:
5536 *
5537 * CPU0 CPU1
5538 * ---- ----
5539 * lock(&type->i_mutex_dir_key#3/2);
5540 * lock(sb_internal#2);
5541 * lock(&type->i_mutex_dir_key#3/2);
5542 * lock(&sb->s_type->i_mutex_key#14);
5543 *
5544 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5545 * sb_start_intwrite() in btrfs_start_transaction().
5546 * Not locking i_mutex of the inodes is still safe because:
5547 *
5548 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5549 * that while logging the inode new references (names) are added or removed
5550 * from the inode, leaving the logged inode item with a link count that does
5551 * not match the number of logged inode reference items. This is fine because
5552 * at log replay time we compute the real number of links and correct the
5553 * link count in the inode item (see replay_one_buffer() and
5554 * link_to_fixup_dir());
5555 *
5556 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5557 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5558 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5559 * has a size that doesn't match the sum of the lengths of all the logged
5560 * names. This does not result in a problem because if a dir_item key is
5561 * logged but its matching dir_index key is not logged, at log replay time we
5562 * don't use it to replay the respective name (see replay_one_name()). On the
5563 * other hand if only the dir_index key ends up being logged, the respective
5564 * name is added to the fs/subvol tree with both the dir_item and dir_index
5565 * keys created (see replay_one_name()).
5566 * The directory's inode item with a wrong i_size is not a problem as well,
5567 * since we don't use it at log replay time to set the i_size in the inode
5568 * item of the fs/subvol tree (see overwrite_item()).
5569 */
5570static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5571 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005572 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005573 struct btrfs_log_ctx *ctx)
5574{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005575 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005576 struct btrfs_root *log = root->log_root;
5577 struct btrfs_path *path;
5578 LIST_HEAD(dir_list);
5579 struct btrfs_dir_list *dir_elem;
5580 int ret = 0;
5581
5582 path = btrfs_alloc_path();
5583 if (!path)
5584 return -ENOMEM;
5585
5586 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5587 if (!dir_elem) {
5588 btrfs_free_path(path);
5589 return -ENOMEM;
5590 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005591 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005592 list_add_tail(&dir_elem->list, &dir_list);
5593
5594 while (!list_empty(&dir_list)) {
5595 struct extent_buffer *leaf;
5596 struct btrfs_key min_key;
5597 int nritems;
5598 int i;
5599
5600 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5601 list);
5602 if (ret)
5603 goto next_dir_inode;
5604
5605 min_key.objectid = dir_elem->ino;
5606 min_key.type = BTRFS_DIR_ITEM_KEY;
5607 min_key.offset = 0;
5608again:
5609 btrfs_release_path(path);
5610 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5611 if (ret < 0) {
5612 goto next_dir_inode;
5613 } else if (ret > 0) {
5614 ret = 0;
5615 goto next_dir_inode;
5616 }
5617
5618process_leaf:
5619 leaf = path->nodes[0];
5620 nritems = btrfs_header_nritems(leaf);
5621 for (i = path->slots[0]; i < nritems; i++) {
5622 struct btrfs_dir_item *di;
5623 struct btrfs_key di_key;
5624 struct inode *di_inode;
5625 struct btrfs_dir_list *new_dir_elem;
5626 int log_mode = LOG_INODE_EXISTS;
5627 int type;
5628
5629 btrfs_item_key_to_cpu(leaf, &min_key, i);
5630 if (min_key.objectid != dir_elem->ino ||
5631 min_key.type != BTRFS_DIR_ITEM_KEY)
5632 goto next_dir_inode;
5633
5634 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5635 type = btrfs_dir_type(leaf, di);
5636 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5637 type != BTRFS_FT_DIR)
5638 continue;
5639 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5640 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5641 continue;
5642
Robbie Koec125cf2016-10-28 10:48:26 +08005643 btrfs_release_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005644 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005645 if (IS_ERR(di_inode)) {
5646 ret = PTR_ERR(di_inode);
5647 goto next_dir_inode;
5648 }
5649
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005650 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005651 iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005652 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005653 }
5654
5655 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005656 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005657 log_mode = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005658 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005659 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005660 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005661 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005662 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005663 iput(di_inode);
5664 if (ret)
5665 goto next_dir_inode;
5666 if (ctx->log_new_dentries) {
5667 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5668 GFP_NOFS);
5669 if (!new_dir_elem) {
5670 ret = -ENOMEM;
5671 goto next_dir_inode;
5672 }
5673 new_dir_elem->ino = di_key.objectid;
5674 list_add_tail(&new_dir_elem->list, &dir_list);
5675 }
5676 break;
5677 }
5678 if (i == nritems) {
5679 ret = btrfs_next_leaf(log, path);
5680 if (ret < 0) {
5681 goto next_dir_inode;
5682 } else if (ret > 0) {
5683 ret = 0;
5684 goto next_dir_inode;
5685 }
5686 goto process_leaf;
5687 }
5688 if (min_key.offset < (u64)-1) {
5689 min_key.offset++;
5690 goto again;
5691 }
5692next_dir_inode:
5693 list_del(&dir_elem->list);
5694 kfree(dir_elem);
5695 }
5696
5697 btrfs_free_path(path);
5698 return ret;
5699}
5700
Filipe Manana18aa0922015-08-05 16:49:08 +01005701static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005702 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01005703 struct btrfs_log_ctx *ctx)
5704{
David Sterba3ffbd682018-06-29 10:56:42 +02005705 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana18aa0922015-08-05 16:49:08 +01005706 int ret;
5707 struct btrfs_path *path;
5708 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005709 struct btrfs_root *root = inode->root;
5710 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005711
5712 path = btrfs_alloc_path();
5713 if (!path)
5714 return -ENOMEM;
5715 path->skip_locking = 1;
5716 path->search_commit_root = 1;
5717
5718 key.objectid = ino;
5719 key.type = BTRFS_INODE_REF_KEY;
5720 key.offset = 0;
5721 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5722 if (ret < 0)
5723 goto out;
5724
5725 while (true) {
5726 struct extent_buffer *leaf = path->nodes[0];
5727 int slot = path->slots[0];
5728 u32 cur_offset = 0;
5729 u32 item_size;
5730 unsigned long ptr;
5731
5732 if (slot >= btrfs_header_nritems(leaf)) {
5733 ret = btrfs_next_leaf(root, path);
5734 if (ret < 0)
5735 goto out;
5736 else if (ret > 0)
5737 break;
5738 continue;
5739 }
5740
5741 btrfs_item_key_to_cpu(leaf, &key, slot);
5742 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5743 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5744 break;
5745
5746 item_size = btrfs_item_size_nr(leaf, slot);
5747 ptr = btrfs_item_ptr_offset(leaf, slot);
5748 while (cur_offset < item_size) {
5749 struct btrfs_key inode_key;
5750 struct inode *dir_inode;
5751
5752 inode_key.type = BTRFS_INODE_ITEM_KEY;
5753 inode_key.offset = 0;
5754
5755 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5756 struct btrfs_inode_extref *extref;
5757
5758 extref = (struct btrfs_inode_extref *)
5759 (ptr + cur_offset);
5760 inode_key.objectid = btrfs_inode_extref_parent(
5761 leaf, extref);
5762 cur_offset += sizeof(*extref);
5763 cur_offset += btrfs_inode_extref_name_len(leaf,
5764 extref);
5765 } else {
5766 inode_key.objectid = key.offset;
5767 cur_offset = item_size;
5768 }
5769
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005770 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
Filipe Manana18aa0922015-08-05 16:49:08 +01005771 root, NULL);
Filipe Manana0f375ee2018-10-09 15:05:29 +01005772 /*
5773 * If the parent inode was deleted, return an error to
5774 * fallback to a transaction commit. This is to prevent
5775 * getting an inode that was moved from one parent A to
5776 * a parent B, got its former parent A deleted and then
5777 * it got fsync'ed, from existing at both parents after
5778 * a log replay (and the old parent still existing).
5779 * Example:
5780 *
5781 * mkdir /mnt/A
5782 * mkdir /mnt/B
5783 * touch /mnt/B/bar
5784 * sync
5785 * mv /mnt/B/bar /mnt/A/bar
5786 * mv -T /mnt/A /mnt/B
5787 * fsync /mnt/B/bar
5788 * <power fail>
5789 *
5790 * If we ignore the old parent B which got deleted,
5791 * after a log replay we would have file bar linked
5792 * at both parents and the old parent B would still
5793 * exist.
5794 */
5795 if (IS_ERR(dir_inode)) {
5796 ret = PTR_ERR(dir_inode);
5797 goto out;
5798 }
Filipe Manana18aa0922015-08-05 16:49:08 +01005799
Filipe Manana657ed1a2016-04-06 17:11:56 +01005800 if (ctx)
5801 ctx->log_new_dentries = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005802 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Filipe Manana18aa0922015-08-05 16:49:08 +01005803 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005804 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005805 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005806 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005807 if (!ret && ctx && ctx->log_new_dentries)
5808 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01005809 BTRFS_I(dir_inode), ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005810 iput(dir_inode);
5811 if (ret)
5812 goto out;
5813 }
5814 path->slots[0]++;
5815 }
5816 ret = 0;
5817out:
5818 btrfs_free_path(path);
5819 return ret;
5820}
5821
Chris Masone02119d2008-09-05 16:13:11 -04005822/*
5823 * helper function around btrfs_log_inode to make sure newly created
5824 * parent directories also end up in the log. A minimal inode and backref
5825 * only logging is done of any parent directories that are older than
5826 * the last committed transaction
5827 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005828static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005829 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005830 struct dentry *parent,
5831 const loff_t start,
5832 const loff_t end,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07005833 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005834 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005835{
Nikolay Borisovf8822742018-02-27 17:37:17 +02005836 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005837 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04005838 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005839 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005840 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005841 u64 last_committed = fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005842 bool log_dentries = false;
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005843 struct btrfs_inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005844
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005845 sb = inode->vfs_inode.i_sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04005846
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005847 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005848 ret = 1;
5849 goto end_no_trans;
5850 }
5851
Miao Xie995946d2014-04-02 19:51:06 +08005852 /*
5853 * The prev transaction commit doesn't complete, we need do
5854 * full commit by ourselves.
5855 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005856 if (fs_info->last_trans_log_full_commit >
5857 fs_info->last_trans_committed) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005858 ret = 1;
5859 goto end_no_trans;
5860 }
5861
Nikolay Borisovf8822742018-02-27 17:37:17 +02005862 if (btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005863 ret = 1;
5864 goto end_no_trans;
5865 }
5866
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005867 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
5868 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04005869 if (ret)
5870 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005871
Filipe Mananaf2d72f42018-10-08 11:12:55 +01005872 /*
5873 * Skip already logged inodes or inodes corresponding to tmpfiles
5874 * (since logging them is pointless, a link count of 0 means they
5875 * will never be accessible).
5876 */
5877 if (btrfs_inode_in_log(inode, trans->transid) ||
5878 inode->vfs_inode.i_nlink == 0) {
Chris Mason257c62e2009-10-13 13:21:08 -04005879 ret = BTRFS_NO_LOG_SYNC;
5880 goto end_no_trans;
5881 }
5882
Miao Xie8b050d32014-02-20 18:08:58 +08005883 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005884 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005885 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005886
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005887 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005888 if (ret)
5889 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005890
Chris Masonaf4176b2009-03-24 10:24:31 -04005891 /*
5892 * for regular files, if its inode is already on disk, we don't
5893 * have to worry about the parents at all. This is because
5894 * we can use the last_unlink_trans field to record renames
5895 * and other fun in this file.
5896 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005897 if (S_ISREG(inode->vfs_inode.i_mode) &&
5898 inode->generation <= last_committed &&
5899 inode->last_unlink_trans <= last_committed) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005900 ret = 0;
5901 goto end_trans;
5902 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005903
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005904 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005905 log_dentries = true;
5906
Filipe Manana18aa0922015-08-05 16:49:08 +01005907 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005908 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005909 * inodes are fully logged. This is to prevent leaving dangling
5910 * directory index entries in directories that were our parents but are
5911 * not anymore. Not doing this results in old parent directory being
5912 * impossible to delete after log replay (rmdir will always fail with
5913 * error -ENOTEMPTY).
5914 *
5915 * Example 1:
5916 *
5917 * mkdir testdir
5918 * touch testdir/foo
5919 * ln testdir/foo testdir/bar
5920 * sync
5921 * unlink testdir/bar
5922 * xfs_io -c fsync testdir/foo
5923 * <power failure>
5924 * mount fs, triggers log replay
5925 *
5926 * If we don't log the parent directory (testdir), after log replay the
5927 * directory still has an entry pointing to the file inode using the bar
5928 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5929 * the file inode has a link count of 1.
5930 *
5931 * Example 2:
5932 *
5933 * mkdir testdir
5934 * touch foo
5935 * ln foo testdir/foo2
5936 * ln foo testdir/foo3
5937 * sync
5938 * unlink testdir/foo3
5939 * xfs_io -c fsync foo
5940 * <power failure>
5941 * mount fs, triggers log replay
5942 *
5943 * Similar as the first example, after log replay the parent directory
5944 * testdir still has an entry pointing to the inode file with name foo3
5945 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5946 * and has a link count of 2.
5947 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005948 if (inode->last_unlink_trans > last_committed) {
Filipe Manana18aa0922015-08-05 16:49:08 +01005949 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5950 if (ret)
5951 goto end_trans;
5952 }
5953
Filipe Manana41bd6062018-11-28 14:54:28 +00005954 /*
5955 * If a new hard link was added to the inode in the current transaction
5956 * and its link count is now greater than 1, we need to fallback to a
5957 * transaction commit, otherwise we can end up not logging all its new
5958 * parents for all the hard links. Here just from the dentry used to
5959 * fsync, we can not visit the ancestor inodes for all the other hard
5960 * links to figure out if any is new, so we fallback to a transaction
5961 * commit (instead of adding a lot of complexity of scanning a btree,
5962 * since this scenario is not a common use case).
5963 */
5964 if (inode->vfs_inode.i_nlink > 1 &&
5965 inode->last_link_trans > last_committed) {
5966 ret = -EMLINK;
5967 goto end_trans;
5968 }
5969
Chris Masond3977122009-01-05 21:25:51 -05005970 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005971 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005972 break;
5973
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005974 inode = BTRFS_I(d_inode(parent));
5975 if (root != inode->root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005976 break;
5977
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005978 if (inode->generation > last_committed) {
5979 ret = btrfs_log_inode(trans, root, inode,
5980 LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005981 if (ret)
5982 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005983 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005984 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005985 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005986
Josef Bacik6a912212010-11-20 09:48:00 +00005987 parent = dget_parent(parent);
5988 dput(old_parent);
5989 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005990 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005991 if (log_dentries)
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005992 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005993 else
5994 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005995end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005996 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005997 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005998 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005999 ret = 1;
6000 }
Miao Xie8b050d32014-02-20 18:08:58 +08006001
6002 if (ret)
6003 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04006004 btrfs_end_log_trans(root);
6005end_no_trans:
6006 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006007}
6008
6009/*
6010 * it is not safe to log dentry if the chunk root has added new
6011 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
6012 * If this returns 1, you must commit the transaction to safely get your
6013 * data on disk.
6014 */
6015int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Nikolay Borisove5b84f7a2018-02-27 17:37:18 +02006016 struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01006017 const loff_t start,
6018 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08006019 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04006020{
Josef Bacik6a912212010-11-20 09:48:00 +00006021 struct dentry *parent = dget_parent(dentry);
6022 int ret;
6023
Nikolay Borisovf8822742018-02-27 17:37:17 +02006024 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
6025 start, end, LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00006026 dput(parent);
6027
6028 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006029}
6030
6031/*
6032 * should be called during mount to recover any replay any log trees
6033 * from the FS
6034 */
6035int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
6036{
6037 int ret;
6038 struct btrfs_path *path;
6039 struct btrfs_trans_handle *trans;
6040 struct btrfs_key key;
6041 struct btrfs_key found_key;
6042 struct btrfs_key tmp_key;
6043 struct btrfs_root *log;
6044 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
6045 struct walk_control wc = {
6046 .process_func = process_one_buffer,
6047 .stage = 0,
6048 };
6049
Chris Masone02119d2008-09-05 16:13:11 -04006050 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006051 if (!path)
6052 return -ENOMEM;
6053
Josef Bacikafcdd122016-09-02 15:40:02 -04006054 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04006055
Yan, Zheng4a500fd2010-05-16 10:49:59 -04006056 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006057 if (IS_ERR(trans)) {
6058 ret = PTR_ERR(trans);
6059 goto error;
6060 }
Chris Masone02119d2008-09-05 16:13:11 -04006061
6062 wc.trans = trans;
6063 wc.pin = 1;
6064
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006065 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006066 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006067 btrfs_handle_fs_error(fs_info, ret,
6068 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006069 goto error;
6070 }
Chris Masone02119d2008-09-05 16:13:11 -04006071
6072again:
6073 key.objectid = BTRFS_TREE_LOG_OBJECTID;
6074 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02006075 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04006076
Chris Masond3977122009-01-05 21:25:51 -05006077 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04006078 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006079
6080 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08006081 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006082 "Couldn't find tree log root.");
6083 goto error;
6084 }
Chris Masone02119d2008-09-05 16:13:11 -04006085 if (ret > 0) {
6086 if (path->slots[0] == 0)
6087 break;
6088 path->slots[0]--;
6089 }
6090 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
6091 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02006092 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006093 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
6094 break;
6095
Miao Xiecb517ea2013-05-15 07:48:19 +00006096 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006097 if (IS_ERR(log)) {
6098 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08006099 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006100 "Couldn't read tree log root.");
6101 goto error;
6102 }
Chris Masone02119d2008-09-05 16:13:11 -04006103
6104 tmp_key.objectid = found_key.offset;
6105 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
6106 tmp_key.offset = (u64)-1;
6107
6108 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006109 if (IS_ERR(wc.replay_dest)) {
6110 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006111 free_extent_buffer(log->node);
6112 free_extent_buffer(log->commit_root);
6113 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006114 btrfs_handle_fs_error(fs_info, ret,
6115 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006116 goto error;
6117 }
Chris Masone02119d2008-09-05 16:13:11 -04006118
Yan Zheng07d400a2009-01-06 11:42:00 -05006119 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006120 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04006121 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04006122
Josef Bacikb50c6e22013-04-25 15:55:30 -04006123 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04006124 ret = fixup_inode_link_counts(trans, wc.replay_dest,
6125 path);
Chris Masone02119d2008-09-05 16:13:11 -04006126 }
Chris Masone02119d2008-09-05 16:13:11 -04006127
Liu Bo900c9982018-01-25 11:02:56 -07006128 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
6129 struct btrfs_root *root = wc.replay_dest;
6130
6131 btrfs_release_path(path);
6132
6133 /*
6134 * We have just replayed everything, and the highest
6135 * objectid of fs roots probably has changed in case
6136 * some inode_item's got replayed.
6137 *
6138 * root->objectid_mutex is not acquired as log replay
6139 * could only happen during mount.
6140 */
6141 ret = btrfs_find_highest_objectid(root,
6142 &root->highest_objectid);
6143 }
6144
Chris Masone02119d2008-09-05 16:13:11 -04006145 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05006146 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04006147 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04006148 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04006149 kfree(log);
6150
Josef Bacikb50c6e22013-04-25 15:55:30 -04006151 if (ret)
6152 goto error;
6153
Chris Masone02119d2008-09-05 16:13:11 -04006154 if (found_key.offset == 0)
6155 break;
6156 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006157 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04006158
6159 /* step one is to pin it all, step two is to replay just inodes */
6160 if (wc.pin) {
6161 wc.pin = 0;
6162 wc.process_func = replay_one_buffer;
6163 wc.stage = LOG_WALK_REPLAY_INODES;
6164 goto again;
6165 }
6166 /* step three is to replay everything */
6167 if (wc.stage < LOG_WALK_REPLAY_ALL) {
6168 wc.stage++;
6169 goto again;
6170 }
6171
6172 btrfs_free_path(path);
6173
Josef Bacikabefa552013-04-24 16:40:05 -04006174 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006175 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04006176 if (ret)
6177 return ret;
6178
Chris Masone02119d2008-09-05 16:13:11 -04006179 free_extent_buffer(log_root_tree->node);
6180 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04006181 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04006182 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006183
Josef Bacikabefa552013-04-24 16:40:05 -04006184 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006185error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04006186 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006187 btrfs_end_transaction(wc.trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006188 btrfs_free_path(path);
6189 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04006190}
Chris Mason12fcfd22009-03-24 10:24:20 -04006191
6192/*
6193 * there are some corner cases where we want to force a full
6194 * commit instead of allowing a directory to be logged.
6195 *
6196 * They revolve around files there were unlinked from the directory, and
6197 * this function updates the parent directory so that a full commit is
6198 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00006199 *
6200 * Must be called before the unlink operations (updates to the subvolume tree,
6201 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04006202 */
6203void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006204 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04006205 int for_rename)
6206{
6207 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006208 * when we're logging a file, if it hasn't been renamed
6209 * or unlinked, and its inode is fully committed on disk,
6210 * we don't have to worry about walking up the directory chain
6211 * to log its parents.
6212 *
6213 * So, we use the last_unlink_trans field to put this transid
6214 * into the file. When the file is logged we check it and
6215 * don't log the parents if the file is fully on disk.
6216 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006217 mutex_lock(&inode->log_mutex);
6218 inode->last_unlink_trans = trans->transid;
6219 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04006220
6221 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006222 * if this directory was already logged any new
6223 * names for this file/dir will get recorded
6224 */
6225 smp_mb();
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006226 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006227 return;
6228
6229 /*
6230 * if the inode we're about to unlink was logged,
6231 * the log will be properly updated for any new names
6232 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006233 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006234 return;
6235
6236 /*
6237 * when renaming files across directories, if the directory
6238 * there we're unlinking from gets fsync'd later on, there's
6239 * no way to find the destination directory later and fsync it
6240 * properly. So, we have to be conservative and force commits
6241 * so the new name gets discovered.
6242 */
6243 if (for_rename)
6244 goto record;
6245
6246 /* we can safely do the unlink without any special recording */
6247 return;
6248
6249record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006250 mutex_lock(&dir->log_mutex);
6251 dir->last_unlink_trans = trans->transid;
6252 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04006253}
6254
6255/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006256 * Make sure that if someone attempts to fsync the parent directory of a deleted
6257 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6258 * that after replaying the log tree of the parent directory's root we will not
6259 * see the snapshot anymore and at log replay time we will not see any log tree
6260 * corresponding to the deleted snapshot's root, which could lead to replaying
6261 * it after replaying the log tree of the parent directory (which would replay
6262 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00006263 *
6264 * Must be called before the actual snapshot destroy operation (updates to the
6265 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006266 */
6267void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02006268 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006269{
Nikolay Borisov43663552017-01-18 00:31:29 +02006270 mutex_lock(&dir->log_mutex);
6271 dir->last_unlink_trans = trans->transid;
6272 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006273}
6274
6275/*
Chris Mason12fcfd22009-03-24 10:24:20 -04006276 * Call this after adding a new name for a file and it will properly
6277 * update the log to reflect the new name.
6278 *
Filipe Mananad4682ba2018-06-11 19:24:28 +01006279 * @ctx can not be NULL when @sync_log is false, and should be NULL when it's
6280 * true (because it's not used).
6281 *
6282 * Return value depends on whether @sync_log is true or false.
6283 * When true: returns BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6284 * committed by the caller, and BTRFS_DONT_NEED_TRANS_COMMIT
6285 * otherwise.
6286 * When false: returns BTRFS_DONT_NEED_LOG_SYNC if the caller does not need to
6287 * to sync the log, BTRFS_NEED_LOG_SYNC if it needs to sync the log,
6288 * or BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6289 * committed (without attempting to sync the log).
Chris Mason12fcfd22009-03-24 10:24:20 -04006290 */
6291int btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006292 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Filipe Mananad4682ba2018-06-11 19:24:28 +01006293 struct dentry *parent,
6294 bool sync_log, struct btrfs_log_ctx *ctx)
Chris Mason12fcfd22009-03-24 10:24:20 -04006295{
David Sterba3ffbd682018-06-29 10:56:42 +02006296 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006297 int ret;
Chris Mason12fcfd22009-03-24 10:24:20 -04006298
6299 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006300 * this will force the logging code to walk the dentry chain
6301 * up for the file
6302 */
Filipe Manana9a6509c2018-02-28 15:55:40 +00006303 if (!S_ISDIR(inode->vfs_inode.i_mode))
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006304 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04006305
6306 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006307 * if this inode hasn't been logged and directory we're renaming it
6308 * from hasn't been logged, we don't need to log it
6309 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006310 if (inode->logged_trans <= fs_info->last_trans_committed &&
6311 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
Filipe Mananad4682ba2018-06-11 19:24:28 +01006312 return sync_log ? BTRFS_DONT_NEED_TRANS_COMMIT :
6313 BTRFS_DONT_NEED_LOG_SYNC;
Chris Mason12fcfd22009-03-24 10:24:20 -04006314
Filipe Mananad4682ba2018-06-11 19:24:28 +01006315 if (sync_log) {
6316 struct btrfs_log_ctx ctx2;
6317
6318 btrfs_init_log_ctx(&ctx2, &inode->vfs_inode);
6319 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6320 LOG_INODE_EXISTS, &ctx2);
6321 if (ret == BTRFS_NO_LOG_SYNC)
6322 return BTRFS_DONT_NEED_TRANS_COMMIT;
6323 else if (ret)
6324 return BTRFS_NEED_TRANS_COMMIT;
6325
6326 ret = btrfs_sync_log(trans, inode->root, &ctx2);
6327 if (ret)
6328 return BTRFS_NEED_TRANS_COMMIT;
6329 return BTRFS_DONT_NEED_TRANS_COMMIT;
6330 }
6331
6332 ASSERT(ctx);
6333 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6334 LOG_INODE_EXISTS, ctx);
6335 if (ret == BTRFS_NO_LOG_SYNC)
6336 return BTRFS_DONT_NEED_LOG_SYNC;
6337 else if (ret)
6338 return BTRFS_NEED_TRANS_COMMIT;
6339
6340 return BTRFS_NEED_LOG_SYNC;
Chris Mason12fcfd22009-03-24 10:24:20 -04006341}
6342